Author: © 2022, Alexey Viktorov
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
SL_TP
ÿþ/********************************************************************\

|                                                          SL_TP.mq5 |

|                                            © 2022, Alexey Viktorov |

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

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

#property copyright "© 2022, Alexey Viktorov"

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

#property version   "1.00"

//---

enum ORD_POS

 {

  ord,  //  Orders only

  pos,  //  Only positions

  all   //  Positions and orders

 };

//long chartID;

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)

ulong order = 0;

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

int OnInit()

 {

//chartID = ChartID();



  return(INIT_SUCCEEDED);

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



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

void OnTick()

 {

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



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

void OnTradeTransaction(const MqlTradeTransaction& trans,

                        const MqlTradeRequest& request,

                        const MqlTradeResult& result)

 {

  if(trans.symbol == _Symbol)

   {

    MqlTradeRequest myRequest;

    MqlTradeResult myResult;

    ZeroMemory(myRequest);

    ZeroMemory(myResult);

    if(ord_pos != pos && trans.type == TRADE_TRANSACTION_ORDER_UPDATE && trans.order_state == ORDER_STATE_PLACED && trans.order > order)

     {

      if(OrderSelect(trans.order) && (OrderGetInteger(ORDER_MAGIC) == idEA || idEA == -1))

       {

        double sl = SL == 0 ? 0.0 : trans.order_type%2 == DEAL_TYPE_BUY ? trans.price-SL*_Point : trans.price+SL*_Point,

               tp = TP == 0 ? 0.0 : trans.order_type%2 == DEAL_TYPE_BUY ? trans.price+TP*_Point : trans.price-TP*_Point;

        myRequest.action      = TRADE_ACTION_MODIFY;

        myRequest.price       = trans.price;

        myRequest.order       = trans.order;

        myRequest.sl          = NormalizeDouble(sl, _Digits);

        myRequest.tp          = NormalizeDouble(tp, _Digits);

        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(!OrderSend(myRequest, myResult))

          Print(myResult.retcode);

        order = trans.order;

       }

     }

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

     {

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

       {

        if(PositionSelectByTicket(trans.position) && (PositionGetInteger(POSITION_MAGIC) == idEA || idEA == -1))

         {

          double sl = SL == 0 ? 0.0 : NormalizeDouble(trans.deal_type == DEAL_TYPE_BUY ? trans.price-SL*_Point : trans.price+SL*_Point, _Digits),

                 tp = TP == 0 ? 0.0 : NormalizeDouble(trans.deal_type == DEAL_TYPE_BUY ? trans.price+TP*_Point : trans.price-TP*_Point, _Digits);

          myRequest.action    = TRADE_ACTION_SLTP;

          myRequest.price     = trans.price;

          myRequest.position  = trans.position;

          myRequest.symbol    = trans.symbol;

          myRequest.sl        = sl;

          myRequest.tp        = tp;

          myRequest.magic     = PositionGetInteger(POSITION_MAGIC);

          if(!OrderSend(myRequest, myResult))

            Print(myResult.comment);

         }

       }

     }

   }

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



/******************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 ---