Help with very simple code

 

Hi, I am very new to this, and I am learning mql4 so I can backtest some of my strategies with the MT4 strategy tester.

I have a very simple strategy that buys in the direction of the last candle printed as soon as a new candle prints.

My EA doesn't seem to work correctly. The results don't make sense for what the strategy is supposed to do. I must have coded it wrong.

Are there any beginner mistakes that you might be able to point out for me?

 

float currentOpen=0;

int start()
{

   if(currentOpen != iOpen("GBPJPY",PERIOD_D1,0))
     {
      
      //Buy if upwards bar
      if(iClose("GBPJPY",PERIOD_H1,0) > iOpen("GBPJPY",PERIOD_H1,0))
        {
            OrderSend("GBPJPY",OP_BUY,0.1,Ask,3,Ask - 10,Ask+10,NULL,0,0,Green);
        }
        
      //Sell if a downwards bar
      if(iClose("GBPJPY",PERIOD_H1,0) < iOpen("GBPJPY",PERIOD_H1,0) )
        {
            OrderSend("GBPJPY",OP_SELL,0.1,Bid,3,Bid + 10,Bid-10,NULL,0,0,Red);
        }
      
      
      //If statement won't execute until next H1 bar now
      currentOpen = iOpen("GBPJPY",PERIOD_H1,0);
     }
     
     return 0;
}

 

 

Thank you in advance for any advice you can give! 

 
   if(currentOpen != iOpen("GBPJPY",PERIOD_D1,0))
     {
      //
      currentOpen = iOpen("GBPJPY",PERIOD_H1,0);
     }

Why are you checking against the daily chart and then setting the value according to the H1 chart?

that doesn't make sense. 

      if(iClose("GBPJPY",PERIOD_H1,0) > iOpen("GBPJPY",PERIOD_H1,0))

 

 If you happen to catch the first tick of a bar, Open will equal the Close. In the strategy tester, you will definitely catch the first tick.

 

That period_d1 was a mistake, forgot to change it when I changed the strategy from daily to hourly time chart, whoops. It didn't work before that anyways.

But disregarding that, if you call iClose on a bar that hasn't closed yet, does it return the current value, or does it just not work?

I see now what I need to change though, I need to shift back one bar when I'm using the if statements that make the orders. Thank you!

 

Awesome, thank you for the resources.

Reason: