#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// © RomainHaenni
#indicator("Dollar Cost Average", "DCA")
# Converted by Sam4Cok@Samer800 - 09/2023 request from usethinkscript.com memeber.
declare lower;
input startingAmount = 0.0;#, "Starting Investment")
input investmentAmount = 500.0;#, "Repeating Investment")
input frequency = 7;#, "Repeats every (in days)", inline="frequency")
input feeRatio = 0.02;#, "Broker Fee")
input startTime = 0100;
input startDate = 20230101;#(timestamp("Jan 01 2023"), "Starting Date")
input endDate = 20300101;#(timestamp("Jan 01 2033"), "Ending Date")
input ShowInfoLabel = yes;
def na = Double.NaN;
def last = isNaN(close);
def assets;# = 0.0
def feeAmount;# = 0.0
def entryPrice;# = 0.0
def invested;# = 0.0
def netProfit;# = 0.0
def totalFee;# = 0.0
def totalTrades;# = 0
def portfolioValue;# = 0.0
def newAssets;
def time = GetTime();
def current = GetAggregationPeriod();
def isdwm = current >= 86400000;
def isintraday = current < 86400000;
def investmentWindow = DaysFromDate(startDate) >=0 and DaysTillDate(endDate) >= 0;
def stDay = DaysFromDate(startDate) ==0 and SecondsFromTime(startTime) == 0;
def stTime = if stDay then time else stTime[1];
def timeDiff_ = floor((time - stTime)/ 1000 / 60 / 60 / 24);
def timeDiff = timeDiff_;
def timeCond =(timeDiff % frequency) == 0;
if investmentWindow {
if startingAmount > 0 and invested[1] == 0 {
totalTrades = totalTrades[1] + 1;
feeAmount = startingAmount * feeRatio;
invested = startingAmount;
newAssets = (startingAmount - feeAmount) / close;
assets = newAssets;
entryPrice = close;
totalFee = feeAmount;
} else
if timeCond and (isdwm or (isintraday and SecondsFromTime(1200) == 0)) {
totalTrades = totalTrades[1] + 1;
feeAmount = investmentAmount * feeRatio;
invested = invested[1] + investmentAmount;
newAssets = (investmentAmount - feeAmount) / close;
entryPrice = (entryPrice[1] * assets + (investmentAmount - feeAmount)) / (assets + newAssets);
assets = assets[1] + newAssets;
totalFee = totalFee[1] + feeAmount;
} else {
totalTrades = totalTrades[1];
feeAmount = feeAmount[1];
invested = invested[1];
newAssets = newAssets[1];
entryPrice = entryPrice[1];
assets = assets[1];
totalFee = totalFee[1];
}
} else {
totalTrades = totalTrades[1];
feeAmount = feeAmount[1];
invested = invested[1];
newAssets = newAssets[1];
entryPrice = entryPrice[1];
assets = assets[1];
totalFee = totalFee[1];
}
netProfit = if investmentWindow then close * assets - entryPrice * assets else netProfit[1];
portfolioValue = if investmentWindow then entryPrice * assets + netProfit else portfolioValue[1];
plot PortValue = if investmentWindow and !last then portfolioValue else na;#, "Portfolio Value"
plot InvstAmnt = if investmentWindow and !last then invested else na;#, "Invested Amount"
InvstAmnt.SEtLineWeight(2);
PortValue.SetDefaultColor(Color.DARK_ORANGE);
InvstAmnt.SetDefaultColor(Color.GREEN);
#-- Label
def info = ShowInfoLabel;
def invst = round(invested, 2);
def pnl = round(netProfit, 2);
def pnlPer = round(netProfit * 100 / invested, 2);
def fee = round(totalFee, 2);
def feePer = round(totalFee * 100 / invested, 3);
def folioValue = round(portfolioValue, 2);
def asses = round(assets, 8);
def entPrice = round(entryPrice, 2);
def valCol = if folioValue > invst then 1 else
if folioValue < invst then -1 else 0;
AddLabel(info, "Invested($" + invst + ")", color.LIME);
AddLabel(info, "A/C Value($" + folioValue + ")", if valCol>0 then Color.GREEN else
if valCol <0 then Color.RED else Color.GRAY);
AddLabel(info, "PnL(" + pnlPer + "%) $" + pnl, if pnl >0 then Color.GREEN else
if pnl <0 then Color.RED else Color.GRAY);
AddLabel(info, "Fees(-" + feePer + "%) $-" + fee, color.PINK);
AddLabel(info, "Shares(" + asses + ")", color.WHITE);
AddLabel(info, "AvgCost/Share($" + entPrice + ")", color.WHITE);
AddLabel(info, "No of Trades(" + totalTrades + ")", color.WHITE);
#-- END of CODE