EA - Open trade if current candle has met conditions; check every new candle

 
Hi,

I'm trying to make an EA as described below:

 Candle 1:

- check every candle 

- current candle may or may not have a signal
- if this candle has a signal, open a trade
- if not, do nothing until next bar

 

The problem is that my current version only opens the trade at the next candle and not the current one, as I meant it to do.

I have tried two different ways to achieve this:

void start(){
  static datetime tmp;
  if (tmp!= Time[0]) {
    tmp =  Time[0];


// if it has signal, open trade

  }
}

 and

 

extern int Quant_Bars=1; // Amount of bars
bool Check_New_Bar=false; // Flag of a new bar

bool Function_New_Bar()
      {
      if(Volume[0]>1) return(false);
      static datetime New_Time = 0;
      bool New_Bar = false;
      if (New_Time!= Time[0])
         {
         New_Time = Time[0];
         New_Bar = true;
         }
      return(New_Bar);
      }


int start()
  {

Check_New_Bar = Function_New_Bar(); // Function call
if (Check_New_Bar==true){

// check if bar has a signal

}
   return(0);

 What may be the issue?

 

Thank you! 

 
You are setting tmp, on each new bar there for testing once per bar. test the signal before.
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

 
raidenfx:

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 

 
raidenfx:

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 

Don't . . . it isn't relevant.  There isn't one . . .  the reason that there is no sleep in the Strategy Tester is because it's not running real time . . . at full speed it runs as fast as start() will allow it,  when start() finishes it is called again,  then again and again . . . don't use sleep()  to time things use Time[] or CurrentTime() or some other variant of actual time . . . 
 
raidenfx:

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];
  }
}
Of course your code doesn't work. If the first test does not open, then you never check again (until the next bar). Do it like I already posted. Once you open, THEN suppress additional.
 

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! 

 
raidenfx:

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.

 

 . . .   because your 2 signals are still true . . .  I suggest you show your code.