Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
The concept of time in Quantopian

A lot of my trading ideas revolve around the concept of time. Quantopian, at least from what i can tell, only gives you a picture of the market at the minute in which the loop is run. Is there a way to reference/store prices from a day ago? two days ago?

To demonstrate a bit what i mean: you can write a code that will trade based on the crossing of moving averages but if a person would trade on this strategy he would look at the positions of the moving averages before they crossed to know if the crossing is a buy or sell signal. From what i can tell there really isnt a way to store values let's say in a list and retrieve them at a later time to compare data from two different dates.

Can anyone help with that?

Hope my explanation was understandable.

All the best,

Daniel

4 responses

There are a few ways to do this. The simplest way is to create a blank list in your initialization, and append values to the list with each bar. You can also use deques if you are familiar with them.

def initialize(context):  
    context.security = sid(8554) #S&P 500  
    # create the blank list  
    context.past_prices = []

def handle_data(context, data):  
    # every bar (either minutely or daily) append the security's price to the list  
    context.past_prices.append(data[context.security].price)  
    # let's log the first bar's price  
    log.info(context.past_prices[0])  
    # now let's log all the prices we have stored  
    log.info(context.past_prices)  
    # you don't have to just store the price, if you want you can also  
    # store volume data or something like a moving average for each day  

Does that help?

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.

With regard to moving averages you can take the price (or shorter moving average) and divided it by the longer moving average. If its greater than 1 the cross is a buy. If its less than 1 the cross is a sell. (This would mean buying not exactly when the two moving averages intersect but when the shorter actually crosses over the longer). As Gus said, there are also a number of other ways to tackle the issue of storing relevant data.

I tried creating a blank list but it appears quantopian doesnt recognize it. I get errors anytime i tried using the list. I declared the empty list both
as context.list = [] which doesn't work and gives me an error and as a global list which doesn't seem to work as well. Also the prices I would append are the minute interval prices. Is there a way to record the day closing price and create a list of daily bars if you will?

@Daniel, you can look at a recent thread as an example. In my backtest source code I used a deque to store the 5 most recent RSI indicators. You can use the same idea to store any value you want.

https://www.quantopian.com/posts/can-lamda-be-used-to-look-back#51e5f09bd3cf61daa0000139

from collections import deque  
import numpy as np

# initialize  
context.list = deque([], maxlen=15)

# append value (deque configured to keep 15 most recent)  
context.list.append(price)

# get average  
avg = np.mean(context.list)