Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
First Working Algo of My Own

Volatile, ugly and not very strategic - but helped me learn the basics and does alright.

5 responses

Um you used a 2000 day MA just a heads up probably not the best choice. I believe it is just a typo though. Here it is with a 200 instead of 2000 day MA.

Thanks Spencer, good catch, was definitely a late-night typo. Like I said - it's ugly.

Interesting approach. I think exit using trailing stoploss gives good exit.
I thought you were harvesting and cutting too early.
So just changed harvest at 2.5x and 0.91x for cutting
Something surprising occurred in early Feb 2015 have a look.

Austin, thanks for sharing. A few thoughts about your algo:

  • In lines 87-90 you are placing 3 orders per stock. I'm guessing you want to place a single stop-limit order? If so you should use the StopLimit parameter in the order.
order(stock,shares_target)  
log.info("Initiating position %s." % str(stock.symbol))  
order(stock, shares_target*-1, style=StopOrder(stop_price))  
order(stock, shares_target*-1, style=LimitOrder(target_price))  
  • To speed up your backtest, you can use panda's mavg calculation. You can use history to get the trailing prices and then index into the dataframe to get the values. Like so:
prices       = history(200, '1d', 'price')  
price         = prices[stock].iloc[-1]  #most recent stock price  
mavg10   = prices[stock].iloc[-10:].mean() #avg of last 10 prices  
mavg50   = prices[stock].iloc[-50:].mean() #avg of last 50 prices  
mavg200 = prices[stock].iloc[-200:].mean() #avg of last 200 prices  
  • I'd suggest to move towards using the more robust order_target functions. These will scale with your portfolio and take out all of the manual order calculations.

  • Also try running your backtest in minute mode, this is a more realistic simulation of the market conditions. Daily mode is a good check if your code compiles and the strategy holds merit, but minute mode gives a closer view of the strategy. Check out this thread for more details.

Hope this helps and looking forward to seeing future iterations!

cheers,
Alisa

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 Alisa -

The three orders was originally intentional to set both an automatic stop-loss and an automatic harvest if the stock reached my target price. I've removed these in favor of only doing the cutting or harvesting in the before_trading_start method.

Thanks for the tip using the Pandas stuff for mavg. Will replace those definitions.