iHigh functions returns values less than previous iHigh candle

 

I want to enter the trade when current candle crosses high of previous candle. But issue there's something wrong that `high` is always less than previous candle high.

In attached screenshot you can see it's printing high less than previous high even bullish candles are forming. 


void OnTick() {
   if ((!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) || (!TerminalInfoInteger(TERMINAL_CONNECTED)) || (SymbolInfoInteger(_Symbol, SYMBOL_TRADE_MODE) != SYMBOL_TRADE_MODE_FULL)) return;

// Trade only if new bar has arrived.
   int bars = Bars(_Symbol, _Period);
   if (LastBars == bars) return;
   LastBars = bars;

   double high=iHigh(_Symbol,PERIOD_CURRENT,0);
   double high1=iHigh(_Symbol,PERIOD_CURRENT,1);

   Print("###### high ",high, " high1 ", high1, " high>high1 ", high>high1);
   
   if ((iHigh(_Symbol, _Period, 0)>iHigh(_Symbol, _Period, 1)) {
      // buy logic here
   } 

}
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
minam14:

I want to enter the trade when current candle crosses high of previous candle. But issue there's something wrong that `high` is always less than previous candle high.

In attached screenshot you can see it's printing high less than previous high even bullish candles are forming. 


Your code appears to be limiting operations to run only upon population of a new bar, but you have an iHigh() set to bar 0.

So then following the first reference to bar 0, LastBars is set to bars and you're left with only the initially appearing high price of the currently forming bar.

 
double high2, close1;

void OnTick() {
        if ((!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) || (!TerminalInfoInteger(TERMINAL_CONNECTED)) || (SymbolInfoInteger(_Symbol, SYMBOL_TRADE_MODE) !=SYMBOL_TRADE_MODE_FULL)) return;

        int bars = Bars(_Symbol, _Period);
        if (LastBars != bars)
        {
                LastBars = bars;

                high2 = iHigh(_Symbol, PERIOD_CURRENT, 2);
                close1 = iClose(_Symbol, PERIOD_CURRENT, 1);

                if (close1 > high2)
                {
                        // buy condition here
                }
        }
}

You might want to rethink your logic, it should be like this:

  • Get the High value from the bar index 2
  • Get the Close value from the bar index 1 (supposing you want to get the Close cross)
  • You will be running the logic off the bar index 0 always
You can repeat the process for selling on low crosses.
 

Thank you everyone for support. After trying all of recommendations, I solved the problem by using Pending orders.

So you can place order and keep one trade per candle.