Using Pine Script to DCA into SPY

Sze Zhong LIM
Data And Beyond
Published in
4 min readAug 26, 2023

To start back testing a strategy on Tradingview.com, we can write a pine script and enter it into the pine editor and run the strategy to know roughly how much we can profit based on that strategy.

We will be using a monthly DCA (Dollar Cost Averaging) strategy whereby we use $1000 to buy the SPY every start of the month. We will only buy whole number contracts and not fractional contracts. This strategy will not roll any unused money to the next months. We will restrict the constraints of this strategy to start from Year 2000. We will sell everything on 24/8/2023 (which is two days back), just to simulate how much we will have, and how much we will have realized should we decide to sell.

We will first log into tradingview.com and choose SPY as the chart we want. Click on the area at 1, and then ensure you choose the right SPY ticker which is a fund ETF in NYSE Arca.

After choosing the correct ticker, click on the Pine Editor button as shown in the figure below.

The editor should open up and you should see the below screen.

You may input the script below into the pine editor, and then click “Add to chart”

//@version=4
strategy("LTI Strategy 1 DCA Every Month", overlay=true, pyramiding=10000)

monthly_investment = 1000

// Calculate the number of shares to buy based on the current closing price
shares_to_buy = monthly_investment / close

// Define the start date (January 1, 2000)
start_date = timestamp(year=2000, month=01, day=01)

// Check if a new month has started
isNewMonth = change(month)

// Buy the calculated number of shares at the start of each month after the start date
if isNewMonth and time >= start_date
strategy.entry("Buy", strategy.long, qty=shares_to_buy)

// Check if the current date is 24th August 2023
should_sell = year == 2023 and month == 8 and dayofmonth == 24

// Sell all positions on 24th August 2023
if should_sell
strategy.close_all()

After clicking on “Add to Chart”, the screen will jump to the “Strategy Tester” automatically. If it did not, do click on “Strategy Tester” to see the screen below.

Clicking on the other tab called “Performance Summary” shows the performance of your portfolio and key metrics.

Clicking on the tab called “List of Trades” shows the individual trades taken. You can even see that all the stocks were sold on 24/8/23 as we coded it to be done (as per highlighted). If you click on the green colored tab, you can export the whole info into a csv file for further analysis either on Python / Excel.

You may view the actual strategy on my trading view page here.

We can compare this strategy with an excel file that does a yearly DCA of 12000 (each month 1000) with a yearly compounded interest of 7%. We would yield similar results, with similar profits of 400+k for 22–23 years.

Compounded Yearly Interest of 7%

If we use a compounded yearly interest of 8% or 10%, we would get the results as below:

Compounded Yearly Interest of 8%
Compounded Yearly Interest of 10%

The difference between 7% and 10% compounded interest is more or less double the profits. Though there are many investment gurus who like to proclaim that DCA into the SPY will garner around 10% compounded interest, it is important to know that this highly depends on the timeframe of DCA and exiting.

--

--