Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Indicator | Sample pine script stochastic divergence

The purpose of this code is to convey an idea to people seeking simple and functional approach to converting pine script to MQL5.

Here is the pine script code:

//@version=5    indicator(title='sample stochastic divergence', overlay=true)  stoch_len = input.int(title='stoch_len', defval=5)    f_top_fractal(_src) =>      _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and          _src[2] > _src[0]  f_bot_fractal(_src) =>      _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and          _src[2] < _src[0]  f_fractalize(_src) =>      f_bot_fractal__1 = f_bot_fractal(_src)      f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0    stoch = ta.stoch(close, high, low, stoch_len)    fractal_top = f_fractalize(stoch) > 0 ? stoch[2] : na  fractal_bot = f_fractalize(stoch) < 0 ? stoch[2] : na    high_prev = ta.valuewhen(fractal_top, stoch[2], 1)  high_price = ta.valuewhen(fractal_top, high[2], 1)  low_prev = ta.valuewhen(fractal_bot, stoch[2], 1)  low_price = ta.valuewhen(fractal_bot, low[2], 1)    regular_bearish_div = fractal_top and high[2] > high_price and stoch[2] < high_prev  regular_bullish_div = fractal_bot and low[2] < low_price and stoch[2] > low_prev    plot(title='H F', series=fractal_top ? high[2] : na, color=regular_bearish_div ? color.maroon : na, offset=-2)  plot(title='L F', series=fractal_bot ? low[2] : na, color=regular_bullish_div ? color.green : na, offset=-2)  plot(title='H D', series=fractal_top ? high[2] : na, style=plot.style_circles, color=regular_bearish_div ? color.maroon  : na , linewidth=3, offset=-2)  plot(title='L D', series=fractal_bot ? low[2] : na, style=plot.style_circles, color=regular_bullish_div ? color.green : na , linewidth=3, offset=-2)    plotshape(title='+RBD', series=regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=color.maroon, textcolor=color.white, offset=-2)  plotshape(title='-RBD', series=regular_bullish_div ? low[2] : na, text='R', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)  

MetaTrader Experts, Indicators, Scripts and Libraries

52935