SavingFace

5 day BTC trading strategy

Aug 28th, 2024 (edited)
408
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | Cryptocurrency | 0 0
  1. //@version=5
  2. strategy("EMA 5/5 Day Long/Short with 4-Year Cycle Filter", overlay=true, initial_capital=1000, currency=currency.USD)
  3.  
  4. // Create an input for the EMA period in the settings
  5. len = input.int(title="EMA Length", defval=5, minval=1)
  6. src = input(close, title="Source")
  7. ema = ta.ema(src, len)
  8.  
  9. // Define the 4-year cycle
  10. halvingYear = input(2011) // Set the last halving year
  11. yearDifference = year(time) - halvingYear
  12. bearMarket = (yearDifference % 4 == 3) // Bear market is typically the third year after halving
  13.  
  14. // Plot the 5 EMA and bear market filter
  15. plot(ema, title="5 EMA", color=color.blue)
  16. bgcolor(bearMarket ? color.red : na, transp=90, title="Bear Market")
  17.  
  18. // Trade Logic
  19. if barstate.isconfirmed
  20. longCondition = close > ema
  21. shortCondition = close < ema
  22. exitLongCondition = close < ema
  23. exitShortCondition = close > ema
  24.  
  25. if (longCondition and not bearMarket)
  26. strategy.entry("Long", strategy.long)
  27. if (exitLongCondition)
  28. strategy.close("Long")
  29. if (shortCondition and bearMarket)
  30. strategy.entry("Short", strategy.short)
  31. if (exitShortCondition)
  32. strategy.close("Short")
  33.  
  34. // Calculate the Average Entry Price
  35. var float avgEntryPrice = na
  36. if strategy.position_size != 0
  37. avgEntryPrice := strategy.position_avg_price
  38. else
  39. avgEntryPrice := na
  40.  
  41. // Plot the average entry line with extension
  42. plot(avgEntryPrice, color=#2196f3, style=plot.style_linebr, linewidth=1, title="Average Entry Price", force_overlay=true)
  43.  
  44. // Dynamically Update Label with Real-Time Intrabar Calculations
  45. var label pctLabel = na
  46. if not na(avgEntryPrice)
  47. if pctLabel != na
  48. label.delete(pctLabel)
  49. // Calculate percentage change and unrealized P/L
  50. pctChange = (close - avgEntryPrice) / avgEntryPrice * 100.0
  51. unrealizedPL = (close - avgEntryPrice) * strategy.position_size
  52. // Create a label with dynamically updated values
  53. pctLabel := label.new(bar_index, avgEntryPrice, "P&L%: " + str.tostring((close - avgEntryPrice) / avgEntryPrice * 100.0, "#.##") + "\nAvg Price: " + str.tostring(avgEntryPrice, "#.##") + "\nUnrealized P/L: " + str.tostring((close - avgEntryPrice) * strategy.position_size, "#.##"), style=label.style_label_left, yloc=yloc.price, color=(close - avgEntryPrice) >= 0 ? #2196f3 : color.new(color.red, 0), textcolor=color.white, size=size.normal)
  54.  
  55. else
  56. if pctLabel != na
  57. label.delete(pctLabel)
  58.  
  59. // Dashboard Table
  60. i_tableTextSize = input.string(title="Dashboard Size", defval="Small", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Dashboards")
  61. table_text_size(s) =>
  62. switch s
  63. "Auto" => size.auto
  64. "Huge" => size.huge
  65. "Large" => size.large
  66. "Normal" => size.normal
  67. "Small" => size.small
  68. => size.tiny
  69.  
  70. tableTextSize = table_text_size(i_tableTextSize)
  71. i_showDashboard = input.bool(title="Performance Summary", defval=true, group="Dashboards", inline="Show Dashboards")
  72.  
  73. f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) =>
  74. _cellText = _title + "\n" + _value
  75. table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, text_size=tableTextSize)
  76.  
  77. if i_showDashboard
  78. var bgcolor = color.new(color.black, 0)
  79. newWin = (strategy.wintrades > strategy.wintrades[1]) and (strategy.losstrades == strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1])
  80. newLoss = (strategy.wintrades == strategy.wintrades[1]) and (strategy.losstrades > strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1])
  81.  
  82. varip int winRow = 0
  83. varip int lossRow = 0
  84. varip int maxWinRow = 0
  85. varip int maxLossRow = 0
  86.  
  87. if newWin
  88. lossRow := 0
  89. winRow := winRow + 1
  90. if winRow > maxWinRow
  91. maxWinRow := winRow
  92.  
  93. if newLoss
  94. winRow := 0
  95. lossRow := lossRow + 1
  96. if lossRow > maxLossRow
  97. maxLossRow := lossRow
  98.  
  99. var table dashTable = table.new(position.top_right, 1, 15, border_width=1)
  100.  
  101. if barstate.islastconfirmedhistory
  102. lastTime = strategy.position_size == 0 ? strategy.closedtrades.exit_time(strategy.closedtrades - 1) : time
  103. dollarReturn = strategy.netprofit
  104. _profit = (strategy.netprofit / strategy.initial_capital) * 100
  105. _numOfDaysInStrategy = (lastTime - strategy.closedtrades.entry_time(0)) / (1000 * 3600 * 24)
  106. _numOfDaysInStrategy := _numOfDaysInStrategy != 0 ? _numOfDaysInStrategy : 1
  107.  
  108. f_fillCell(dashTable, 0, 3, "Percent Per Day", str.tostring(_profit / _numOfDaysInStrategy, '##.###') + "%", _profit > 0 ? color.teal : color.maroon, color.white)
  109. _winRate = (strategy.wintrades / strategy.closedtrades) * 100
  110. f_fillCell(dashTable, 0, 4, "Percent Profitable:", str.tostring(_winRate, '##.##') + "%", _winRate < 50 ? color.maroon : _winRate < 75 ? #999900 : color.teal, color.white)
  111. f_fillCell(dashTable, 0, 5, "Profit Factor:", str.tostring(strategy.grossprofit / strategy.grossloss, '##.###'), strategy.grossprofit > strategy.grossloss ? color.teal : color.maroon, color.white)
  112. f_fillCell(dashTable, 0, 6, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white)
  113. f_fillCell(dashTable, 0, 7, "Winning Trades:", str.tostring(strategy.wintrades, '######'), bgcolor, color.white)
  114. f_fillCell(dashTable, 0, 8, "Max Wins In A Row:", str.tostring(maxWinRow, '######'), bgcolor, color.white)
  115. f_fillCell(dashTable, 0, 9, "Losing Trades:", str.tostring(strategy.losstrades, '######'), bgcolor, color.white)
  116. f_fillCell(dashTable, 0, 10, "Max Losses In A Row:", str.tostring(maxLossRow, '######'), bgcolor, color.white)
  117.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment