I'm trying to convert a particular QQE indicator written in pine language (TradingView) to MQ5 for MT5 and facing issues
Hi,
I'm new to MQL5 programming and have been going through different API functions trying to see what works. Unfortunately I'm facing a lot of trouble in trying to convert the logic from a basic QQE indicator (written in pine) to an MT5 indicator.
The indicator should just plot two lines. The trend line (FastAtrRsiTL), and the RSI line (RsiMa).
I first started writing code without compiling, and then when I felt that the logic was 90% there I kept working to the point that everything compiles without warnings or errors.
I'm unable to see the lines and I'm confused both with OnInit() and OnExecute() logic.
Here is the pine script with the required logic:
Here is my effort to convert this to a working MT5 indicator (.mq5):
int handle = INVALID_HANDLE;
OnInit() { handle = iRSI(); }
OnCalculate()
{
double buffer[];
CopyBuffer(handle, ..., buffer);
}
Hi,
I'm new to MQL5 programming and have been going through different API functions trying to see what works. Unfortunately I'm facing a lot of trouble in trying to convert the logic from a basic QQE indicator (written in pine) to an MT5 indicator.
The indicator should just plot two lines. The trend line (FastAtrRsiTL), and the RSI line (RsiMa).
I first started writing code without compiling, and then when I felt that the logic was 90% there I kept working to the point that everything compiles without warnings or errors.
I'm unable to see the lines and I'm confused both with OnInit() and OnExecute() logic.
Here is the pine script with the required logic:
Here is my effort to convert this to a working MT5 indicator (.mq5):
int startBar = prev_calculated - (prev_calculated == rates_total);;
SetIndexBuffer(3, AtrRsi, INDICATOR_CALCULATIONS);
int totalBars = Bars(Symbol(), Period());
input int RSI_Period = 14; // RSI periods to use
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
Hi,
I'm new to MQL5 programming and have been going through different API functions trying to see what works. Unfortunately I'm facing a lot of trouble in trying to convert the logic from a basic QQE indicator (written in pine) to an MT5 indicator.
The indicator should just plot two lines. The trend line (FastAtrRsiTL), and the RSI line (RsiMa).
I first started writing code without compiling, and then when I felt that the logic was 90% there I kept working to the point that everything compiles without warnings or errors.
I'm unable to see the lines and I'm confused both with OnInit() and OnExecute() logic.
Here is the pine script with the required logic:
Here is my effort to convert this to a working MT5 indicator (.mq5):
int limit = rates_total - prev_calculated;
RsiMa[i - 1]
for (int i = startBar; i < rates_total; i++)
for (int i = MathMax(startBar, RSI_Period); i < rates_total; i++)
return prev_calculated;
Thank you. I took your suggestions and now the code is modified to this (see code block).
I am seeing the lines now, and it's visually what I wanted. But I face a strange problem now - when I change to other timeframes, the lines disappear. The indicator is always showing 0 digits as well, it looks like it has trouble updating or something.
#property version "1.00" #property indicator_separate_window #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_level1 30 #property indicator_level2 70 #property indicator_buffers 9 #property indicator_plots 2 #property indicator_type1 DRAW_LINE #property indicator_type2 DRAW_LINE #property indicator_color1 clrGreen #property indicator_color2 clrRed #property indicator_label1 "FastAtrRsiTL" #property indicator_label2 "RSIndex" input int RSI_Period = 14; input int SF = 5; input double QQE_factor = 1.618; double Rsi[]; double RsiMa[]; double AtrRsi[]; double MaAtrRsi[]; double dar[]; double longband[]; double shortband[]; double RSIndex[]; double FastAtrRsiTL[]; int totalBars = Bars(Symbol(), Period()); int Wilders_Period; int StartBar; int rsiHandle = INVALID_HANDLE; int OnInit() { IndicatorSetString(INDICATOR_SHORTNAME, "QQE exactrade"); Wilders_Period = RSI_Period * 2 - 1; // Get handle of RSI indicator rsiHandle = iRSI(Symbol(), Period(), RSI_Period, PRICE_CLOSE); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, RSI_Period); PlotIndexSetInteger(0, PLOT_LINE_STYLE, STYLE_SOLID); PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 1); PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrRed); PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, RSI_Period); PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID); PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1); PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrGreen); IndicatorSetInteger(INDICATOR_DIGITS, 3); // Set index buffers and plot lines SetIndexBuffer(0, FastAtrRsiTL, INDICATOR_DATA); SetIndexBuffer(1, RsiMa, INDICATOR_DATA); SetIndexBuffer(2, Rsi, INDICATOR_CALCULATIONS); SetIndexBuffer(3, AtrRsi, INDICATOR_CALCULATIONS); SetIndexBuffer(4, MaAtrRsi, INDICATOR_CALCULATIONS); SetIndexBuffer(5, dar, INDICATOR_CALCULATIONS); SetIndexBuffer(6, longband, INDICATOR_CALCULATIONS); SetIndexBuffer(7, shortband, INDICATOR_CALCULATIONS); SetIndexBuffer(8, RSIndex, INDICATOR_CALCULATIONS); 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 startBar = prev_calculated - (prev_calculated == rates_total); int limit = rates_total - prev_calculated; //int startBar = prev_calculated; // Copy data from RSI indicator to Rsi buffer CopyBuffer(rsiHandle, 0, 0, rates_total, Rsi); for (int i = startBar; i < rates_total; i++) { // Calculate RSI Moving Average RsiMa[i] = iMAOnArray(Rsi, totalBars, SF, 0, MODE_EMA, i); // Calculate ATR RSI AtrRsi[i] = MathAbs(RsiMa[i - 1] - RsiMa[i]); // Calculate MA ATR RSI MaAtrRsi[i] = iMAOnArray(AtrRsi, totalBars, Wilders_Period, 0, MODE_EMA, i); // Calculate Delta Fast ATR RSI dar[i] = iMAOnArray(MaAtrRsi, totalBars, Wilders_Period, 0, MODE_EMA, i) * QQE_factor; double DeltaFastAtrRsi = dar[i]; // Calculate new shortband and longband values double newshortband = RsiMa[i] + DeltaFastAtrRsi; double newlongband = RsiMa[i] - DeltaFastAtrRsi; // Crossover condition: RsiMa crosses above longband if (RsiMa[i - 1] > longband[i - 1] && RsiMa[i] > longband[i - 1]) { longband[i] = MathMax(longband[i - 1], newlongband); } else { longband[i] = newlongband; } // Crossover condition: RsiMa crosses below shortband if (RsiMa[i - 1] < shortband[i - 1] && RsiMa[i] < shortband[i - 1]) { shortband[i] = MathMin(shortband[i - 1], newshortband); } else { shortband[i] = newshortband; } // Determine the value of FastAtrRsiTL based on trend if (RsiMa[i] > shortband[i - 1] && RsiMa[i - 1] < shortband[i - 1]) { // Trend is up FastAtrRsiTL[i] = longband[i]; } else if (RsiMa[i] < longband[i - 1] && RsiMa[i - 1] > longband[i - 1]) { // Trend is down FastAtrRsiTL[i] = shortband[i]; } } return rates_total; } /* MQL5 implementation of iMAOnArray by Budyoni Damyanov */ double iMAOnArray(double& array[], int total, int period, int ma_shift, ENUM_MA_METHOD ma_method, int shift) { double buf[]; ArrayResize(buf, total); switch (ma_method) { case MODE_SMA: { double sum = 0; int pos = total - 1; for (int i = 1; i < period; i++, pos--) sum += array[pos]; while (pos >= 0) { sum += array[pos]; buf[pos] = sum / period; sum -= array[pos + period - 1]; pos--; } return buf[shift + ma_shift]; } case MODE_EMA: { double pr = 2.0 / (period + 1); int pos = total - 2; while (pos >= 0) { if (pos == total - 2) buf[pos + 1] = array[pos + 1]; buf[pos] = array[pos] * pr + buf[pos + 1] * (1 - pr); pos--; } return buf[shift + ma_shift]; } case MODE_SMMA: { double sum = 0; int pos = total - period; while (pos >= 0) { if (pos == total - period) { sum = 0; for (int i = 0, k = pos; i < period; i++, k++) { sum += array[k]; buf[k] = 0; } } else { sum = buf[pos + 1] * (period - 1) + array[pos]; } buf[pos] = sum / period; pos--; } return buf[shift + ma_shift]; } case MODE_LWMA: { double sum = 0.0, lsum = 0.0; double price; int weight = 0, pos = total - 1; for (int i = 1; i <= period; i++, pos--) { price = array[pos]; sum += price * i; lsum += price; weight += i; } pos++; int i = pos + period; while (pos >= 0) { buf[pos] = sum / weight; if (pos == 0) break; pos--; i--; price = array[pos]; sum = sum - lsum + price * period; lsum -= array[i]; lsum += price; } return buf[shift + ma_shift]; } } return 0; }
Thank you. I took your suggestions and now the code is modified to this (see code block).
I am seeing the lines now, and it's visually what I wanted. But I face a strange problem now - when I change to other timeframes, the lines disappear. The indicator is always showing 0 digits as well, it looks like it has trouble updating or something.
edit -
I just noticed you posted more suggestions now, I'll take these into account and make a new modification. Cheers.
Thank you. I took your suggestions and now the code is modified to this (see code block).
I am seeing the lines now, and it's visually what I wanted. But I face a strange problem now - when I change to other timeframes, the lines disappear. The indicator is always showing 0 digits as well, it looks like it has trouble updating or something.
// Copy data from RSI indicator to Rsi buffer CopyBuffer(rsiHandle, 0, 0, rates_total, Rsi);
edit -
I just noticed you posted more suggestions now, I'll take these into account and make a new modification. Cheers.
I hope I made it clear enough. Consult the documentation on TimeSeries, maybe use ArrayAsSeries, or an offset variable...
To be honest it's not clear to me right now. I will have to look at it again another day. I overestimated how difficult it would be to translate this logic into an mq5 script.
As I am new to this, it will take time to fully understand. I feel that you've pointed me in the right direction and that's fulfilling enough for now at this time.
I changed OnCalculate() now like so:
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 startBar = prev_calculated - (prev_calculated == rates_total); int totalBars = rates_total; // Check if all data calculated https://www.mql5.com/en/docs/series/copybuffer if (BarsCalculated(rsiHandle) < rates_total) { Print("Could not copy data, no new data calculated"); return prev_calculated; } // in case all data can't be coppied int to_copy; if (prev_calculated > rates_total || prev_calculated <= 0){ to_copy = rates_total; } else{ to_copy = rates_total - prev_calculated; // Last value is always copied to_copy++; } // Try to copy if (CopyBuffer(rsiHandle, 0, 0, to_copy, Rsi) <= 0) { Print("Could not copy data, no new data calculated"); return prev_calculated; } for (int i = MathMax(startBar, RSI_Period); i < rates_total; i++){ // Calculate RSI Moving Average RsiMa[i] = iMAOnArray(Rsi, totalBars, SF, 0, MODE_EMA, i); // Calculate ATR RSI AtrRsi[i] = MathAbs(RsiMa[i - 1] - RsiMa[i]); // Calculate MA ATR RSI MaAtrRsi[i] = iMAOnArray(AtrRsi, totalBars, Wilders_Period, 0, MODE_EMA, i); // Calculate Delta Fast ATR RSI dar[i] = iMAOnArray(MaAtrRsi, totalBars, Wilders_Period, 0, MODE_EMA, i) * QQE_factor; double DeltaFastAtrRsi = dar[i]; // Calculate new shortband and longband values double newshortband = RsiMa[i] + DeltaFastAtrRsi; double newlongband = RsiMa[i] - DeltaFastAtrRsi; // Crossover condition: RsiMa crosses above longband if (RsiMa[i - 1] > longband[i - 1] && RsiMa[i] > longband[i - 1]){ longband[i] = MathMax(longband[i - 1], newlongband); } else{ longband[i] = newlongband; } // Crossover condition: RsiMa crosses below shortband if (RsiMa[i - 1] < shortband[i - 1] && RsiMa[i] < shortband[i - 1]){ shortband[i] = MathMin(shortband[i - 1], newshortband); } else { shortband[i] = newshortband; } // Determine the value of FastAtrRsiTL based on trend if (RsiMa[i] > shortband[i - 1] && RsiMa[i - 1] < shortband[i - 1]){ // Trend is up FastAtrRsiTL[i] = longband[i]; } else if (RsiMa[i] < longband[i - 1] && RsiMa[i - 1] > longband[i - 1]){ // Trend is down FastAtrRsiTL[i] = shortband[i]; } } return rates_total; }
but now I see no lines.. nothing. I'm giving up for now.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I'm new to MQL5 programming and have been going through different API functions trying to see what works. Unfortunately I'm facing a lot of trouble in trying to convert the logic from a basic QQE indicator (written in pine) to an MT5 indicator.
The indicator should just plot two lines. The trend line (FastAtrRsiTL), and the RSI line (RsiMa).
I first started writing code without compiling, and then when I felt that the logic was 90% there I kept working to the point that everything compiles without warnings or errors.
I'm unable to see the lines and I'm confused both with OnInit() and OnExecute() logic.
Here is the pine script with the required logic:
Here is my effort to convert this to a working MT5 indicator (.mq5):