Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- strategy(
- title = "Auto Super Trend",
- initial_capital = 1000,
- overlay = true,
- commission_type = strategy.commission.percent,
- commission_value= 0.075)
- sma_average = "SMA"
- ema_average = "EMA"
- wma_average = "WMA"
- hma_average = "HMA"
- vwma_average = "VWMA"
- rma_average = "RMA"
- alma_average = "ALMA"
- // Inputs
- source = input(close, title = "Source")
- atr_length = input(16, title = "ATR - Length", type = input.integer, minval = 1)
- atr_multiplier = input(4.4, title = "ATR - Multiplier", type = input.float, minval = 0.1, step = 0.1)
- average_length = input(200, title = "Average - Length", type = input.integer, minval = 1)
- average_type = input(ema_average, title = "Average - Type", options = [sma_average, ema_average, wma_average, hma_average, vwma_average, rma_average, alma_average])
- average_alma_offset = input(0.85, title = "Average - ALMA - Offset", type = input.float, minval = 0.05, step = 0.05)
- average_alma_sigma = input(10, title = "Average - ALMA - Sigma", type = input.integer, minval = 1)
- trend_wicks = input(false, title = "Trend - Wicks", type = input.bool)
- // TODO: Re-think this
- use_resolution = input(false)
- resolution = input("15", type = input.resolution)
- trade_both = "Both", trade_long = "Long", trade_short = "Short"
- trade_direction = input("Both", title = "Trade - Direction", options = [trade_both, trade_long, trade_short])
- trade_leverage = input(5, title = "Trade - Leverage", type = input.integer, minval = 1, maxval = 100)
- trade_stop = input(10, title = "Trade - Stop Loss %", type = input.float, minval = 0.5, step = 0.5, maxval = 100)
- trade_trail_threshold = input(5, title = "Trade - Trail Stop Threshold %", type = input.float, minval = 0.5, step = 0.5, maxval = 100)
- trade_trail = input(5, title = "Trade - Trail Stop %", type = input.float, minval = 0.5, step = 0.5, maxval = 100)
- trade_risk = input(100, title = "Trade - Risk %", type = input.integer, step = 1, minval = 1, maxval = 100)
- test_year = input(2019, "Test - Year", type = input.integer, minval = 1970, maxval = 2222)
- test_month = input(01, "Test - Month", type = input.integer, minval = 1, maxval = 12)
- test_day = input(01, "Test - Day", type = input.integer, minval = 1, maxval = 31)
- // Functions
- get_round(value, precision) => round(value * (pow(10, precision))) / pow(10, precision)
- get_atr() =>
- atr(atr_length) * atr_multiplier
- get_average(values, length, type) =>
- type == sma_average ? sma(values, length) :
- type == ema_average ? ema(values, length) :
- type == wma_average ? wma(values, length) :
- type == hma_average ? wma(2 * wma(values, length / 2) - wma(values, length), round(sqrt(length))) :
- type == vwma_average ? vwma(values, length) :
- type == rma_average ? rma(values, length) :
- type == alma_average ? alma(values, length, average_alma_offset, average_alma_sigma) : na
- frame = use_resolution ? timeframe.period : resolution
- low_average = security(syminfo.tickerid, frame, get_average(low, average_length, average_type))
- high_average = security(syminfo.tickerid, frame, get_average(high, average_length, average_type))
- // Strategy
- atr = get_atr()
- trend_bot = hl2 - atr
- trend_bot_prev = nz(trend_bot[1], trend_bot)
- trend_bot := (trend_wicks ? low[1] : close[1]) > trend_bot_prev ? max(trend_bot, trend_bot_prev) : trend_bot
- trend_top = hl2 + atr
- trend_top_prev = nz(trend_top[1], trend_top)
- trend_top := (trend_wicks ? high[1] : close[1]) < trend_top_prev ? min(trend_top, trend_top_prev) : trend_top
- // Trade Conditions
- position_signal = 0, position_signal := nz(position_signal[1], 0)
- // TODO: Test re-generating signals with each higher high
- enter_long_condition = position_signal < 1 and (trend_wicks ? high : close) > trend_top_prev
- enter_short_condition = position_signal > -1 and (trend_wicks ? low : close) < trend_bot_prev
- exit_long_condition = position_signal < 0 and position_signal[1] > 0
- exit_short_condition = position_signal > 0 and position_signal[1] < 0
- position_signal :=
- enter_long_condition ? 1 :
- enter_short_condition ? -1 :
- exit_long_condition or exit_short_condition ? 0 :
- position_signal[1]
- // Positions
- test_time = timestamp(test_year, test_month, test_day, 0, 0)
- // TODO: Known bug with opening positions
- if (time >= test_time and strategy.opentrades == 0)
- contracts = get_round((strategy.equity * trade_leverage / close) * (trade_risk / 100), 4)
- if (trade_direction == trade_both or trade_direction == trade_long)
- strategy.entry(
- "LONG",
- strategy.long,
- qty = contracts,
- when = enter_long_condition)
- if (trade_direction == trade_both or trade_direction == trade_short)
- strategy.entry(
- "SHORT",
- strategy.short,
- qty = contracts,
- when = enter_short_condition)
- in_long = strategy.position_size > 0
- in_short = strategy.position_size < 0
- float long_high = na
- float short_low = na
- long_high := in_long ? high >= nz(long_high[1], low) ? high : long_high[1] : na
- short_low := in_short ? low <= nz(short_low[1], high) ? low : short_low[1] : na
- threshold_difference = (strategy.position_avg_price / trade_leverage) * (trade_trail_threshold / 100)
- long_trail_threshold = in_long ? strategy.position_avg_price + threshold_difference : na
- short_trail_threshold = in_short ? strategy.position_avg_price - threshold_difference : na
- long_trail = in_long and long_high > long_trail_threshold ?
- long_high - (long_high / trade_leverage) * (trade_trail / 100) : na
- short_trail = in_short and short_low < short_trail_threshold ?
- short_low + (short_low / trade_leverage) * (trade_trail / 100) : na
- stop_difference = (strategy.position_avg_price / trade_leverage) * (trade_stop / 100)
- long_stop = in_long ? long_high > long_trail_threshold ? long_trail : strategy.position_avg_price - stop_difference : na
- short_stop = in_short ? short_low < short_trail_threshold ? short_trail : strategy.position_avg_price + stop_difference : na
- strategy.exit("S/L", "LONG",
- stop = long_stop,
- qty = abs(get_round(strategy.position_size, 4)))
- strategy.exit("S/L", "SHORT",
- stop = short_stop,
- qty = abs(get_round(strategy.position_size, 4)))
- strategy.close_all(when = abs(change(position_signal)) > 0)
- // Plots
- plot(strategy.position_avg_price, color = color.blue, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(long_stop, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(short_stop, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(long_high, color = color.purple, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(short_low, color = color.purple, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(long_trail_threshold, color = color.orange, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(short_trail_threshold, color = color.orange, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(long_trail, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
- plot(short_trail, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
- bgcolor(color = position_signal > 0 ? color.green : color.red)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement