ADX and DI for v4 in MQL5 — converted from Pine Script
The ADX and DI for v4 indicator by BeikabuOyaji, published on TradingView under the Mozilla Public License 2.0, converted to MetaTrader 5 by the Stratlate engine. The conversion was executed against the original on 500 synthetic bars and compared value by value: equivalent (fidelity 1.000).
Inputs
| Input | Default |
|---|---|
| len | 14 |
| th | 20 |
Plots
DI+, DI-, ADX.
Converted MQL5
Save as adx-and-di.mq5 next to PineRuntime.mqh (included with every conversion), compile in MetaEditor, then review and backtest before trading. Output of the engine alone; the AI review available after sign-in may improve it further.
//+------------------------------------------------------------------+
//| pine-transpiler 0.6.46 (pinec) - generated code
//| source sha256: de8c63d6864c status: CLEAN
//| notes: 0 unsupported, 0 warn, 0 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| ADX and DI for v4
//| Converted from Pine Script by pine-transpiler.
//| Review before trading: see the accompanying report for every
//| construct that was approximated or could not be converted.
//+------------------------------------------------------------------+
#property copyright "Converted by pine-transpiler"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 4
#property indicator_label1 "DI+"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 1
#property indicator_label2 "DI-"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrDodgerBlue
#property indicator_width2 1
#property indicator_label3 "ADX"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrDodgerBlue
#property indicator_width3 1
#property indicator_label4 "Plot4"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrDodgerBlue
#property indicator_width4 1
#include "PineRuntime.mqh"
//--- plot buffers
double Buf0[];
double Buf1[];
double Buf2[];
double Buf3[];
//--- script-level state
// The newest bar's index: `last_bar_index` and `barstate.islast`.
int __pineLastBar = 0;
// Series-valued names are arrays because their history is read;
// the rest stay scalars.
double TrueRange = PINE_NA;
double DirectionalMovementPlus = PINE_NA;
double DirectionalMovementMinus = PINE_NA;
double SmoothedTrueRange[];
double SmoothedDirectionalMovementPlus[];
double SmoothedDirectionalMovementMinus[];
double DIPlus = PINE_NA;
double DIMinus = PINE_NA;
double DX = PINE_NA;
double ADX = PINE_NA;
//--- live-tick rollback: the state each bar started from
double __sv_TrueRange;
double __si_TrueRange;
double __sv_DirectionalMovementPlus;
double __si_DirectionalMovementPlus;
double __sv_DirectionalMovementMinus;
double __si_DirectionalMovementMinus;
double __sv_DIPlus;
double __si_DIPlus;
double __sv_DIMinus;
double __si_DIMinus;
double __sv_DX;
double __si_DX;
double __sv_ADX;
double __si_ADX;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_TrueRange = TrueRange;
__sv_DirectionalMovementPlus = DirectionalMovementPlus;
__sv_DirectionalMovementMinus = DirectionalMovementMinus;
__sv_DIPlus = DIPlus;
__sv_DIMinus = DIMinus;
__sv_DX = DX;
__sv_ADX = ADX;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
TrueRange = __sv_TrueRange;
DirectionalMovementPlus = __sv_DirectionalMovementPlus;
DirectionalMovementMinus = __sv_DirectionalMovementMinus;
DIPlus = __sv_DIPlus;
DIMinus = __sv_DIMinus;
DX = __sv_DX;
ADX = __sv_ADX;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_TrueRange = TrueRange;
__si_DirectionalMovementPlus = DirectionalMovementPlus;
__si_DirectionalMovementMinus = DirectionalMovementMinus;
__si_DIPlus = DIPlus;
__si_DIMinus = DIMinus;
__si_DX = DX;
__si_ADX = ADX;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
TrueRange = __si_TrueRange;
DirectionalMovementPlus = __si_DirectionalMovementPlus;
DirectionalMovementMinus = __si_DirectionalMovementMinus;
DIPlus = __si_DIPlus;
DIMinus = __si_DIMinus;
DX = __si_DX;
ADX = __si_ADX;
pine_draw_reset();
__pineInitGen++;
}
double __ti0[]; // materialised ta.* source: DX
//--- inputs
input int len = 14;
input int th = 20;
int OnInit()
{
SetIndexBuffer(0, Buf0, INDICATOR_DATA);
SetIndexBuffer(1, Buf1, INDICATOR_DATA);
SetIndexBuffer(2, Buf2, INDICATOR_DATA);
SetIndexBuffer(3, Buf3, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, C'76,175,80');
PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'242,54,69');
PlotIndexSetInteger(2, PLOT_LINE_COLOR, C'49,27,146');
PlotIndexSetInteger(3, PLOT_LINE_COLOR, C'54,58,69');
IndicatorSetString(INDICATOR_SHORTNAME, "ADX and DI for v4");
__pineSaveInitial();
return INIT_SUCCEEDED;
}
//--- one bar == one evaluation of the original Pine script
void PineBar(const int __pineBar,
const double &open[], const double &high[],
const double &low[], const double &close[],
const long &volume[],
const datetime &time[])
{
if(__pineBar > 0)
{
SmoothedTrueRange[__pineBar] = SmoothedTrueRange[__pineBar - 1];
SmoothedDirectionalMovementPlus[__pineBar] = SmoothedDirectionalMovementPlus[__pineBar - 1];
SmoothedDirectionalMovementMinus[__pineBar] = SmoothedDirectionalMovementMinus[__pineBar - 1];
}
TrueRange = MathMax(MathMax((high[__pineBar] - low[__pineBar]), MathAbs((high[__pineBar] - pine_nz(pine_hist(close, __pineBar, (int)(1)))))), MathAbs((low[__pineBar] - pine_nz(pine_hist(close, __pineBar, (int)(1))))));
DirectionalMovementPlus = (((high[__pineBar] - pine_nz(pine_hist(high, __pineBar, (int)(1)))) > (pine_nz(pine_hist(low, __pineBar, (int)(1))) - low[__pineBar])) ? MathMax((high[__pineBar] - pine_nz(pine_hist(high, __pineBar, (int)(1)))), 0) : 0);
DirectionalMovementMinus = (((pine_nz(pine_hist(low, __pineBar, (int)(1))) - low[__pineBar]) > (high[__pineBar] - pine_nz(pine_hist(high, __pineBar, (int)(1))))) ? MathMax((pine_nz(pine_hist(low, __pineBar, (int)(1))) - low[__pineBar]), 0) : 0);
SmoothedTrueRange[__pineBar] = 0.0;
SmoothedTrueRange[__pineBar] = ((pine_nz(pine_hist(SmoothedTrueRange, __pineBar, (int)(1))) - pine_div(pine_nz(pine_hist(SmoothedTrueRange, __pineBar, (int)(1))), len)) + TrueRange);
SmoothedDirectionalMovementPlus[__pineBar] = 0.0;
SmoothedDirectionalMovementPlus[__pineBar] = ((pine_nz(pine_hist(SmoothedDirectionalMovementPlus, __pineBar, (int)(1))) - pine_div(pine_nz(pine_hist(SmoothedDirectionalMovementPlus, __pineBar, (int)(1))), len)) + DirectionalMovementPlus);
SmoothedDirectionalMovementMinus[__pineBar] = 0.0;
SmoothedDirectionalMovementMinus[__pineBar] = ((pine_nz(pine_hist(SmoothedDirectionalMovementMinus, __pineBar, (int)(1))) - pine_div(pine_nz(pine_hist(SmoothedDirectionalMovementMinus, __pineBar, (int)(1))), len)) + DirectionalMovementMinus);
DIPlus = (pine_div(SmoothedDirectionalMovementPlus[__pineBar], SmoothedTrueRange[__pineBar]) * 100);
DIMinus = (pine_div(SmoothedDirectionalMovementMinus[__pineBar], SmoothedTrueRange[__pineBar]) * 100);
DX = (pine_div(MathAbs((DIPlus - DIMinus)), (DIPlus + DIMinus)) * 100);
__ti0[__pineBar] = DX;
ADX = pine_sma(__ti0, __pineBar, (int)(len));
Buf0[__pineBar] = DIPlus;
Buf1[__pineBar] = DIMinus;
Buf2[__pineBar] = ADX;
Buf3[__pineBar] = th;
}
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[])
{
// Pine evaluates oldest-to-newest. MQL4 hands OnCalculate
// SERIES-indexed arrays (0 == newest) while MQL5 hands over
// oldest-first, so the direction is forced explicitly rather
// than relying on either default. `x[n]` is then index i - n
// in both dialects, and the generated logic is identical.
ArraySetAsSeries(open, false);
ArraySetAsSeries(high, false);
ArraySetAsSeries(low, false);
ArraySetAsSeries(close, false);
ArraySetAsSeries(tick_volume, false);
ArraySetAsSeries(volume, false);
ArraySetAsSeries(time, false);
ArrayResize(SmoothedTrueRange, rates_total);
ArrayResize(SmoothedDirectionalMovementPlus, rates_total);
ArrayResize(SmoothedDirectionalMovementMinus, rates_total);
ArrayResize(__ti0, rates_total);
if(prev_calculated == 0)
{
ArrayInitialize(SmoothedTrueRange, PINE_NA);
ArrayInitialize(SmoothedDirectionalMovementPlus, PINE_NA);
ArrayInitialize(SmoothedDirectionalMovementMinus, PINE_NA);
ArrayInitialize(__ti0, PINE_NA);
}
__pineLastBar = rates_total - 1;
const int start = (prev_calculated > 1) ? prev_calculated - 1 : 0;
static bool __pineRealVolume = false;
if(start == 0)
{
__pineRestoreInitial();
__pineSnapBar = -1;
__pineRealVolume = rates_total > 0 && volume[rates_total - 1] > 0;
}
__pineRealtime = prev_calculated > 0;
for(int __pineBar = start; __pineBar < rates_total; __pineBar++)
{
if(__pineBar == __pineSnapBar)
__pineRestoreState(); // a re-run of this bar: back to its start
else if(__pineBar == __pineLastBar)
{
__pineSaveState(); // the forming bar: remember the state it starts from
__pineSnapBar = __pineBar;
}
if(__pineRealVolume) PineBar(__pineBar, open, high, low, close, volume, time);
else PineBar(__pineBar, open, high, low, close, tick_volume, time);
}
return rates_total;
}Original Pine Script
© BeikabuOyaji — Mozilla Public License 2.0. Reproduced for attribution and comparison; the converted code above is a derivative work offered under the same licence.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BeikabuOyaji
//@version=4
study("ADX and DI for v4")
len = input(14)
th = input(20)
TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0
SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus
DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)
plot(DIPlus, color=color.green, title="DI+")
plot(DIMinus, color=color.red, title="DI-")
plot(ADX, color=color.navy, title="ADX")
hline(th, color=color.black)Convert your own Pine Script to MQL5
Your first conversion is free, in either direction. Paste a TradingView indicator or strategy and get MQL5 or MQL4 source, with every construct the engine could not map flagged by line.
Open the converterMore: all example conversions · how Pine Script maps to MQL5 · how the engine measures