Commit Graph
32 Commits
Author SHA1 Message Date
Delaney Granizo-Mackenzie 0fd78cd54a BUG: Fixed random dips in returns as shown to user.
Previously the last sale price was not correctly being set on
positions when the transaction arrived before the trade event.
The last sale price was defaulted to zero and never updated. This resulted
in one holding stocks that were bough >>0 and now had value 0 from
the perspective of returns. The returns would display correctly again
when the next trade of that security happened. For most securities trading is
frequent enough that there's no issue, but for some illiquid ones it took
hours to fix itself.

Updated test_perf_tracking:TestPerformanceTracker.test_minute_tracker
This test was based on assuming that last_sale_price was zero,
allowing the sharpe ratio to be calculated. The sharpe ratio can no longer
be calculated for this specific tested scenario and the test has been changed
accordingly.
2014-07-29 11:07:13 -04:00
Scott Sanderson d610ea0a3b DOC: Rename 'guid' to 'id' in dividend tracking logic. 2014-07-18 15:04:20 -04:00
Scott Sanderson 4712891e88 ENH: Remove dividends from the event stream.
Removes support for handling dividends as part of the algorithm
simulation stream, replacing it with an API in `TradingAlgorithm` for
supplying dividends as a DataFrame.
2014-07-18 15:04:20 -04:00
Scott Sanderson a8431944aa MAINT: Add comments and rename methods in PerformanceTracker.
The function that handles a market close for daily frequency changed from
`handle_market_close` to `handle_market_close_daily`.

The function that is called at on the closing minute each day when running
minutely changed from `handle_intraday_close` to
`handle_intraday_market_close`.
2014-07-18 15:04:20 -04:00
Scott Sanderson ecd9bff0d6 PERF/BUG: Make the portfolio property call updated_portfolio.
Make the portfolio property on TradingAlgorithm call `updated_portfolio`
internally.  This prevents needless recomputation of the portfolio between
calls to `handle_data`, and also prevents issues where the portfolio object
could be unexpectedly modified in place in the body of a `handle_data` call.

Noteworthy finding in the course of investigating this bug:

If you modify a Python dictionary while iterating over it, the language will
only throw an exception if the size of the dictionary changes between loop
iterations; this means that you can do:
```
x = {1:1, 2:2, 3:3}
for k in x:
    old_val = x[k]
    del x[k]
    x[f(k)] = old_val
    print k
```
and you'll only get an error if f(k) is already a key in the dictionary.
This can lead to bizarre/nondeterministic behavior in the key iterator.
2014-05-27 11:20:13 -04:00
Richard Frank 17df628171 DOC: Replaced "i.e." with "e.g." where appropriate in module 2014-04-24 14:01:05 -04:00
Richard Frank f21bbe58fc ENH: Allow for stock dividends, and in particular, Google's
recent 2 for 1 stock split, where 1 class C share was distributed
for each share of class A held.

Now a dividend can specify a sid and ratio of stock that will be paid
to owners of the original security.  If the ratio is 2.0, then for every
existing share, two shares will be paid.
2014-04-24 14:00:39 -04:00
Eddie Hebert b5dbaf88d1 BUG: Prevent out of sync market closes in performance tracker.
In situations where the performance tracker has been reset or patched
to handle state juggling with warming up live data, the `market_close`
member of the performance tracker could end up out of sync with the
current algo time as determined by the

The symptom was dividends never triggering, because the end of day
checks would not match the current time.

Fix by having the tradesimulation loop be responsible, in minute/minute
mode, for advancing the market close and passing that value to the
performance tracker, instead of having the market close advanced by
the performance tracker as well.
2014-03-30 13:33:45 -04:00
twiecki 4bdecd6402 STY: PEP8 fixes. 2014-03-26 20:46:20 +09:00
Eddie Hebert 7ce971fa17 MAINT: Use more clearly named cumulative risk returns containers.
Change `_period_returns` to `_cumulative_returns`, so that there
is less mental overhead/confusion when reading through the risk
module.
2014-03-20 16:00:34 -04:00
Richard Frank 7a3f73cf1d BUG: Update perf period state when positions are changed by splits
Otherwise, self._position_amounts will be out of sync with position.amount, etc.
That value is used to calculate pnl and returns, so test for them.
2014-03-11 19:08:22 -04:00
Eddie Hebert 4860a966b3 REL: Update copyright year on all files changed since the new year. 2014-03-07 22:31:41 -05:00
Richard Frank 5020c36f8d BUG: Fix cost basis calculation
Cost basis calculation now takes direction of txn into account.

Closing a long position or covering a short shouldn't affect the cost basis.
2014-03-05 14:48:58 -05:00
Richard Frank e7ec629510 MAINT: Cleaned up sid checks and exception types
Removed unnecessary parens

Keeping NameError reserved for when locals or globals are not found.
Exception is what we use for the other sid checks, so now they are consistent.
2014-03-05 14:40:05 -05:00
Eddie Hebert e3096e9afc MAINT: Remove unused rounding method.
round_to_nearest is no longer referred elsewhere.
2014-02-26 21:55:14 -05:00
Eddie Hebert 51e8b3244e ENH: Filter out empty positions from portfolio container.
To help prevent algorithms from operating on positions that are
not in the existing universe of stocks.

Formerly, iterating over positions would return positions for stocks
which had zero shares held. (Where an explicit check in algorithm
code for `pos.amount != 0` could prevent from using a non-existent
position.)
2014-01-10 14:30:29 -05:00
Eddie Hebert b4959e46cf MAINT: Use six for Python 3 compatible names and behavior.
Use the six module to import functions and types that are
consistent between Python 2 and 3, so that one code base can
support both versions.

- Use integer types instead of int and long.
- Use string_types instead of basestring.
- Account for iteritems, itervalues, iterkeys.
- Use six.moves for filter and zip, reduce
- Use compatible bytes for md5 hasher.
- xrange and range
2014-01-07 11:33:50 -05:00
fawceandEddie Hebert 6d46eb71ea PERF: moved performance calculation out of inner loop
lazy loading for portfolio
less repeating in performance period updates
2013-11-19 10:39:57 -05:00
Eddie Hebert 3f89904e33 MAINT: Remove unused calculations of max_leverage, et al.
In the performance period the max_leverage, max_capital_used,
cumulative_capital_used were calculated but not used.

At least one of those calculations, max_leverage, was causing a
divide by zero error.
Instead of papering over that error, the entire calculation was
a bit suspect so removing, with possibility of adding it back in
later with handling the case (or raising appropriate errors) when
the algorithm has little cash on hand.
2013-11-06 14:22:20 -05:00
Jonathan KamensandEddie Hebert 73faf9133e MAINT: Clean up imports of zipline.finance.trading
Use "from zipline.finance import trading" instead of "import
zipline.finance.trading as trading".
2013-10-29 13:50:14 -04:00
Eddie Hebert 37c56b9aa4 MAINT: Use Series throughout for daily returns.
Remove the lists of DailyReturn objects in favor of using pd.Series
to store the return values.

Should make it easier to inspect the values when stepping through,
make the windowing of data to a certain range more facile by using,
and have some performance increases due to removing object creation
and member access.
2013-10-19 23:06:18 -04:00
fawceandEddie Hebert f8ce7d944b ENH: Add downsampling to BatchTransform.
So that with minute data, 2.5 orders of magnitude of data can
be cut, allowing for longer window_lenghts, when the daily
values are what are desired for a signal.
2013-10-11 16:48:08 -04:00
Eddie Hebert bfa94e9c91 ENH: Approximate stats for the first day of minute emission.
Volatility needs mulitple values to calculate the stddev,
so provide a day with zero returns to base the first day against.
2013-10-10 18:37:53 -04:00
Richard Frank 822e21fa84 MAINT: Factored out update_position method
and changed Position default last_sale_date to None
2013-10-10 16:40:14 -04:00
Eddie Hebert 6f9a03aa76 MAINT: Use return scalars in performance instead of object.
Remove another case of creating a DailyReturn object, in favor
of passing the return scalar directly to the risk module.
2013-10-02 15:57:00 -04:00
Eddie Hebert ac6a15f20a MAINT: Use pandas normalize_date instead of datetime.replace
Continue standardizing on using the date normalization provided
by pandas.
2013-10-02 15:52:16 -04:00
Eddie Hebert 31b85239f3 MAINT: Force float in position values in period. 2013-10-01 19:58:06 -04:00
Eddie Hebert 75360610a9 MAINT: Use pd.Series to keep track of positions values in a period.
Instead of using a raw np.array and keeping track of an index into
that array, use a pd.Series to track the last_sale_price and amounts
in a vector format.
2013-10-01 18:43:01 -04:00
Eddie Hebert df9575982a MAINT: Remove extra Series creation in performance to risk.
Instead of creating a new Series object each time values are
passed from performance to risk, pass the scalar values directly.
2013-10-01 17:06:59 -04:00
Eddie Hebert 20113872ee MAINT: Use a Series for returns instead of list.
Make the granularity and range of the returns more explicit.
2013-10-01 16:48:26 -04:00
Eddie Hebert 052e9b6b95 MAINT: Remove extra assignment of emission rate.
Remove a doubled line in performance tracker.
2013-09-27 15:15:21 -04:00
Eddie Hebert 9dd52be73b MAINT: Split performance module into submodules.
So that when searching code for `returns` and `update`, it is
easier to discern which performance class is affected.

Should be no functional changes.
2013-09-26 13:38:27 -04:00