Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
new to quantopian: looking for functionality

Hello all,
I am new to quantopian and i am looking for a specific functionality: i want to issue buy/sell orders not only based on the stock data, but also based on the length of time i have a position open. For example: i expect a position that is a long time in the portfolio to have a higher return than a position that has just been opened.
The portfolio object doesn't seem to have a buy_date of the position or position_lifetime. Any possibilities ?

regards,
Geert

5 responses

You could always initiate your own array that stores the current time at the moment a position is opened

It's been confirmed that any expando property hung off of the "context" object will be persisted across sessions and across system disruptions (if running in real time). So you could manage another collection, like what Ethan mentioned, to keep track of dates of position opening.

Alternatively, you can use the expando off of the data[stock] object. NOTE: this technique has not been confirmed as persistent by the Quantopian techs.

def initialize(context):  
    context.ZZZ = symbols("SPY")[0]

def handle_data(context, data):  
    for stock in data:  
        if ('OpenDate' not in data[stock]):  
            order_target_percent(stock, 1.0 / len(data))  
            data[stock].OpenDate = get_datetime()  
            data[stock].CountDown = 50  
            print("{0} opened: {1}".format(  
                    stock.symbol, data[stock].OpenDate))  
        else:  
            data[stock].CountDown -= 1  
            if (data[stock].CountDown <= 0):  
                order_target_percent(stock, 0.0)  
                del data[stock].OpenDate  
                del data[stock].CountDown  
                print("{0} closed: {1}".format(  
                    stock.symbol, get_datetime()))

    if ('CountDown' in data[context.ZZZ]):  
        record(CountDown = data[context.ZZZ].CountDown)  

Note the use of the following expandos that are made available throughout the test:

data[stock].OpenDate = get_datetime()  
data[stock].CountDown = 50  

Once not needed one would use the del data[stock].OpenDate. to remove it.
And one would use the test if ('OpenDate' not in data[stock]): to determine if the expando existed.

Ethan's suggestion is definitely preferred. The help doc says it reasonably well:

context is an augmented Python dictionary used for holding state between methods. Properties can be accessed using dot notation (context.some_property).

We don't support adding properties to the data object.

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.

Well, that sucks.
It was a beautiful solution too.
Cramming all the TA goodies into/onto a hanging context object just isn't as elegant as expandos on the data[stock] object.
It does "work" just not across system resets that the context is guaranteed to persist over. So it's not advised.

Hi Dan,

Might be a good idea to publish some technical specs. on the use of context for storing data. Under the hood, how does it work under Q backtesting/paper, & IB paper/live trading? Are you writing to disk immediately, or at the end of every minute bar (there's a ~ 10-second housekeeping period, right?)? Or maybe context gets written to disk only after the market closes? Memory limits? Etc.

I just figure there might be some good, bad, and ugly uses of context.

Grant