MTF Stochastic – trading bot for Bybit

Free TradingView strategy script - ByBit Bitcoin Trading Bot

Top FREE Tradingview strategy For ByBit this week!

To make our community more profitable we decided to start a series in which we will review open-source TradingView strategies. The aim of the review is to select a strategy (or a number of strategies) from TradingView and run a reality check on it, optimise them and convert into alerts, so you can set it up as a free trading bot on Wunderbit Trading platform.

What we provide

  • Open-strategy source

  • Strategy adjustment for particular exchange, pair and timeframe

  • Backtest


This week we found a free stochastic indicator strategy for a 30 min time frame working for long and short entries for BTC perpetual contracts on ByBit Exchnage. The original idea was taken from: 03.freeman. Follow this guide to set up a free ByBit trading bot.

You will find the original strategy code here: MTF stochastic strategy.


  • This is a trend strategy and works better in the trending market

  • We added the trend identifier using the EMA and SMA interaction

  • We Added Take profit and stop loss levels

  • We added inputs for the period selection, so you could see how the strategy is performing on a monthly basis.


Applicable to ByBit: BTC-Perpetual contracts 30 min



Long Length for Main Stochastic


Long SmoothK For Main Stochastic


Long SmoothD For Main Stochastic


Long Upper Line Value


Short Length For Main Stochastic


Short SmoothK For Main Stochastic


Short SmoothD For Main Stochastic


Short Lower Line Value


Long Stop Loss %


Trailing Stop Long


Short Stop Loss %


Trailing Stop Short


Revised strategy script code

You can copy this code and paste it into your TradingView.

//Updated by: Wunderbit Trading
//Original Idea by: 03.freeman

strategy("MTF stochastic strategy", overlay=true, pyramiding=3, commission_type=strategy.commission.percent, commission_value=0.07, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD )
len_long = input(21, minval=1, title="Long Length for Main Stochastic")
smoothK_long = input(11, minval=1, title="Long SmoothK for Main Stochastic")
smoothD_long = input(21, minval=1, title="Long SmoothD for Main Stochastic")
upLine_long = input(73, minval=50, maxval=90, title=" Long Upper Line Value?")

// current stochastic calculation long
k_long = sma(stoch(close, high, low, len_long), smoothK_long)
d_long = sma(k_long, smoothD_long)

//mtf stochastic calculation smoothed with period long
MTF_period_long= timeframe.period=='5'?'1':timeframe.period=='15'?'5':timeframe.period=='30'?'15':timeframe.period=='60'?'30':timeframe.period=='240'?'60':timeframe.period=='D'?'240':timeframe.period=='W'?'D':'M'
mtfK_long = linreg(security(syminfo.tickerid,MTF_period_long, sma(stoch(close, high, low, len_long), smoothK_long)),len_long,0)
mtfD_long = linreg(security(syminfo.tickerid,MTF_period_long, sma(k_long, smoothD_long)),len_long,0)

len_short = input(55, minval=1, title="Short Length for Main Stochastic")
smoothK_short = input(35, minval=1, title="Short SmoothK for Main Stochastic")
smoothD_short = input(11, minval=1, title="Short SmoothD for Main Stochastic")
lowLine_short = input(13, minval=10, maxval=50, title=" Short Lower Line Value?")

// current stochastic calculation long
k_short = sma(stoch(close, high, low, len_short), smoothK_short)
d_short = sma(k_short, smoothD_short)

//mtf stochastic calculation smoothed with period long
MTF_period_short= timeframe.period=='5'?'1':timeframe.period=='15'?'5':timeframe.period=='30'?'15':timeframe.period=='60'?'30':timeframe.period=='240'?'60':timeframe.period=='D'?'':timeframe.period=='W'?'D':'M'
mtfK_short = linreg(security(syminfo.tickerid,MTF_period_short, sma(stoch(close, high, low, len_short), smoothK_short)),len_short,0)
mtfD_short = linreg(security(syminfo.tickerid,MTF_period_short, sma(k_short, smoothD_short)),len_short,0)

//long_tp_inp = input(0.5, title='Long Take Profit %', step=0.1)/10
long_sl_inp = input(3, title='Long Stop Loss %', step=0.1)/100
long_trailing = input(1.1, title='Trailing Stop Long', step=0.1) / 100

//long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)

//short_tp_inp = input(0.5, title='Short Take Profit %', step=0.1)/100
short_sl_inp = input(3, title='Short Stop Loss %', step=0.1)/100
short_trailing = input(1.1, title='Trailing Stop short', step=0.1) / 100

//short_take_level = strategy.position_avg_price * (1 - short_tp_inp)
short_stop_level = strategy.position_avg_price * (1 + short_sl_inp)


entry_long = crossover(mtfK_long, 50) and k_long > 50 and change(k_long, 1) > 0 and k_long > d_long and mtfK_long > mtfD_long
exit_long = crossunder(mtfD_long, upLine_long)

entry_short = crossunder(mtfD_short, 50) and k_short < 50 and change(k_short, 1) < 0 and k_short < d_short and mtfK_short < mtfD_short
exit_short = crossunder(mtfK_short, lowLine_short)

/// PERIOD ///
testStartYear = input(2019, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(9999, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(31, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false


if testPeriod()
    if strategy.position_size == 0 or strategy.position_size > 0
        strategy.entry(id="Long", long=true, when=entry_long, comment="Enter Long Comment")
    strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level,trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick, comment="Exit Long Comment")
    strategy.close(id="Long", when=exit_long, comment = "Exit Long Comment")

    if strategy.position_size == 0 or strategy.position_size < 0
        strategy.entry(id="Short", long=false, when=entry_short, comment = "Enter Short Comment")
    strategy.exit("Take Profit/ Stop Loss","Short", stop=short_stop_level, trail_points=entry_price_short * short_trailing / syminfo.mintick, trail_offset=entry_price_short * short_trailing / syminfo.mintick, comment = "Exit Short Comment")
    strategy.close(id="Short", when=exit_short, comment = "Exit Short Comment")

Follow this guide to automate your TradingView STRATEGY alerts. (no need for study script).

Last updated