Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Local Variable assigned but never used error

Okay, I'm sure the answers staring me in the face; however, I've tried everything and I'm completely stuck at this point.

Is there a way I can use a variable in multiple functions? I keep getting an error anytime I use a variable that was defined in another function.
I've tried the record() ; however, it's not working for me.

Ex:
schedule_function(my_func1, date_rules.every_day(), time_rules.market_open(hours=1))
schedule_function(my_func2, date_rules.every_day(), time_rules.market_close(hours=1))

def my_func1:
x = 1 +1

def my_func2:
y = x+5

4 responses

It's very hard to debug with just a snippet - it would be easier if you shared a backtest with the error.

But, based on what you've got - my guess is that you're not putting your information on the context object. Try something like this:

def my_func1(context, data):  
    context.x = 1 + 1

def my_func2(context, data):  
    context.y = context.x + 5  
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.

Thanks Dan for the reply...I really appreciate it.
I had to do some deep diving and I been researching about global and local variables which helped me figure out my issue.
Global and Local variables were not discussed in the Quantopian tutorial, so this error had me pulling my hair out.

I'm attempting to put forth a valiant effort on learning this on my own; so I want to struggle through these issues without posting my algo code and having someone else solve it for me (that will be my last option).

The Quantopian style is, in general, to avoid global variables. We put everything onto context and pass that around.

Hmm, okay...global variables are not working for me. I give up, I think i've pulled out all my hair. Why am I getting a "Undefined Name sma" error here even after my function has return the sma variables in a list