mql4forex

My indicator only has one value in the buffer , which is giving me a straight line


I am trying to convert an indicator I found on tradingview (which uses pinescript) to MQL4:

==============================================

BullTrend = (close - lowest(low, 50)) / atr(5)

BearTrend = (highest(high, 50) - close) / atr(5)

BearTrend2= -1*BearTrend 

Trend = BullTrend - BearTrend

==============================================

The code is supposed to perform the calculation shown in the my mql4 version on every bar. However, it shows the same value as far back as I try to go.

How do I make sure that the calculation is performed on each bar or if there is a more efficient way to code this I would appreciate that highly. :)

//+------------------------------------------------------------------+
//|                                                         BBPT.mq4 |
//|                                                          Maxwell |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Maxwell"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot Trend
#property indicator_label1  "Trend"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot BullTrend
#property indicator_label2  "BullTrend"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot BearTrend2
#property indicator_label3  "BearTrend2"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- indicator buffers
double         TrendBuffer[];
double         BullTrendBuffer[];
double         BearTrend2Buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrWhite);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrGreen);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrRed);
   
   SetIndexLabel(0,"Trend");
   SetIndexLabel(1,"BullTrend");
   SetIndexLabel(2,"BearTrend2");
   
   
   SetIndexBuffer(0,TrendBuffer);
   SetIndexBuffer(1,BullTrendBuffer);
   SetIndexBuffer(2,BearTrend2Buffer);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int uncalculatedBar = rates_total - prev_calculated;
         
   for(int i=0; i < uncalculatedBar; i++)
   { 
      double lowestPrice = Low[iLowest(Symbol(),0,MODE_LOW,50,0)];
      double highestPrice = High[iHighest(Symbol(),0,MODE_HIGH,50,0)];
      double lowestCloseCalc = (iClose(NULL,0,0) - lowestPrice);
      double highestCloseCalc = (highestPrice - iClose(NULL,0,0));
      double atr = (iATR(NULL,0,5,0));
      double BullTrend = lowestCloseCalc / (atr);
      double BearTrend = highestCloseCalc / (atr);
      double BearTrend2 = (-1 * BearTrend);
      double Trend = (BullTrend - BearTrend);
   
   
      TrendBuffer[i] = Trend;
      BullTrendBuffer[i] = BullTrend;
      BearTrend2Buffer[i] = BearTrend2;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Solution

  • You have put all your calcs in a loop but you have hard coded them to look at bar 0 each time instead of bar i. Low[iLowest(Symbol(),0,MODE_LOW,50,0)] should be Low[iLowest(Symbol(),0,MODE_LOW,50,i)] and so on...

    //+------------------------------------------------------------------+
    //|                                                         BBPT.mq4 |
    //|                                                          Maxwell |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Maxwell"
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    #property indicator_separate_window
    #property indicator_buffers 3
    #property indicator_plots   3
    //--- plot Trend
    #property indicator_label1  "Trend"
    #property indicator_type1   DRAW_LINE
    #property indicator_color1  clrWhite
    #property indicator_style1  STYLE_SOLID
    #property indicator_width1  1
    //--- plot BullTrend
    #property indicator_label2  "BullTrend"
    #property indicator_type2   DRAW_LINE
    #property indicator_color2  clrMediumSeaGreen
    #property indicator_style2  STYLE_SOLID
    #property indicator_width2  1
    //--- plot BearTrend2
    #property indicator_label3  "BearTrend2"
    #property indicator_type3   DRAW_LINE
    #property indicator_color3  clrRed
    #property indicator_style3  STYLE_SOLID
    #property indicator_width3  1
    //--- indicator buffers
    double         TrendBuffer[];
    double         BullTrendBuffer[];
    double         BearTrend2Buffer[];
    
    
    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+
    int OnInit()
      {
    //--- indicator buffers mapping
       SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrWhite);
       SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrGreen);
       SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrRed);
       
       SetIndexLabel(0,"Trend");
       SetIndexLabel(1,"BullTrend");
       SetIndexLabel(2,"BearTrend2");
       
       
       SetIndexBuffer(0,TrendBuffer);
       SetIndexBuffer(1,BullTrendBuffer);
       SetIndexBuffer(2,BearTrend2Buffer);
       
    //---
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Custom indicator iteration function                              |
    //+------------------------------------------------------------------+
    int OnCalculate(const int rates_total,
                    const int prev_calculated,
                    const datetime &time[],
                    const double &open[],
                    const double &high[],
                    const double &low[],
                    const double &close[],
                    const long &tick_volume[],
                    const long &volume[],
                    const int &spread[])
      {
    //---
       int uncalculatedBar = rates_total - prev_calculated;
             
       for(int i=0; i < uncalculatedBar; i++)
       { 
          double lowestPrice = Low[iLowest(Symbol(),0,MODE_LOW,50,i)];
          double highestPrice = High[iHighest(Symbol(),0,MODE_HIGH,50,i)];
          double lowestCloseCalc = (iClose(NULL,0,i) - lowestPrice);
          double highestCloseCalc = (highestPrice - iClose(NULL,0,i));
          double atr = (iATR(NULL,0,5,i));
          double BullTrend = lowestCloseCalc / (atr);
          double BearTrend = highestCloseCalc / (atr);
          double BearTrend2 = (-1 * BearTrend);
          double Trend = (BullTrend - BearTrend);
       
       
          TrendBuffer[i] = Trend;
          BullTrendBuffer[i] = BullTrend;
          BearTrend2Buffer[i] = BearTrend2;
       }
       
    //--- return value of prev_calculated for next call
       return(rates_total);
      }
    //+------------------------------------------------------------------+