ddcpb

L2PR BO ver

Sep 22nd, 2020 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  3. // © blackcat1402
  4. // Based on Original L2 Price Retracement Reminder Strategy
  5. //@version=4
  6. strategy("L2 PRRS - BO", overlay=true,  
  7.   max_bars_back = 5000,
  8.   currency=currency.NONE,
  9.   initial_capital=100000,
  10.   default_qty_type=strategy.fixed,  
  11.   default_qty_value=100,
  12.   commission_type=strategy.commission.percent,
  13.   commission_value=0,
  14.   process_orders_on_close=true,
  15.   calc_on_every_tick=true,
  16.   calc_on_order_fills=false,
  17.   pyramiding = 0,
  18.   slippage = 0)
  19.  
  20. strat_dir_input = input(title="Strategy Direction", defval="all", options=["long", "short", "all"])
  21. strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.all
  22. strategy.risk.allow_entry_in(strat_dir_value)
  23.  
  24.  
  25. testStartYear = input(2020, "Backtest Start Year")
  26. testStartMonth = input(8, "Backtest Start Month")
  27. testStartDay = input(15, "Backtest Start Day")
  28. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  29. testPeriodValid = time >= testPeriodStart
  30.  
  31. //function definition
  32. xsa(src,len,wei) =>
  33.     sum = 0.0
  34.     ma = 0.0
  35.     out = 0.0
  36.     sum := nz(sum[1]) - nz(src[len]) + src
  37.     ma := na(src[len]) ? na : sum/len
  38.     out := na(out[1]) ? ma : (src*wei+out[1]*(len-wei))/len
  39.     out
  40.  
  41. //inputs
  42. //moving average lookback for rsi
  43. xsalb = input(3, title="xsalookback", type=input.integer, minval = 1)
  44. //overbought threshold
  45. obth = input(85, title="overbought threshold", type=input.integer, minval = 1)
  46. // set trade expiry time manually in bars
  47. timeframe = input(title="Timeframe", type=input.integer, minval=1, defval=3)
  48.  
  49. //algorithm
  50. //calculate RSI(3)
  51. lc = close[1]
  52. rsin = ((xsa(max((close - lc),0),xsalb,1) / xsa(abs((close - lc)),xsalb,1)) * 100)
  53.  
  54.  
  55. //define fast line as ema3 and slow line as ema21
  56. ff = ema(close,3)
  57. ma21 = ema(close,21)
  58.  
  59.  
  60. //up trend with lime and down trend with red
  61. barcolor(ma21>=ma21[1]?color.lime:color.red)
  62. barcolor(ma21<ma21[1]?color.red:color.lime)
  63.  
  64.  
  65. // use crosses to identify buy and selling points
  66. // plotchar(crossover(ff,ma21), char='B', location=location.belowbar, color=color.lime,size=size.small)
  67. // plotchar(crossunder(ff,ma21), char='S', location=location.abovebar, color=color.red,size=size.small)
  68.  
  69.  
  70. shortCondition = crossover(ff,ma21) and testPeriodValid
  71. longCondition = crossunder(ff,ma21) or crossunder(rsin,obth) and testPeriodValid
  72.  
  73. // plotchar(buy, char='B', location=location.belowbar, color=color.lime,size=size.small)
  74. // plotchar(sel, char='S', location=location.abovebar, color=color.red,size=size.small)
  75.  
  76.  
  77. //if RSI(3) crossunder overbought threshold then it is a selling point due to retracement happen
  78. // plotchar(crossunder(rsin,obth), char='S', location=location.abovebar, color=color.yellow,size=size.small)
  79.  
  80.  
  81. //plot guideline and bull-bear boarder
  82. guideline = ema((ema(close,4)+ema(close,6)+ema(close,12)+ema(close,24))/4,2)
  83. boarder = sma(close,27)
  84.  
  85. plot(guideline, color=color.white,linewidth=2)
  86. plot(boarder, color=color.yellow,linewidth=2)
  87.  
  88. //strategy setting
  89.  
  90. shortLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
  91. longLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
  92.  
  93. // Determine stop loss price
  94. shortStopPrice  = strategy.position_avg_price * (1 - longLossPerc)
  95. longStopPrice = strategy.position_avg_price * (1 + shortLossPerc)
  96.  
  97. if (longCondition and guideline>boarder)
  98.     strategy.entry("Buy", strategy.long)
  99. if (shortCondition and guideline>boarder)
  100.     strategy.entry("Sell", strategy.short)
  101. //if (shortCondition and guideline>boarder and strategy.position_size > 0 )
  102. //    strategy.close("Buy")
  103. //if (longCondition and guideline<boarder and strategy.position_size < 0 )
  104. //    strategy.close("Sell")
  105.  
  106. // triggers trade expiry time as set by timeframe input
  107. bars = barssince(strategy.opentrades == 0)
  108. strategy.close_all(when=(bars>=timeframe))
Add Comment
Please, Sign In to add comment