trailingstop_v2

Author: Copyright 2020, Zakhvatkin Aleksandr.
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
trailingstop_v2
ÿþ//+------------------------------------------------------------------+

//|                                                 trailingstop.mq4 |

//|                            Copyright 2020, Zakhvatkin Aleksandr. |

//|                              https://www.mql5.com/ru/users/z.a.m |

//+------------------------------------------------------------------+

#property copyright "Copyright 2020, Zakhvatkin Aleksandr."

#property link      "https://www.mql5.com/ru/users/z.a.m"

#property version   "1.00"

#property strict

//--- input parameters

input int      PlusPoints=100;

input int      TralingStep=10;

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

//---

   for(int i=0;i<OrdersTotal();i++)

     {

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) return;

      string _osymb = OrderSymbol();

      string _csymb = Symbol();

      int _type=OrderType();

      int _ticket=OrderTicket();

      if(_osymb!=_csymb) continue;

      if(_type==2||_type==3||_type==4||_type==5) continue;

      double _osl=OrderStopLoss();

      double _otp=OrderTakeProfit();

      double _oop=OrderOpenPrice();

      double _tsl=MarketInfo(_csymb,MODE_STOPLEVEL);

      if(_type==0)

        {

         if(_osl<_oop) _osl=_oop;

         if(_osl+PlusPoints*Point<=Bid)

           {

            double _nsl=Bid-PlusPoints*Point+TralingStep*Point;

            if(Bid-_nsl<_tsl*Point) _nsl=Bid-_tsl*Point;

            _nsl=NormalizeDouble(_nsl,Digits);

            if(_nsl<=_osl) continue;

            if(!OrderModify(_ticket,_oop,_nsl,_otp,0,CLR_NONE))

              {

               ResetLastError();

               Print("Order modification failed #",GetLastError());

               return;

              }

            else Print("The order was modified successfully!");  

           }

        }

      if(_type==1)

        {

         if(_osl>_oop) _osl=_oop;

         if(_osl-PlusPoints*Point>=Ask)

           {

            double _nsl=Ask+PlusPoints*Point-TralingStep*Point;

            if(_nsl-Ask<_tsl*Point) _nsl=Ask+_tsl*Point;

            _nsl=NormalizeDouble(_nsl,Digits);

            if(_nsl>=_osl) continue;

            if(!OrderModify(_ticket,_oop,_nsl,_otp,0,CLR_NONE))

              {

               ResetLastError();

               Print("Order modification failed #",GetLastError());

               return;

              }

            else Print("The order was modified successfully!");  

           }

        }  

     }

  }

//+------------------------------------------------------------------+

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