Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.74 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.float, minval = 0.1, step = 0.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 (strategy.position_size > 0)
  156.  
  157. take_profit := strategy.position_avg_price * (1 + (trade_profit_take * (count / 100)))
  158.  
  159. if (high > take_profit)
  160. count := count + 1
  161.  
  162. amount = nz(strategy.position_size * (trade_profit_close / 100), 0.0)
  163.  
  164. // Limit order
  165. strategy.order(
  166. "T/P", strategy.short,
  167. limit = take_profit,
  168. qty = get_round(amount, 2),
  169. oca_name = "LONG OCA",
  170. oca_type = strategy.oca.reduce,
  171. when = abs(strategy.position_size) > 0.5)
  172.  
  173. // Market order
  174. strategy.order(
  175. "E/L", strategy.short,
  176. qty = abs(strategy.position_size),
  177. oca_name = "LONG OCA",
  178. oca_type = strategy.oca.cancel,
  179. when = exit_long_condition)
  180.  
  181. // Market order
  182. strategy.order(
  183. "S/L", strategy.short,
  184. stop = st_line,
  185. qty = abs(strategy.position_size),
  186. oca_name = "LONG OCA",
  187. oca_type = strategy.oca.cancel)
  188.  
  189.  
  190. if(strategy.position_size < 0)
  191.  
  192. take_profit := strategy.position_avg_price * (1 + (trade_profit_take * (count / 100)))
  193.  
  194. if (low < take_profit)
  195. count := count - 1
  196.  
  197. amount = nz(abs(strategy.position_size) * (trade_profit_close / 100), 0.0)
  198.  
  199. // Limit order
  200. strategy.order(
  201. "T/P", strategy.long,
  202. limit = take_profit,
  203. qty = get_round(amount, 2),
  204. oca_name = "SHORT OCA",
  205. oca_type = strategy.oca.reduce,
  206. when = abs(strategy.position_size) > 0.5)
  207.  
  208. // Market order
  209. strategy.order(
  210. "E/L", strategy.long,
  211. qty = abs(strategy.position_size),
  212. oca_name = "SHORT OCA",
  213. oca_type = strategy.oca.cancel,
  214. when = exit_long_condition)
  215.  
  216. // Market order
  217. strategy.order(
  218. "S/L", strategy.long,
  219. stop = st_line,
  220. qty = abs(strategy.position_size),
  221. oca_name = "SHORT OCA",
  222. oca_type = strategy.oca.cancel)
  223.  
  224. bool plot_liquidation = na
  225.  
  226. if(can_long)
  227. if(enter_long_condition and strategy.position_size == 0)
  228.  
  229. trade_leverage := round(min((strategy.equity * (trade_risk / 100)) / (close - st_trend_bot), trade_max_leverage))
  230. contracts = min(get_round(strategy.equity * trade_leverage / close, 2), trade_max_contract)
  231.  
  232. // Market order
  233. strategy.order(
  234. "LONG",
  235. strategy.long,
  236. qty = contracts,
  237. oca_name = "LONG OCA")
  238.  
  239. count := 1
  240. liquidation := close - (close / trade_leverage)
  241. plot_liquidation := true
  242.  
  243.  
  244. if (can_short)
  245. if (enter_short_condition and strategy.position_size == 0)
  246.  
  247. trade_leverage := round(min((strategy.equity * (trade_risk / 100)) / (st_trend_top - close), trade_max_leverage))
  248. contracts = min(get_round(strategy.equity * trade_leverage / close, 2), trade_max_contract)
  249.  
  250. // Market order
  251. strategy.order(
  252. "SHORT",
  253. strategy.short,
  254. qty = contracts,
  255. oca_name = "SHORT OCA")
  256.  
  257. count := -1
  258. liquidation := close + (close / trade_leverage)
  259. plot_liquidation := true
  260.  
  261.  
  262. label label_leverage = na
  263.  
  264. if (plot_liquidation)
  265. label_leverage := label.new(na, na,
  266. "L " + tostring(trade_leverage),
  267. color = #363a45,
  268. textcolor = color.white,
  269. style = label.style_labeldown,
  270. yloc = yloc.abovebar)
  271.  
  272.  
  273. plot(take_profit, color = color.blue, style = plot.style_cross)
  274. plot(liquidation, color = color.yellow, style = plot.style_cross)
  275.  
  276. bgcolor(is_session_open(range_session) ? color.white : na, transp = 95)
  277.  
  278. color_green = #4FC100
  279. color_red = #E50003
  280. color_transp = color.new(color.white, 100)
  281.  
  282. get_color(values, min, max) =>
  283. i = abs(max - min) / 20
  284. result =
  285. values <= min + i * 1 ? #E50003 :
  286. values <= min + i * 2 ? #E30D00 :
  287. values <= min + i * 3 ? #E12100 :
  288. values <= min + i * 4 ? #DF3500 :
  289. values <= min + i * 5 ? #DD4800 :
  290. values <= min + i * 6 ? #DB5B00 :
  291. values <= min + i * 7 ? #D96E00 :
  292. values <= min + i * 8 ? #D78000 :
  293. values <= min + i * 9 ? #D59200 :
  294. values <= min + i * 10 ? #D3A400 :
  295. values <= min + i * 11 ? #D1B500 :
  296. values <= min + i * 12 ? #CFC600 :
  297. values <= min + i * 13 ? #C2CD00 :
  298. values <= min + i * 14 ? #AECB00 :
  299. values <= min + i * 15 ? #9AC900 :
  300. values <= min + i * 16 ? #87C700 :
  301. values <= min + i * 17 ? #74C500 :
  302. values <= min + i * 18 ? #61C300 :
  303. values <= min + i * 19 ? #4FC100 :
  304. #3DBF00
  305.  
  306. plot_trend_bot = plot(st_trend == trend_up ? st_trend_bot : na, color = color_green, linewidth = 1, transp = 50, style = plot.style_linebr)
  307. plot_trend_top = plot(st_trend == trend_down ? st_trend_top : na, color = color_red, linewidth = 1, transp = 50, style = plot.style_linebr)
  308. plot_mid = plot(ohlc4, color = color_transp)
  309.  
  310. fill(plot_mid, plot_trend_bot, color = color_green)
  311. fill(plot_mid, plot_trend_top, color = color_red)
  312.  
  313. plot_jma_fast = plot(jma_fast, title="JMA Fast", linewidth = 1, color = get_color(jma_fast_slope, -2, 2), transp = 0)
  314. plot_jma_slow = plot(jma_slow, title="JMA Slow", linewidth = 1, color = get_color(jma_slow_slope, -2, 2), transp = 0)
  315.  
  316. fill(plot_jma_fast, plot_jma_slow, color = get_color((jma_fast_slope + jma_slow_slope) / 2, -1, 1))
  317.  
  318. plot(range_high, title = "Range High", color = color_green, linewidth = 1, style = plot.style_cross)
  319. 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