Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. //@version=4
  3. // kviateq, ahancock ©
  4.  
  5. trend_up = 1
  6. trend_down = -1
  7.  
  8. strategy(
  9. "unnamed [Strategy]",
  10. overlay = true,
  11. initial_capital = 10000,
  12. precision = 8,
  13. commission_type = strategy.commission.percent,
  14. commission_value= 0.05)
  15.  
  16.  
  17. // Inputs
  18. st_length = input(48, title = "Super Trend - Length", type = input.integer, minval = 1, step = 1)
  19. st_multiplier = input(8, title="Super Trend - Multiplier", type = input.float, minval = 1, step = 0.1)
  20.  
  21. jma_fast_length = input(125, title = "JMA - Fast Length", type = input.integer, minval = 1)
  22. jma_fast_phase = input(75, title = "JMA - Fast Phase", type = input.integer, minval = 1)
  23. jma_fast_power = input(1, title = "JMA - Fast Power", type = input.float, minval = 0.1)
  24.  
  25. jma_slow_length = input(350, title = "JMA - Slow Length", type = input.integer, minval = 1)
  26. jma_slow_phase = input(75, title = "JMA - Slow Phase", type = input.integer, minval = 1)
  27. jma_slow_power = input(1, title = "JMA - Slow Power", type = input.float, minval = 0.1)
  28.  
  29. range_resolution = input("D", title = "Range - Resolution", type = input.resolution)
  30. range_session = input("2300-2330:1234567", title = "Range - Session")
  31.  
  32. trade_both = "Both", trade_long = "Long", trade_short = "Short"
  33. trade_direction = input("Both", title = "Trade - Direction", options = [trade_both, trade_long, trade_short])
  34.  
  35. trade_risk = input(10, title = "Trade - Risk %", type = input.integer, step = 1, minval = 1, maxval = 100)
  36. trade_max_leverage = input(25, title = "Trade - Max Leverage %", type = input.integer, minval = 1, step = 1, maxval = 100)
  37. trade_max_contract = input(150, title = "Trade - Max Contracts", type = input.integer, minval = 1, step = 1, maxval = 1000)
  38.  
  39. trade_profit_take = input(1, title = "Trade - Profit Take %", type = input.float, minval = 0.1, step = 0.1)
  40. trade_profit_close = input(10, title = "Trade - Profit Close Take %", type = input.integer, minval = 1)
  41.  
  42. use_test_range = input(false, "Use Test Range", type = input.bool)
  43. test_year = input(2016, "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.  
  48. // Functions
  49. get_round(value, precision) => round(value * (pow(10, precision))) / pow(10, precision)
  50.  
  51. get_jma(values, length, phase, power) =>
  52.  
  53. jma = 0.0
  54. e0 = 0.0
  55. e1 = 0.0
  56. e2 = 0.0
  57.  
  58. ratio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
  59. beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2)
  60. alpha = pow(beta, power)
  61.  
  62. e0 := (1 - alpha) * values + alpha * nz(e0[1], values)
  63. e1 := (values - e0) * (1 - beta) + beta * nz(e1[1])
  64. e2 := (e0 + ratio * e1 - nz(jma[1], values)) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1])
  65.  
  66. jma := e2 + nz(jma[1], values)
  67. jma_slope = jma - nz(jma[1], values)
  68.  
  69. [jma, jma_slope]
  70.  
  71. get_super_trend(high_values, low_values, length, multiplier) =>
  72.  
  73. trend_bot = hl2 - atr(length) * multiplier
  74. trend_bot_prev = nz(trend_bot[1], trend_bot)
  75. trend_bot := low_values[1] > trend_bot_prev ? max(trend_bot, trend_bot_prev) : trend_bot
  76.  
  77. trend_top = hl2 + atr(length) * multiplier
  78. trend_top_prev = nz(trend_top[1], trend_top)
  79. trend_top := high_values[1] < trend_top_prev ? min(trend_top, trend_top_prev) : trend_top
  80.  
  81. trend_dir = 0
  82. trend_dir := high_values > trend_top_prev ? trend_up : low_values < trend_bot_prev ? trend_down : trend_dir[1]
  83.  
  84. trend_line = trend_dir == trend_up ? trend_bot : trend_top
  85.  
  86. [trend_top, trend_bot, trend_dir, trend_line]
  87.  
  88. is_new_bar(ses) =>
  89. t = time(range_resolution, ses)
  90. na(t[1]) and not na(t) or t[1] < t
  91.  
  92. is_session_open(ses) =>
  93. not na(time(timeframe.period, ses))
  94.  
  95.  
  96. // Strategy
  97. [st_trend_top, st_trend_bot, st_trend, st_line] = get_super_trend(close, close, st_length, st_multiplier)
  98.  
  99. [jma_fast, jma_fast_slope] = get_jma(close, jma_fast_length, jma_fast_phase, jma_fast_power)
  100. [jma_slow, jma_slow_slope] = get_jma(close, jma_slow_length, jma_slow_phase, jma_slow_power)
  101.  
  102. float session_high = na
  103. float session_low = na
  104.  
  105. float range_high = na
  106. float range_low = na
  107.  
  108. if (is_session_open(range_session))
  109. if (is_new_bar(range_session))
  110. session_high := high
  111. session_low := low
  112. else
  113. session_high := max(session_high[1], high)
  114. session_low := min(session_low[1], low)
  115. else
  116. session_high := session_high[1]
  117. session_low := session_low[1]
  118. range_high := nz(range_high, session_high)
  119. range_low := nz(range_low, session_low)
  120.  
  121.  
  122. // Conditions
  123. can_trade = not is_session_open(range_session)
  124.  
  125. range_long_condition = crossover(close, session_high)
  126. range_short_condition = crossunder(close, session_low)
  127.  
  128. jma_long_condition = close > jma_slow and close > jma_fast and jma_fast > jma_slow
  129. jma_short_condition = close < jma_slow and close < jma_fast and jma_fast < jma_slow
  130.  
  131. st_long_condition = crossover(close, st_line) //st_trend == trend_up
  132. st_short_condition = crossunder(close, st_line)// st_trend == trend_down
  133.  
  134. enter_long_condition = can_trade and jma_long_condition and (st_long_condition or range_long_condition and close > st_line)
  135. enter_short_condition = can_trade and jma_short_condition and (st_short_condition or range_short_condition and close < st_line)
  136.  
  137. exit_long_condition = not can_trade
  138. exit_short_condition = not can_trade
  139.  
  140.  
  141. // Backtesting
  142.  
  143. can_long = trade_direction == trade_long or trade_direction == trade_both
  144. can_short = trade_direction == trade_short or trade_direction == trade_both
  145.  
  146. int count = na, count := strategy.position_size == 0 ? na : nz(count[1])
  147.  
  148. float take_profit = na, take_profit := strategy.position_size == 0 ? na : nz(take_profit[1])
  149. float liquidation = na, liquidation := strategy.position_size == 0 ? na : nz(liquidation[1])
  150.  
  151. float trade_leverage = na
  152.  
  153. strategy.cancel_all()
  154.  
  155. if(can_long)
  156. if(enter_long_condition and strategy.position_size == 0)
  157.  
  158. trade_leverage := min(round((strategy.equity / (close - st_trend_bot)) * (trade_risk / 100)), trade_max_leverage)
  159. contracts = min(get_round(strategy.equity * trade_leverage / close, 2), trade_max_contract)
  160.  
  161. // Market order
  162. strategy.order(
  163. "LONG",
  164. strategy.long,
  165. qty = contracts,
  166. oca_name = "LONG OCA")
  167.  
  168. count := 1
  169. liquidation := close - (close / trade_leverage)
  170. take_profit := close * (1 + (trade_profit_take * (count / 100)))
  171.  
  172.  
  173. if (can_short)
  174. if (enter_short_condition and strategy.position_size == 0)
  175.  
  176. trade_leverage := min(round((strategy.equity / (st_trend_top - close)) * (trade_risk / 100)), trade_max_leverage)
  177. contracts = min(get_round(strategy.equity * trade_leverage / close, 2), trade_max_contract)
  178.  
  179. // Market order
  180. strategy.order(
  181. "SHORT",
  182. strategy.short,
  183. qty = contracts,
  184. oca_name = "SHORT OCA")
  185.  
  186. count := 1
  187. liquidation := close + (close / trade_leverage)
  188. take_profit := close * (1 - (trade_profit_take * (count / 100)))
  189.  
  190. if (strategy.position_size > 0)
  191.  
  192. if (high > take_profit)
  193. count := count + 1
  194.  
  195. take_profit := strategy.position_avg_price * (1 + (trade_profit_take * (count / 100)))
  196. amount = nz(strategy.position_size * (trade_profit_close / 100), 0.0)
  197.  
  198. // Limit order
  199. strategy.order(
  200. "T/P", strategy.short,
  201. limit = take_profit,
  202. qty = get_round(amount, 2),
  203. oca_name = "LONG OCA",
  204. oca_type = strategy.oca.reduce,
  205. when = abs(strategy.position_size) > 0.5)
  206.  
  207. // Market order
  208. strategy.order(
  209. "E/L", strategy.short,
  210. qty = get_round(abs(strategy.position_size), 2),
  211. oca_name = "LONG OCA",
  212. oca_type = strategy.oca.cancel,
  213. when = exit_long_condition)
  214.  
  215. // Market order
  216. strategy.order(
  217. "S/L", strategy.short,
  218. stop = st_line,
  219. qty = get_round(abs(strategy.position_size), 2),
  220. oca_name = "LONG OCA",
  221. oca_type = strategy.oca.cancel)
  222.  
  223.  
  224. if(strategy.position_size < 0)
  225.  
  226. if (low < take_profit)
  227. count := count + 1
  228.  
  229. take_profit := strategy.position_avg_price * (1 - (trade_profit_take * (count / 100)))
  230. amount = nz(abs(strategy.position_size) * (trade_profit_close / 100), 0.0)
  231.  
  232. // Limit order
  233. strategy.order(
  234. "T/P", strategy.long,
  235. limit = take_profit,
  236. qty = get_round(amount, 2),
  237. oca_name = "SHORT OCA",
  238. oca_type = strategy.oca.reduce,
  239. when = abs(strategy.position_size) > 0.5)
  240.  
  241. // Market order
  242. strategy.order(
  243. "E/L", strategy.long,
  244. qty = get_round(abs(strategy.position_size), 2),
  245. oca_name = "SHORT OCA",
  246. oca_type = strategy.oca.cancel,
  247. when = exit_long_condition)
  248.  
  249. // Market order
  250. strategy.order(
  251. "S/L", strategy.long,
  252. stop = st_line,
  253. qty = get_round(abs(strategy.position_size), 2),
  254. oca_name = "SHORT OCA",
  255. oca_type = strategy.oca.cancel)
  256.  
  257.  
  258. label label_leverage = na
  259.  
  260. if (not na(trade_leverage))
  261. label_leverage := label.new(na, na,
  262. "L " + tostring(trade_leverage),
  263. color = #363a45,
  264. textcolor = color.white,
  265. style = label.style_labeldown,
  266. yloc = yloc.abovebar)
  267.  
  268.  
  269. plot(take_profit, color = color.blue, style = plot.style_cross)
  270. plot(liquidation, color = color.yellow, style = plot.style_cross)
  271.  
  272. bgcolor(is_session_open(range_session) ? color.white : na, transp = 95)
  273.  
  274. color_green = #4FC100
  275. color_red = #E50003
  276. color_transp = color.new(color.white, 100)
  277.  
  278. get_color(values, min, max) =>
  279. i = abs(max - min) / 20
  280. result =
  281. values <= min + i * 1 ? #E50003 :
  282. values <= min + i * 2 ? #E30D00 :
  283. values <= min + i * 3 ? #E12100 :
  284. values <= min + i * 4 ? #DF3500 :
  285. values <= min + i * 5 ? #DD4800 :
  286. values <= min + i * 6 ? #DB5B00 :
  287. values <= min + i * 7 ? #D96E00 :
  288. values <= min + i * 8 ? #D78000 :
  289. values <= min + i * 9 ? #D59200 :
  290. values <= min + i * 10 ? #D3A400 :
  291. values <= min + i * 11 ? #D1B500 :
  292. values <= min + i * 12 ? #CFC600 :
  293. values <= min + i * 13 ? #C2CD00 :
  294. values <= min + i * 14 ? #AECB00 :
  295. values <= min + i * 15 ? #9AC900 :
  296. values <= min + i * 16 ? #87C700 :
  297. values <= min + i * 17 ? #74C500 :
  298. values <= min + i * 18 ? #61C300 :
  299. values <= min + i * 19 ? #4FC100 :
  300. #3DBF00
  301.  
  302. plot_trend_bot = plot(st_trend == trend_up ? st_trend_bot : na, color = color_green, linewidth = 1, transp = 50, style = plot.style_linebr)
  303. plot_trend_top = plot(st_trend == trend_down ? st_trend_top : na, color = color_red, linewidth = 1, transp = 50, style = plot.style_linebr)
  304. plot_mid = plot(ohlc4, color = color_transp)
  305.  
  306. fill(plot_mid, plot_trend_bot, color = color_green)
  307. fill(plot_mid, plot_trend_top, color = color_red)
  308.  
  309. plot_jma_fast = plot(jma_fast, title="JMA Fast", linewidth = 1, color = get_color(jma_fast_slope, -2, 2), transp = 0)
  310. plot_jma_slow = plot(jma_slow, title="JMA Slow", linewidth = 1, color = get_color(jma_slow_slope, -2, 2), transp = 0)
  311.  
  312. fill(plot_jma_fast, plot_jma_slow, color = get_color((jma_fast_slope + jma_slow_slope) / 2, -1, 1))
  313.  
  314. plot(range_high, title = "Range High", color = color_green, linewidth = 1, style = plot.style_cross)
  315. plot(range_low, title = "Range Low", color = color_red, linewidth = 1, style = plot.style_cross)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement