Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help Me Please *novice*
moving_av1 = data[context.tsla].mavg(5)  
    moving_av2 = data[context.tsla].mavg(15)  
    current_price = data[context.tsla].price  
    cash = context.portfolio.cash  
    if moving_av2 > current_price:  
        #trading price is below the 15 day ma, looking for rebound  
        if moving_av1 > moving_av2:  
            #still pulling back, bearish crossover coming  
            pass  
        elif moving_av1 < moving_av2:  
            #buy, bearing crossover compelete, looking for entry on lowest MACDhist  
            if (((moving_av2)-(moving_av1))/(moving_av2) > .005  
                #MACD difference is greater than .5%, buy  
                #share calculation  
                **if current_price*10 > cash  
                    print'not enough funds'  
                elif current_price*10 < cash  
                    order(context.tsla, +10)**  
            elif (((moving_av2)-(moving_av1))/(moving_av2) < .005  
                #not good enough  
                pass  
    elif moving_av2 < current_price:  
        #trading price is above 15 day ma, looking for resistance  
        if moving_av1 > moving_av2:  
            #sell, bullish cross compelete, MACDhist gaining, resistance getting close  
            if (((moving_av2)-(moving_av1))/(moving_av2) > -.005  
                #MACD difference is greater than .5%, sell  
                #share calculation  
                *order(context.tsla, -10)*  
            elif (((moving_av2)-(moving_av1))/(moving_av2) < -.005  
                #not good enough  
                pass  
        elif moving_av1 < moving_av2:  
            #bullish cross coming, moving up  
            pass  

Im getting an error syntax where the bold is at. not sure why!
Will it error out at the italics also then or no?

Also, if statements are linear, how would I look this function so that the orders happen every time the conditions are met and not just the first time? I've tried while and for statements but it errors out every time.

Thank you, anyone!

2 responses

so bold is the

if current_price*10 > cash  
                    print'not enough funds'  
                elif current_price*10 < cash  
                    order(context.tsla, +10)  

and the italics are
order(context.tsla, -10)

thank you!

Hi Stuart,

Thanks for posting! In Python, almost any statement (if, else, while, for, def) end with a ':'. So in this case you would need to have ':' at the end of your if statement.

if current_price*10 > cash:  
    print 'not enough funds'  
elif current_price*10 < cash:  
    order(context.tsla, +10)  

Try it out and let me know if that works.

Seong

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.