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

Hey guys,

New here, so forgive anything obvious I'm missing, but I'm working on a day trading system, and I can't seem to think of an appropriate way to store the 'entry' value I calculate from the ATR in my first function, so that I can then lookup that entry value for each specific stock in my handling function.

If anyone has any idea of how I could do this I would greatly appreciate it.

3 responses

I think the easiest way to do this is to create a dictionary that stores each stock's entry. For example, you can access a certain stock's entry by doing:

context.entries[stock]  

I made an example below. I also have to check that we have previously calculated an entry for a certain stock, which is why I added the first part to the if statement.

Hopefully that helps!

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.

Or you could use the zipline SIDData object as a dynamic property base. You can effectively stick any named property to a SIDData object and it will hang around as long as you need it. This backtest does a gap open test instead of your other given test.

The strategy keeps the SIDData objects in a context.S collection. And then you can just stick stuff to them:

context.S[stock].MyFavoriteFood = "Twinkies"  
context.S[stock].ConsumedPerYear = 18234  

Also notice the history() call is outside of the 'for' loop since history already has the history for every stock in data (so no need to re-run history() inside the loop each time).
Another point generally (although it might not apply so directly to the code above): At one time I recall running into a cryptic message doing for stock in context.stocks that took a skype session with Q to figure it out, it was merely because a stock had no trades during that bar, and I was bitten by it way down the line in another function, so ever since then I stick with for stock in data, or for some other purposes for stock in context.portfolio.positions, just never for stock in context.stocks, recommend shying away from that. You can see MT has some safeguards.
Notice the vertical alignment of equal signs in Market Tech code, makes for easier reading.
You can even run wild with white space like I do:

        c.highs  = history(period, '1d', 'high') .bfill().fillna(1)  
        c.lows   = history(period, '1d', 'low')  .bfill().fillna(1)  
        c.prices = history(period, '1d', 'price').bfill().fillna(1)  

...or:

        if show_beta:       do_show_beta(      c, sec)  
        if show_volatility: do_show_volatility(c, sec)  
        if show_drawdown:   do_show_drawdown(  c, sec, price)  

(c = context) There's virtually no performance hit, Python runs a built version that removes all white space, and comments.
Part of the reason I posted that is that I'm not sure of what I'm doing with the fills in case anyone has a definitive tip.