Limiting EA to one trade per bar

 

Hi all,

I am working on an EA that only trades once per bar. 

I am using time to check if the current bar is at least x hours ahead of the previous bar. However it does not execute a trade on backtests, but it does when I remove the time check conditions. 

Here's the pseudocode:

static int order_time 

int start()

{

if(order_time ==0 || TimeHour(TimeCurrent()) >= order_time + x )
{
   if(condition1= true)
   {
   OrderSend();
   order_time = TimeHour(TimeCurrent());
   }
}
return(0);
}

 Any hints would be appreciated, thanks! 

 
luxe: Any hints would be appreciated, thanks! 
  1. Add print statements dumping your variables before and in IF statements and find out why?
  2. Name your variables carefully. Order_time is not a datetime, the name is misleading.
  3. IF the last order was opened at 2300 what happens next?
    if(order_time ==0 || TimeHour(TimeCurrent()) >= order_time + x 
                           0..23                 >=     23     + x Will this ever be true again?
    How about
    datetime order_time; // Common variables (global) are always static.
    int start(){
    
       if(timeCurrent() > order_Time + x * 3600) ..
       :
       order_time = TimeCurrent(); // or Time[0]
 

Thanks for the response.

I don't think adding print statements will help because I am back testing the EA using the strategy tester.

I've changed order_time to datetime type and the time check conditions but it still won't execute a trade. It should execute at least once because order_time == 0 is true for the time around. 

Also if I comment out the reassignment of order_time in the second if statement, it works properly. 

 
luxe:I don't think adding print statements will help because I am back testing the EA using the strategy tester.
Why not? Isn't the purpose of a "strategy tester" is to help you in your testing?
 

Oh I just realized that print statements will print onto the journal tab. Didn't know that before. This should make debugging this much easier.

 Thanks! 

 EDIT: Figured out that the order_time variable was reassigned even when the ordersend() function returned an error and did not actually send a trade. Quick fix and now it works

Reason: