Back-testing our strategy - Programming for Finance with Python - part 5



In this tutorial, we cover actually executing our strategy against historical prices and historical fundamental data in a backtest. Here, we see how we did, compared to a benchmark, like the S&P 500 ETF, $SPY. We use Quantopian to back test our strategy, since Quantopian automatically comes with a plethora of statistics and options for back-testing trading strategies. sample code: http://pythonprogramming.net http://hkinsley.com https://twitter.com/sentdex http://sentdex.com http://seaofbtc.com

Comments

  1. They use 255 as a limit because that is the highest number you can store if 8 bits of memory are allocated for storing a particular number in the universe of shares. It is the same issue that Pacman had with the number of levels you could play :)
  2. Hi, for all of you requesting Quantopian 2 updates. Someone active at Quantopian rewrote Harrison's code to make it Quantopian 2 compatible..https://www.quantopian.com/posts/sentdexs-quantopian-tutorials-updated-by-me-for-quantopian-2-algorithm-2-videos-4-7
  3. gives syntax error for the def handle_data, where could be the problem?
  4. I'm just curious: have you monetized your programming skills in the financial market yet? Are you actually trading with self-developed algorithm?
  5. And now... with QUANTOPIAN 2, instead of "universe", what can we do ??
  6. Quantopian 2 no longer has a limit of # of assets and also doesn't allow iteration over data. Can you provide an example of how we would achieve the same results in this video under the new api? Thanks just getting started.
  7. I don't know if you still read these comments, but i'll try anyways. First of all, thanks so much for the vids, they are super helpful. As you may know, quantopian has updated a few of their python commands and how they are calling variables and this is leading to some problems for me. One of the things is that they switched out context.fundamentals they have switched to fundamentals_df, or at least this is what i'm getting from it. This being like this, when I try to
    update_universe(context.fundamentals.columns.values) or update_universe(fundamentals_df.columns.values)
    it gives me an error. Can you explain this?

    I uploaded a pic on imgur if you're having trouble with my horrible explanation.
    http://imgur.com/JzCV7xU

    Thanks!
  8. I did everything exact as in the vid and it came back saying: "There was a problem starting your backtest. Please try again."

    Not sure what the deal is.
  9. Under the "try:" section, if I wanted to add multiple fundamentals, what's the code?  Do I do:

    if context.fundamentals[stock]['roa'] > 0.08 and context.fundamentals[stock]['roe'] > 0.15 and context.fundamentals[stock]['net_margin'] > 0.15:

    or do I do...

    if context.fundamentals[stock]['roa'] > 0.08 and ['roe'] > 0.15 and ['net_margin'] > 0.15:

    ...or what?
  10. Great Job. I like your Data analysis and finance videos.

    I have an error that I can find on the line of context.fundamentals[stock].['pe_ratio'] < 11:

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

    What can it be?
  11. as always great job, thank you for accepting me on LinkedIn, you said you read a lot.. can you recommend a book about stock investment.
  12. I will eventually check this out, but from python perspective what is try and except doing? Also the context.limit is the max number of stocks that are part of our universe of stocks? Thanks
  13. I got an error that says:
    'update_universe' only permitted within before_trading_start function
    though I copied exactly (it seems) the same code in this video.
    I think the problem is in the position of update_universe within or outside a loop,.
  14. You keep crushing it in all your videos.
  15. Hi Harrison, thank you for such great tutorials. I do a lot of learning off youtube videos for various other topics, and I've got to say that your videos are hands down the most helpful and engaging. I will be sure to make a donation. I have one minor question about one line of code. Would it be ok to use the following code for current_position, or would something different happen under the hood? 

        current_positions = context.portfolio.positions
        
        for stock in data:
            current_position = current_positions[stock].amount

    Once again, thank you for your great teaching ability.
  16. Love these videos!  As a newbie, I found I had two pressing questions when watching, that  I think other newbies might have.   First, could you further explain why plausible_investment is equal to cash divided by ten? That hardcoded ten is driving me nuts. Next, at the 5m20s mark you give us the first mention of update_universe().  Thanks to your explanation, I can now imagine that companies are going to be drifting into and out of our top 10 based upon the fundamental metrics filter we created.  So, my question is... when the universe is updated, is a company that drifted out of the top 10 still available to trade in the handle_data method?  Thanks.
  17. how do you know which attributes are available to use for an object?
  18. thank you +sentdex
  19. As others have said, these tutorials are great! Thank you for putting in the effort, much appreciated. Is there anything you'd recommend to get up to speed with the purely financial side? So far been hitting investopedia for definitions (sharpe ratio, pe ratio etc.) but it would be good to have an understanding of the basics prior to writing code. Thanks again!
  20. 8:   context.fundamentals = get_fundamentals(
    9:        query(
    10:            fundamentals.valuation_ratios.pb_ratio.
    11:            fundamentals.valuation_ratios.pe_ratio.
    12:        )
    13:        .filter(
    14:            fundamentals.valuation_ratios.pe_ratio < 14
    15:        )

    I have SyntaxError: invalid syntax at line 12.
    I dont see any differences on code from video
    Wheres error?


Additional Information:

Visibility: 16217

Duration: 12m 16s

Rating: 86