Advertisement
Maurizio-Ciullo

Bollinger Bands - Breakout Strategy Modificata N BTC-USDT BINANCE 5H

Mar 2nd, 2023
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=5
  2. strategy("Bollinger Bands - Breakout Strategy Modificata N BTC-USDT BINANCE 5H",overlay=true, initial_capital=1000, default_qty_type=strategy.cash, commission_type=strategy.commission.percent, commission_value=0.025, slippage = 1, backtest_fill_limits_assumption = 1, default_qty_value=1000)
  3.  
  4.  
  5. // Define the length of the Bollinger Bands
  6. bbLengthInput = input.int (15,title="Length", group="Bollinger Bands", inline="BB")
  7. bbDevInput = input.float (2.0,title="StdDev", group="Bollinger Bands", inline="BB")
  8.  
  9. // Define the settings for the Trend Filter
  10. trendFilterInput = input.bool(false, title="Above/Below", group = "Trend Filter", inline="Trend")
  11. trendFilterPeriodInput = input(223,title="", group = "Trend Filter", inline="Trend")
  12. trendFilterType = input.string (title="", defval="EMA",options=["EMA","SMA","RMA", "WMA"], group = "Trend Filter", inline="Trend")
  13.  
  14. volatilityFilterInput = input.bool(true,title="StdDev", group = "Volatility Filter", inline="Vol")
  15. volatilityFilterStDevLength = input(15,title="",group = "Volatility Filter", inline="Vol")
  16. volatilityStDevMaLength = input(15,title=">MA",group = "Volatility Filter", inline="Vol")
  17.  
  18. // Trade Direction Filter
  19.  
  20. tradeDirectionInput = input.string("Auto",options=["Auto", "Long&Short","Long Only", "Short Only"], title="Trade", group="Direction Filter", tooltip="Auto: if a PERP is detected (in the symbol description), trade long and short\n Otherwise as per user-input")
  21.  
  22. tradeDirection = switch tradeDirectionInput
  23.     "Auto" => str.contains(str.lower(syminfo.description), "perp") ? strategy.direction.all : strategy.direction.long
  24.     "Long&Short" => strategy.direction.all
  25.     "Long Only" => strategy.direction.long
  26.     "Short Only" => strategy.direction.short
  27.     => strategy.direction.all
  28.  
  29. strategy.risk.allow_entry_in(tradeDirection)
  30.  
  31.  
  32. // Calculate and plot the Bollinger Bands
  33. [bbMiddle, bbUpper, bbLower] = ta.bb (close, bbLengthInput, bbDevInput)
  34.  
  35. plot(bbMiddle, "Basis", color=color.orange)
  36. bbUpperPlot = plot(bbUpper, "Upper", color=color.blue)
  37. bbLowerrPlot = plot(bbLower, "Lower", color=color.blue)
  38. fill(bbUpperPlot, bbLowerrPlot, title = "Background", color=color.new(color.blue, 95))
  39.  
  40.  
  41. // Calculate and view Trend Filter
  42.  
  43. float tradeConditionMa = switch trendFilterType
  44.     "EMA" => ta.ema(close, trendFilterPeriodInput)
  45.     "SMA" => ta.sma(close, trendFilterPeriodInput)
  46.     "RMA" => ta.rma(close, trendFilterPeriodInput)
  47.     "WMA" => ta.wma(close, trendFilterPeriodInput)
  48.     // Default used when the three first cases do not match.
  49.     => ta.wma(close, trendFilterPeriodInput)
  50.  
  51.  
  52. trendConditionLong  = trendFilterInput ? close > tradeConditionMa : true
  53. trendConditionShort = trendFilterInput ? close < tradeConditionMa : true
  54. plot(trendFilterInput ? tradeConditionMa : na, color=color.yellow)
  55.  
  56. // Calculate and view Volatility Filter
  57.  
  58. stdDevClose = ta.stdev(close,volatilityFilterStDevLength)
  59. volatilityCondition = volatilityFilterInput ? stdDevClose > ta.sma(stdDevClose,volatilityStDevMaLength) : true
  60.  
  61. bbLowerCrossUnder =  ta.crossunder(close, bbLower)
  62. bbUpperCrossOver =  ta.crossover(close, bbUpper)
  63.  
  64. bgcolor(volatilityCondition ? na : color.new(color.red, 95))
  65.  
  66.  
  67. // Date Filter
  68.  
  69. start = input.time(timestamp("2017-01-01"), "Start", group="Date Filter")
  70. finish = input.time(timestamp("2050-01-01"), "End", group="Date Filter")
  71.  
  72. date_filter = time >= start and time <= finish ? true : false
  73.  
  74. // Entry and Exit Conditions
  75.  
  76. entryLongCondition = bbUpperCrossOver and trendConditionLong and volatilityCondition and date_filter
  77. entryShortCondition = bbLowerCrossUnder and trendConditionShort and volatilityCondition and date_filter
  78.  
  79. exitLongCondition = bbLowerCrossUnder
  80. exitShortCondition = bbUpperCrossOver
  81.  
  82. // Orders
  83.  
  84. if entryLongCondition
  85.     strategy.entry("EL", strategy.long)
  86.  
  87. if entryShortCondition
  88.     strategy.entry("ES", strategy.short)
  89.  
  90. if exitShortCondition
  91.     strategy.close("EL")
  92.  
  93. if exitShortCondition
  94.     strategy.close("ES")
  95.  
  96.  
  97.  
  98. // Long SL/TP/TS
  99.  
  100. xl_ts_percent      = input.float(2,step=0.5, title= "TS", group="Exit Long", inline="LTS", tooltip="Trailing Treshold %")
  101. xl_to_percent      = input.float(0.5, step=0.5, title= "TO", group="Exit Long", inline="LTS", tooltip="Trailing Offset %")
  102.  
  103. xl_ts_tick = xl_ts_percent * close/syminfo.mintick/100
  104. xl_to_tick = xl_to_percent * close/syminfo.mintick/100
  105.  
  106. xl_sl_percent      = input.float (2, step=0.5, title="SL",group="Exit Long", inline="LSLTP")
  107. xl_tp_percent      = input.float(9, step=0.5, title="TP",group="Exit Long", inline="LSLTP")
  108.  
  109. xl_sl_price = strategy.position_avg_price * (1-xl_sl_percent/100)
  110. xl_tp_price = strategy.position_avg_price * (1+xl_tp_percent/100)
  111.  
  112. strategy.exit("XL+SL/TP", "EL", stop=xl_sl_price, limit=xl_tp_price, trail_points=xl_ts_tick, trail_offset=xl_to_tick,comment_loss= "XL-SL", comment_profit = "XL-TP",comment_trailing = "XL-TS")
  113.  
  114. // Short SL/TP/TS
  115. xs_ts_percent      = input.float(2,step=0.5, title= "TS",group="Exit Short", inline ="STS", tooltip="Trailing Treshold %")
  116. xs_to_percent      = input.float(0.5, step=0.5, title= "TO",group="Exit Short", inline ="STS", tooltip="Trailing Offset %")
  117.  
  118. xs_ts_tick = xs_ts_percent * close/syminfo.mintick/100
  119. xs_to_tick = xs_to_percent * close/syminfo.mintick/100
  120.  
  121. xs_sl_percent      = input.float (2, step=0.5, title="SL",group="Exit Short", tooltip="Stop Loss %")
  122. xs_tp_percent      = input.float(9, step=0.5, title="TP",group="Exit Short",  tooltip="Take Profit %")
  123.  
  124. xs_sl_price = strategy.position_avg_price * (1+xs_sl_percent/100)
  125. xs_tp_price = strategy.position_avg_price * (1-xs_tp_percent/100)
  126.  
  127. strategy.exit("XS+SL/TP", "ES", stop=xs_sl_price, limit=xs_tp_price, trail_points=xs_ts_tick, trail_offset=xs_to_tick,comment_loss= "XS-SL", comment_profit = "XS-TP",comment_trailing = "XS-TS")
  128.  
  129.  
  130. max_intraday_loss = input.int(100, title="Max Intraday Loss (Cash)", group="Risk Management")
  131.  
  132. strategy.risk.max_intraday_loss(max_intraday_loss, strategy.cash)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement