Compare a last candle to last i candles

 

Hello I need to probably correct my code... I trying to figure out a comparison of last candle(body) with previouse i candles

input double Lots = 0.1;
input double RiskPercent = 5;
input int BarsBack = 10;

double Body1 = 0; 
double Bodyi = 0;
int z = 0;  
CTrade trade;



int OnInit(){ 

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)  {  
}




void OnTick(){
   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);   

   double open0 = iOpen(_Symbol,PERIOD_CURRENT,0);       open0 = NormalizeDouble(open0,_Digits);
   double high0 = iHigh(_Symbol,PERIOD_CURRENT,0);       high0 = NormalizeDouble(high0,_Digits);
   double low0 = iLow(_Symbol,PERIOD_CURRENT,0);         low0 = NormalizeDouble(low0,_Digits); 
   double close0 = iClose(_Symbol,PERIOD_CURRENT,0);     close0 = NormalizeDouble(close0,_Digits);  
      
   double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);       open1 = NormalizeDouble(open1,_Digits);   
   double high1 = iHigh(_Symbol,PERIOD_CURRENT,1);       high1 = NormalizeDouble(high1,_Digits);
   double low1 = iLow(_Symbol,PERIOD_CURRENT,1);         low1 = NormalizeDouble(low1,_Digits); 
   double close1 = iClose(_Symbol,PERIOD_CURRENT,1);     close1 = NormalizeDouble(close1,_Digits);


/*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/ 
if(close1 > open1){  //checking a buy situation
   Body1 = close1-open1; //calc size of last candle
   
   for(int i = 2; i >= BarsBack; i++){  //looping for comparing last bar with last i candles
       double open = iOpen(_Symbol,PERIOD_CURRENT,i);       open = NormalizeDouble(open,_Digits);
       double close = iClose(_Symbol,PERIOD_CURRENT,i);     close = NormalizeDouble(close,_Digits); 

       if(close > open){ //calc size of i last candle if candle is bullish
          Bodyi = close-open;
        } 
        if(close < open){//calc size of i last candle if candle is bearish
          Bodyi = open-close;
        } 
       
       if(Body1 > Bodyi){  // comparing last candle with x last candle
          z++; // if it is true add 1 to z
       }
       
       
   }
   if(z >= BarsBack){ //if all compares was true plase a buy
          Print(__FUNCTION__," > Buy Signal...");
          trade.Buy(Lots,_Symbol,0,low1,close1+(close1-open1));
       }
}

My code is not working and not opening any trades, thank you 

 
  1.    double open0 = iOpen(_Symbol,PERIOD_CURRENT,0);       open0 = NormalizeDouble(open0,_Digits);

    All prices you get from the terminal are normalized.

  2. NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

 

thank you William Roeder 

But the problem is in my "Buy formula"

if(close1 > open1){  //checking a buy situation
   Body1 = close1-open1; //calc size of last candle
   
   for(int i = 2; i >= BarsBack; i++){  //looping for comparing last bar with last i candles
       double open = iOpen(_Symbol,PERIOD_CURRENT,i);       
       double close = iClose(_Symbol,PERIOD_CURRENT,i);     
       
       if(close > open){ //calc size of i last candle if candle is bullish
          Bodyi = close-open;
        } 
        if(close < open){//calc size of i last candle if candle is bearish
          Bodyi = open-close;
        } 
       
       if(Body1 > Bodyi){  // comparing last candle with x last candle
          z++; // if it is true add 1 to z
       }
       
       
   }
   if(z >= BarsBack){ //if all compares was true plase a buy
          Print(__FUNCTION__," > Buy Signal...");
          trade.Buy(Lots,_Symbol,0,low1,close1+(close1-open1));
       }
}
 

I fixed it.

input double Lots = 0.01;
input double RiskPercent = 5;
input int BarsBack = 5;
input int BarShift = 0;

double Body1 = 0; 
double Bodyi = 0;
int z = 0;  
CTrade trade;



int OnInit(){ 

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)  {  
}




void OnTick(){
   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);   

   double open0 = iOpen(_Symbol,PERIOD_CURRENT,0);       open0 = NormalizeDouble(open0,_Digits);
   double high0 = iHigh(_Symbol,PERIOD_CURRENT,0);       high0 = NormalizeDouble(high0,_Digits);
   double low0 = iLow(_Symbol,PERIOD_CURRENT,0);         low0 = NormalizeDouble(low0,_Digits); 
   double close0 = iClose(_Symbol,PERIOD_CURRENT,0);     close0 = NormalizeDouble(close0,_Digits);  
      
   double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);       open1 = NormalizeDouble(open1,_Digits);   
   double high1 = iHigh(_Symbol,PERIOD_CURRENT,1);       high1 = NormalizeDouble(high1,_Digits);
   double low1 = iLow(_Symbol,PERIOD_CURRENT,1);         low1 = NormalizeDouble(low1,_Digits); 
   double close1 = iClose(_Symbol,PERIOD_CURRENT,1);     close1 = NormalizeDouble(close1,_Digits);




/*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/   /*BUY*/ 
if(close1 > open1){  
   Body1 = close1-open1; 

   for(int i = 0; i <= BarsBack; i++){ 
       int totalPositions=PositionsTotal();

       double open = iOpen(_Symbol,PERIOD_CURRENT,i+BarShift);       
       double close = iClose(_Symbol,PERIOD_CURRENT,i+BarShift);     
       
       Bodyi = close-open; 
       Bodyi = MathAbs(Bodyi);
 
       if(Body1 > Bodyi) { 
          z=z+1;
       }else  z=0;  
       
       Comment("\n i ",i,
               "\n z ",z,
               "\n ",
               "\n openi ",open,
               "\n closei ",close,
               "\n ",
               "\n Bodyi ",Bodyi,
               "\n Body1 ",Body1);
       
       if(totalPositions == 0 && z == BarsBack){ 
          Print(__FUNCTION__," > Buy Signal...");
          trade.Buy(Lots,_Symbol,0,low1/*SL*/,close1+(close1-open1)/*TP*/);
           
       }
       
   } 
  
   
}
Reason: