Commit Graph

420 Commits

Author SHA1 Message Date
Richard Frank 54ad18e2ff MAINT: Simplified event stream creation for tests 2014-03-11 19:08:22 -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
Eddie Hebert a203f69635 PERF: Remove alias_dt transform in favor of property on SIDData.
Adding a copy of the Event's dt field as datetime via the
`alias_dt` generator, so that the API was forgiving and allowed
both datetime and dt on a SIDData object, was creating noticeable
overhead, even on an noop algorithms.

Instead of incurring the cost of copying the datetime value and
assigning it to the Event object on every event that is passed
through the system, add a property to SIDData which acts as an
alias `datetime` to `dt`.

Eventually support for `data['foo'].datetime` may be removed,
and could be considered deprecated.
2014-03-07 10:55:59 -05:00
twiecki eccaf8d53d ENH: Add symbol api function
A symbol() lookup feature was added to Quantopian.
By adding the same API function to zipline we can
make copy&pasting of a zipline algo to Quantopian
easier.
2014-03-05 17:15:44 -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 e459c2729c MAINT: Refactored unit tests to remove duplication
of arguments to factory.create methods.

Also added checking of the perf period cost basis results after each txn.
2014-03-05 14:48:42 -05:00
Eddie Hebert 6cdd5ddb10 BUG: Fix max drawdown calculation.
The input into max drawdown was incorrect, causing the bad results.
i.e. the `compounded_log_returns` were not values representative of
the algorithms total return at a given time, though
`calculate_max_drawdown` was treating the values as if they were.
Instead, use the `algorithm_period_returns` series, which does provide
the total return.

Update risk answer key with an Excel calculation of max drawdown
to help corroborate the calculations.

Also, remove `compounded_log_returns`, (which actually had stopped
being the `compounded_log_returns` at some point), since the max
drawdown was the only calculation using the values in that series.
2014-02-27 17:16:35 -05:00
Colin Alexander 011ed09dc2 ENH: Extended commission PerShare method to allow a minimum cost per trade.
Also added unit tests (test_perf_tracking.TestCommissionEvents) to test
all commission models.
2014-02-25 14:37:08 -05:00
Eddie Hebert e4d2527eca ENH: Limit handle_data to times with market data.
To prevent cases where custom data types had unaligned timestamps,
only call handle_data when market data passes through.

Custom data that comes before market data will still update
the data bar. But the handling of that data will only be done
when there is actionable market data.
2014-02-10 22:12:38 -05:00
Jamie Kirkpatrick 147242339d BUG: ensure perf stats are generated for all days
When running with minutely emissions the simulator would report to the
user that it simulated 'n - 1' days (where n is the number of days
specified in the simulation params).  Now the correct number of trading
days are reported as being simulated.
2014-01-30 16:04:29 -05:00
Jamie Kirkpatrick 45844bac31 BUG: adjust benchmark events to match market hours
Previously benchmark events were emitted at 0:00 on the day the
benchmark related to: in 'minute' emission mode this meant that
the benchmarks were emitted before any intra-day trades were
processed.

See: https://github.com/quantopian/zipline/issues/241
2014-01-30 16:01:58 -05:00
Moises Trovo 64b28d15f8 ENH: Add Brazilian calendar for BMF&Bovespa stock market
Add Brazilian Bovespa Stock Market calendar
2014-01-16 12:53:11 -05:00
Thomas Wiecki b69590a2f7 ENH: Factor out API methods. Add support for algo scripts.
This is a step towards the goal of uniting Quantopian scripts
and zipline.

To make the syntax of zipline identical to Quantopian
we break out the API methods (like order) and turn them into
functions. To access the algo object we add a thread local reference
to the current algorithm that is accessed in the API functions.

TradingAlgorithm now takes either a string or two functions
(initialize and handle_data) that it executes.

Use api method decorator for methods available in algoscript.

Ported appropriate algorithm tests from internal code.
2014-01-16 12:07:33 -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 c53196b5ea BUG: Make write of binary file compatible with Python 3.
Open the destination file with the byte flag to avoid error in
Python 3 with mismatch of str and byte.
2014-01-07 18:01:25 -05:00
Eddie Hebert 7274748275 STY: Remove unused import.
Remove unused heapq from test perf tracking.

Mea culpa.
2014-01-07 16:42:21 -05:00
Eddie Hebert f093b2fb59 MAINT: Adjust for comparison changes in Python 3.
Use date sorted sources instead, instead of sorting with second
argument of Event, etc. since the `heapq.merge` behavior is using
the second part of the tuple, thus requiring a richer set of comparison
methods, which would only be used in the test context.

Use `date_sorted_sources` instead, so that sorting is done on algo time
and source id.
2014-01-07 11:57:52 -05:00
Eddie Hebert 9326a732a4 MAINT: Make exception handling tests compatible between Python 2 and 3
Python 3 removes the `.message` attribute, so use `str` instead.

Also, the divide by zero message has changed slightly between versions,
so just check for the exception type, instead of also checking the message.
2014-01-07 11:57:49 -05:00
Eddie Hebert d06f35623a MAINT: Make walk usage in example tests compatible with Python 3
The rename of walk is not provided by six, so check the import error
via an exception.

Also, callback behavior slightly changes between the two versions,
so instead iterate over the walked files and call what was formerly
a callback, directly as a function.
2014-01-07 11:57:45 -05:00
Eddie Hebert 36f8b77290 MAINT: Support both Python 2 and 3 next interfaces.
Python 3 uses the `__next__` method instead of `next`,
and uses the syntax of `next(foo)` accordingly.

Add `__next__` and `next` side-by-side so both Python 2 and 3 have
a method that can be used during iteration.
2014-01-07 11:46:57 -05:00
Eddie Hebert 68b78a6914 MAINT: Explicitly convert map to list when converting answer key values.
For compatibility with iterator version of map in Python 3.

Also always use iterator version of map,
so that code path is exercised in Python 2.
2014-01-07 11:43:38 -05:00
Eddie Hebert 98956f19ed MAINT: Make answer key reading compatible with Python 3.
Ensure reading of bytes and checking against byte type, when
parsing the Excel spreadsheet which contains the answers.
2014-01-07 11:42:12 -05:00
Eddie Hebert e458e8c3c5 MAINT: Use explicit relative pathing for Python 3 compatibility.
Python 3 requires submodules to have more explicit pathing, so use
the dot syntax to declare submodules which are in the same directory
as another module.
2014-01-07 11:36:41 -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
Eddie Hebert 54ddd1c109 MAINT: print function clean up in preparation for Python 3
- Use `print()` function for all print calls
- Fix strip and format calls that were on the outside of the
  print function for some reason.
  (Which were breaking in Python 3 because of print returning None.)
- Remove commented out print calls.
2014-01-04 20:55:43 -05:00
David Stephens e5786b2593 ENH: Add calendar and test for Toronto stock exchange.
Note that the calendar test is decorated with @nottest (as per the other calendar test functions).  I've run the test to confirm the calendar works.  The differences between the env (Yahoo Finance of GSPTSE) and the calendar are illustrated in the tradingcalendar_tse file and are confirmed to be errors on Yahoo Finance's part.
2013-12-27 13:27:14 -05:00
Eddie Hebert 6f3a3dab04 TST: Defend against non-trading days as period start in commission test.
A bug in the create_random_simulation_parameters allows the period
start to be a non-trading day.

That bug was causing the commission tests to randomly fail, e.g.
when the period start was on Good Friday, because the commission was
created on hour three of Good Friday, instead of the next Monday.
When it hit that case, the test commission is never processed.

Defend against that bug by using the first open of the simulation
parameters which is more guaranteed to be during market hours,
when creating the test commission.

This is in place of fixing the bug in the random parameters function
or making the parameters non-random, which are other potential fixes.
2013-12-10 13:07:25 -05:00
fawce 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 20e0ad191b TST: Disable test covering London stock exchange.
Changes to trading calendar and environments for supporting market
minutes, etc. have made the non-NYSE stock exchange support lag.

Disabling the test, with the intent of bringing support back up to
parity with NYSE.
2013-11-12 12:12:35 -05:00
Eddie Hebert 4d11ca131e TST: Use Timestamp instead of Delorean for trading calendar test.
Removes another use of Delorean in favor of Timestamp 'today'.
2013-11-11 10:38:04 -05:00
Richard Frank 2492feb938 ENH: Keep track of total commissions as attribute on Order
Value is summed from TRANSACTION and COMMISSION events.
Defaults to None, meaning unset.
2013-11-01 17:23:50 -04:00
Eddie Hebert a192ba01a2 STY: Used named args in hardcoded test objects.
Instead of unrolling a dictionary, use named args.
2013-11-01 15:28:35 -04:00
Jonathan Kamens 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 32c1f93572 TST: Updates tests to fit fixed stop behavior.
Change the answers to the stop order tests to match the corrected
behavior, where results are opposite from previous behavior.
2013-10-28 20:40:32 -04:00
Eddie Hebert 0c71f95016 TST: Parameterize stop order test.
So that we can more clearly demarcate each case of buy/sell and
price compared to stop, and their expected outputs.

Also, add comment about the current behavior versus the behavior
that will be moved to in an upcoming fix.
2013-10-28 15:12:29 -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
Eddie Hebert 37e1e74391 TST: Add logging of sim params for perf tracker tests.
These tests use the random simulation parameters, which is leading
to an intermittent failure.

We may want to consider removing the randomness, but in the meantime
the randomness is exposing a case where the cost basis is not the value
expected, so logging the sim parameter values to help track down what
parameters cause the failure.
2013-10-16 12:07:14 -04:00
John Ricklefs 9ac180d4bb BUG: Ensure compounded_log_returns set on first dt. 2013-10-11 13:06:11 -04:00
Eddie Hebert 1bad245675 ENH: Use annualized returns for beta and alpha.
So that the units match the other risk calculations, also
use annualized returns for beat and alpha.

Update answer key to match values calculated on the first day.

Also, update performance tracker test so that the returns used
are fractional instead of > 1, so that the annualized numbers are
more in line with real world values.
2013-10-11 00:27:03 -04:00
Eddie Hebert dad34d2ddb TST: Add annualized alpha and beta to answer key.
Add a column that uses annualized mean returns as the inputs into
alpha and beta.
2013-10-11 00:27:03 -04:00
Eddie Hebert dcae6af67b ENH: Annualize information ratio.
Use annualized values for information, so that it is calculated
using the same units as sharpe, etc.
2013-10-11 00:27:03 -04:00
Eddie Hebert 0ebdb2fe77 ENH: Annualize sortino ratio.
Use annualized values for sortino, so that it is calculated using the
same units as sharpe, etc.
2013-10-11 00:27:03 -04:00
Eddie Hebert 1afc9069b1 TST: Update answer key with annualized sortino and information.
Include in the Excel answer sheet the sortino and information
metrics using annualized inputs.
2013-10-10 18:37:53 -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
Eddie Hebert 433f97c38f ENH: Improve headline Sharpe risk calculations.
This could perhaps be labelled BUG, as well.

Change the Sharpe (and algorithm volatiilty) value used to compare
algorithms/backtests so that it is annualized and uses daily returns.

Previously, the Sharpe metric was using the same calculation style
as the fixed size periods, i.e. 3 Month, 6 Month, etc., which can
use the geometric mean when comparing against the risk free.

Change the Sharpe calculation to use the arithmetic mean differenc
against the risk free rate, using daily (non-compounded) values.

Also, use annualized mean returns.
2013-10-10 18:37:53 -04:00
Thomas Wiecki 65637b9430 ENH: Add option of instantly filling orders. 2013-10-01 20:30:01 -04:00
Eddie Hebert df7b9c0273 MAINT: Remove date_utils module.
Most of the functions in date_utils can be done via pandas.
The other functions are no longer used for loading, etc. so remove
the date_utils module to reduce the total surface area of Zipline core.
2013-09-30 23:30:07 -04:00
Eddie Hebert b928cbe0d0 TST: Move minute frequency risk test into risk test module. 2013-09-25 15:09:20 -04:00
Eddie Hebert cd3a63415c MAINT: Use pandas for volatility in risk metrics.
Continue on path of converting values stored inside of risk metrics
to use a DataFrame instead of storing multiple lists.

Also, the need for latest_dt in getting the current volatility for
the sharpe calculation, shows that we need to set the lastest_dt at
the beginning of the update loop.
2013-09-25 11:25:57 -04:00