I am a newbies in programming, I have this code from many lessons together.
I have a list of order, have or have not SL, TP set, when I run this EA, it should monitor all open orders, for example a Long order the EA should change the SL to the last swing low seen in TF M30 and set TP of the same order to last swing high seen in H1 .
When I ran it, I can see in the terminal log orders has been modified
....
2020.05.18 22:14:18.616 '1123091273': order #97826121 sell 0.02 EURCHF at 1.06096 was modified -> sl: 0.00000 tp: 1.06052
2020.05.18 22:14:18.408 '1123091273': modify order #97826121 sell 0.02 EURCHF at 1.06096 sl: 0.00000 tp: 1.05150 -> sl: 0.00000 tp: 1.06052
2020.05.18 22:12:44.529 Expert AutomaticStopLoss AutoSetAtM30LastswingHL_plusminus2 EURCHF,M1: loaded successfully
but the terminal view trade list for order 97826121 SL and TP stay 0,0 so this code does not changed anything.
Also what else can I use instead of Sleep() to let the code run only once, then only run again update SL/TP when a significant swing high or low newly appears in the M30 orH1 time frame.
Could you guide me to correct this fault? thank you
[/code]
I have a list of order, have or have not SL, TP set, when I run this EA, it should monitor all open orders, for example a Long order the EA should change the SL to the last swing low seen in TF M30 and set TP of the same order to last swing high seen in H1 .
When I ran it, I can see in the terminal log orders has been modified
....
2020.05.18 22:14:18.616 '1123091273': order #97826121 sell 0.02 EURCHF at 1.06096 was modified -> sl: 0.00000 tp: 1.06052
2020.05.18 22:14:18.408 '1123091273': modify order #97826121 sell 0.02 EURCHF at 1.06096 sl: 0.00000 tp: 1.05150 -> sl: 0.00000 tp: 1.06052
2020.05.18 22:12:44.529 Expert AutomaticStopLoss AutoSetAtM30LastswingHL_plusminus2 EURCHF,M1: loaded successfully
but the terminal view trade list for order 97826121 SL and TP stay 0,0 so this code does not changed anything.
Also what else can I use instead of Sleep() to let the code run only once, then only run again update SL/TP when a significant swing high or low newly appears in the M30 orH1 time frame.
Could you guide me to correct this fault? thank you
Inserted Code
[code] //+------------------------------------------------------------------+ //| AutomaticStopLoss.mq4 | //| Ivan @ forexfctory.com forum | //| //+------------------------------------------------------------------+ #property copyright "Ivan @ forexfactory.com forum " #property link "" enum ENUM_CHARTSYMBOL { CurrentChartSymbol=0,AllOpenOrder=1 }; input ENUM_CHARTSYMBOL ChartSymbolSelection=AllOpenOrder; extern int mult=0,StopLoss = 9, TakeProfit = 25; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int init() { Alert("Init..."); if(Digits == 5 || Digits ==3) { StopLoss=StopLoss*10; TakeProfit=TakeProfit*10; mult=10; } else mult=1; return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int deinit() { Alert("DeInit..."); return(0); } /////////////////////////////////////////////////////////////////////////// //+------------------------------------------------------------------+ //| AutoSet TP and SL.mq4 | //| Copyright 2019, Catalin Zachiu | //| https://www.mql5.com/en/users/catalinzachiu | //+------------------------------------------------------------------+ #property copyright "Copyright 2019, Catalin Zachiu" #property link "https://www.mql5.com/en/users/catalinzachiu" #property version "1.00" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double Short_SL = High[iHighest(Symbol(),PERIOD_M30,MODE_HIGH,48,0)]; double Long_SL = Low[iLowest(Symbol(),PERIOD_M30,MODE_LOW,48,0)];//M30*2=H1; H1*4=H4 H4*6=D1 double Short_TP=Low[iLowest(Symbol(),PERIOD_H1,MODE_LOW,24,0)];//M30*2=H1; H1*4=H4 double Long_TP=High[iHighest(Symbol(),PERIOD_H1,MODE_HIGH,24,0)]; double Set_TP_At,Set_SL_At; //+------------------------------------------------------------------+ //|program start function | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { int total = OrdersTotal(); if(Bars<100 || IsTradeAllowed()==false) return; if(CalculateCurrentOrders()!=0) { // for(int b=total-1; b>=0; b--) for(int b=0; b<OrdersTotal(); b++) { if(OrderSelect(b, SELECT_BY_POS)==false) { break; } if(OrderCloseTime()!=0 || OrderSymbol()!=Symbol()) { continue; } if(OrderType()==OP_BUY) Set_TP_At=Long_TP -2*Point*mult; if(Set_TP_At>0 && Set_TP_At>Ask) if(OrderTakeProfit()>=0) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Set_TP_At,0,clrNONE)) Print("Take Profit Updated TO New Level ",OrderTicket()); } Set_SL_At=Long_SL -2*Point*mult; if(Set_SL_At>0 && Set_SL_At<Bid) if(OrderStopLoss()>=0) { Alert("OrderStopLoss()>=0 "); if(!OrderModify(OrderTicket(),OrderOpenPrice(),Set_SL_At,OrderTakeProfit(),0,clrNONE)) Print("Stop Loss Updated TO New Level ",OrderTicket()); Alert("OrderModify",Set_SL_At, " ",OrderTicket()); } if(OrderType()==OP_SELL) Set_TP_At=Short_TP+2*Point*mult; if(Set_TP_At>0 && Set_TP_At<Bid) if(OrderTakeProfit()>=0) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Set_TP_At,0,clrNONE)) Print("Take Profit Updated TO New Level ",OrderTicket()); } if(Set_SL_At>0 && Set_SL_At>Ask) Set_SL_At=Short_SL+2*Point*mult; if(OrderStopLoss()>=0) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),Set_SL_At,OrderTakeProfit(),0,clrNONE)) Print("Stop Loss Updated TO New Level ",OrderTicket()); } } //--- Sleep(5000); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CalculateCurrentOrders() { int buys=0,sells=0; //--- for(int i=0; i<OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(ChartSymbolSelection==CurrentChartSymbol && OrderSymbol()!=Symbol()) continue; if(OrderType()==OP_BUY) { buys++; } if(OrderType()==OP_SELL) { sells++; } } if(buys>0) return(buys); else return(-sells); //--- } //+------------------------------------------------------------------+