Commit Graph

107 Commits

Author SHA1 Message Date
Stewart Douglas 501fd58fdf ENH: Replace update_asset_finder with write_data
The write_data methods invokes the relevant AssetDBWriter subclass
to write data to the database. update_asset_finder is no longer
a relevant method since the AssetFinder is strictly a reader class.
2015-09-10 11:53:24 -04:00
jfkirk 67c56f768b ENH: Adds auto-closing feature and implements for Futures 2015-07-31 10:38:44 -04:00
jfkirk 8d5bfd3c91 BUG: Aligns performance packet generation between minute and daily modes 2015-07-21 13:25:39 -04:00
Eddie Hebert 36319122cc PERF: Change asset finder to be backed by sqlite3.
Attack the startup bottleneck of creating the asset finders caches for a
large universe, which was between 1-2 seconds on development and
production machines.

Instead, allow the AssetFinder to be passed a sqlite3 file that has
already been populated and then hydrate asset objects only when an
equity is referenced for the first time.

To create aforementioned sqlite3, create an AssetFinder with an db_path
and `create_table` set to True. If `create_table` is set to False, the
prepopulated data in the sqlite file found at db_path will be used.

Default behavior is to use an in memory database.

Behavior that changes:

- Fuzzy lookup now only works on one character, that character needs to be
specified at write/metadata consumption time, since the fuzzy lookup key
is created by dropping the character from each symbol.

- Overwriting partially written metadata is no longer
  supported. i.e. some unit tests allowed for inserting just the identifier,
  and then later updating the symbol, end_date, etc.

  Instead of building an upsert behavior at this time, this patch
  changes the unit tests so that the data for each asset is only
  inserted once.

Other notes:

- populate_cache is now removed, since there is no longer a two step
  process of inserting metadata and then realizing that metadata into
  assets. _spawn_asset is rolled into insert_metadata, so that a call to
  insert_metadata both converts the metadata and makes it available in
  the data store.
2015-07-14 09:54:38 -04:00
jfkirk efa6d8dbce ENH: Adds a perf tracker method to handle SIDs leaving the universe 2015-07-09 17:03:21 -04:00
jfkirk 9291a89599 BUG: Prevents payout of dividend on final trading close 2015-06-24 21:45:55 -04:00
jfkirk bfc992363e TST: Fixes broken test_close_position_event 2015-06-12 17:17:10 -04:00
jfkirk a5d1f79a37 TST: Reconciles tests with asset management system 2015-06-11 11:35:49 -04:00
warren-oneill 44fbdff4ac added CLOSE_POSITION as source type, added pt.close_position_event(), added process_close_postion(), added close processing to tradesimulation, added unittest for close_position_event 2015-06-04 14:23:46 +02:00
Eddie Hebert dba0a99a16 PERF: Use specific methods for processing events.
By having both the trade simulation main loop route events to "process"
methods based on event type and the process methods also checking event
type, there was some duplicated effort in doing that comparison many
times.

A particular case where this was noted in profiling was for the
`process_event` function which was checking if the type was not a trade
and returning early, when in a larger universe of stocks the value
returned False 99% of the time.

Instead provide separate process functions specific to each type,
e.g. e.g. `process_trade` and `process_transaction` and route traffic to
those functions in tradesimulation.

For a universe of 160 stocks on both no-op algo and an algo that rebuys
its universe every day, saw about a 10% increase locally.

Also:

- Add process_benchmark to blotter since internal subclass relies on
logic on benchmark, this allows the internal process_trade to be a
`pass`.

- Add warning on unrecoginzed event types.
2015-05-08 12:44:32 -04:00
Eddie Hebert 72ab9e74dd MAINT: Remove unused event_count from tracker.
event_count is not referenced anywhere, so remove extra bit of state
tracking.
2015-05-05 16:31:02 -04:00
Jonathan Kamens e942275108 STY: Flake8
Upgrade the version of the flake8, pep8, and mccabe PyPI packages, and
make the code changes necessary for compatibility with the updated
packages.
2015-03-19 17:21:25 -04:00
Dale Jung 7892a6943f RFT: Remove Position management from PerformancePeriod. This cuts down
on the number of per-tick update that occur since they were duplicated
per each PerformancePeriod. Also opens up the path to cythonizing the
entire object
2015-03-18 22:48:14 -04:00
fawce 6c3e1e1ba4 added tests for performance pack fields 2015-01-07 21:47:13 -05:00
fawce 22cb6dcb40 added leverage and gross leverage to account.
added tests and conditions for account values.
2014-12-18 17:07:19 -05:00
Thomas Wiecki 820115f7be MAINT: Replace iterkv with iteritems.
iterkv is being deprecated as of pandas 0.14.
2014-10-22 17:25:37 +02:00
Brian Fink 50c5b73a7b add account object to context 2014-10-10 17:10:45 -04:00
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 1039919340 STY: In test_perf_tracking, import datetime/timedelta via from-import. 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
Eddie Hebert 2debde31ba BLD/STY: Upgrade to latest versions of lint checkers.
Upgrade pep8 1.4.6 -> 1.5.7
Upgrade pyflakes 0.7.3 -> 0.8.1

Also, tweak some line indentations which now show up as errors,
because of the fixes/changes to visual indent detection between
pep8 versions.
2014-05-30 12:44:10 -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
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
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
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
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 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 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
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
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 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
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 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
John Ricklefs 7b8769b3e7 BUG: Fix div-by-zero error in cost_basis adjustment. 2013-08-27 13:24:06 -04:00
John Ricklefs 191715a148 ENH: Add support for asynchronous commission events. 2013-08-26 14:18:32 -04:00
Thomas Wiecki b89886297f STY: autopep8 codebase. 2013-08-08 16:46:44 -04:00
Jean Bredeche 8f1d599fc6 fixing some bugs with splits (ratios and empty positions) 2013-07-24 15:26:15 -07:00
Jean Bredeche 6fc077a573 ENH: Add support for splits in zipline.
When a split is encountered, open positions and open orders
are updated accordingly.
2013-07-23 16:22:58 -04:00
Jonathan Kamens d833503e50 BUG: Use context in lieu of "use_environment" decorator
The "use_environment" decorator is too side-effectful (e.g.,
connecting to Yahoo! Finance or another data source) to be used as a
decorator to a function that gets evaluated during module load. This
causes problems, e.g., if Zipline is being used in a gevent
environment, when the trading environment created by the decorator
argument tries to use greenlets when gevent hasn't been fully
initialized.

Since the decorator is nothing more than a context-manager wrapper,
this commit removes the decorator and replaces its use with contexts,
i.e., "with" statements.
2013-06-24 17:13:14 -04:00
Eddie Hebert 2b5b670493 MAINT: Change signature of create_transaction.
- Change the expected type for order information from the string
  of the order id to an `Order` object, so that it matches the same
  abstraction level as passing in an event.
- Change the order (not to be confused with the parameter named `order`)
  of the parameters so that they go from left to right in order of
  static -> dynamic, i.e. the parameters most likely to change within
  each invoration are the amount and price, with amount more likely
  to change than price.
2013-06-10 17:10:48 -04:00
Eddie Hebert 7d26168359 MAINT: Reduce number of parameters for create_transaction.
create_transaction accepted both sid and order, which in all cases
was derived from the current event, so remove `sid` and `order`,
replacing them with event

If there is a scenario where sid and order need to be set independently
of each other, then the underlying Transaction object can be called
directly.

Looking towards making writing custom slippage models slightly easier
by removing the redundancy.
2013-06-10 15:36:58 -04:00