Commit Graph

2107 Commits

Author SHA1 Message Date
Eddie Hebert acd0aeacf0 PERF: Reduce number of times the downside mask is created.
Assign the downside mask, `rets < mar` to a value instead of
calculating the downsides twice.
2014-04-16 16:25:02 -04:00
Eddie Hebert 1406f8e9ba PERF: Remove the drop of 'null return' from cumulative returns.
The check of existence of the null return key, and the drop of said
return on every single bar was adding unneeded CPU time when an
algorithm was run with minute emissions.

Instead, add the 0.0 return with an index of the trading day before
the start date.

The removal of the `null return` was mainly in place so that the
period calculation was not crashing on a non-date index value;
with the index as a date, the period return can also approximate
volatility (even though the that volatility has high noise-to-signal
strength because it uses only two values as an input.)
2014-04-16 15:48:13 -04:00
Eddie Hebert ea7d988721 DOC: Update link to latest risk answer key. 2014-04-15 16:17:39 -04:00
Eddie Hebert 101baad0f8 BUG: Fix change of type of period.sharpe
The factoring out of the Sharpe calculation changed behavior
so that both period and cumulative return nans when there is
no volatility; however before that change period returned 0.0.

This breaks existing consumers which expected a non-nan value
for period results.

Smooth out that change by checking the value after the sharpe
has been calculated and reset nan's to 0.0
2014-04-14 18:45:33 -04:00
Eddie Hebert 7cc24cec1f BUG: Fix numerous cumulative and period risk calculations.
The calculations that are expected to change are:
- cumulative.beta
- cumulative.alpha
- cumulative.information
- cumulative.sharpe
- period.sortino

* Explanation of how risk calculations are changing

** Risk Fixes for Both Period and Cumulative

*** Downside Risk

   Use sample instead of population for standard deviation.

   Add a rounding factor, so that if the two values are close for a given
   dt, that they do not count as a downside value, which would throw off
   the denominator of the standard deviation of the downside diffs.

*** Standard Deviation Type

    Across the board the standard deviation has been standardized to using
    a 'sample' calculation, whereas before cumulative risk was monstly using
    'population'. Using `ddof=1` with `np.std` calculates as if the values
    are a sample.

** Cumulative Risk Fixes

*** Beta

   Use the daily algorithm returns and benchmarks instead of annualized
   mean returns.

*** Volatility

   Use sample instead of population with standard deviation.

   The volatility is an input to other calculations so this change affects
   Sharpe and Information ratio calculations.

*** Information Ratio

   The benchmark returns input is changed from annualized benchmark returns
   to the annualized mean returns.

*** Alpha

   The benchmark returns input is changed from annualized benchmark returns
   to the annualized mean returns.

** Period Risk Fixes

*** Sortino

    Use the downside risk of the daily return vs. the mean algorithm returns
    for the minimum acceptable return instead of the treasury return.

    The above required adding the calculation of the mean algorithm returns
    for period risk.

    Also, use algorithm_period_returns and tresaury_period_return as the
    cumulative Sortino does, instead of using algorithm returns for both
    inputs into the Sortino calculation.

* Other Supporting Changes

** answer_key

   Add new mappings for downside risk and Sortino as well as
   re-address the index mappings because of changes to the answer key
   spread sheet.

** test_risk_cumulative

   Change the decimal precision to expect higher precision.
   The calculations are now more aligned with the answer key, so we can
   expect higher precision. In particular now that the standard deviation
   type matches everywhere in both the Python implementation and the answer
   sheet, the precision of the first value no longer has to be glossed over.

** test_events_through_risk

  Change the results which are used as a canary for risk changes,
  since we do expect Sharpe to change with this change..
2014-04-14 16:44:28 -04:00
Eddie Hebert 12f6b95982 TST: Standardize order of test assert params.
So that the np.assert_almost_equals messages have the correct
ACTUAL and DESIRED values in all messages.
2014-04-14 16:00:38 -04:00
Eddie Hebert 76e4334d64 TST: Change cumulative risk test to use some style as rest of suite.
Change the algorithm volatility test to use the same iterkv style
as the rest of the suite, as it was useful to be able to zero in
on the offending date when debugging changes to the risk module.
2014-04-14 15:57:21 -04:00
twiecki 63e3aa4c1e BLD: Remove duplicate six entry. 2014-04-14 09:04:02 -04:00
twiecki bbd533ac27 DOC: Add @sdrdis to contributors. 2014-04-14 08:05:31 -04:00
twiecki b0220ab225 BLD: Add six as dependency. 2014-04-14 08:03:48 -04:00
Thomas Wiecki 89ba2a6979 BLD: Updated conda files. Adds support for windows builds. 2014-04-14 08:01:58 -04:00
twiecki 5dbedfdce8 Merge branch 'history_port' 2014-04-10 16:05:46 -04:00
twiecki e261438d01 ENH: Adapt history() to work on zipline. 2014-04-10 15:59:26 -04:00
Eddie Hebert c9b1a3f1c7 WIP: Initial port of history. 2014-04-10 15:58:47 -04:00
twiecki 7517032e8d STY: More pep8 fixes. 2014-04-10 13:01:44 -04:00
twiecki 5cb2919b10 STY: pep8 fixes. 2014-04-10 10:57:12 -04:00
Eddie Hebert ec136c265e BLD: Fix import of answer key for compatibility with Python 3.
Python 3 requires explicit relative pathing.
2014-04-10 09:57:00 -04:00
twiecki ecd80b88ba ENH: Add OHLC to simulated data source 2014-04-10 08:55:29 -04:00
Eddie Hebert c77e840833 BLD: Peg CI versions of lint checker libraries.
Match the revisions found in the dev requirements file, so that the
versions used during development and continuous integration.

Pegging the versions in two separate places may end being brittle,
but suppress the build failure because the unpegged installation
of flake8 pulled in newer versions of pyflakes and pep8.
2014-04-10 04:36:23 -04:00
Eddie Hebert 618d554da1 TST: Use benchmark returns from spreadsheet.
The risk unit tests were using the public Yahoo! data instead
of the returns from the answer key spreadsheet, change the RiskPeriod's
created in tests to use the values in the benchmark returns
column of the answer key.

Also, change the spreadsheet's benchmark volatility calculation
to use sample.
The use of population was exposed when the input values were
corrected.
2014-04-09 23:55:31 -04:00
Eddie Hebert 4bf8ab0f8d ENH: Remove dependency on benchmark for trading day calendar.
Instead of the benchmarks' index, use the trading calendar to
populate the environment's trading days.

Remove `extra_date` field, since unlike the benchmarks list,
the trading calendar can generate future dates, so dates for
current day trading do not need to be appended.

Motivations:
- The source for the open and close/early close calendar and the
  trading day calendar is now the same, which should help prevent
  potential issues due to misalignment.
- Allows configurations where the benchmark is provided as a
  generator based data source to need to supply a second benchmark
  list just to populate dates.
2014-04-01 21:14: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 c09d501fbf BUG: Random source did not generate last days' event 2014-03-29 23:53:11 +09:00
Eddie Hebert ad08164582 MAINT: Remove unused setup from trading calendar test.
The values in the setup are unused in the test suite, so remove.

Also remove imports.
2014-03-27 23:05:52 -04:00
Eddie Hebert 352c8a6a8a MAINT: Change variable name in test transforms comprehension.
Using `n` conflicts with using `n` in an interactive debugger, like
pdb. Use `name` instead.
2014-03-27 22:38:57 -04:00
Eddie Hebert 95b379d567 BUG: Fix misalignment of downside series calc when using exact dates.
An oddity that was exposed while working on making the return series
passed to the risk module more exact, the series comparison between
the returns and mean returns was unbalanced, because the mean returns
were not masked down to the downside data points; however, in most,
if not all cases this was papered over by the call to `.valid()`
2014-03-27 20:51:48 -04:00
Jeremiah Lowin 25659f9672 MAINT: Use current_data instead of last_sale_price
It seems more clear to get price values from
`self.trading_client.current_data[sid].price` than
from `self.portfolio.positions[sid].last_sale_price`.

The two values are the same, so this is just a readability change,
but it is also the same behavior as in `self.order_value()` and it's
good to have them all be the same.
2014-03-27 15:25:14 -04:00
Eddie Hebert 64b2a7377c TST: Disable talib default params test.
The data zipline_transform.window is always evaluating to empty,
thus the actual checks are not being used because of the
`if not data` done before running the `asserts`.

This behavior should be fixed, and we should either remove the
`not data` check, or bubble up that the check is being hit too
many times; but in the meantime, disabling this test which takes
a non-trivial amount of time to run.

When run as an algorithm, outside of unit tests the talib wrapper
does work, so the cause of the window always being empty may be to
due to the machinery of the unit test.
2014-03-26 15:47:09 -04:00
twiecki 4bdecd6402 STY: PEP8 fixes. 2014-03-26 20:46:20 +09:00
Suminda Dharmasena 56c5c7e45a Add symbol to __all__
Currently symbol is not in __all__. So added it.
2014-03-26 17:52:35 +09:00
Eddie Hebert 51750a3a35 MAINT: Factor out downside risk calculation.
Move the downside risk calculation into the main risk module;
so that the same calculation can eventually be used by both
the period and cumulative calculations, to prevent implementation
drift.
2014-03-25 13:23:08 -04:00
Jeremiah Lowin d00edbcf04 BUG: check that self.logger exists
`self.logger` is initialized as `None` and there is no guarantee that
users have set it, so check that it exists before trying to pass
messages to it.
2014-03-24 22:06:03 -04:00
Eddie Hebert 51c8a310be MAINT: Remove unused members of TradingEnvironment.
The following members are no longer referenced elsewhere:
- `full_trading_day`
- `early_close_trading_day`
2014-03-24 21:56:17 -04:00
Richard Frank a6184b87e3 MAINT: Set initial values so to_dict can be called immediately
Previously, it would raise an exception.
2014-03-24 15:44:46 -04:00
Richard Frank bb50d996ed MAINT: Using dictionary literal syntax and combining lines
for readability
2014-03-24 15:44:46 -04:00
twiecki 3eb810ad97 ENH: Add simulated random trade source.
This adds a new data source that emits events
with certain user-specified frequency (minute
or daily).

This allows users to backtest and debug an
algorithm in minute mode to provide a cleaner
path towards Quantopian.
2014-03-22 21:22:22 -04:00
Eddie Hebert 803b58c8aa MAINT: More precise extraction of returns from returns container.
Use slice to date, `[:dt]` instead of `pd.Series.valid` to extract
from returns containers.

Using `valid` lead to some confusion when debugging tests, because
it papers over missing data.

The use of `.valid` was based on the assumption that all values
from the zeroth date to the current algo date are populated,
with no trailing values.
`[:dt]` extracts the same data, but in a hopefully more precise
and explicit fashion.
2014-03-21 17:22:10 -04: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 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 778da20468 BUG: Fix floating point error in order(). 2014-03-06 12:32:19 -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
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
twiecki 63f1c791da DOC: Changed conda install channel. 2014-03-05 13:13:07 -05:00
Richard Frank 76b11e8b84 MAINT: Removed unnecessary floating point checks in slippage
Cleaned up some other expressions in slippage as well

Fixes #277
2014-03-04 18:36:29 -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