Market Shift Levels [ChartPrime] in MQL5 — converted from Pine Script
The Market Shift Levels [ChartPrime] indicator by ChartPrime, 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).
Plots
Market Shift Levels.
Converted MQL5
Save as market-shift-levels-chartprime.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: 2d1571387dca status: CLEAN
//| notes: 0 unsupported, 0 warn, 6 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Market Shift Levels [ChartPrime]
//| 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_chart_window
#property indicator_buffers 5
#property indicator_plots 2
#property indicator_label1 "Market Shift Levels"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 1
#property indicator_label2 "Candles Color"
#property indicator_type2 DRAW_CANDLES
#property indicator_color2 clrDodgerBlue
#property indicator_width2 1
#include "PineRuntime.mqh"
//--- plot buffers
double Buf0[];
double Buf1[];
double Buf2[];
double Buf3[];
double Buf4[];
//--- 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.
bool lbl_data = false;
double level[];
double hma1[];
double hma2 = PINE_NA;
double vol_sum = PINE_NA;
color shift_col = clrNONE;
//--- live-tick rollback: the state each bar started from
bool __sv_lbl_data;
bool __si_lbl_data;
double __sv_hma2;
double __si_hma2;
double __sv_vol_sum;
double __si_vol_sum;
color __sv_shift_col;
color __si_shift_col;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_lbl_data = lbl_data;
__sv_hma2 = hma2;
__sv_vol_sum = vol_sum;
__sv_shift_col = shift_col;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
lbl_data = __sv_lbl_data;
hma2 = __sv_hma2;
vol_sum = __sv_vol_sum;
shift_col = __sv_shift_col;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_lbl_data = lbl_data;
__si_hma2 = hma2;
__si_vol_sum = vol_sum;
__si_shift_col = shift_col;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
lbl_data = __si_lbl_data;
hma2 = __si_hma2;
vol_sum = __si_vol_sum;
shift_col = __si_shift_col;
pine_draw_reset();
__pineInitGen++;
}
color color_shiftUp = clrNONE; // input whose default is not a compile-time constant
color color_shiftDn = clrNONE; // input whose default is not a compile-time constant
double __st0[]; // accumulator for one stateful call site
double __stb0[]; // secondary buffer (e.g. true range for ATR)
double __st1[]; // accumulator for one stateful call site
double __stb1[]; // secondary buffer (e.g. true range for ATR)
double __ti0[]; // materialised ta.* source: hma2
//--- inputs
input int length = 55; // Length
input string InlineInput_0 = "Volume"; // Label Data:
int OnInit()
{
__pineMaxLabels = 500;
lbl_data = (InlineInput_0 == "Volume");
SetIndexBuffer(0, Buf0, INDICATOR_DATA);
SetIndexBuffer(1, Buf1, INDICATOR_DATA);
SetIndexBuffer(2, Buf2, INDICATOR_DATA);
SetIndexBuffer(3, Buf3, INDICATOR_DATA);
SetIndexBuffer(4, Buf4, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_CANDLES);
IndicatorSetString(INDICATOR_SHORTNAME, "Market Shift Levels [ChartPrime]");
__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[])
{
color_shiftUp = pine_color_rgb((int)(36), (int)(213), (int)(128));
color_shiftDn = pine_color_rgb((int)(225), (int)(43), (int)(43));
if(__pineBar == 0)
{
level[__pineBar] = (double)(PINE_NA);
}
if(__pineBar > 0)
{
level[__pineBar] = level[__pineBar - 1];
hma1[__pineBar] = hma1[__pineBar - 1];
}
lbl_data = (InlineInput_0 == "Volume");
hma1[__pineBar] = pine_hma(close, __pineBar, (int)(length));
hma2 = pine_hist(hma1, __pineBar, (int)(5));
vol_sum = ((pine_hist(volume, __pineBar, (int)(2)) + pine_hist(volume, __pineBar, (int)(1))) + pine_hist(volume, __pineBar, (int)(0)));
__ti0[__pineBar] = hma2;
if(pine_crossover(hma1, __ti0, __pineBar))
{
level[__pineBar] = low[__pineBar];
}
__ti0[__pineBar] = hma2;
if(pine_crossunder(hma1, __ti0, __pineBar))
{
level[__pineBar] = high[__pineBar];
}
shift_col = ((close[__pineBar] < level[__pineBar]) ? color_shiftDn : color_shiftUp);
if((((pine_hist(high, __pineBar, (int)(2)) < level[__pineBar]) && (high[__pineBar] < level[__pineBar])) && (pine_hist(high, __pineBar, (int)(1)) > level[__pineBar])))
{
pine_label_style(pine_label_new((__pineBar - 1), pine_hist(high, __pineBar, (int)(1)), (DoubleToString((lbl_data ? vol_sum : pine_hist(high, __pineBar, (int)(1))), 2) + "\n⬙")), (color)(clrNONE), (color)(C'242,54,69'), true, ANCHOR_LOWER, 0, "");
}
if((((pine_hist(low, __pineBar, (int)(2)) > level[__pineBar]) && (pine_hist(low, __pineBar, (int)(1)) < level[__pineBar])) && (low[__pineBar] > level[__pineBar])))
{
pine_label_style(pine_label_new((__pineBar - 1), pine_hist(low, __pineBar, (int)(1)), ("⬘\n" + DoubleToString((lbl_data ? vol_sum : pine_hist(high, __pineBar, (int)(1))), 2))), (color)(clrNONE), (color)(C'0,230,118'), true, ANCHOR_UPPER, 0, "");
}
0 /* barcolor dropped: visual only */;
Buf0[__pineBar] = ((level[__pineBar] != pine_hist(level, __pineBar, (int)(1))) ? PINE_NA : level[__pineBar]);
Buf1[__pineBar] = open[__pineBar];
Buf2[__pineBar] = high[__pineBar];
Buf3[__pineBar] = low[__pineBar];
Buf4[__pineBar] = close[__pineBar];
}
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(level, rates_total);
ArrayResize(hma1, rates_total);
ArrayResize(__st0, rates_total);
ArrayResize(__stb0, rates_total);
ArrayResize(__st1, rates_total);
ArrayResize(__stb1, rates_total);
ArrayResize(__ti0, rates_total);
if(prev_calculated == 0)
{
ArrayInitialize(level, PINE_NA);
ArrayInitialize(hma1, PINE_NA);
ArrayInitialize(__st0, PINE_NA);
ArrayInitialize(__stb0, PINE_NA);
ArrayInitialize(__st1, PINE_NA);
ArrayInitialize(__stb1, 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;
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, "pine_");
}Original Pine Script
© ChartPrime — Mozilla Public License 2.0. Reproduced for attribution and comparison; the converted code above is a derivative work offered under the same licence.
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartPrime
//@version=6
indicator("Market Shift Levels [ChartPrime]", max_labels_count = 500, overlay = true)
// --------------------------------------------------------------------------------------------------------------------}
// 📌 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎
// --------------------------------------------------------------------------------------------------------------------{
int length = input.int(55, "Length")
bool lbl_data = input.string("Volume", "Label Data: ", ["Volume", "Price"]) == "Volume"
color color_shiftUp = input.color(color.rgb(36, 213, 128), "", inline = "col")
color color_shiftDn = input.color(color.rgb(225, 43, 43), "", inline = "col")
// --------------------------------------------------------------------------------------------------------------------}
// 📌 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
// --------------------------------------------------------------------------------------------------------------------{
var level = float(na)
float hma1 = ta.hma(close,length)
float hma2 = hma1[5]
float vol_sum = (volume[2] + volume[1] + volume[0])
if ta.crossover(hma1, hma2)
level := low
if ta.crossunder(hma1, hma2)
level := high
color shift_col = close < level ? color_shiftDn : color_shiftUp
// --------------------------------------------------------------------------------------------------------------------}
// 📌 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
// --------------------------------------------------------------------------------------------------------------------{
plot(level != level[1] ? na : level, "Market Shift Levels", color = bar_index % 2 == 0 ? color(na) : chart.fg_color, style = plot.style_linebr)
// Possible Reversals
if high[2] < level and high < level and high[1] > level
label.new(bar_index-1, high[1]
, str.tostring(lbl_data ? vol_sum : high[1], lbl_data ? format.volume : "#,####.###") + "\n⬙"
, color = color(na)
, textcolor = color.red
, style = label.style_label_down)
if low[2] > level and low[1] < level and low > level
label.new(bar_index-1, low[1]
, "⬘\n" + str.tostring(lbl_data ? vol_sum : high[1], lbl_data ? format.volume : "#,####.###")
, color = color(na)
, textcolor = color.lime
, style = label.style_label_up)
// Candles Color
barcolor(shift_col, title = "Bar Color")
plotcandle(open, high, low, close, title='Candles Color', color = shift_col, wickcolor=shift_col, bordercolor = shift_col, force_overlay = true)
// --------------------------------------------------------------------------------------------------------------------}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