Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Quantopian Tutorial Series

This tutorial is aimed at helping anyone with Quantopian, so that means you! If you're lost/confused/curious about something, ask questions!

The tutorials come in both video and text-based versions. The video tutorials are posted on the YouTube channel first, then I write the written-versions to release on PythonProgramming.net, so you should be able to consume the content in whatever method you prefer.

Source code is posted along with the written-versions of the code, and they eventually wind up on the PythonProgramming Github as well.

The series can be found here: Finance with Python, Zipline, and Quantopian Tutorials

The initial batch of this series is fully released, which covers many of the basics of marrying Python, Quantopian, and general Algorithmic trading. I will add more strategies in time, based on requests and suggestions.

Table of Contents:

  1. Introduction to Quantopian and Zipline
  2. Basics of a Strategy
  3. Back-testing a Strategy
  4. Accessing Fundamental Data via Quantopian
  5. Back-Testing our strategy
  6. Using Schedule Function to rebalance portfolio
  7. Stop-Loss
  8. Shorting Companies
  9. Quantopian's Fetcher (fetch_csv)
  10. Trading Logic with fetched csv signals
  11. Shorting based on sentiment signals from fetch_csv
  12. Paper Trading a Strategy
  13. Understanding the goals of Funds
  14. Building a Machine Learning framework
  15. Creating Machine Learning Classifier Feature Sets
  16. Creating our Machine Learning Classifiers
  17. Testing our Machine Learning Strategy
  18. Understanding Leverage
  19. Pipeline API introduction

Questions? Comments? Suggestions? Requests? Let me know!

29 responses

Harrison, thanks for this. Looking forward to future videos.

Harrison,
What are your plans for machine learning? Another SVM with fundamentals, pattern recognition, maybe something new? Looking forward to it, I love machine learning and AI.

Thanks looking forwards to this series.

I will look forward to the csv fetcher one I struggle with some of the more complex usages of fetcher.

Would a random forest model be possible?
Thank you for all of the high quality tutorials.

@**Calvin Hobbes**: I'm still toying with the options. Ideally, ML with long term, historical, fundamental data would be one of them, but I see no reason to not have a few examples. SKLearn is situated in such a way that we can change the classifier algo extremely easily. I've been toying around with the ML and so far the biggest hurdle is that history isn't supported by the fundamentals. Ideally, we'd just grab history for the last 8 years and then test against recent 2 years, but this isn't an option, so instead we have to build sets as we go, and then train either daily, or once a day... etc, and this is very inefficient. So, fundamentals with an SVM, probably using multiple classifiers to vote, and probably a random forest against pricing movements. I may also wind up just digging into Quandl with the fetcher for historical fundamentals, but it would be really nice to avoid the fetcher, since it has a lot of implications as well.

@**Spencer Singleton**: What are some examples of the complex uses that you struggle with? We'll be using the fetcher to trade on a signals file, where we use logic to trade on a specific signal, sell on a specific signal, and ignore others.

@**Jeff Foster**: It wouldn't be my first choice, but I almost certainly will do one since we can easily query for a history of prices unlike fundamentals. This will be probably the most efficient form of ML possible right now, so it would be silly to not try it out. I saw one when I was poking around, it's here: https://www.quantopian.com/posts/simple-machine-learning-example

What I meant by that is that if I am just pulling data from one csv file with very few columns I am fine. Unfortunately when I try to use several files with many columns and try to rename and then calling the data back in I struggle to get it to run the way I would like it to run. Also in the tutorial it would be great if you could go into detail about manipulating the data that you can pull. I learned what I did about fetch csv by looking at examples more than anything so just going into detail about utilizing data would be fantastic.

@Spencer Singleton: Ah, okay. Yeah, we may wind up pulling and merging multiple Quandl CSVs, so that might be just what you need. Are you familiar with how the Pandas module works? That's where the data winds up, into a Pandas dataframe.

I understand the more basic uses of Pandas module, but when it begins to get more complex I struggle to manipulate the data to the level which I desire. This is mainly due to learning from seeing others code in action and the multiple uses of Pandas have really limited examples (two is the most I have seen) some of the ideas which I have in mind require pulling five csvs and using the data from these. In addition renaming columns and some other csv data manipulation. Hopefully you can go into that as far as examples and explanation go. Really looking forward to watching your tutorials by the way. I will await new episodes with the vigor that I have for a new GoT episode. Really appreciate you taking to time to make these.

-Spencer

@Spencer Singleton: Alright cool. I probably wont get too crazy with Pandas in this series, but I think I can probably incorporate what you're looking for here. I am also planning to put out an in-depth pandas series later on this year. I currently have a basic series here: http://pythonprogramming.net/dashboard/#tab_dataanalysis, though you may be beyond that one.

Updated through to part 11 to include stop-loss, shorting, and the fetch_csv functionality.

Nice can't wait to watch

Harrison, you have been busy. Excellent tutorials - thank you., I have learnt a stack of python thanks to you.

Text-based versions (including source code) of this series are now available up to part 18.

Hey Harrison, just saw that you posted these. Thanks very much for making them easily available to the community like this.

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.

As I am new here and to algo trading.
This would definitely help me.

Thank you for the post.

Hey Harrison!

Thanks so much for these tutorials! They are very easy to understand and follow, and I prefer them over others because you build the code from the ground up with the viewer, instead of jumping into a huge mess of someone else's code!

I haven't finished the series yet, but I hope you know when you are working with autocomplete after you type fundamentals try typing this in "pe ratio". It seems like the box just uses some simple regexp to search for related strings. If you type "pb ratio" in the box it will find the pb_ratio and autocomplete it!

Thanks so much for all your work!

  • Andrew

Hello Harrison/All,
Had a couple of newbie questions so here they are:
1. is there a way to set position size to be, say 2% of trading capital?
2. can i output what names triggered in my system for the next trading day? I would like to keep track of them on a daily basis.

Thanks so much!
Adam

  1. Creating our Machine Learning Classifiers

I cloned the code from section 16. I only look at SPY and added a bit of code near the end to compare the prediction with actually what happened... It's pretty much a coin toss.

I was really excited to try out ML. My guess is that we need to "feed" it A LOT more information than just the previous 100 days of up/downs. Does anyone have example code to add other indicators to the training code. For example add volume, up/down of the DJIA, up/downs of other sectors, etc..

Jonathan

nice tutorials, thanks a lot..

So much thxs!

Began a short batch of updates to this series. Starting with: Pipeline API introduction

Hey Harrison - just wanted to thank you for creating these videos. I started watching them this summer and they really helped me get started on Quantopian. Well done!

  1. Thanks for these tutorials, they are very helpful.

  2. I am on part 6 of the series and everything is making sense except for one part of the code

def initialize(context):  
    context.limit = 10  
    schedule_function(rebalance,  
                     date_rule = date_rules.every_day(),  
                     time_rule = time_rules.market_open())  

def rebalance(context, data):  
    for stock in context.portfolio.positions:  
        if stock not in context.fundamentals and stock in data:  
            order_target_percent(stock, 0)  


# Will be called on every trade event for the securities you specify.  
def before_trading_start(context):  
    context.fundamentals = get_fundamentals(  
        query(  
            fundamentals.valuation_ratios.pb_ratio,  
            fundamentals.valuation_ratios.pe_ratio,  
        )  
        .filter(  
            fundamentals.valuation_ratios.pe_ratio < 14  
        )  
        .filter(  
            fundamentals.valuation_ratios.pb_ratio < 2  
        )  
        .order_by(  
            fundamentals.valuation.market_cap.desc()  
        )  
        .limit(context.limit)  
    )  


    update_universe(context.fundamentals.columns.values)  



def handle_data(context, data):  
    cash = context.portfolio.cash  
    current_positions = context.portfolio.positions  
    for stock in data:  
        current_position = context.portfolio.positions[stock].amount  
        stock_price = data[stock].price  
        plausible_investment = cash / context.limit  
        share_amount = int(plausible_investment / stock_price)  
        try:  
            if stock_price < plausible_investment:  
                if current_position == 0:  
                    if context.fundamentals[stock]['pe_ratio'] < 11:  
                        order(stock, share_amount)  

        except Exception as e:  
            print(str(e))  

I don't understand the logic behind the if statement in the rebalance function.
if stock not in context.fundamentals and stock in data:

I don't understand why the second conditional is necessary. I know the update universe function makes it so that data holds the same stocks as context.fundamentals in addition to those in our portfolio, and we are trying to sell a stock if it is no longer in context.fundamentals because the pe ratio has changed but is in data because it is still in our portfolio. However we already have a for loop going through our portfolio, so why is the second conditional still necessary? I tested the algo with the conditional removed and know it results in errors but I can't figure out why?

Thanks for the help!

Hi Harrison,

  1. Your tutorial series are great!

  2. I've tried running your example code described in "Simple Quantopian Pipeline Strategy" (at the end of https://pythonprogramming.net/quantopian-pipeline-strategy/?completed=/quantopian-pipeline-api-tutorial/). According to your screenshot, the Total-Return is 238.2%. But I got -40.61% with the same conditions as yours. Why?

Besides, I am not sure if the calculated SMAs from the program correct or not. For example, the following are the 50ma, _200ma and ma_ration from 2016-03-15:
... 2016-03-15before_trading_start:23INFOmy_universe: _200ma _50ma ma_ratio
Equity(49567 [MTBC_P]) 13704.910112 61479.761239 4.485966
Equity(25348 [MNRK]) 7.108667 15.783251 2.220283
Equity(49046 [VXUP]) 4.964020 10.954155 2.206711
Equity(46251 [ABIL
W]) 0.499272 0.972146 1.947126
Equity(24073 [EEQ]) 8.770063 17.043083 1.943325
...

But as I check the stocks in www.stockcharts.com, for exampel the EEQ (Enbridge Energy Management, L.L.C.), the SMA50 is 16.88, the SMA200 is 23.77, i.e. the ma_ratio must be < 1, the chart is going down.

Thank you very much, Harrison for these great videos.

Thank you very much for these videos!

May be i think this is most well-known tutorial video in korea ML community site

Our some users are learned by these and we always appreciate you

Because, in korea websites , there is no(or few) tutorial video

thanks to you, we can reduce much time

THANK YOU ! :)

ps. i am korea high school student so i m not good at english, plz understand my english

Seriously? Each ticker has its own machine learning model?