SavingFace

DCA Calculator With Average Entry

Sep 27th, 2024 (edited)
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | Cryptocurrency | 0 0
  1. //@version=5
  2. indicator("DCA Average & Sats", overlay=true)
  3.  
  4. // Input for selecting start and end years
  5. startYear = input.int(2020, title="Starting Year", minval=2000, maxval=2025)
  6. endYear = input.int(2024, title="Ending Year", minval=2000, maxval=2025)
  7.  
  8. // Input to select DCA frequency: daily, weekly, or monthly
  9. dcaFrequency = input.string("Once a Month", title="DCA Frequency", options=["Once a Day", "Once a Week", "Once a Month"])
  10.  
  11. // Input to choose table position: top, middle, or bottom
  12. tablePosition = input.string("Top", title="Table Position", options=["Top", "Middle", "Bottom"])
  13.  
  14. // Input to toggle SATs display
  15. showSats = input.bool(true, title="Show SATs on Chart")
  16.  
  17. // Get the current year from the timestamp
  18. currentYear = year
  19.  
  20. // Define the amount to invest per period
  21. investmentAmount = input.float(100, title="Investment Amount")
  22.  
  23. // Check if the current year is within the selected range
  24. isWithinYearRange = currentYear >= startYear and currentYear <= endYear
  25.  
  26. // Define DCA period based on the selected frequency
  27. var bool shouldInvest = false
  28. if dcaFrequency == "Once a Day"
  29. shouldInvest := ta.change(time("D"))
  30. else if dcaFrequency == "Once a Week"
  31. shouldInvest := ta.change(time("W"))
  32. else if dcaFrequency == "Once a Month"
  33. shouldInvest := ta.change(time("M"))
  34.  
  35. // Accumulate investments over time
  36. var float totalInvested = 0
  37. var float totalShares = 0
  38.  
  39. // Perform DCA only within the specified year range
  40. if (isWithinYearRange)
  41. if shouldInvest
  42. totalInvested := totalInvested + investmentAmount
  43. totalShares := totalShares + investmentAmount / close
  44.  
  45. // Calculate SATs per investment
  46. satsPerInvestment = (investmentAmount / close) * 100000000 // 1 Bitcoin = 100,000,000 SATs
  47.  
  48. // Plot a label with the SAT value if the option is enabled
  49. if showSats
  50. label.new(bar_index, low, text="SATs: " + str.tostring(satsPerInvestment, "#.##"), style=label.style_label_down, color=#ff5a00, textcolor=color.black, size=size.small)
  51.  
  52. // Calculate the average entry price
  53. averagePrice = totalInvested / totalShares
  54.  
  55. // Plot the average DCA price on the chart as a line
  56. plot(averagePrice, color=#ff5a00, title="Average DCA Price")
  57.  
  58. // Calculate the percentage difference from average entry to current price
  59. percentageDifference = ((close - averagePrice) / averagePrice) * 100
  60.  
  61. // Calculate P&L (Profit and Loss)
  62. P_L = (close - averagePrice) * totalShares
  63.  
  64. // Calculate Total Invested + P&L (Current Value)
  65. totalValue = totalInvested + P_L
  66.  
  67. // Plot a tiny circle on the chart when DCA happens, only within the year range
  68. plotshape(series=shouldInvest and isWithinYearRange, location=location.belowbar, style=shape.circle, size=size.tiny, color=#ff59007f, title="DCA Point")
  69.  
  70. // Add a label at the latest bar that follows the plot of the average price, and delete previous label to prevent repetition
  71. var label avgEntryLabel = na
  72. if not na(avgEntryLabel)
  73. label.delete(avgEntryLabel)
  74. avgEntryLabel := label.new(bar_index, averagePrice, text="Average Entry\n" + str.tostring(percentageDifference, "#.##") + "%", style=label.style_label_left, color=#ff5a00, textcolor=color.black, size=size.small)
  75.  
  76. // Determine table position based on user input
  77. var table dcaTable = na
  78. if tablePosition == "Top"
  79. dcaTable := table.new(position.top_right, 5, 5, border_color=color.black, frame_color=color.black, bgcolor=color.gray)
  80. else if tablePosition == "Middle"
  81. dcaTable := table.new(position.middle_right, 5, 5, border_color=color.black, frame_color=color.black, bgcolor=color.gray)
  82. else
  83. dcaTable := table.new(position.bottom_right, 5, 5, border_color=color.black, frame_color=color.black, bgcolor=color.gray)
  84.  
  85. // Clear and update the table with total invested, average entry, percentage difference, P&L, and total value (Invested + P&L)
  86. table.cell(dcaTable, 0, 0, "Total Invested", text_color=color.white, bgcolor=#ff5a00)
  87. table.cell(dcaTable, 1, 0, "$" + str.tostring(totalInvested, "#.##"), text_color=#000000)
  88.  
  89. table.cell(dcaTable, 0, 1, "Average Entry", text_color=color.white, bgcolor=#ff5a00)
  90. table.cell(dcaTable, 1, 1, "$" + str.tostring(averagePrice, "#.##"), text_color=#000000)
  91.  
  92. table.cell(dcaTable, 0, 2, "% Diff", text_color=color.white, bgcolor=#ff5a00)
  93. table.cell(dcaTable, 1, 2, str.tostring(percentageDifference, "#.##") + "%", text_color=#000000)
  94.  
  95. table.cell(dcaTable, 0, 3, "P&L", text_color=color.white, bgcolor=#ff5a00)
  96. table.cell(dcaTable, 1, 3, "$" + str.tostring(P_L, "#.##"), text_color=#000000)
  97.  
  98. table.cell(dcaTable, 0, 4, "Total Value", text_color=color.white, bgcolor=#ff5a00)
  99. table.cell(dcaTable, 1, 4, "$" + str.tostring(totalValue, "#.##"), text_color=#000000)
  100.  
Advertisement
Add Comment
Please, Sign In to add comment