//+------------------------------------------------------------------+ //| Set_Stop_Limit.mq4 | //| Copyright © 2015, Gehtsoft USA LLC | //| http://fxcodebase.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2015, Gehtsoft USA LLC" #property link "http://fxcodebase.com" #property version "1.00" #property strict input bool Only_For_One_Symbol=false; input bool Set_Stop=true; input int Stop=50; input bool Set_Limit=true; input int Limit=100; int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void OnTick() { int OT=OrdersTotal(); double Old_Stop, Old_Limit, New_Stop, New_Limit; double Open_Price; bool Enable_Modify; for (int i=OT-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(Only_For_One_Symbol || OrderSymbol()==Symbol()) { Open_Price=OrderOpenPrice(); Old_Stop=OrderStopLoss(); Old_Limit=OrderTakeProfit(); New_Stop=Old_Stop; New_Limit=Old_Limit; Enable_Modify=false; if(OrderType()==OP_BUY) { if (Set_Stop && MathAbs(Open_Price-Old_Stop-Stop*Point)>Point) { Enable_Modify=true; New_Stop=Open_Price-Stop*Point; } if (Set_Limit && MathAbs(Old_Limit-Open_Price-Limit*Point)>Point) { Enable_Modify=true; New_Limit=Open_Price+Limit*Point; } } if(OrderType()==OP_SELL) { if (Set_Stop && MathAbs(Old_Stop-Open_Price-Stop*Point)>Point) { Enable_Modify=true; New_Stop=Open_Price+Stop*Point; } if (Set_Limit && MathAbs(Open_Price-Old_Limit-Limit*Point)>Point) { Enable_Modify=true; New_Limit=Open_Price-Limit*Point; } } if (Enable_Modify) { OrderModify(OrderTicket(), Open_Price, New_Stop, New_Limit, 0); } } } } void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { }