Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Passing history frequency as string variable problem

Hello,

I am trying to pass history frequency ('1m' , '1d') to the history function as a string variable but I get the error:
Error history() only supports the 'frequency' values '1d' or '1m'.

def initialize(context):  
    context.stock =  symbol('AAPL')

def handle_data(context, data):

    tf = '1d'  
    price_history = history(5,tf,'price')[context.stock]  

I also tried :

def initialize(context):  
    context.stock =  symbol('AAPL')

def handle_data(context, data):

    tf = '1d'  
    price_history = history(5,str(tf),'price')[context.stock]

The reason I want to do this, is that I have a class and I want to make instances for my stock universe (daily, minutely, hourly...), and then pass them the time-frames as variables in the init , I can only now copy the whole class and rename them to class_minute, class_daily which is not an object oriented approach.

any help is appreciated

2 responses

Hi Adham,

The history function only accepts string literals for the resolution so the input cannot actually be a string variable. If you would like to replicate the variable logic, you can make an if statement around the history call so that you call history(5, '1d', 'price') if one condition is satisfied and history(5, '1m', 'price') if the other is satisfied.

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.

Thank you for the reply!

Although I'd rather copy the whole class for code efficiency because I have a bunch of history functions to call, that would be a lot of if statements,
but thank you any way :)