Scalping Line in MQL5 — converted from Pine Script

The Scalping Line indicator by KivancOzbilgic, 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).

Direction
Pine Script → MQL5
Kind
indicator, separate window
Verdict
EQUIVALENT on 500 bars
Engine
pinec 0.6.46
Conversion notes
2 informational
Source
43 lines of Pine v4

Inputs

InputDefault
Sourceclose
Percent1.0
Main Period100
Signal Period7

Converted MQL5

Save as scalping-line.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: 39711c7ffa5b   status: CLEAN
//| notes: 0 unsupported, 0 warn, 2 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Scalping Line
//| 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   3
#property indicator_label1  "Plot1"
#property indicator_type1   DRAW_FILLING
#property indicator_color1  clrDodgerBlue
#property indicator_width1  1
#property indicator_label2  "SLI"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_width2  2
#property indicator_label3  "Plot3"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_width3  1

#include "PineRuntime.mqh"

//--- plot buffers
double PineFill0[];
double PineFill1[];
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 MA = PINE_NA;
double ssMA = PINE_NA;
double signalline = PINE_NA;
double ScalpLine = PINE_NA;
double color1 = PINE_NA;

//--- live-tick rollback: the state each bar started from
double __sv_MA;
double __si_MA;
double __sv_ssMA;
double __si_ssMA;
double __sv_signalline;
double __si_signalline;
double __sv_ScalpLine;
double __si_ScalpLine;
double __sv_color1;
double __si_color1;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_MA = MA;
   __sv_ssMA = ssMA;
   __sv_signalline = signalline;
   __sv_ScalpLine = ScalpLine;
   __sv_color1 = color1;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   MA = __sv_MA;
   ssMA = __sv_ssMA;
   signalline = __sv_signalline;
   ScalpLine = __sv_ScalpLine;
   color1 = __sv_color1;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_MA = MA;
   __si_ssMA = ssMA;
   __si_signalline = signalline;
   __si_ScalpLine = ScalpLine;
   __si_color1 = color1;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   MA = __si_MA;
   ssMA = __si_ssMA;
   signalline = __si_signalline;
   ScalpLine = __si_ScalpLine;
   color1 = __si_color1;
   pine_draw_reset();
   __pineInitGen++;
}

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 __src[];   // Pine's `src` as a real series, filled per bar
double __ts0[];   // materialised ta.* source: pine_sma(__src, __pineBar, (int)(MathCeil(pine_div(mainperiod, 2))))
double __ts1[];   // materialised ta.* source: 0
double __ti0[];   // materialised ta.* source: ScalpLine

//--- inputs
input ENUM_APPLIED_PRICE src = PRICE_CLOSE;   // Source
input double percent = 1.0;   // Percent
input int mainperiod = 100;   // Main Period
input int signalperiod = 7;   // Signal Period

int OnInit()
{
   if(percent < 0) { Print("Percent", " must be at least 0"); return INIT_PARAMETERS_INCORRECT; }
   SetIndexBuffer(0, PineFill0, INDICATOR_DATA);
   SetIndexBuffer(1, PineFill1, 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);
   PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'136,14,79');
   IndicatorSetString(INDICATOR_SHORTNAME, "Scalping Line");
   __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[])
{
   __src[__pineBar] = pine_source(src, open, high, low, close, __pineBar);
   __ts0[__pineBar] = pine_sma(__src, __pineBar, (int)(MathCeil(pine_div(mainperiod, 2))));
   __ts1[__pineBar] = 0;
   MA = pine_sma(__ts0, __pineBar, (int)((MathFloor(pine_div(mainperiod, 2)) + 1)));
   ssMA = ((MA > (close[__pineBar] + pine_div((MA * percent), 100))) ? MA : ((MA < (close[__pineBar] - pine_div((MA * percent), 100))) ? MA : close[__pineBar]));
   signalline = pine_sma(close, __pineBar, (int)(signalperiod));
   ScalpLine = (signalline - ssMA);
   color1 = ((ScalpLine >= 0) ? C'76,175,80' : C'242,54,69');
   __ti0[__pineBar] = ScalpLine;
   pine_alertcond(pine_crossover(__ti0, __ts1, __pineBar), "SLI BUY Signal!", 1253188629, __pineBar, __pineLastBar);
   __ti0[__pineBar] = ScalpLine;
   pine_alertcond(pine_crossunder(__ti0, __ts1, __pineBar), "SLI SELL Signal!", 641036419, __pineBar, __pineLastBar);
   Buf2[__pineBar] = ScalpLine;
   Buf3[__pineBar] = 0;
   PineFill0[__pineBar] = Buf2[__pineBar];
   PineFill1[__pineBar] = Buf3[__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(__st0, rates_total);
   ArrayResize(__stb0, rates_total);
   ArrayResize(__st1, rates_total);
   ArrayResize(__stb1, rates_total);
   ArrayResize(__src, rates_total);
   ArrayResize(__ts0, rates_total);
   ArrayResize(__ts1, rates_total);
   ArrayResize(__ti0, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(__st0, PINE_NA);
      ArrayInitialize(__stb0, PINE_NA);
      ArrayInitialize(__st1, PINE_NA);
      ArrayInitialize(__stb1, PINE_NA);
      ArrayInitialize(__src, PINE_NA);
      ArrayInitialize(__ts0, PINE_NA);
      ArrayInitialize(__ts1, 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

© KivancOzbilgic — 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/

// © KivancOzbilgic



//@version=4

study("Scalping Line", overlay=false)

src = input(close, title="Source")

percent = input(1.0,"Percent", type=input.float, step=0.1, minval=0)

mainperiod = input(100,"Main Period")

signalperiod = input(7,"Signal Period")

MA = sma(sma(src, ceil(mainperiod / 2)), floor(mainperiod / 2) + 1)

ssMA = MA>close+MA*percent/100 ? MA : MA<close-MA*percent/100 ? MA : close

signalline = sma(close,signalperiod)

ScalpLine = signalline-ssMA



k1=plot(ScalpLine,"SLI",color.maroon,2)

k2=plot(0,"")



color1= ScalpLine>=0 ? color.green : color.red

fill(k1,k2,color=color1,transp=80)

alertcondition(crossover(ScalpLine,0), title="BUY ALARM!", message="SLI BUY Signal!")

alertcondition(crossunder(ScalpLine,0), title="SELL ALARM!", message="SLI SELL Signal!")

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 converter

More: all example conversions · how Pine Script maps to MQL5 · how the engine measures