Because you only check if your buy/sell condition is true. Instead you need check for a change in condition. | void OnTick(){ static bool isBuy=false; bool isBuyPre = isBuy; isBuy = MACD1< 0 && MACD2 >0; if(!isBuy) OrderCloseAll(OP_BUY); // No longer a buy // else OrderOpen(OP_BUY) your code just opens on continuing buy signal else if (!isBuyPre) OrderOpen(OP_BUY); // Open on a new buy signalNote the above does not handle chart changes |
Because you only check if your buy/sell condition is true. Instead you need check for a change in condition. |
Note the above does not handle chart changes |
could help me put in the code?
WHRoeder showed you one way to do it.
Another way that would give you more wider window to use the signal could be the following :
Define a global variable like this :
extern double TakeProfit=10000; extern double Lots=0.01; extern double TrailingStop=500; //--- string lastSignalUsed="No signal"; // this is the new one
Then ,add the code to buy and sell conditions as follows :
if(MACD1<0 && MACD2>0&& lastSignalUsed!="Buy")//new condition added { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EA SKY TRADE",123456,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("îòêðûòà ïîçèöèÿ BUY : ",OrderOpenPrice()); lastSignalUsed="Buy";// if the buy signal was used and order placed } } else Print("Îøèáêà ïðè îòêðûòèè BUY ïîçèöèè : ",GetLastError()); return(0); }
and
if(MACD1>0 && MACD2<0 && lastSignalUsed!="Sell")// new condition added { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"EA SKY TRADE",123456,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("îòêðûòà ïîçèöèÿ SELL : ",OrderOpenPrice()); lastSignalUsed="Sell";// if the sell signal was used and order placed } } else Print("Îøèáêà ïðè îòêðûòèè SELL ïîçèöèè : ",GetLastError()); return(0); }
If the terminal is restarted, EA will possibly use the same signal again.
I've cleaned up the warnings a bit and reattached the file.
Hope it helps.
//+------------------------------------------------------------------+ //| EA SKY TRADE | //| | //| https://forum.mql4.com/62312 | //+------------------------------------------------------------------+ extern double TakeProfit=10000; extern double Lots=0.01; extern double TrailingStop=800; //--- string lastSignalUsed="No signal"; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { double MACD1,MACD2; int cnt,ticket,total; MACD1=iMACD(NULL,0,8,12,1,PRICE_CLOSE,MODE_MAIN,1); MACD2=iMACD(NULL,0,12,120,1,PRICE_CLOSE,MODE_MAIN,1); total=OrdersTotal(); if(total<1) { if(AccountFreeMargin()<(1*Lots)) { Print("Account Free Margin= ",AccountFreeMargin()); return(0); } if(MACD1>0 && MACD2>0 && lastSignalUsed!="Buy") { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EA SKY TRADE",123456,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("BUY Order Open Price : ",OrderOpenPrice()); lastSignalUsed="Buy"; } } else Print("BUY Get Last Error : ",GetLastError()); return(0); } if(MACD1<0 && MACD2<0 && lastSignalUsed!="Sell") { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"EA SKY TRADE",123456,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("SELL Order Open Price: ",OrderOpenPrice()); lastSignalUsed="Sell"; } } else Print("SELL Get Last Error : ",GetLastError()); return(0); } return(0); } for(cnt=0;cnt<total;cnt++) { if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) { Print("OrderSelect() failed with error : "+GetLastError()); } if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { if( (MACD1>0 && MACD2<0) || //neutral zone (MACD1<0 && MACD2>0) || //neutral zone (MACD1<0 && MACD2<0) //CLOSE BUY ) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) { Print("OrderClose() failed with error : "+GetLastError()); return(0); } } if(TrailingStop>0) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()<Bid-Point*TrailingStop) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green)) { Print("OrderModify() failed with error : "+GetLastError()); } return(0); } } } } else { if( (MACD1>0 && MACD2<0) || //neutral zone (MACD1<0 && MACD2>0) || //neutral zone (MACD1>0 && MACD2>0) //CLOSE SELL ) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet)) { Print("OrderClose() failed with error : "+GetLastError()); return(0); } } if(TrailingStop>0) { if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red)) { Print("OrderModify() failed with error : "+GetLastError()); return(0); } } } } } } } return(0); } //+------------------------------------------------------------------+
OK sky6nove,
I think I've got most of it but some things need more explaining.
1.To fix problem number 1 you need to tell me what error you get when it doesn't close. It may be that the data isn't very good , gaps in data,wrong indicator parameters, etc.
2.You can have that but since you have the conditions for buy and sell inside the : if(total<1), you need to change that.
You could use a function to check the signal for you at every tick and a couple of datetime variables to keep the time when the cross or neutral happens . If you need help with that let us know.
3. You can use a function for trailing by the low of the lowest candle 1-17 and condition that from user input .
4. About the MACD indicator, try this and let me know if it works :
Open a new chart on any pair. Add MACD indicator to the chart. Change MACD inputs to your EA settings of 8,12,1 or 12,120,1 and see what happens.
MACD1=iMACD(NULL,0,8,12,1,PRICE_CLOSE,MODE_MAIN,1); MACD2=iMACD(NULL,0,12,120,1,PRICE_CLOSE,MODE_MAIN,1);
If you get an error in the experts tab* and the indicator is deleted from chart, it means that those parameters are wrong.
* if you need help to get to the experts tab, let us know.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
My EA works as follows
--------------------------------------------------------------------------------------------------------------
"BUY"
When MACD1 is less than 0 and MACD2 is greater than 0 it opens a BUY
MACD1< 0 && MACD2 >0
ORDERCLOSE
And when they are close to the same effect
MACD1 <0 && MACD2 <0 | | MACD1> 0 && MACD2> 0
--------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
"SELL"
When MACD1 is greater than 0 and is less than 0 MACD2 it opens a SELL
MACD1 >0 && MACD2< 0
ORDERCLOSE
And when they are close to the same effect
MACD1 <0 && MACD2 <0 | | MACD1> 0 && MACD2> 0
---------------------------------------------------------------------------------------------------------------
I put the "TrailingStop" but when it hits the "TrailingStop" he re-opens the position
I wonder how can I stop the "TrailingStop" and wait for the new EA MACD signal?
EX. EA open BUY and "TrailingStop" was reached for him to open BUY another must wait another intersection of line 0 in reverse.