Average Sentiment Oscillator in MQL5 — converted from Pine Script
The Average Sentiment 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 |
|---|---|
| Period? | 10 |
| Calculation Method: | 0 |
Converted MQL5
Save as average-sentiment-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: 243a38709357 status: CLEAN
//| notes: 0 unsupported, 0 warn, 2 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Average Sentiment 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 2
#property indicator_plots 2
#property indicator_label1 "Plot1"
#property indicator_type1 DRAW_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 2
#include "PineRuntime.mqh"
//--- plot buffers
double Buf0[];
double Buf1[];
//--- 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 intrarange = PINE_NA;
double grouplow = PINE_NA;
double grouphigh = PINE_NA;
double groupopen = PINE_NA;
double grouprange = PINE_NA;
double K1 = PINE_NA;
double K2 = PINE_NA;
double intrabarbulls = PINE_NA;
double groupbulls = PINE_NA;
double intrabarbears = PINE_NA;
double groupbears = PINE_NA;
double TempBufferBulls = PINE_NA;
double TempBufferBears = PINE_NA;
double ASOBulls = PINE_NA;
double ASOBears = PINE_NA;
//--- live-tick rollback: the state each bar started from
double __sv_intrarange;
double __si_intrarange;
double __sv_grouplow;
double __si_grouplow;
double __sv_grouphigh;
double __si_grouphigh;
double __sv_groupopen;
double __si_groupopen;
double __sv_grouprange;
double __si_grouprange;
double __sv_K1;
double __si_K1;
double __sv_K2;
double __si_K2;
double __sv_intrabarbulls;
double __si_intrabarbulls;
double __sv_groupbulls;
double __si_groupbulls;
double __sv_intrabarbears;
double __si_intrabarbears;
double __sv_groupbears;
double __si_groupbears;
double __sv_TempBufferBulls;
double __si_TempBufferBulls;
double __sv_TempBufferBears;
double __si_TempBufferBears;
double __sv_ASOBulls;
double __si_ASOBulls;
double __sv_ASOBears;
double __si_ASOBears;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_intrarange = intrarange;
__sv_grouplow = grouplow;
__sv_grouphigh = grouphigh;
__sv_groupopen = groupopen;
__sv_grouprange = grouprange;
__sv_K1 = K1;
__sv_K2 = K2;
__sv_intrabarbulls = intrabarbulls;
__sv_groupbulls = groupbulls;
__sv_intrabarbears = intrabarbears;
__sv_groupbears = groupbears;
__sv_TempBufferBulls = TempBufferBulls;
__sv_TempBufferBears = TempBufferBears;
__sv_ASOBulls = ASOBulls;
__sv_ASOBears = ASOBears;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
intrarange = __sv_intrarange;
grouplow = __sv_grouplow;
grouphigh = __sv_grouphigh;
groupopen = __sv_groupopen;
grouprange = __sv_grouprange;
K1 = __sv_K1;
K2 = __sv_K2;
intrabarbulls = __sv_intrabarbulls;
groupbulls = __sv_groupbulls;
intrabarbears = __sv_intrabarbears;
groupbears = __sv_groupbears;
TempBufferBulls = __sv_TempBufferBulls;
TempBufferBears = __sv_TempBufferBears;
ASOBulls = __sv_ASOBulls;
ASOBears = __sv_ASOBears;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_intrarange = intrarange;
__si_grouplow = grouplow;
__si_grouphigh = grouphigh;
__si_groupopen = groupopen;
__si_grouprange = grouprange;
__si_K1 = K1;
__si_K2 = K2;
__si_intrabarbulls = intrabarbulls;
__si_groupbulls = groupbulls;
__si_intrabarbears = intrabarbears;
__si_groupbears = groupbears;
__si_TempBufferBulls = TempBufferBulls;
__si_TempBufferBears = TempBufferBears;
__si_ASOBulls = ASOBulls;
__si_ASOBears = ASOBears;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
intrarange = __si_intrarange;
grouplow = __si_grouplow;
grouphigh = __si_grouphigh;
groupopen = __si_groupopen;
grouprange = __si_grouprange;
K1 = __si_K1;
K2 = __si_K2;
intrabarbulls = __si_intrabarbulls;
groupbulls = __si_groupbulls;
intrabarbears = __si_intrabarbears;
groupbears = __si_groupbears;
TempBufferBulls = __si_TempBufferBulls;
TempBufferBears = __si_TempBufferBears;
ASOBulls = __si_ASOBulls;
ASOBears = __si_ASOBears;
pine_draw_reset();
__pineInitGen++;
}
double __ti0[]; // materialised ta.* source: TempBufferBulls
double __ti1[]; // materialised ta.* source: TempBufferBears
//--- inputs
input int length = 10; // Period?
input int mode = 0; // Calculation Method:
int OnInit()
{
if(length < 1) { Print("Period?", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
if(length > 100) { Print("Period?", " must be at most 100"); return INIT_PARAMETERS_INCORRECT; }
if(mode < 0) { Print("Calculation Method:", " must be at least 0"); return INIT_PARAMETERS_INCORRECT; }
if(mode > 2) { Print("Calculation Method:", " must be at most 2"); return INIT_PARAMETERS_INCORRECT; }
SetIndexBuffer(0, Buf0, INDICATOR_DATA);
SetIndexBuffer(1, Buf1, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, C'41,98,255');
PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'242,54,69');
IndicatorSetString(INDICATOR_SHORTNAME, "Average Sentiment 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[])
{
intrarange = (high[__pineBar] - low[__pineBar]);
grouplow = pine_lowest(low, __pineBar, (int)(length));
grouphigh = pine_highest(high, __pineBar, (int)(length));
groupopen = pine_hist(open, __pineBar, (int)((length - 1)));
grouprange = (grouphigh - grouplow);
K1 = ((intrarange == 0) ? 1 : intrarange);
K2 = ((grouprange == 0) ? 1 : grouprange);
intrabarbulls = pine_div((pine_div(((close[__pineBar] - low[__pineBar]) + (high[__pineBar] - open[__pineBar])), 2) * 100), K1);
groupbulls = pine_div((pine_div(((close[__pineBar] - grouplow) + (grouphigh - groupopen)), 2) * 100), K2);
intrabarbears = pine_div((pine_div(((high[__pineBar] - close[__pineBar]) + (open[__pineBar] - low[__pineBar])), 2) * 100), K1);
groupbears = pine_div((pine_div(((grouphigh - close[__pineBar]) + (groupopen - grouplow)), 2) * 100), K2);
TempBufferBulls = ((mode == 0) ? pine_div((intrabarbulls + groupbulls), 2) : ((mode == 1) ? intrabarbulls : groupbulls));
TempBufferBears = ((mode == 0) ? pine_div((intrabarbears + groupbears), 2) : ((mode == 1) ? intrabarbears : groupbears));
__ti0[__pineBar] = TempBufferBulls;
ASOBulls = pine_sma(__ti0, __pineBar, (int)(length));
__ti1[__pineBar] = TempBufferBears;
ASOBears = pine_sma(__ti1, __pineBar, (int)(length));
Buf0[__pineBar] = ASOBulls;
Buf1[__pineBar] = ASOBears;
}
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(__ti0, rates_total);
ArrayResize(__ti1, rates_total);
if(prev_calculated == 0)
{
ArrayInitialize(__ti0, PINE_NA);
ArrayInitialize(__ti1, 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("Average Sentiment Oscillator", shorttitle="ASO")
length=input(10,"Period?",minval=1,maxval=100)
mode=input(0,"Calculation Method:",minval=0,maxval=2)
intrarange=high-low
grouplow=lowest(low,length)
grouphigh=highest(high,length)
groupopen=open[length-1]
grouprange=grouphigh-grouplow
K1=intrarange==0 ? 1 : intrarange
K2=grouprange==0 ? 1 : grouprange
intrabarbulls=((((close-low)+(high-open))/2)*100)/K1
groupbulls=((((close-grouplow)+(grouphigh-groupopen))/2)*100)/K2
intrabarbears=((((high-close)+(open-low))/2)*100)/K1
groupbears=((((grouphigh-close)+(groupopen-grouplow))/2)*100)/K2
TempBufferBulls= mode==0 ? (intrabarbulls+groupbulls)/2 : mode==1 ? intrabarbulls : groupbulls
TempBufferBears= mode==0 ? (intrabarbears+groupbears)/2 : mode==1 ? intrabarbears : groupbears
ASOBulls=sma(TempBufferBulls,length)
ASOBears=sma(TempBufferBears,length)
plot(ASOBulls,color=color.blue,linewidth=2)
plot(ASOBears,color=color.red,linewidth=2)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