Back to Posts
Listen to Thread

Hi guys,

I finally got around to playing with quantopian tonight, and I have a few questions (apologies, I am sure I could figure out the answers with some time but then they wouldn't be answered in public!):

  • can we access our strategy's rolling risk/return profile from within? This would be helpful in implementing the ever-necessary variations of Kelly criterion position sizing.
  • how are the minute bars fed into the handle_data function, every minute or every minute that has data? If the latter, what about when observing multiple stocks? Do we detect the blankness with NaN, or 0 volume, or something else?
  • has any one already explored building equal-volume frames from the one minute data?

More as I get to them!

Simon.

Reiterating the point #2, when the mavg refers to trading days, does that refer to "data units that have hit handle_data" or actual days?

Hi Simon, thanks for the questions, we love having them.

  1. The position objects in the portfolio have the data you need (cost basis, last trade), but we actually only calculate the risk and performance measures at the end of each simulated trading day.
  2. In a single stock simulation, we pass only bars with data. In multi-stock simulation we pass bars with data from any stock. Stocks without new data in the bar have the prior bar reiterated. Each element in the data object passed to handle_data has a datetime property, which you can check to know if the bar for that stock is new. In a batch_transform, we provide a trailing window of bars cross-sectionally for all stocks in the simulation. In case of an empty bar we start by placing a NaN, then we fill forward, and then drop rows that still have NaNs in any column. Feedback is mixed on the filling forward/dropping of NaNs, so we'd love to know what you'd like to see.
  3. Not publicly on the forums, so you could be first!

thanks and keep the questions coming,
fawce

mavg and the other rolling transforms operate on trading days, but the window slides per bar. So if you are using minute bar data, with 3 days trailing, when the simulation reaches Wednesday at 3pm, the first bar in the trailing window for the calculation will be from Friday at 3pm. Trading day calculations account for market holidays and closures.

Hello Fawce (and welcome, Simon),

I don't understand your description "Stocks without new data in the bar have the prior bar reiterated. Each element in the data object passed to handle_data has a datetime property, which you can check to know if the bar for that stock is new." My understanding from previous experience and guidance is that in the multi-sid case, we need to test for trade information with:

if stock in data:  

In the attached backtest, if I comment out the line "if stock in data" I get this error:

At Jan 03, 2012, your algorithm referenced price for 41290 (FLN). There was no trade information for this security in this bar.
To avoid this exception, use if sid(41290) in data: ....

The problematic line:

price = data[stock].price

Clone Algorithm
5
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 Grant, you point out another very important kind of missing data - securities that didn't exist (pre-IPO) until after the beginning of your backtest's date range, or those that ceased to exist (bankruptcy, acquisition) before the end of the date range.

We have special UI-level handling for these two cases, because with 10+ years of data, they are quite common. The UI will automatically adjust the start/end of the backtest to reflect the available trading history for each of the securities you specify in your code. To demonstrate, I set the UI to run over the full range of history for your clone above and recorded the UI aids that appear: http://screencast.com/t/WyIEZliR

As you can see in the video, we highlight the adjusted date ranges in blue and then if you click the link in the date picker dropdown, you can turn off the auto adjusting.

Even after we trim the backtest date range down to dates where the security existed, the securities may not trade in every minute (or even day) bar. The behavior I described above is for these missing bars.

Thanks for pointing out this case!

thanks,
fawce

The batch_transform takes as parameters trading days again, so it seems to me that any analysis on a sub-day level has to be done by hand in the handle_data function. Is that a fair assessment? (It's not a big deal, I just wanted to make sure I am not missing anything obvious with these utility methods -- they seem geared towards end-of-day systems, but keeping in mind intraday actionability and ex ante executions)

Hello Simon,

Have a look at the attached backtest (see the log output...you'll need to re-run the backtest with minute data). It turns out that the batch transform will accept a refresh period of zero, which causes it to update every minute. However, the batch transform is not efficient in extracting the data, so it will bog down the backtest if called every minute. But, I think it could be used periodically during the trading day. Reportedly, there is an effort to speed it up.

There's also something funky about the trailing window...it grows during the trading day with a refresh period of zero...not a big deal, but you should just be aware that the length changes.

Grant

Clone Algorithm
5
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. Making progress!

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