Help with Bollinger bands

 

Hello,

I'm new to mql5 and programming with c++ so bear with me as it might take some explaining to help me understand. I'm trying to print the current values of bollinger bands in meta trader 5. I realize there is other topics on this namely https://www.mql5.com/en/forum/242393. But I read through it and I still don't understand. I thought the solution was to just put return on the end of the copy Buffer to array but all that does is bring up an error. I also looked at the bollinger bands definition with the sample code and I just don't understand any of it. I attached my file and the read out that I'm getting is -3.2 something when I do the lowerband value and when I put in comment lowerbandarray[0] it comes up with nothing. Can anyone explain what is going on?

#include<Trade\Trade.mqh>
CTrade trade;



void CheckTrailingStop(double Ask)
{
 //f price.close is greater by 30
   double TicketPriceOpen = PositionGetDouble(POSITION_PRICE_OPEN);
   for(int i=PositionsTotal()-1; i>=0; i--)
   {
   string symbol=PositionGetSymbol(i);
   if(_Symbol==symbol)
  {
      long PositionTicket=PositionGetInteger(POSITION_TICKET);
      double SL =SymbolInfoDouble(_Symbol,SYMBOL_BID);
      if(TicketPriceOpen+.0005<SL)
      {
      //trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0);
      trade.PositionClose(PositionTicket,0.0005);
      }
   }
  }
}

void OnTick()
  {
  string entry = "";
  double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK), _Digits);
   
   // get the buying price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   double ct=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_LAST), _Digits);
   
   // We Create an array for the prices
   MqlRates PriceInfo[];
   
   // Sort the price array from the current candle downwarss
   ArraySetAsSeries(PriceInfo, true);
   
   // Fill array with price data from current candle#0, for 3 candles store in  array
   int PriceData =CopyRates(Symbol(),Period(),0,3,PriceInfo);
   double UpperBandArray[];
   double LowerBandArray[];
   ArraySetAsSeries(UpperBandArray, true);
   ArraySetAsSeries(LowerBandArray, true);
   
   // define bollinger bands, 20 candles, shift=0, deviation=2
   int BollingerBandsDefinition=iBands(_Symbol,_Period,20,0,2,PRICE_CLOSE);
   
   //Copy price info into the array
   CopyBuffer(BollingerBandsDefinition,1,0,3, UpperBandArray) ;
   CopyBuffer(BollingerBandsDefinition,3,0,3, LowerBandArray) ;

   //Calculate ExpertAdvisor for the current candle[value for candle 0]
   double myUpperBandValue=UpperBandArray[0];
   double myLowerBandValue=LowerBandArray[0];
   Comment(myUpperBandValue);
   //Calculate ExpertAdvisor for the current candle[value for candle 0]
   double myLastUpperBandValue=UpperBandArray[1];
   double myLastLowerBandValue=LowerBandArray[1];
   
   Comment (""); // empyt chart output
      
      //create array for rsi and one for bollinger band
   double myRSIArray[]; // add upper band as well for your trading strategy
      
      
      // define rsi settings,14 candles
   int myRSIDefinition = iRSI (_Symbol,_Period,14,PRICE_CLOSE);
   Comment (""); 
      
      //sort price data from current candle downwards
   ArraySetAsSeries(myRSIArray,true);
      
      //Define EA, current candle, 3 candles, savein array
   CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
   // stop loss?
  
  Comment (LowerBandArray[0]);
      // calculate rsiert of the current candle
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   if ( // check if we have a re-entry from above
             (PriceInfo[0].close>=myUpperBandValue-.0002)
            &&(myRSIArray[0]>= 50)
            &&(myRSIArray[0]<= 70)
            //&&(myRSIArray[0]-myRSIArray[1]<=-1)
            //&&(PositionsTotal()<5)
          
          )
           {
           entry="sell";
            
           }
         
      if(entry=="sell")
      trade.Sell(0.10,NULL,Bid,0,(Bid-150*_Point),NULL);
      
      
      
   if ( // check if we have a re-entry from above initial buy
             (PriceInfo[0].close<=myLowerBandValue+.0002)
           &&(myRSIArray[0]>= 30)
           &&(myRSIArray[0]<= 50)
           //&&(myRSIArray[0]-myRSIArray[1]>=1)
           //&&(PositionsTotal()<5)
          
          
         )
            {
            entry="buy";
            
            }
   if(entry=="buy")// && PositionsTotal()<1
      //buy 10
      trade.Buy(0.10,NULL,Ask,0,(Ask-150*_Point),NULL);
      
      
  if(PriceInfo[0].close<=myLowerBandValue+4)
  {
  for(int i=PositionsTotal()-1; i>=0; i--)
  {
  string symbol=PositionGetSymbol(i);
   if(_Symbol==symbol)
   {
   long PositionTicket=PositionGetInteger(POSITION_TICKET);
   long type=PositionGetInteger(POSITION_TYPE);
   if(type==POSITION_TYPE_BUY)
   {
   trade.PositionClose(PositionTicket,0.0005);
   }
   
   
   }
  }
  }
  return(INIT_SUCCEEDED);
   
  }

  
Bollinger Bands ®
Bollinger Bands ®
  • www.mql5.com
Bollinger Bands ® technical indicator (BB) is similar to Envelopes. The only difference is that the bands of Envelopes are plotted a fixed distance (%) away from the moving average, while the Bollinger Bands are plotted a certain number of standard deviations away from it. Standard deviation is a measure of volatility, therefore Bollinger Bands...
Files:
 

Unless it is very long please copy and paste your code using the code button (Alt+S) so that people don't have to download your code to check it.

I have done it for you this time.