Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Fun with averages - newbie needs feedback :)

Hi all,

I'm relatively new to the Quantopian world, and thought I'd try out a very simple strategy using some moving averages after having intuitively noticed some patterns in charts I've looked at. Would love some feedback on my code, how to improve it, how to make it more accurate, how to make it more readable, and also how to improve my strategy and logic to fix any mistakes or assumptions. All in all, just general feedback for someone pretty new would be much appreciated.

One question I have is about the position values. I noticed that a ways into the backtest I start going negative in my cash balances — how do I avoid this? I would ideally like to develop positions that don't require "leveraged" or "margin" positions.

Any input greatly appreciated!

(By the way: Love this community. Good to finally be posting after having seen some great work from others.)

13 responses

Jacob,

This is a great start and I think you're definitely on the right track. I liked that you put in a check to see if shares are already existing (shares >= 100).

There are a couple ways to go about dealing with negative cash and leverage :

These are just a few resources to help you get started, and if you have questions implementing these into your algo don't hesitate to ask!

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

Thanks for the direction! I'm curious, couldn't you just call some sort of reference to your cash in a more simple manner? For example:

if context.cash > data[stock].close_price:  
    #buy shares  

Jacob,

I see where you're coming from. Something like this?

if context.portfolio.cash > data[stock].close_price*number_of_orders:  
    #buy shares  

This is perfectly fine for a good heuristic, but you would need a check for open orders if you're planning on using this with minutely data

if get_open_orders(sid=stock): # skip order until this is fulfilled  

Hi Seong,
Yeah you get what I'm thinking. But what does the second block of code in your response do? What is the if statement actually checking in this case?

Hi Jacob,

Essentially it checks to see there are any existing open orders for the current 'stock', so if there are no open orders, then the if-statement won't pass

PSEUDOCODE: If (there exists any open orders): Run this code  

Not sure if you're asking about the Python syntax, but to give you an idea of what this means

# This will print out (5)  
if 5:  
    print 5

# This won't print out (5)  
if None: # an if-statement will fail if the clause is false or if nothing exists  
    print 5  

I hope that clears things up,

Seong

Thanks Seong, I appreciate the clarification. I understand Python syntax and if statements and everything, I'm still just a little new to this IDE/API structure and so didn't know exactly what this particular if statement was doing.

No problem :) Glad to help out with more questions or if you want to just toss around ideas about the strategy

Yeah I'd definitely like to toss around a few ideas. Here is an update to the algo. It still dips into the negatives a little but not nearly as much as before, which I'd probably be okay with, since a small amount of leverage is probably reasonable with such a large initial cash balance.

And yes, let me know what ideas you'd have to improve the strategy in any way!

Good start. The only thing is you cheery picked stocks in which you saw strong momentum to do a momentum backtest, which makes you exposed to lookahead bias.

Thanks for feedback, Roy! I actually just picked stocks that jumped to mind because I was really just trying to practice testing strategies on multiple securities, which I hadn't done before. I will backtest this on other stocks that might not have seen as much good fortune and follow up. Any recommendations for good stocks to test?

Depends on what you want to do, IMO. Some stocks are good for momentum, some are good for spreads, some are good for event speculation, etc. But these behaviors are also time-varying, so be careful.

Is there a way to run a backtest on preset lists of stocks (i.e. exchanges, indices, etc) or on a random selection of a given number of stocks?

Jacob,

Yes, you can use

set_universe(universe.DollarVolumeUniverse(floor_percentile=98.0, ceiling_percentile=100.0))  

which you can iterate through later using
for stock in data.keys(): print data[stock].price For more information on this, check out this link