Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
DCA with ETFs, need some assistance please.

Hi,

I am totally need to Python and quantopian. I am actually a C# developer. I am trying to build simple Dollar Cost Average strategy (it is strictly not a strategy as indexing is not meant to generate any Alpha. But the idea is to match the market the best we can where we want to keep commission and expense ratios to be extremely small).

Sure we can use a Vanguard Index fund in UK (were I am from), as Vanguard recently launched the fund in UK. But I work in Denmark and the EU version of the Vanguard Index fund is just too expensive. Plus I want give quantopian a try.

I figured I will buy VOO through Interactive Broker every month and use say 500 dollars as a capital.

what i have done so far (nothing really) is the following:

Initialize the context and get voo

def initialize(context):
context.voo = sid(40107) # VOO

/* I NEED THE FOLLOWING FUNCTION */ // Boolean test to see if today is a trading date for this strategy: the first def IsFirstTradingDateOfMonth(date)

def handle_data(context, data):
orderValue = 500 # 500 dollars we are going to put aside every month

if IsFirstTradingDateOfMonth(??) # <--- do i put data.date?? ....
amount = orderValue / data[context.voo].price
if amount > 0
order(sid(24), amount)

4 responses

Hi Joyanta,

Does this algo behave the way you described?

thanks,
fawce

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Actually, this is a bit better - it accounts for the fact that trades placed today are filled in simulation of tomorrow's data (lookahead bias avoidance), and it skips over weekends to check if the next business day is in the next month. The custom plot in these backtests is just the accumulated shares in the position.

thanks,
fawce

WOW. thanks for a such a quick reply. I will test it and will check it against excel and get back to you. Will take a week as have work coding to do for the rest of the week!!!

Hi Fawce,

I am just going through the results, but before I can go any further I just wanted ask you few things. and Just added a back test of Dollar Cost Average with ETF (SPY). And I have modified what you have done slightly.

Assumptions:
- Initially I had VOO (the vanguard but it doesn't have the data as far back as 2002), so I am using SPY instead
- I am not putting any commissions and other charges.
- DCA means putting a fixed amount of cash, so I am putting a dollar value of 500 USD every month (rather a cap of 500 USD). Code below.

 if next_trade_day.month != current_month:  
        qty = 500 / data[context.spy].price  
        # this is the last trading day of the month  
        # place orders, which will be simulated tomorrow.  
        order(context.spy, qty)  
        # store the month of the prior month  
        context.last_month = current_month  
    record(spy_shares=context.portfolio.positions[context.spy].amount)  

Observations:

  1. Price of SPY from Quantopean (QP) vs the Adjusted Close prices from Yahoo are quite out 1. Then again this is a data issue. Please do elaborate if you could. I guess we are looking for normalised data (adjusted for dividends, splits etc and if I am correct yahoo prices are normalised right??).
  2. Benchmark (if I am correct, I read somewhere it is s&p 500 index, which is just as well :)) is showing a return of 55.19% (result attached). But I am checking it against 2 and derive the following:

(date, price) Start:(01/02/2002, 1106.73)
End:(01/03/2013, 1569.19)

Holding Period Result (HPR) = (1569.19/1569.19) -1 = 0.417861628.
The Benchmark is showing 58.55%. I am not if the return is that, or can you let me know what the calculation is done via HPR calculation.

(Interestingly DCA actually shows 40.03%. This I will also verify with also going forward).

Yahoo SPDR S&P 500 (SPY) historical prices relevant for this simulation
S&P 500 Benchmark from Yahoo