Back to Posts
Listen to Thread

In our help documentation, we provide both a very basic algorithm to use as a sample and a multi-SID example. This is the multi-SID example.

You should press the "Clone Algorithm" button below, and that will clone the code into your "My Algorithms" page. You can then hack it to your heart's content! Try using different stocks, different buy/sell logic, etc.

The comments in this algorithm are fairly detailed, and should be helpful in setting up your own multi-SID algorithm.

This algo also demonstrates how to write logs, import datetime, and use timezone (pytz).

Clone Algorithm
252
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
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.

Hey Dan, I know this sounds like newb question but I'm confused by the first for statement. When we are iterating and setting the price, notional, and tradeday how do the variables keep the scope until they are used in the second for statement? Wouldn't Python set the three variables for Apple but then just replace the values when the stock changes to Alcoa?

Let me know if I missed a major part, I'm still reading the docs.

Xavier,

Thanks for the question. Your reading of the code is correct. The notional limit here is on the total notional value of the portfolio, rather than on the individual positions. So, you are correct that we are over-writing a single notional value.

Position limits would be a much more effective tool - you could easily update this code to use a single loop rather than two, and to keep a dictionary of notional values keyed by the stock.

thanks,
fawce

A new notional line in the second For statement will contain the individual security position size. Or does that muck something up with the logs?
Also, what is the best way to compare the contributive performance of individual securities?

Thanks for any help. Rob

Rob, If I'm understanding you correctly, that new line you want to add shouldn't mess up anything with the logs.

I think there are a few ways to compare the contributive performance. In this example I use record() to plot each position's value from Dan's algorithm above. You could also log.info() each position's value, record each security's price, etc. Does that help?

Clone Algorithm
9
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
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.

Thanks Gus, I did not know how to plot position sizes like that. Is it possible to plot the individual stocks' realized p/l? without pasting pages of back-test transaction data into Excel, that is.

best,

It should be possible. So you have to compute the average profit on winning trades and the average profit on losing trades, then just divide them and use record(). I'm not totally familiar with realized profit/loss, but Quantopian has already taken commission into account on trades so I think profit/loss here is automatically realized profit/loss, but I could be wrong. Anyway, your process order should be something like this:

  1. Detect when a trade begins and store the starting price.
  2. Detect when a trade ends, and calculate either the gain or loss. Maybe have two lists, one with gains and one with losses that you append results of each trade to.
  3. With those lists you can calculate p/l.

With multiple stocks it gets more complicated, but it can still be done with two dimensional lists, etc. I suggest looking into either deques or Quantopian's batch_transform to work with data more easily.

Does that help? Let me know if you have any more questions.

Log in to reply to this thread.
Not a member? Sign up!