discuss the sample algorithm

I encourage you to press the "Clone Algorithm" button below. That will copy the code into your "My Algorithms" page. You can then hack, edit, tweak, modify, and change it as you see fit. A couple easy changes you can try:

  • Change the sid to a different stock
  • Change the vwap to be a different number of days

You can see how the backtest results change with even tiny code updates.

This very basic algorithm comes from our help documentation. In the backtest object below, click the "Source Code" tab to see the code. Read the comments in that code carefully, and you'll get a good idea how to write an algorithm in Quantopian.

Also, you might find the Video Tutorial helpful. It walks you through the same programming concepts. Watch it on YouTube.

P.S. This algorithm was last updated on 11/13/12.

Clone Algorithm
7145
Loading...
Backtest from to with initial capital ( data)
Custom data:
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
23 responses

uvxy

svxy

thanks

I have a small question, if you're up 45% with this, why don't you use this to trade in the real world?

Hi Paul,

try cloning the algorithm and play with the following parameters:

  • use a realistic budget
  • use a realistic commission per trade or share
  • trade a different stock
  • trade for a longer timeframe, especially in a time when the APPL stocks didn't skyrocket :)

You will likely notice, that the algorithm will barely outperform the benchmark.

-Chris

Similar strategy with different parameters for trading SPY(SPDR S&P 500 ETF TRUST).

Clone Algorithm
453
Loading...
Backtest from to with initial capital ( data)
Custom data:
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

if you're up 45% with this, why don't you use this to trade in the
real world?

This algorithm suffers from bias - in retrospect, Apple was a great pick. But there's no way for me to know that will be true going forward.

I will say that the commission is realistic, and it works in different budgets, too. But the "apple-went-up-tons" bias make this a terrible idea as an actual production algorithm.

Hi Dan,

Momentum trading is a good idea. It's one of the great huristics. By using S&P 500 ETF(same with the benchmark) and a 11-year backtest, this heuristic is confirmed again.

Di Chen

My sense is that there is a strong bias in a lot of the algorithms cooked up on Quantopian, with backtests that include the 2008-2009 collapse. My own experience is that it is relatively easy to devise an algorithm that avoids the downturn...the problem is that the algorithm either tracks the market or under-performs it under normal conditions. Di Chen's algorithm gains mostly by avoiding the 2008-2009 market drop, but then appears to track the market...so, it is not obvious that it would be a good choice to use for future trading. --Grant

Hi guys, I'm new to this platform and have been playing around with some simple scripts. Is there a way to apply some sort of a tax rate to the dividends received in the portfolio, since dividends are taxed differently from trading or capital gains?

Dominic,

Dividends aren't directly accessible in back-testing (more information here), however they are accounted for in overall returns (except special dividends)

This version has better performance. What I like is 12.3% max drawdown. But it can't beat the benchmark during 2009 - 2013.

Clone Algorithm
453
Loading...
Backtest from to with initial capital ( data)
Custom data:
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

I don't believe the strategy is behaving as you'd expect, as illustrated by its NAV and the Notional it is trading.

Also note that the min notional was changed to -100,000 vs. -1,000,000.

Clone Algorithm
83
Loading...
Backtest from to with initial capital ( data)
Custom data:
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Going short is much riskier than going long. So I changed the min notional.

hi all, just a thought, anyone thought of making the number of stocks which are short and long variable and can be calculated by a tracker? say if the tracker spots the trend moving up, it will put more stocks in long rather than short

Ivan, do you mind putting this on a new thread by itself?

I am new to this platform, and am trying to understand code. Why do these samples set context.min to a negative number ?

context.min = -100000

If I understand the code correctly

notional = context.portfolio.positions[context.spy].amount * price

both amount and price cant be negative which implies notional is never negative.

I would expect context.min = 0 in these examples.

Hey Sushanth,
Actually, amount can be negative since we can sell shares by shorting them. So in this case, we are effectively borrowing a security to sell with the expectation that the price will fall. It's sort of a weird concept at first.

Gus

Sushanth,
think like if you are very confident that the stock price of a stock will drop soon. So you'll borrow the stock @ Px= 100.00 USD and you pay a set interest on the borrowing (for simplicity let's assume the borrowing rate is irrelevant). As you borrow the position you immediately sell the same position @ Px=100.00 USD in the market.
Let's assume you were right and the price of the stock do drop let's say @ Px = 50.00 USD.
Then, you buy back the stock at Px=50.00 USD and cover your short position (give back your borrowed position). Effectively you have earned 50.00 USD, assuming interest on your borrowing is irrelevant. On the flip side, stock Px can go up, in which case I'll let you figure out the impact :)

Sometimes have to wonder if the buying "on margin" is on purpose, the first algo spends 70 thousand over budget allocated and the last one goes in the hole nearly half a million within two years.
An example of a few lines you could add to your own code to watch out for that if it isn't your intention.

def initialize(context):  
    [ your code ]

    # make at least higher than gui setting  
    context.cashlow = 9999999999999

def handle_data(context, data):  
    [ your code ]

    cash = context.portfolio.cash  
    if cash < context.cashlow:  
        context.cashlow = cash  
        print cash  
        record(cashlow=cash)

2002-01-03 PRINT 1000000.0  
2002-11-26 PRINT 930191.999916  
2002-11-29 PRINT 836181.999884  
2002-12-02 PRINT 742261.999878  
2002-12-03 PRINT 649681.999869  
2002-12-04 PRINT 557286.999866  
2003-05-16 PRINT 465368.339466  
2003-05-19 PRINT 187748.339386  
2003-05-29 PRINT -3051.66063417  
2003-06-06 PRINT -102302.660638  
2003-06-09 PRINT -200444.660646  
2003-06-30 PRINT -298164.660658  
2003-08-05 PRINT -487601.660678

Wondering why there is such a variability in returns solely based off little changes in starting capital. I ran an elementary code (just equity buy orders) with a million dollars and it produced returns not even remotely close to the thousand dollar starting capital I had in another run. Is it the commission thats making the difference?

"with a million dollars and it produced returns not even remotely close to the thousand dollar starting capital I had in another run"

Here is the reason for that. So I use a run summary quite a bit.

Hello everyone. I am new to this and I try this algorithm. After I saw this algorithm's comments, I came across a problem. Suppose the algorithm's cash limit is 1000000 USD and in the algorithm's code we define context.max_notional = 1000000.1.
the code is:

      if price < vwap * 0.995 and notional > context.min_notional:  
              order(context.aapl,-100)  
          elif price > vwap * 1.005 and notional < context.max_notional:  
              order(context.aapl,+100)

Suppose I have cost my cash to buy shares and in the account only remain 1000 USD. If the code "order(context.aapl,+100) " work, and in order to buy 100 shares of aapl , I have to spend 2000 USD. The account cash's limit is 1000000 USD, after the "order(context.aapl,+100)" work, I spent 1001000 USD actually. I think this is a problem. Maybe I am wrong. Can you help me?