MT5 not recognizing simple if statement

 

I have a problem with MT5. I was building a more advanced EA and I had strange problems. Then I decided to go to basics and see what is the problem. Here is the simple code I wrote, and it is not performing how it should.


input ENUM_TIMEFRAMES timeframe = PERIOD_M15;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
double old_price_close=0;
double old_price_high=0;
double old_price_low=0;
double old_price_open=0;
void OnTick()
  {
//---
   
   double price_close1 = iClose(Symbol(),timeframe,1);
   double price_high1 = iHigh(Symbol(),timeframe,1);
   double price_low1 = iLow(Symbol(),timeframe,1);
   double price_open1 = iOpen(Symbol(),timeframe,1);
   
   if(price_close1!=old_price_close || price_high1!=old_price_high || price_low1!=old_price_low || price_open1!=old_price_open)
   {
   
      old_price_close=price_close1;
      old_price_high=price_high1;
      old_price_low=price_low1;
      old_price_open=price_open1;
      
      double MA1 = iMA(Symbol(),timeframe,MA1period,1,MODE_EMA,PRICE_CLOSE);
      double MA2 = iMA(Symbol(),timeframe,MA2period,1,MODE_EMA,PRICE_CLOSE);
      
      
      if(MA1<MA2){
         trade.Buy(0.1,NULL,price_close1,price_close1-0.001,price_close1+0.001,NULL);
      }
      
   }  }

The first logic is for checking if there is a new candle close/open.

When there is, it should take a trade if EMA 20 is below the EMA 50).

But here are the results in the strategy tester... It opens trades everywhere!

 
Luka Savić:

I have a problem with MT5. I was building a more advanced EA and I had strange problems. Then I decided to go to basics and see what is the problem. Here is the simple code I wrote, and it is not performing how it should.


The first logic is for checking if there is a new candle close/open.

When there is, it should take a trade if EMA 20 is below the EMA 50).

But here are the results in the strategy tester... It opens trades everywhere!

Your code is written in such a way that it cannot be called on each TICK.

The way you did, it will only work as you want, if called on each opening of a new candle.. 

 
rrocchi:

Your code is written in such a way that it cannot be called on each TICK.

The way you did, it will only work as you want, if called on each opening of a new candle.. 

I want my code to work on opening of a new candle, but the problem is that the tester is not recognizing the if statement, on every new candle open it opens a trade, doesnt care about EMAs..

You can try it yourself. I wanted to put a picture put forum rules didnt allow that big message

 
  1. Luka Savić: The first logic is for checking if there is a new candle close/open.
       if(price_close1!=old_price_close || price_high1!=old_price_high || price_low1!=old_price_low || price_open1!=old_price_open)
    
    1. Doubles are rarely equal. Understand the links in:
                The == operand. - MQL4 programming forum #2 2013.06.07

    2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
                New candle - MQL4 programming forum #3 2014.04.04


  2.       double MA1 = iMA(Symbol(),timeframe,MA1period,1,MODE_EMA,PRICE_CLOSE);
          double MA2 = iMA(Symbol(),timeframe,MA2period,1,MODE_EMA,PRICE_CLOSE);
    

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

 
William Roeder:
    1. Doubles are rarely equal. Understand the links in:
                The == operand. - MQL4 programming forum #2 2013.06.07

    2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
                New candle - MQL4 programming forum #3 2014.04.04


  1. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010


I found a video on YT... I can't check values of MAs like this, i need to put them into buffers... coming from MT4 to MT5...

Thanks..

Reason: