Copy //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)