Forecast Oscillator in MQL5 — converted from Pine Script
The Forecast Oscillator 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).
Inputs
| Input | Default |
|---|---|
| src | close |
| Length | 14 |
Plots
TSF.
Converted MQL5
Save as forecast-oscillator.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: 7c8c26f42074 status: CLEAN
//| notes: 0 unsupported, 0 warn, 1 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Forecast Oscillator
//| 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 3
#property indicator_plots 2
#property indicator_label1 "TSF"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_label2 "Plot2"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrDodgerBlue
#property indicator_width2 1
#include "PineRuntime.mqh"
//--- plot buffers
double Buf0[];
double Buf1[];
double Buf2[];
color __pal0[64]; // the colours plot 0 has drawn so far; see pine_color_slot
int __paln0 = 0;
//--- 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 lrc = PINE_NA;
double lrc1 = PINE_NA;
double lrs = PINE_NA;
double TSF[];
double fosc[];
bool col12 = false;
bool col32 = false;
double color2 = PINE_NA;
//--- live-tick rollback: the state each bar started from
double __sv_lrc;
double __si_lrc;
double __sv_lrc1;
double __si_lrc1;
double __sv_lrs;
double __si_lrs;
bool __sv_col12;
bool __si_col12;
bool __sv_col32;
bool __si_col32;
double __sv_color2;
double __si_color2;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_lrc = lrc;
__sv_lrc1 = lrc1;
__sv_lrs = lrs;
__sv_col12 = col12;
__sv_col32 = col32;
__sv_color2 = color2;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
lrc = __sv_lrc;
lrc1 = __sv_lrc1;
lrs = __sv_lrs;
col12 = __sv_col12;
col32 = __sv_col32;
color2 = __sv_color2;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_lrc = lrc;
__si_lrc1 = lrc1;
__si_lrs = lrs;
__si_col12 = col12;
__si_col32 = col32;
__si_color2 = color2;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
lrc = __si_lrc;
lrc1 = __si_lrc1;
lrs = __si_lrs;
col12 = __si_col12;
col32 = __si_col32;
color2 = __si_color2;
pine_draw_reset();
__pineInitGen++;
}
double __src[]; // Pine's `src` as a real series, filled per bar
//--- inputs
input ENUM_APPLIED_PRICE src = PRICE_CLOSE; // src
input int len = 14; // Length
int OnInit()
{
if(len < 1) { Print("Length", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
SetIndexBuffer(0, Buf0, INDICATOR_DATA);
SetIndexBuffer(1, Buf1, INDICATOR_COLOR_INDEX);
SetIndexBuffer(2, Buf2, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'41,98,255');
IndicatorSetString(INDICATOR_SHORTNAME, "Forecast Oscillator");
__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);
if(__pineBar > 0)
{
TSF[__pineBar] = TSF[__pineBar - 1];
fosc[__pineBar] = fosc[__pineBar - 1];
}
lrc = pine_linreg(__src, __pineBar, (int)(len), (int)(0));
lrc1 = pine_linreg(__src, __pineBar, (int)(len), (int)(1));
lrs = (lrc - lrc1);
TSF[__pineBar] = (pine_linreg(__src, __pineBar, (int)(len), (int)(0)) + lrs);
fosc[__pineBar] = pine_div((100 * (pine_source(src, open, high, low, close, __pineBar) - pine_hist(TSF, __pineBar, (int)(1)))), pine_source(src, open, high, low, close, __pineBar));
col12 = (fosc[__pineBar] > pine_hist(fosc, __pineBar, (int)(1)));
col32 = (fosc[__pineBar] < pine_hist(fosc, __pineBar, (int)(1)));
color2 = (col12 ? C'33,196,0' : (col32 ? C'150,0,18' : C'41,98,255'));
double __pinePlotValue0 = fosc[__pineBar];
double __pinePlotColour0 = (double)(color2);
Buf0[__pineBar] = __pinePlotValue0;
Buf1[__pineBar] = pine_color_slot(0, pine_na(__pinePlotColour0) ? clrNONE : (color)__pinePlotColour0, __pal0, __paln0);
Buf2[__pineBar] = 0;
}
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(TSF, rates_total);
ArrayResize(fosc, rates_total);
ArrayResize(__src, rates_total);
if(prev_calculated == 0)
{
ArrayInitialize(TSF, PINE_NA);
ArrayInitialize(fosc, PINE_NA);
ArrayInitialize(__src, 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("Forecast Oscillator", overlay=false)
src = input(close)
len = input(defval=14, minval=1, title="Length")
lrc = linreg(src, len, 0)
lrc1 = linreg(src,len,1)
lrs = (lrc-lrc1)
TSF = linreg(src, len, 0)+lrs
fosc=100*(src-TSF[1])/src
col12 = fosc > fosc[1]
col32 = fosc < fosc[1]
color2 = col12 ? #21C400 : col32 ? #960012 : color.blue
plot(fosc, color = color2,linewidth=2, title = "TSF")
hline(0, linestyle=2, color=color.blue)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