Modify_All_TP_SL_v1

Author: © Tecciztecatl 2016-2018
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
Modify_All_TP_SL_v1
ÿþ//+------------------------------------------------------------------+

//|                                             Modify_All_TP_SL.mq4 |

//|                                                  © Tecciztecatl  |

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

#property copyright     "© Tecciztecatl 2016-2018"

#property link          "https://www.mql5.com/en/users/tecciztecatl"

#property version       "2.00"

#property description   "The script modifies all orders (market and pending) on the symbol Take Profit and Stop Loss."

#property script_show_inputs

#property strict



input double TakeProfit=0; //Take Profit (price)

input double StopLoss=0;   //Stop Loss (price)

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

//| Script program start function                                    |

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

void OnStart()

  {

   if(OrdersTotal()>0)

     {

      for(int i=OrdersTotal()-1; i>=0; i--)

        {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

           {

            double tp=NormalizeDouble(TakeProfit,_Digits);

            double sl=NormalizeDouble(StopLoss,_Digits);



            if(OrderSymbol()==Symbol())

              {

               int ticket=OrderTicket();

               if(tp<=0) tp=OrderTakeProfit();

               if(sl<=0) sl=OrderStopLoss();

               if(OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)

                 {

                  if(OrderType()==OP_SELL)

                    {

                     if(tp>Ask) tp=OrderTakeProfit();

                     if(sl<Ask) sl=OrderStopLoss();

                    }

                  else

                    {

                     if(tp>=OrderOpenPrice()) tp=OrderTakeProfit();

                     if(sl<=OrderOpenPrice()) sl=OrderStopLoss();

                    }

                 }

               if(OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)

                 {

                  if(OrderType()==OP_BUY)

                    {

                     if(tp<Bid) tp=OrderTakeProfit();

                     if(sl>Bid) sl=OrderStopLoss();

                    }

                  else

                    {

                     if(tp<=OrderOpenPrice()) tp=OrderTakeProfit();

                     if(sl>=OrderOpenPrice()) sl=OrderStopLoss();

                    }

                 }

               Ord_Modify(ticket,tp,sl);

              }

           }

        }

     }

  }

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

//|                                                                  |

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

void Ord_Modify(int ticket,double tp,double sl)

  {

   ResetLastError();

   if(OrderCloseTime()==0 && (tp!=OrderTakeProfit() || sl!=OrderStopLoss()))

     {

      bool i2=false;

      while(!i2 && !IsStopped())

        {

         i2=OrderModify(ticket,OrderOpenPrice(),sl,tp,0,clrNONE);

         if(i2==false)

           {

            Print("Error modifying orders, #"+IntegerToString(GetLastError()));

            RefreshRates();

            Sleep(500);

           }

        }

     }

  }

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

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