Help in Accessing Previous Candles Close Value

 

Hi all,

Any help would be greatly appreciated as this has me stumped at the minute:


I'm looking to access a candle close 2 back to see if it closes above the upper Bollinger band

Then looking at the candle 1 back to see if it closes below the upper Bollinger band


I'm getting a match on the on the first IF statement but never on the 2nd

But if I do remove the first IF statement then it will match the 2nd just to ensure i hadn't messed something up


I'm not sure if I'm using IClose correctly or the Bollinger bands correctly in the code


Code is below if it is any help



   //Current Candle Bollinger Bands

   double LowBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0);

   double MidBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_MAIN,0);

   double UpperBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_HIGH,0);

   

   //Previous Candle Bollinger Bands

   double PrevLowBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_LOWER,1);

   double PrevMidBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_MAIN,1);

   double PrevUpperBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_HIGH,1);

   

      //2nd Previous Candle Bollinger Bands

   double PrevTwoLowBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_LOWER,2);

   double PrevTwoMidBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_MAIN,2);

   double PrevTwoUpperBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_HIGH,2);

   

   double PrevTwoScho = iStochastic(_Symbol,0,5,3,3,MODE_EMA,0,MODE_MAIN,2);

   double PrevScho = iStochastic(_Symbol,0,5,3,3,MODE_EMA,0,MODE_MAIN,1);

   double Scho = iStochastic(_Symbol,0,5,3,3,MODE_EMA,0,MODE_MAIN,0);

   

   //If Last 2 Close was Above their Upper bollinger

   if(iClose(_Symbol,0,2) > PrevTwoUpperBollinger && PrevTwoScho > 80){

      Alert("Two Above Match");

      //If Last Close was Below their Upper bollinger

      if (iClose(_Symbol,0,1) < PrevUpperBollinger){

         Alert("One Closed Below");

         signal = "One Closed Below";

      }

         if (Close[0] < UpperBollinger && Scho < 80 ){

         //Send Order

         Alert("Order Would Open Here");

         }

      

   }   

 
laingo93:

Hi all,

Any help would be greatly appreciated as this has me stumped at the minute:


I'm looking to access a candle close 2 back to see if it closes above the upper Bollinger band

Then looking at the candle 1 back to see if it closes below the upper Bollinger band


I'm getting a match on the on the first IF statement but never on the 2nd

But if I do remove the first IF statement then it will match the 2nd just to ensure i hadn't messed something up


I'm not sure if I'm using IClose correctly or the Bollinger bands correctly in the code


Code is below if it is any help



   //Current Candle Bollinger Bands

   double LowBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0);

   double MidBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_MAIN,0);

   double UpperBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_HIGH,0);

   

   //Previous Candle Bollinger Bands

   double PrevLowBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_LOWER,1);

   double PrevMidBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_MAIN,1);

   double PrevUpperBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_HIGH,1);

   

      //2nd Previous Candle Bollinger Bands

   double PrevTwoLowBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_LOWER,2);

   double PrevTwoMidBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_MAIN,2);

   double PrevTwoUpperBollinger = iBands(_Symbol,0,20,2,0,PRICE_CLOSE,MODE_HIGH,2);

   

   double PrevTwoScho = iStochastic(_Symbol,0,5,3,3,MODE_EMA,0,MODE_MAIN,2);

   double PrevScho = iStochastic(_Symbol,0,5,3,3,MODE_EMA,0,MODE_MAIN,1);

   double Scho = iStochastic(_Symbol,0,5,3,3,MODE_EMA,0,MODE_MAIN,0);

   

   //If Last 2 Close was Above their Upper bollinger

   if(iClose(_Symbol,0,2) > PrevTwoUpperBollinger && PrevTwoScho > 80){

      Alert("Two Above Match");

      //If Last Close was Below their Upper bollinger

      if (iClose(_Symbol,0,1) < PrevUpperBollinger){

         Alert("One Closed Below");

         signal = "One Closed Below";

      }

         if (Close[0] < UpperBollinger && Scho < 80 ){

         //Send Order

         Alert("Order Would Open Here");

         }

      

   }   

Please use the Code Button to Insert Code into your post; it is barely readable the way it currently is.

If you are trying to Send a Trade when the Price Breaks and Re-Enters the Bollinger Band, I would suggest you only use Close[0] and a static bool Variable.

Use the static bool to know if a BreakOut as Occured. Close[0] will be Higher then UpBand[0].
If a BreakOut occurs, bool is now true; you now look for a BreakIn. Close[0] will be Lower then UpBand[0].
Once the BreakIn is confirmed, reset the static bool and Enter your Trade.

This would prevent you from missing trades and cancels a lot of iBands Function Calls, making it more reliable and efficient.

 

Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
          Messages Editor