Dollar Cost Averaging (Portfolio)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © miraalgo

//@version=5
indicator("Dollar Cost Averaging (Portfolio)",shorttitle="DCAP",precision=4)
var invest=0.0
var qty1=0.0,var qty2=0.0,var qty3=0.0,var qty4=0.0,var qty5=0.0,var qty6=0.0,var qty7=0.0,var qty8=0.0,var qty9=0.0,var qty10=0.0
var counter=0.0
var value=0.0
var yearly=1.0
t1=input.time(defval=timestamp("14 May 2010"),title="start")
t2=input.time(defval=timestamp("14 May 2030"),title="finish")


saving1=input.int(10,step=1,title="Invest 1",inline="1")
symbol1 = input.source(close, "Source",inline="1")
saving2=input.int(0,step=1,title="Invest 2",inline="2")
symbol2 = input.symbol("NYSE:IBM", "Symbol",inline="2" )
saving3=input.int(0,step=1,title="Invest 3",inline="3")
symbol3 = input.symbol("NYSE:IBM", "Symbol",inline="3" )
saving4=input.int(0,step=1,title="Invest 4",inline="4")
symbol4 = input.symbol("NYSE:IBM", "Symbol",inline="4" )
saving5=input.int(0,step=1,title="Invest 5",inline="5")
symbol5 = input.symbol("NYSE:IBM", "Symbol",inline="5" )
saving6=input.int(0,step=1,title="Invest 6",inline="6")
symbol6 = input.symbol("NYSE:IBM", "Symbol",inline="6" )
saving7=input.int(0,step=1,title="Invest 7",inline="7")
symbol7 = input.symbol("NYSE:IBM", "Symbol",inline="7" )
saving8=input.int(0,step=1,title="Invest 8",inline="8")
symbol8 = input.symbol("NYSE:IBM", "Symbol",inline="8" )
saving9=input.int(0,step=1,title="Invest 9",inline="9")
symbol9 = input.symbol("NYSE:IBM", "Symbol",inline="9" )
saving10=input.int(0,step=1,title="Invest 10",inline="10")
symbol10 = input.symbol("NYSE:IBM", "Symbol",inline="10" )

c1  = symbol1
c2  = request.security(symbol2, timeframe.period, close)
c3  = request.security(symbol3, timeframe.period, close)
c4  = request.security(symbol4, timeframe.period, close)
c5  = request.security(symbol5, timeframe.period, close)
c6  = request.security(symbol6, timeframe.period, close)
c7  = request.security(symbol7, timeframe.period, close)
c8  = request.security(symbol8, timeframe.period, close)
c9  = request.security(symbol9, timeframe.period, close)
c10 = request.security(symbol10, timeframe.period, close)


if time>t1 and time<t2 and timeframe.isdwm==true
    counter:=counter+1
    if na(c1) == false
        qty1:= qty1 + (saving1/c1)
    else
        saving1:=0
    if na(c2) == false
        qty2:= qty2 + (saving2/c2)
    else
        saving2:=0
    if na(c3) == false
        qty3:= qty3 + (saving3/c3)
    else
        saving3:=0
    if na(c4) ==false
        qty4:= qty4 + (saving4/c4)
    else
        saving4:=0
    if na(c5) ==false
        qty5:= qty5 + (saving5/c5)
    else
        saving5:=0
    if na(c6) ==false
        qty6:= qty6 + (saving6/c6)
    else
        saving6:=0
    if na(c7) ==false
        qty7:= qty7 + (saving7/c7)
    else
        saving7:=0
    if na(c8) ==false
        qty8:= qty8 + (saving8/c8)
    else
        saving8:=0
    if na(c9) ==false
        qty9:= qty9 + (saving9/c9)
    else
        saving9:=0
    if na(c10) ==false
        qty10:= qty10 + (saving10/c10)
    else
        saving10:=0
    
    invest:=invest + (saving1+saving2+saving3+saving4+saving5+saving6+saving7+saving8+saving9+saving10)

value1 = na(c1) ==false ? (qty1*c1) : 0
value2 = na(c2) ==false ? (qty2*c2) : 0
value3 = na(c3) ==false ? (qty3*c3) : 0
value4 = na(c4) ==false ? (qty4*c4) : 0
value5 = na(c5) ==false ? (qty5*c5) : 0
value6 = na(c6) ==false ? (qty6*c6) : 0
value7 = na(c7) ==false ? (qty7*c7) : 0
value8 = na(c8) ==false ? (qty8*c8) : 0
value9 = na(c9) ==false ? (qty9*c9) : 0
value10 = na(c10) ==false ? (qty10*c10) : 0

value :=value1 + value2 + value3 + value4 +value5 +value6 +value7 +value8 +value9 +value10   
vi=value/invest
if timeframe.ismonthly
    barly=math.pow(vi,(1/counter))
    yearly:=math.pow(barly,12/timeframe.multiplier)
if timeframe.isweekly
    barly=math.pow(vi,(1/counter))
    yearly:=math.pow(barly,52/timeframe.multiplier)
if timeframe.isdaily
    barly=math.pow(vi,(1/counter))
    yearly:=math.pow(barly,365/timeframe.multiplier)
yearly:=(yearly-1)
savingperbar=saving1+saving2+saving3+saving4+saving5+saving6+saving7+saving8+saving9+saving10
plot(savingperbar,color=color.white,title="Investment")
plot(invest,color=color.white,title="Total Investment")
plot(value,color=value>invest ? color.green:color.red,title="Value")
plot(vi,color=color.yellow,title="Value/Investment")
plot(yearly,color=color.yellow,title="Yearly Profit/Loss (approx)")
plot((qty1*c1),title="asset 1",color= #7fb3d5,display=display.none)
plot((qty2*c2),title="asset 2",color=#2980b9,display=display.none)
plot((qty3*c3),title="asset 3",color=#1f618d ,display=display.none)
plot((qty4*c4),title="asset 4",color=color.blue,display=display.none)
plot((qty5*c5),title="asset 5",color=#d2b4de,display=display.none)
plot((qty6*c6),title="asset 6",color=#bb8fce,display=display.none)
plot((qty7*c7),title="asset 7",color=#a569bd,display=display.none)
plot((qty8*c8),title="asset 8",color=#f5cba7,display=display.none)
plot((qty9*c9),title="asset 9",color=#f0b27a,display=display.none)
plot((qty10*c10),title="asset 10",color=#eb984e,display=display.none)




