Author: © 2023, Alexey Viktorov
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
SL_TP_v2
ÿþ/********************************************************************\

|                                                       SL_TP_v2.mq5 |

|                                            © 2023, Alexey Viktorov |

|                       https://www.mql5.com/ru/users/alexeyvik/news |

\********************************************************************/

#property copyright "© 2023, Alexey Viktorov"

#property link      "https://www.mql5.com/ru/users/alexeyvik/news"

#property version   "2.00"

//---

enum ORD_POS

 {

  ord,  //  Orders only

  pos,  //  Only positions

  all   //  Positions and orders

 };

enum SYMBOLS

 {

  oneSymbol,  //  Current symbol only

  allSymbols  //  All symbols

 };

//long chartID;

input SYMBOLS     one_all = oneSymbol;  //  Symbols current only or all

input ORD_POS     ord_pos = all;        //  positions or orders

input int         TP      = 500;        //  Take profit

input int         SL      = 200;        //  Stop loss

input int         idEA    = -1;         //  Magic (0 - @CG=K5, -1 - ! ?>78F88)

MqlTradeRequest myRequest;

MqlTradeResult myResult;

MqlTradeCheckResult myCheck;

bool first_start = true;

/*******************Expert initialization function*******************/

int OnInit()

 {

//chartID = ChartID();

  return(INIT_SUCCEEDED);

 }/******************************************************************/



/************************Expert tick function************************/

void OnTick()

 {

  if((TP > 0 || SL > 0) && first_start)

   {

    int i = 0;

    ulong order = 0;

    if(ord_pos != pos && OrdersTotal() > 0)

     {

      do

       {

        order = OrderGetTicket(i);

        string symbol = OrderGetString(ORDER_SYMBOL);

        if(order > 0)

          OrderModify(order, symbol);

        i++;

       }

      while(order > 0 && !IsStopped());

     }

    //---

    i = 0;

    ulong position = 0;

    if(ord_pos != ord && PositionsTotal() > 0)

     {

      do

       {

        position = PositionGetTicket(i);

        string symbol = PositionGetString(POSITION_SYMBOL);

        if(position > 0)

          PositionModify(position, symbol);

        i++;

       }

      while(position > 0 && !IsStopped());

     }

    first_start = false;

   }

 }/******************************************************************/



/*********************TradeTransaction function**********************/

void OnTradeTransaction(const MqlTradeTransaction& trans,

                        const MqlTradeRequest& request,

                        const MqlTradeResult& result)

 {

  if(TP > 0 || SL > 0)// && trans.symbol == _Symbol || one_all == allSymbols)

   {

    if(ord_pos != pos && trans.type == TRADE_TRANSACTION_ORDER_ADD)

     {

      if(OrderSelect(trans.order))

        OrderModify(trans.order, trans.symbol);

     }

    if(ord_pos != ord && trans.type == TRADE_TRANSACTION_DEAL_ADD)

     {

      if(HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)

        PositionModify(trans.position, trans.symbol);

     }

   }

 }/******************************************************************/



/********************************************************************/

void OrderModify(ulong order, string symbol)

 {

  ZeroMemory(myRequest);

  ZeroMemory(myResult);

  ZeroMemory(myCheck);

  if(OrderSelect(order))

   {

    if((symbol == _Symbol && (OrderGetInteger(ORDER_MAGIC) == idEA || idEA == -1)) || one_all == allSymbols)

     {

      while(OrderSelect(order) && OrderGetInteger(ORDER_STATE) != ORDER_STATE_PLACED && !IsStopped()){}

      ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);

      double price = OrderGetDouble(ORDER_PRICE_OPEN);

      double point = SymbolInfoDouble(symbol, SYMBOL_POINT);

      double sl = SL == 0 ? 0.0 : orderType%2 == ORDER_TYPE_BUY ? price-SL*point : price+SL*point,

             tp = TP == 0 ? 0.0 : orderType%2 == ORDER_TYPE_BUY ? price+TP*point : price-TP*point;

      if(TP == 0 && SL == 0)

        return;

      myRequest.action      = TRADE_ACTION_MODIFY;

      myRequest.price       = price;

      myRequest.order       = order;

      myRequest.symbol      = symbol;

      myRequest.sl          = normalizePrice(sl, symbol);

      myRequest.tp          = normalizePrice(tp, symbol);

      myRequest.magic       = OrderGetInteger(ORDER_MAGIC);

      myRequest.type_time   = (ENUM_ORDER_TYPE_TIME)OrderGetInteger(ORDER_TYPE_TIME);

      myRequest.expiration  = (datetime)OrderGetInteger(ORDER_TIME_EXPIRATION);

      myRequest.deviation   = 50;

      if(OrderCheck(myRequest, myCheck))

       {

        if(!OrderSend(myRequest, myResult))

          Print(myResult.comment);

       }

      else

        printf("%s, ticket %i %s ", symbol, order, myCheck.comment);

     }

   }

 }/******************************************************************/



/********************************************************************/

void PositionModify(ulong position, string symbol)

 {

  ZeroMemory(myRequest);

  ZeroMemory(myResult);

  ZeroMemory(myCheck);

  if(PositionSelectByTicket(position))

   {

    if((symbol == _Symbol && (PositionGetInteger(POSITION_MAGIC) == idEA || idEA == -1)) || one_all == allSymbols)

     {

      MqlTick mqlTick;

      SymbolInfoTick(symbol, mqlTick);

      ENUM_POSITION_TYPE positionType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

      double price = PositionGetDouble(POSITION_PRICE_OPEN);

      double point = SymbolInfoDouble(symbol, SYMBOL_POINT);

      double sl = SL == 0 ? 0.0 : normalizePrice(positionType == POSITION_TYPE_BUY ? fmin(mqlTick.bid, price)-SL*point : fmax(mqlTick.ask, price)+SL*point, symbol),

             tp = TP == 0 ? 0.0 : normalizePrice(positionType == POSITION_TYPE_BUY ? fmax(mqlTick.bid, price)+TP*point : fmin(mqlTick.ask, price)-TP*point, symbol);

      myRequest.action    = TRADE_ACTION_SLTP;

      myRequest.price     = price;

      myRequest.position  = position;

      myRequest.symbol    = symbol;

      myRequest.sl        = sl;

      myRequest.tp        = tp;

      myRequest.magic     = PositionGetInteger(POSITION_MAGIC);

      if(OrderCheck(myRequest, myCheck))

       {

        if(!OrderSend(myRequest, myResult))

          Print(myResult.comment);

       }

      else

        printf("%s, ticket %i %s ", symbol, position, myCheck.comment);

     }

   }

 }/******************************************************************/



/********************************************************************/

double normalizePrice(double price, string symbol = NULL)

 {

  symbol = symbol == NULL ? _Symbol : symbol;

  double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);

  price = floor(price/tickSize)*tickSize;

  return(price);

 }/******************************************************************/



/******************Expert deinitialization function******************/

void OnDeinit(const int reason)

 {

//---

 }/******************************************************************/

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---