Translate spine script (tradingview) to mql indicator (meta trader 4)

Viewed 1176

I would like to convert a Pine script from trading view script to mq4 indicator for meta trader 4.

Here is the script to translate from trading view Pine script:


rsiLength = input(title="RSI Length", type=input.integer, defval=14)
lbLength = input(title="Look Back Length", type=input.integer, defval=5)
avgLength = input(title="Average Length", type=input.integer, defval=8)
drawSig = input(title="Signal On/Off?", type=input.bool, defval=false)
sigLength = input(title="Signal Length", type=input.integer, defval=6)
src = input(title="Source", type=input.source, defval=close)
ob = input(title="Over Bought", type=input.integer, defval=80)
os = input(title="Over Sold", type=input.integer, defval=20)

avgRSI(rsiLength, lookBack, avgLen) =>
    rsi = rsi(src, rsiLength)
    rsiLowest = lowest(rsi, lookBack)
    rsiHighest = highest(rsi, lookBack)
    avgRSILow = sma(rsi - rsiLowest , avgLen)
    avgHiLow = sma(rsiHighest - rsiLowest, avgLen)

    return = 100 / (.01 + avgHiLow / avgRSILow)
    return


rsi = avgRSI( rsiLength, lbLength, avgLength )

plot(series=rsi, title="StochRSI", color=color.red, transp=0)
signal = drawSig ? sma(rsi, sigLength) : na
plot(series=signal, title="StochRSI Signal", color=color.orange, transp=0)
h1 = hline(price=ob, title="OB")
h2 = hline(price=os, title="OS")
fill(h1, h2)

I don't understand this two lines:

    avgRSILow = sma(rsi - rsiLowest , avgLen)
    avgHiLow = sma(rsiHighest - rsiLowest, avgLen)

This is my code to metatrader 4:

double lineBuffer[];
int rsiLength = 5;
int lbLength = 5;
int avgLength = 8;
int sigLength = 6;


int OnInit()
  {
   SetIndexBuffer(0, lineBuffer);
   return(INIT_SUCCEEDED);
  }

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 rsi = iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, i);
      double rsiLowest = lowestRSI(lbLength, i);
      double rsiHighest = highestRSI(lbLength, i);
      double avgRSILow = lowAverageRsi(avgLength, i, rsiLowest);
      double avgHiLow = lowHighRsiAverage(avgLength, rsiLowest, rsiHighest);
      lineBuffer[i] =  100 / (.01 + avgHiLow / avgRSILow);
   }
   return(rates_total);
}

// Return lowest rsi value for the last x values (->loopback) from a specific index (->start)
double lowestRSI(int loopback, int start) {
   double lowest_rsi = iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, start);
   for(int i = start + 1; i <= start + loopback; i++){
      if(iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, i) < lowest_rsi){
         lowest_rsi = iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, i);
      }
   }
   return lowest_rsi;
}

// Return highest rsi value for the last x values (->loopback) from a specific index (->start)
double highestRSI(int loopback, int start) {
   double highest_rsi = iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, start);
   for(int i = start + 1; i <= start + loopback; i++){
      if(iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, i) > highest_rsi){
         highest_rsi = iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, i);
      }
   }
   return highest_rsi;
}

// Calculate rsi average for last x values (-> loopback) minus lowestRsi
double lowAverageRsi(int loopback, int start, double lowestRsi) {
   double low_average_rsi = 0;
   for(int i = start; i < start + loopback; i++){
      low_average_rsi = low_average_rsi + ((iRSI(NULL, NULL, rsiLength, PRICE_CLOSE, i) - lowestRsi) / loopback);
   }
   return low_average_rsi;
}

// Calculate avergage for last x values between two values
double lowHighRsiAverage(int loopback, double lowestRsi, double highestRSI) {
   double low_high_rsi_average = 0;
   for(int i = 0; i < loopback; i++){
      low_high_rsi_average = low_high_rsi_average + ((highestRSI - lowestRsi) / loopback);
   }
   return low_high_rsi_average;
}

I'm stuck, I don't know what to do, if you can help me that would be great

1 Answers

idk if you want the code to be explained or if you need work around due to mt4 coding nature.

avgRSILow = sma(rsi, avgLen) - sma(rsiLowest , avgLen)
avgHiLow = sma(rsiHighest, avgLen) - sma(rsiLowest, avgLen)

the code above gives the same result as

avgRSILow = sma(rsi - rsiLowest , avgLen)
avgHiLow = sma(rsiHighest - rsiLowest, avgLen)

its just x amount of values back being averages using the sma function and subtraction the value for one another. idk the nature of the code that's up to the creator but he is just avg results of the past x amount lengh or rsi values with the avg of the lowest value of x amount of lookback.

if you still have doubts just try to explain what you don't understand better...

Related