Back to Community
Sentdex's Quantopian Tutorials Updated (by me) for Quantopian 2: Algorithm 2 Videos 4-7

This is the second in my series updating Sentdex's Tutorials for Quantopian 2. It covers parts 4-7 of Sentdex's tutorial

(Here's the first.)

This second algorithm is a simple screen for 'value' using fundamental data and again highlights some differences between appropriate structure of algorithms in Quantopian 1 to Quantopian 2.

  1. Instead of using stock in data to ensure that a stock is
    tradeable, you should use data.can_trade(stock)
  2. The fundamental data fetching should be done through the pipeline
    API, not through the query language. There is suppose to be a
    tutorial coming out on the pipeline API shortly. I'm still a little
    uncertain on whether or not the fundamental query syntax is fully
    deprecated or if it still has use cases. If you know please speak
    up!
  3. Lastly, for any daily / weekly / monthly strategy, the trades should
    be executed through some sort of scheduled rebalance function and
    not through a handle_data call. You will notice an extreme difference in backtesting speed between the two.

I also put my own order entry syntax that I think is much cleaner than Sentdex's. I hope you agree.

Finally, I'm 95% certain that the stop-order that he uses in his tutorial is not a stop loss order. What his order is doing is "wait until the stock pulls back another 0.5% to enter the position." If you wanted a true stop-loss order you would have to enter one order to buy and a second order to sell if the trade went against you X%. And since open orders are canceled at the end of each day in backtesting, this would have to be re-entered every day. I have not included stop-loss logic in this one, let me know if you really want to see it and I'll add it in.

Clone Algorithm
74
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 575194455060050f89acff71
There was a runtime error.
6 responses

Thanks for these posts, Michael! The explanations are incredibly useful for any new users following along with Sentdex's tutorials. Looking forward to the next post.

@RyanBranch Here's the next post. Glad to hear your that they are useful to you!

Hi Michael, it IS a very useful work you're doing here, thanks for that. I'm new to Python and to Quantopian, so please bear with me for a while.
In the following piece in the end of the code:

for stock in context.portfolio.positions:  
        if data.can_trade(stock) & (stock not in context.output.index):  
            order_target(stock,0)  

What does this part mean (stock not in context.output.index)

I removed the selling part from your code to replicate the exact results from Sentdex Lesson 4-5, but it's not the same. In his case returns were 3.2% and after running this code I get -4% . I wonder why is this difference?

Here is the backtest results from running your code with the same dates as from the Video Lesson - 5

Clone Algorithm
17
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 579c4e11c6785912d49ab5bf
There was a runtime error.

This one gives almost the same result as in the video 5. Thanks again

Clone Algorithm
17
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 579f4afb54396310024f12b2
There was a runtime error.

Thanks man!