MTF Stochastic
MTF Stochastic - free TradingView bot strategy and study.
Last updated
MTF Stochastic - free TradingView bot strategy and study.
Last updated
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 bot on Wunderbit Trading platform.
Open-strategy source
Strategy adjustment for particular exchange, pair and timeframe
Backtest
Revised study script that you can apply in Wunderbit Trading platform for bot trading.
This week we found a nice strategy for a 30 min time frame working for long and short entries for BTC-perpetual futures on FTX. The original idea was taken from: 03.freeman.You will find the original strategy code here: MTF stochastic strategy.
IMPORTANT!
This strategy was adjusted to make faster entry on lower time frames.
We added separate inputs for short conditions
You must set up trailing stops so the strategy can be executed according to backtest
We added inputs for the period selection, so you could see how the strategy is performing on a monthly basis.
Settings – Applicable to FTX: BTC-PERP 30 min
You can copy this code and paste it into your TradingView.
//Updated by: Wunderbit Trading
//Original Idea by: 03.freeman
//@version=4
strategy("MTF stochastic strategy", overlay=false, pyramiding=0, 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(20, minval=1, title="Long Length for Main Stochastic")
smoothK_long = input(8, minval=1, title="Long SmoothK for Main Stochastic")
smoothD_long = input(8, minval=1, title="Long SmoothD for Main Stochastic")
upLine_long = input(85, 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(61, minval=1, title="Short Length for Main Stochastic")
smoothK_short = input(36, minval=1, title="Short SmoothK for Main Stochastic")
smoothD_short = input(15, minval=1, title="Short SmoothD for Main Stochastic")
lowLine_short = input(15, 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(5, title='Long Stop Loss %', step=0.1)/100
long_trailing = input(1.3, 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(5, title='Short Stop Loss %', step=0.1)/100
short_trailing = input(1.3, 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)
/// STRATEGY CONDITIONS ///
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
entry_price_long=valuewhen(entry_long,close,0)
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
entry_price_short=valuewhen(entry_short,close,0)
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
//// STRATEGY EXECUTION ////
if testPeriod()
strategy.entry(id="Long", long=true, when=entry_long)
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)
strategy.close(id="Long", when=exit_long, comment = "Exit")
strategy.entry(id="Short", long=false, when=entry_short)
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)
strategy.close(id="Short", when=exit_short, comment = "Exit")
You can copy this code and paste it into your TradingView.
//Created by: Wunderbit Trading
//@version=4
study("MTF stochastic strategy", overlay=true, max_bars_back=5000)
//
//this strategy is inspired to bobby thread in forexfactory forum
//
len_long = input(20, minval=1, title="Long Length for Main Stochastic")
smoothK_long = input(8, minval=1, title="Long SmoothK for Main Stochastic")
smoothD_long = input(8, minval=1, title="Long SmoothD for Main Stochastic")
upLine_long = input(85, 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(61, minval=1, title="Short Length for Main Stochastic")
smoothK_short = input(36, minval=1, title="Short SmoothK for Main Stochastic")
smoothD_short = input(15, minval=1, title="Short SmoothD for Main Stochastic")
lowLine_short = input(15, 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'?'240':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 TAKE TAKE PROFIT / TRAILING STOP ///
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
long_trailing_act_price = input(1.2,'Long Trailing Activation Price', step=0.1)/100
long_trailing_profit_input=input(1.2, title="Long Trailing Profit %", step=0.1)/100
/// SHORT TAKE TAKE PROFIT / TRAILING STOP ///
short_sl_inp = input(5, title='Short Stop Loss %', step=0.1)/100
short_trailing_act_price_inp = input(1.2,'Short Trailing Activation Price', step=0.1)/100
short_trailing_profit_input=input(1.2, title="Long Trailing Profit %", step=0.1)/100
/// STRATEGY CONDITIONS ///
/// LONG CONDITION ///
isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)
isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)
entry_long = not isEntry_Long and (crossover(mtfK_long, 50) and k_long > 50 and change(k_long, 1) > 0 and k_long > d_long and mtfK_long > mtfD_long)
entry_price_long=valuewhen(entry_long,close,0)
long_traling_activation_price = entry_price_long * (1 + long_trailing_act_price)
//plot(long_traling_activation_price, color=color.green)
long_trailing_condition = open > long_traling_activation_price
highest_price = valuewhen(long_trailing_condition, high, 0)
highest_pine(src, len) =>
max = src[0]
for i = 1 to len by 1
if src[i] > max
max := src[i]
max
max
long_o = 0.0
long_o := highest_pine(high, barssince(entry_long))
//plot(long_o, color=color.orange)
long_valuewhen_1 = valuewhen(long_trailing_condition, high, 0)
long_valuewhen_2 = valuewhen(long_trailing_condition, high, 0)
highest_price := long_trailing_condition == false ? na :
long_valuewhen_1 > long_o ? long_valuewhen_2 : highest_price[1]
long_trailing_profit = long_o * (1 - long_trailing_profit_input)
//plot(long_trailing_profit, color=color.white)
long_trailing = long_trailing_condition ? long_trailing_profit : na
SL_long = entry_price_long * (1 - long_sl_inp)
exit_long = not isExit_Long and (crossunder(mtfD_long, upLine_long) or low < SL_long or low < long_trailing)
if (entry_long)
isEntry_Long := true
isExit_Long := false
if (exit_long)
isEntry_Long := false
isExit_Long := true
//// SHORT CONDITION
isEntry_Short = false
isEntry_Short := nz(isEntry_Short[1], false)
isExit_Short = false
isExit_Short := nz(isExit_Short[1], false)
entry_short = not isEntry_Short and (crossunder(mtfD_short, 50) and k_short < 50 and change(k_short, 1) < 0 and k_short < d_short and mtfK_short < mtfD_short)
entry_price_short=valuewhen(entry_short,close,0)
short_traling_activation_price = entry_price_short * (1 - short_trailing_act_price_inp)
//plot(short_traling_activation_price, color=color.green)
short_trailing_condition = close < short_traling_activation_price
lowest_price = valuewhen(short_trailing_condition, low, 0)
lowest_pine(src, len) =>
max = src[0]
for i = 1 to len by 1
if src[i] < max
max := src[i]
max
max
short_o = 0.0
short_o := lowest_pine(low, barssince(entry_short))
//plot(short_o, color=color.orange)
short_valuewhen_1 = valuewhen(short_trailing_condition, low, 0)
short_valuewhen_2 = valuewhen(short_trailing_condition, low, 0)
lowest_price := short_trailing_condition == false ? na :
short_valuewhen_1 > short_o ? short_valuewhen_2 : lowest_price[1]
short_trailing_profit = short_o * (1 + short_trailing_profit_input)
//plot(short_trailing_profit, color=color.white)
short_trailing = short_trailing_condition ? short_trailing_profit : na
SL_short = entry_price_short * (1 + short_sl_inp)
exit_short = not isExit_Short and (crossover(mtfK_short, lowLine_short) or high > SL_short or high > short_trailing)
if (entry_short)
isEntry_Short := true
isExit_Short := false
if (exit_short)
isEntry_Short := false
isExit_Short := true
//// STRATEGY EXECUTION ////
alertcondition(entry_long, title="Enter Long")
alertcondition(exit_long, title="Exit Long")
plotshape(series=entry_long, text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=exit_long, text="EXIT BUY",style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)
// SHORT
alertcondition(entry_short, title="Enter Short")
alertcondition(exit_short, title="Exit Short")
plotshape(series=entry_short, text="SELL", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plotshape(series=exit_short, text="EXIT SELL",style=shape.triangleup, location=location.belowbar, color=color.purple, size=size.small)
Input
Value
Long Length for Main Stochastic
20
Long SmoothK For Main Stochastic
8
Long SmoothD For Main Stochastic
8
Long Upper Line Value
85
Short Length For Main Stochastic
61
Short SmoothK For Main Stochastic
36
Short SmoothD For Main Stochastic
15
Short Lower Line Value
15
Long Stop Loss %
5
Trailing Stop Long
1.3
Short Stop Loss %
5
Trailing Stop Short
1.3