Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Issue with posting code in forum

(I created another post for the actual question I was asking, by cloning my algorithm) There seems to be an issue with forum posting where some aspects of Python code are not viewable ion the post. What is showing below is not what I actually have between the "..." "..." in my post, could someone from support take a look, specifically, the if statement is not displaying correctly.

moving_average = ta.MA(timeperiod=30)

def initialize(context):  
    context.stocks = [sid(40107),sid(12652)]  
    context.voo = sid(40107)  
    context.dltr = sid(12652)  
    context.prev_voo_ma = 0  
    context.prev_dltr_ma = 0  
    context.portfolio.starting_cash = 1000

def handle_data(context, data):

    moving_average_data = moving_average(data)  
    voo_ma = moving_average_data[context.voo]  
    dltr_ma = moving_average_data[context.dltr]  
    log.info("prev_ma: " + str(context.prev_voo_ma))  
    log.info("cur_ma: " + str(voo_ma))  
    if voo_ma  context.prev_voo_dltr:  
        order_percent(context.voo, -1)  
        order_percent(context.dltr, 1)  
    else:  
        order_percent(context.dltr, -1)  
        order_percent(context.voo, 1)  
    context.prev_voo_ma = voo_ma  
    context.prev_dltr_ma = dltr_ma  
2 responses

Hi Rick,

Thanks for the post. Sorry about the problem with less-than and greater-than. We evidently broke that recently when we put in a new forum model. For now, what you can do is "share" the full backtest and that will faithfully reproduce the code.

For your main question, what you want to do is to add cash management to your algorithm. You need to test if your order is goign to drive you into negative cash, and not execute the order if it would!

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, I totally understand, you have a great product, keep up the good work, I created a new post by sharing the algorithm, thanks for the info!