Convert Pinescript Indicator to MT5

 

Hi I am trying to convert a pinescript code into mql5 but the indicator values that I am getting on mt5 terminal do not match to the Tradingview Indicator. Any help would be appreciated.

Pinescript Code:

study("Average Force", "AF")
af(Series, High, Low, Period, PostSmooth) =>     period       =     max(1, int(Period))     highestHigh  = highest(High,  period)     lowestLow    =  lowest( Low,  period)     HHminusLL    = highestHigh - lowestLow     averageForce = HHminusLL==0.0 ? 0.0 : (Series - lowestLow) / HHminusLL - 0.5     sma(averageForce, max(1, int(PostSmooth))) period = input(18, "Period", input.integer, minval=1) smooth = input( 6, "Smooth", input.integer, minval=1) AF = af(close, high, low, period, smooth) Color = AF>0.0 ? color.yellow : color.fuchsia plot(AF, color=Color, style=plot.style_columns)

MQL5 Code:

#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <MovingAverages.mqh>
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  Red
#property indicator_width1  2
#property indicator_label1  "Average Force"

//--- input parameters
input int InpPeriod = 30;                   // Period
input int Smooth    = 18;                    // Smooth

double    AFBuffer[];
double    hhBuffer[];
double    llBuffer[];
double    hhMinusLL[];
double    af[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,AFBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,hhBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,llBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,hhMinusLL,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,af,INDICATOR_CALCULATIONS);

//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpPeriod);
//---
   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[])
  {
//---
   if(rates_total<InpPeriod)
      return(0);
   int start;
   if(prev_calculated==0)
      start=0;
   else
      start=prev_calculated-1;
   for(int i=start; i<rates_total; i++)
     {
      hhBuffer[i]  = iHigh(_Symbol,PERIOD_CURRENT,InpPeriod);
      llBuffer[i]  = iLow(_Symbol,PERIOD_CURRENT,InpPeriod);

      hhMinusLL[i] = hhBuffer[i] - llBuffer[i];
      if(hhMinusLL[i] == 0)
         af[i]=0;
      else
         af[i] = (close[i] - llBuffer[i])/(hhMinusLL[i] - 0.5);
     }

   SimpleMAOnBuffer(rates_total,prev_calculated,0,Smooth,af,AFBuffer);


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Screenshot for TradingView Indicator:


Screenshot from MT5:


 
Utkarsh Katiyar:

Hi I am trying to convert a pinescript code into mql5 but the indicator values that I am getting on mt5 terminal do not match to the Tradingview Indicator. Any help would be appreciated.


Probably you need:

      hhBuffer[i]  = iHigh(_Symbol, _Period, iHighest(_Symbol, _Period, MODE_HIGH, InpPeriod, i));
      llBuffer[i]  = iLow(_Symbol, _Period, iLowest(_Symbol, _Period, MODE_LOW, InpPeriod, i));
 
Stanislav Korotky #:

Probably you need:

Thanks Stan. I tried it but it did not work, latest values are showing as zero. I have a feeling I might be going wrong somewhere in the below code:

 if(rates_total<InpPeriod)
      return(0);
   int start;
   if(prev_calculated==0)
      start=0;
   else
      start=prev_calculated-1;

Current State after making the recommended changes: 


 
Utkarsh Katiyar #:

Thanks Stan. I tried it but it did not work, latest values are showing as zero. I have a feeling I might be going wrong somewhere in the below code:

Current State after making the recommended changes: 


Probably you need:

      hhBuffer[i]  = iHigh(_Symbol, _Period, iHighest(_Symbol, _Period, MODE_HIGH, InpPeriod, rates_total - 1 - i));
      llBuffer[i]  = iLow(_Symbol, _Period, iLowest(_Symbol, _Period, MODE_LOW, InpPeriod, rates_total - 1 - i));
 
Stanislav Korotky #:
rates_total - 1 - i

Still not working. Only negative values are showing up now.


 
Utkarsh Katiyar #:

Still not working. Only negative values are showing up now.


You have a typo, should be no parentheses in denominator:

af[i] = (close[i] - llBuffer[i]) / hhMinusLL[i] - 0.5;
 
Stanislav Korotky #:

You have a typo, should be no parentheses in denominator:

It worked. Thanks Stan for all your help and not giving up on this post.