int start(){ : static datetime lastSig; if (lastSig == Time[0]) return; // Don't set lastSig yet, wait until you have a signal. : if (isSignal){ // Time to open t=OrderSend(...) if (t<0) Alert(... else lastSig = time[0]; // Prevent multiple orders on the same bar.
I've tried the following, but it still doesn't seem to be working.
It's still opening one candle too late..
int start(){ static datetime lastSig; if (lastSig == Time[0]) return; if (conditionBuy1() == true && conditionBuy2() == true){ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EA Buy",1234,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } else{ lastSig = Time[0]; } }
Thank you
I've tried the following, but it still doesn't seem to be working.
It's still opening one candle too late..
Perhaps this wasn't true at the start of the bar only during it . . .
if (conditionBuy1() == true && conditionBuy2() == true)
. . . so then the earliest possible opportunity to open a trade was at the NEXT bar . . .
Actually, that may be the problem, RaptorUK.
I just noticed that sleep() does not work in backtest and that may be what's making it skip the current candle.
I'm googling a sleep() alternative for backtesting
I've tried the following, but it still doesn't seem to be working.
It's still opening one candle too late..
int start(){ static datetime lastSig; if (lastSig == Time[0]) return; if (isSignal){...} else { lastSig = Time[0]; } }
I implemented what you suggested on my last post, did I not?
Or am I missing something? thanks
Thank you for the help, I had misplaced the "lastSig = Time[0]" bit!
It's working as I meant it to do (:
Thank you WHRoeder and RaptorUK
edit. actually I've managed to make it open on the current candle and the next one lol figuring out what line is wrong
I'm sorry for making 3 posts in a row, but I've been messing with my code for hours and hours and I can't fix it.
Currently it's opening a trade on the current candle (which is what I want), but it's also opening on the next one.
I have the following code:
int start(){ static datetime lastSig; if (lastSig == Time[0]) return; int ticket; // get spread static double spread; spread=MarketInfo(Symbol(),MODE_SPREAD); // using another function CloseOrders(1234); Sleep(20000); if (isSignalCond1() == true && isSignalCond2() == true){ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+(TakeProfit-spread)*Point,"EA - Buy",1234,0,Green); if(ticket<0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else lastSig = Time[0]; } if (isSignalCond3() == true && isSignalCond4() == true){ ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-(TakeProfit-spread)*Point,"EA - Sell",1234,0,Red); if(ticket<0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else lastSig = Time[0]; } }
I appreciate all the help and sorry for being a pain in the ass, but I think I've tried everything but the correct fix to make this work properly lol
Thank you!
I'm sorry for making 3 posts in a row, but I've been messing with my code for hours and hours and I can't fix it.
Currently it's opening a trade on the current candle (which is what I want), but it's also opening on the next one.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to make an EA as described below:
Candle 1:
- check every candle
and
What may be the issue?
Thank you!