Systematic Investment Plan
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator(title='Systematic Investment Plan',overlay=false, timeframe='D')
startYear = input.int(2010, maxval=2999, minval=0, title='Start Year', group='Inital Investment', tooltip='Year on which investment is started', confirm=true)
initialDeposit = input.float(10000, "Deposit", minval=0, step=5000, group="Inital Investment", tooltip='Initial one time deposit to start with', confirm=true)
recurringDepositFrequency = input.string("monthly", "Frequency", group="Recurring Investment",  options=["monthly", "weekly"], tooltip="Periodic investment frequency", confirm=true)
recurringDeposit = input.float(1000, "Deposit", step=1000, minval=0, group="Recurring Investment", tooltip="Periodic investment", confirm=true)

var buyAndHoldQty = 0.0
var totalInvestment = 0.0

term = recurringDepositFrequency == "monthly"? month : weekofyear
var started = false
recDep = ta.change(term)!=0? recurringDeposit : 0
if(year >= startYear and ta.change(term)!=0)
    bnhAmount = started ? recDep : initialDeposit
    started := true
    bnhPrice = close
    bnhQty = bnhAmount/bnhPrice
    if(bnhQty!=0)
        totalInvestment += bnhAmount
        buyAndHoldQty += bnhQty

bnhEquity = close*buyAndHoldQty

plot(bnhEquity, "Equity", color=color.new(color.green, 80), style=plot.style_area)
plot(totalInvestment, "Investment", color=color.new(color.purple, 50), style=plot.style_area)