Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. //@version=4
  2. strategy(
  3. title = "Auto Super Trend",
  4. initial_capital = 1000,
  5. overlay = true,
  6. commission_type = strategy.commission.percent,
  7. commission_value= 0.075)
  8.  
  9. sma_average = "SMA"
  10. ema_average = "EMA"
  11. wma_average = "WMA"
  12. hma_average = "HMA"
  13. vwma_average = "VWMA"
  14. rma_average = "RMA"
  15. alma_average = "ALMA"
  16.  
  17. // Inputs
  18. source = input(close, title = "Source")
  19.  
  20. atr_length = input(16, title = "ATR - Length", type = input.integer, minval = 1)
  21. atr_multiplier = input(4.4, title = "ATR - Multiplier", type = input.float, minval = 0.1, step = 0.1)
  22.  
  23. average_length = input(200, title = "Average - Length", type = input.integer, minval = 1)
  24. average_type = input(ema_average, title = "Average - Type", options = [sma_average, ema_average, wma_average, hma_average, vwma_average, rma_average, alma_average])
  25.  
  26. average_alma_offset = input(0.85, title = "Average - ALMA - Offset", type = input.float, minval = 0.05, step = 0.05)
  27. average_alma_sigma = input(10, title = "Average - ALMA - Sigma", type = input.integer, minval = 1)
  28.  
  29. trend_wicks = input(false, title = "Trend - Wicks", type = input.bool)
  30.  
  31. // TODO: Re-think this
  32. use_resolution = input(false)
  33. resolution = input("15", type = input.resolution)
  34.  
  35. trade_both = "Both", trade_long = "Long", trade_short = "Short"
  36. trade_direction = input("Both", title = "Trade - Direction", options = [trade_both, trade_long, trade_short])
  37. trade_leverage = input(5, title = "Trade - Leverage", type = input.integer, minval = 1, maxval = 100)
  38. trade_stop = input(10, title = "Trade - Stop Loss %", type = input.float, minval = 0.5, step = 0.5, maxval = 100)
  39. trade_trail_threshold = input(5, title = "Trade - Trail Stop Threshold %", type = input.float, minval = 0.5, step = 0.5, maxval = 100)
  40. trade_trail = input(5, title = "Trade - Trail Stop %", type = input.float, minval = 0.5, step = 0.5, maxval = 100)
  41. trade_risk = input(100, title = "Trade - Risk %", type = input.integer, step = 1, minval = 1, maxval = 100)
  42.  
  43. test_year = input(2019, "Test - Year", type = input.integer, minval = 1970, maxval = 2222)
  44. test_month = input(01, "Test - Month", type = input.integer, minval = 1, maxval = 12)
  45. test_day = input(01, "Test - Day", type = input.integer, minval = 1, maxval = 31)
  46.  
  47. // Functions
  48. get_round(value, precision) => round(value * (pow(10, precision))) / pow(10, precision)
  49.  
  50. get_atr() =>
  51. atr(atr_length) * atr_multiplier
  52.  
  53. get_average(values, length, type) =>
  54. type == sma_average ? sma(values, length) :
  55. type == ema_average ? ema(values, length) :
  56. type == wma_average ? wma(values, length) :
  57. type == hma_average ? wma(2 * wma(values, length / 2) - wma(values, length), round(sqrt(length))) :
  58. type == vwma_average ? vwma(values, length) :
  59. type == rma_average ? rma(values, length) :
  60. type == alma_average ? alma(values, length, average_alma_offset, average_alma_sigma) : na
  61.  
  62.  
  63. frame = use_resolution ? timeframe.period : resolution
  64.  
  65. low_average = security(syminfo.tickerid, frame, get_average(low, average_length, average_type))
  66. high_average = security(syminfo.tickerid, frame, get_average(high, average_length, average_type))
  67.  
  68. // Strategy
  69. atr = get_atr()
  70.  
  71. trend_bot = hl2 - atr
  72. trend_bot_prev = nz(trend_bot[1], trend_bot)
  73. trend_bot := (trend_wicks ? low[1] : close[1]) > trend_bot_prev ? max(trend_bot, trend_bot_prev) : trend_bot
  74.  
  75. trend_top = hl2 + atr
  76. trend_top_prev = nz(trend_top[1], trend_top)
  77. trend_top := (trend_wicks ? high[1] : close[1]) < trend_top_prev ? min(trend_top, trend_top_prev) : trend_top
  78.  
  79. // Trade Conditions
  80. position_signal = 0, position_signal := nz(position_signal[1], 0)
  81.  
  82. // TODO: Test re-generating signals with each higher high
  83. enter_long_condition = position_signal < 1 and (trend_wicks ? high : close) > trend_top_prev
  84. enter_short_condition = position_signal > -1 and (trend_wicks ? low : close) < trend_bot_prev
  85.  
  86. exit_long_condition = position_signal < 0 and position_signal[1] > 0
  87. exit_short_condition = position_signal > 0 and position_signal[1] < 0
  88.  
  89. position_signal :=
  90. enter_long_condition ? 1 :
  91. enter_short_condition ? -1 :
  92. exit_long_condition or exit_short_condition ? 0 :
  93. position_signal[1]
  94.  
  95. // Positions
  96. test_time = timestamp(test_year, test_month, test_day, 0, 0)
  97.  
  98. // TODO: Known bug with opening positions
  99. if (time >= test_time and strategy.opentrades == 0)
  100. contracts = get_round((strategy.equity * trade_leverage / close) * (trade_risk / 100), 4)
  101.  
  102. if (trade_direction == trade_both or trade_direction == trade_long)
  103. strategy.entry(
  104. "LONG",
  105. strategy.long,
  106. qty = contracts,
  107. when = enter_long_condition)
  108.  
  109. if (trade_direction == trade_both or trade_direction == trade_short)
  110. strategy.entry(
  111. "SHORT",
  112. strategy.short,
  113. qty = contracts,
  114. when = enter_short_condition)
  115.  
  116. in_long = strategy.position_size > 0
  117. in_short = strategy.position_size < 0
  118.  
  119. float long_high = na
  120. float short_low = na
  121.  
  122. long_high := in_long ? high >= nz(long_high[1], low) ? high : long_high[1] : na
  123. short_low := in_short ? low <= nz(short_low[1], high) ? low : short_low[1] : na
  124.  
  125. threshold_difference = (strategy.position_avg_price / trade_leverage) * (trade_trail_threshold / 100)
  126.  
  127. long_trail_threshold = in_long ? strategy.position_avg_price + threshold_difference : na
  128. short_trail_threshold = in_short ? strategy.position_avg_price - threshold_difference : na
  129.  
  130. long_trail = in_long and long_high > long_trail_threshold ?
  131. long_high - (long_high / trade_leverage) * (trade_trail / 100) : na
  132. short_trail = in_short and short_low < short_trail_threshold ?
  133. short_low + (short_low / trade_leverage) * (trade_trail / 100) : na
  134.  
  135. stop_difference = (strategy.position_avg_price / trade_leverage) * (trade_stop / 100)
  136.  
  137. long_stop = in_long ? long_high > long_trail_threshold ? long_trail : strategy.position_avg_price - stop_difference : na
  138. short_stop = in_short ? short_low < short_trail_threshold ? short_trail : strategy.position_avg_price + stop_difference : na
  139.  
  140. strategy.exit("S/L", "LONG",
  141. stop = long_stop,
  142. qty = abs(get_round(strategy.position_size, 4)))
  143.  
  144. strategy.exit("S/L", "SHORT",
  145. stop = short_stop,
  146. qty = abs(get_round(strategy.position_size, 4)))
  147.  
  148. strategy.close_all(when = abs(change(position_signal)) > 0)
  149.  
  150. // Plots
  151. plot(strategy.position_avg_price, color = color.blue, linewidth = 2, style = plot.style_linebr, transp = 0)
  152.  
  153. plot(long_stop, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
  154. plot(short_stop, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
  155.  
  156. plot(long_high, color = color.purple, linewidth = 2, style = plot.style_linebr, transp = 0)
  157. plot(short_low, color = color.purple, linewidth = 2, style = plot.style_linebr, transp = 0)
  158.  
  159. plot(long_trail_threshold, color = color.orange, linewidth = 2, style = plot.style_linebr, transp = 0)
  160. plot(short_trail_threshold, color = color.orange, linewidth = 2, style = plot.style_linebr, transp = 0)
  161.  
  162. plot(long_trail, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
  163. plot(short_trail, color = color.red, linewidth = 2, style = plot.style_linebr, transp = 0)
  164.  
  165. bgcolor(color = position_signal > 0 ? color.green : color.red)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement