Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculating DPO

Hi, I am totally new to Quantopian and also new to coding (aside from Fortran 20 years ago).
I am trying to replicate a timing signal that I have been using in Vectorvest using the DPO.
Here is what I coded inside a scheduled function.
The reference security will be to time the market, and I will put trades using context.security.
I was struggling getting the right value for the lookback period for the security.
As such, what if I want a DPO of 11 days, the lookback period will have a value of 6.5. How does it work with the array of prices... ??
Thank you

DPO_period = 20  
Lookback_period = ((DPO_period/2) + 1)  
DPO_price = data.history(reference_security, 'price', DPO_period,'1d')  
SMA_DPO = DPO_price.mean()  
Lookback_price = data.history(reference_security, 'price', Lookback_period,'1d')  
DPO = (Lookback_price[(-Lookback_period)] - SMA_DPO)  
log.info(DPO)  
record(DPO = DPO)

For some reason, I cannot get the same DPO values as other software.

4 responses

DPO = Price {n/2 + 1} periods ago less n-day SMA
DPO(20) = Price {20/2 + 1} periods ago less 20-day SMA
DPO(20) = Price 11 periods ago less 20-day SMA

def DPO(context, data):  
    reference_security = symbol('SPY')  
    DPO_period = 20  
    Lookback_period = ((DPO_period/2) + 1)  
    DPO_prices = data.history(reference_security, 'price', DPO_period,'1d')  
    SMA_DPO = DPO_prices.mean()  
    DPO = DPO_prices[-Lookback_period-1] - SMA_DPO  

Hi Vladimir, Thank you for the explanation and the fix.
Now, how would I do to calculate a 20day SMA, but on the date, 11 days ago?
I looked around and found pandas.rolling_mean()
Not sure how to implement it.
Thanks

Hey Matt,

Here's an example of calculating a 20 day SMA from 11 days ago using data.history. As you can see, we take the last 31 days of data, discard all but the first 20, and take the mean.

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.

Great, Thank you Nathan