Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("DCA Average & Sats", overlay=true)
- // Input for selecting start and end years
- startYear = input.int(2020, title="Starting Year", minval=2000, maxval=2025)
- endYear = input.int(2024, title="Ending Year", minval=2000, maxval=2025)
- // Input to select DCA frequency: daily, weekly, or monthly
- dcaFrequency = input.string("Once a Month", title="DCA Frequency", options=["Once a Day", "Once a Week", "Once a Month"])
- // Input to choose table position: top, middle, or bottom
- tablePosition = input.string("Top", title="Table Position", options=["Top", "Middle", "Bottom"])
- // Input to toggle SATs display
- showSats = input.bool(true, title="Show SATs on Chart")
- // Get the current year from the timestamp
- currentYear = year
- // Define the amount to invest per period
- investmentAmount = input.float(100, title="Investment Amount")
- // Check if the current year is within the selected range
- isWithinYearRange = currentYear >= startYear and currentYear <= endYear
- // Define DCA period based on the selected frequency
- var bool shouldInvest = false
- if dcaFrequency == "Once a Day"
- shouldInvest := ta.change(time("D"))
- else if dcaFrequency == "Once a Week"
- shouldInvest := ta.change(time("W"))
- else if dcaFrequency == "Once a Month"
- shouldInvest := ta.change(time("M"))
- // Accumulate investments over time
- var float totalInvested = 0
- var float totalShares = 0
- // Perform DCA only within the specified year range
- if (isWithinYearRange)
- if shouldInvest
- totalInvested := totalInvested + investmentAmount
- totalShares := totalShares + investmentAmount / close
- // Calculate SATs per investment
- satsPerInvestment = (investmentAmount / close) * 100000000 // 1 Bitcoin = 100,000,000 SATs
- // Plot a label with the SAT value if the option is enabled
- if showSats
- label.new(bar_index, low, text="SATs: " + str.tostring(satsPerInvestment, "#.##"), style=label.style_label_down, color=#ff5a00, textcolor=color.black, size=size.small)
- // Calculate the average entry price
- averagePrice = totalInvested / totalShares
- // Plot the average DCA price on the chart as a line
- plot(averagePrice, color=#ff5a00, title="Average DCA Price")
- // Calculate the percentage difference from average entry to current price
- percentageDifference = ((close - averagePrice) / averagePrice) * 100
- // Calculate P&L (Profit and Loss)
- P_L = (close - averagePrice) * totalShares
- // Calculate Total Invested + P&L (Current Value)
- totalValue = totalInvested + P_L
- // Plot a tiny circle on the chart when DCA happens, only within the year range
- plotshape(series=shouldInvest and isWithinYearRange, location=location.belowbar, style=shape.circle, size=size.tiny, color=#ff59007f, title="DCA Point")
- // Add a label at the latest bar that follows the plot of the average price, and delete previous label to prevent repetition
- var label avgEntryLabel = na
- if not na(avgEntryLabel)
- label.delete(avgEntryLabel)
- 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)
- // Determine table position based on user input
- var table dcaTable = na
- if tablePosition == "Top"
- dcaTable := table.new(position.top_right, 5, 5, border_color=color.black, frame_color=color.black, bgcolor=color.gray)
- else if tablePosition == "Middle"
- dcaTable := table.new(position.middle_right, 5, 5, border_color=color.black, frame_color=color.black, bgcolor=color.gray)
- else
- dcaTable := table.new(position.bottom_right, 5, 5, border_color=color.black, frame_color=color.black, bgcolor=color.gray)
- // Clear and update the table with total invested, average entry, percentage difference, P&L, and total value (Invested + P&L)
- table.cell(dcaTable, 0, 0, "Total Invested", text_color=color.white, bgcolor=#ff5a00)
- table.cell(dcaTable, 1, 0, "$" + str.tostring(totalInvested, "#.##"), text_color=#000000)
- table.cell(dcaTable, 0, 1, "Average Entry", text_color=color.white, bgcolor=#ff5a00)
- table.cell(dcaTable, 1, 1, "$" + str.tostring(averagePrice, "#.##"), text_color=#000000)
- table.cell(dcaTable, 0, 2, "% Diff", text_color=color.white, bgcolor=#ff5a00)
- table.cell(dcaTable, 1, 2, str.tostring(percentageDifference, "#.##") + "%", text_color=#000000)
- table.cell(dcaTable, 0, 3, "P&L", text_color=color.white, bgcolor=#ff5a00)
- table.cell(dcaTable, 1, 3, "$" + str.tostring(P_L, "#.##"), text_color=#000000)
- table.cell(dcaTable, 0, 4, "Total Value", text_color=color.white, bgcolor=#ff5a00)
- table.cell(dcaTable, 1, 4, "$" + str.tostring(totalValue, "#.##"), text_color=#000000)
Advertisement
Add Comment
Please, Sign In to add comment