Commit Graph

255 Commits

Author SHA1 Message Date
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
Delaney Granizo-Mackenzie 3521a11ed4 ENH: Added informative message for calling order in init.
Previously, calling order() in initalize resulted in a weird
stack trace. It now returns a well formulated error that is
readable to the user through the API. Adding a slippage
kwarg to test_algorithm and simfactor was necessary because
slippage can only be called during init. Previously initaliazed
was never set to true and calls to init-only function were sprinkled
around the code in non-init sections. Code changes were to enforce
init-only rules.
2014-07-08 14:03:53 -04:00
Thomas Wiecki 10885e1b77 MAINT: One way to set sim_params and data_frequency.
There were sevaral places you could supply sim_params
in TradingAlgorithm (__init__, run). This got confusing
as its not clear who updated what and which one was the
correct one to use at each time.

Then there were to ways to define data_frequency, one in
__init__() and one in the sim_params which also added code
complexity.

This refactor makes it explicit that sim_params are to be
passed to __init__() only. Moreover, data_frequency is
only stored in sim_params. For backwards compatibility,
it can still be supplied separately but will link to
the one in sim_params.

For example, you could create new sim params via:

sim_params = create_simulation_parameters(data_frequency='minute')
algo = MyAlgo(sim_params)
algo.run(data)

In addition, perf_tracker only gets initialized in one place:
_create_generator() which should also make the various ways
of running an algorithm more deterministic.

This also fixes a bug with SimulationParameters where
you could not change the period_start. Unfortunately, the
current implementation still requieres an implicit call to
update the internal variables.
2014-06-30 17:28:02 +02:00
Thomas Wiecki 96bdb22db9 BUG: RollingPanel was not behaving correctly in corner cases.
There quite some bugs in certain corner cases. Dropping of obsolete
axes was not working correctly, roll over could cause obsolete axes
to not drop. The tests are much more stringent now as well.
2014-06-14 21:07:02 +02:00
Scott Sanderson 6e92b40ed9 MAINT/TEST: Move to_utc from history_cases.py to test_utils.py. 2014-06-09 17:40:06 -04:00
Thomas Wiecki 2a73873097 BUG: Remove output arg before calling run_pipeline
The IPython magic still created an output file because
the output argument was only removed after the pipeline
was run. This fix simply removes the argument before
the call to run_pipline() when running the IPython magic.
2014-06-09 17:17:37 +02:00
Scott Sanderson bad4c9a439 ENH: Prep work for supporting '1m' history.
Overhauls `HistoryContainer` in prep for support of more than one frequency.

Major changes:

   - Methods/variables referring to "day" have been renamed/generalized.
     - `current_day_panel` became `buffer_panel`, which is now a `RollingPanel`
     - `prior_day_panel` became a dictionary mapping `Frequency` objects to
       "digest panels", which are instances of `RollingPanel`.

   - Hard-coded daily rollover replaced with a notion of a "current window" for
     each unique frequency managed by the panel.

     - When the end of the current window is reached for a given frequency, we
       compute an aggregate bar (code refers to this as a "digest"), which is
       appended to a panel associated with that frequency.

     - Window rollover dates are managed by a pair of dictionaries,
       `cur_window_starts` and `cur_window_closes`.  The `Frequency` class is
       responsible for computing window bounds based on the open/close of the
       previous window.

   - Semantic change to the `open_price` field: `open_price` now always
     contains the price of the first trade occurring in the given window.
     Previously it contained the price of the first minute in the window,
     returning NaN it the security happened not to trade in the first minute.
2014-06-05 15:25:48 -04:00
Scott Sanderson c3075f0ece ENH: Add a classmethod to TradingAlgorithm to get all API methods. 2014-05-14 11:24:33 -04:00
Scott Sanderson 644486e6da ENH: Add trading controls to zipline API.
Adds four new methods to the Zipline API that can be used as circuit-breakers
to interrupt the execution of an algorithm.  The API methods are:

`set_max_position_size`
`set_max_order_size`
`set_max_order_count`
`set_long_only`

Internally, these methods are implemented by each registering a TradingControl
callback object with the TradingAlgorithm.  During
TradingAlgorithm.__validate_order_params (and thus before any side-effects of
the order call occur), each callback's `validate` method is called with
information about the order to be placed and the algorithm's current state,
raising an exception if the callback detects that an error condition has been breached.
2014-05-12 17:51:09 -04:00
twiecki c9a75c7b42 STY: Rename run_algo to run_pipeline. 2014-05-07 15:34:42 -04:00
twiecki 2bfc2f9d93 DOC: Add doc strings. 2014-05-07 15:34:41 -04:00
twiecki f5086e4b0e ENH: Add IPython cell magic.
When zipline is imported it checks whether
it runs in the IPython notebook. If it does,
it registers a %%zipline magic that takes the
same arguments as the CLI with the addition of
a -o for specifying the output variable to store
the performance frame in.

The algo code in the cell is, as of yet, executed
in its own environment rather than that of the
IPython NB which is probably what we want.

Also adds cli option to save the perf dataframe
to a pickle file.

Also adds an IPython notebook buyapple example.
2014-05-07 15:34:41 -04:00
twiecki f9fded97ac ENH: Implement CLI.
Add a CLI that reads in an algorithm, loads data,
run the algorithm, and output performance metrics.

The examples are adapted to the new zipline API and
analyses are split into separate files.

Also add config files that run the example
algorithms with preset settings.
2014-05-07 15:34:36 -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
Richard Frank 0e4f3f957a BUG: ValueError for 'bars' masked by UnboundLocalError
for 'freq'
2014-04-23 17:57:46 -04:00
Scott Sanderson 47bfc2b536 MAINT: Clean up set_algo_instance usage in TradingAlgorithm.
TradingAlgorithm always uses set_algo_instance in pairs of
set_algo_instance(self) and set_algo_instance(None).  Refactoring this to use a
context manager.
2014-04-17 16:19:37 -04:00
twiecki e261438d01 ENH: Adapt history() to work on zipline. 2014-04-10 15:59:26 -04:00
twiecki 4bdecd6402 STY: PEP8 fixes. 2014-03-26 20:46:20 +09:00
Eddie Hebert 4860a966b3 REL: Update copyright year on all files changed since the new year. 2014-03-07 22:31:41 -05:00
twiecki d900338e00 BUG: Py3 compatibility changes. 2014-02-16 10:59:29 -05:00
twiecki f0322015bd BUG: Fix pandas indexing in trading calendar. 2014-02-11 15:52:43 -05:00
twiecki d91c18b1c2 BUG: Python 3 fix. 2014-01-30 18:48:46 -05:00
Michael Schatzow 59bcd097d5 ENH: Add hdf5 and csv source.
This creates a data source for csv and hdf5 files, a generator to create a sample csv, and a pytables generator to go from a list of dated gzipped csv's in a directory to a pytables data source.

This does not add a unittest yet which we should write for the future.
2014-01-30 16:47:27 -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 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 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
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
Richard Frank adb4de740e MAINT: Removed unused parameter 2013-12-20 15:46:49 -05:00
Richard Frank f70ae02694 PERF: Speed up get_open_and_closes by assigning entire columns
to DataFrame, instead of row by row
2013-12-20 15:46:49 -05:00
Eddie Hebert bbad5b386a MAINT: Removed unused test_utils.check functions.
The test_utils were the only references to the blist package,
removing so that blist can be removed as a dependency.
2013-12-02 13:19:00 -05:00
Eddie Hebert f8755a5602 STY: Remove unused imports. 2013-11-12 13:26:08 -05:00
Eddie Hebert a2a56f7c63 MAINT: Remove noop_environment.
The code that was consuming noop_environment now uses a
real trading environment.

As more behavior relies on an accurate trading calendar, maintaining
the noop environment was a constraint that was more overhead than it
is worth.
2013-11-12 13:11:52 -05:00
Eddie Hebert 102cfcbe5b BUG: Fix bad dates from test factory.
Passing the exchange time timestamp to is_market_hours was ending
up with odd behavior due to conversion back to UTC when checking
the is_trading_day boolean.
2013-11-12 12:45:05 -05:00
Eddie Hebert aff8311f2f MAINT: Change name of column in open and close frame.
Use more natural sounding 'market_open' and 'market_close'.
2013-11-11 16:05:10 -05:00
Eddie Hebert 48be898a13 ENH: Add open and closes to trading calendar.
Use the early closes to populate a DataFrame which includes
the open and close minute for each day.

To be used by the environment instead of calculating each value
mid-backtest.
2013-11-11 15:48:44 -05:00
Eddie Hebert 43b85cffb0 MAINT: Calculate tradingcalendar with days beyond the current day.
To make 'next open' calculations more straight ahead, calculate more
than enough days in the trading calendar.
2013-11-11 15:48:44 -05:00
Eddie Hebert 796b9fb67a MAINT: Use Timestamp for calculating next_trading_dt in test factory.
Instead of Delorean, use pandas Timestamp.

Could also consider using environment trading_days directly.
2013-11-11 11:44:40 -05:00
Eddie Hebert dd95b6bfa3 MAINT: Use Timestamp instead of Delorean for tradingcalendar end.
Instead of Delorean, use pandas `today` behavior for Timestamp.
2013-11-11 10:13:05 -05: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
Jonathan Kamens 0a7539b6de MAINT: flake8 2013-10-29 12:02:51 -04:00
Jonathan Kamens c3ddbc258d ENH: Accept simulation parameters and benchmark source args to create_test_zipline 2013-10-29 11:39:07 -04:00
Jonathan Kamens e36f70f541 ENH: Allow market data loader to be specified to create_simulation_parameters 2013-10-29 11:17:41 -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 800210fbb3 MAINT: Ensure that test sources only provide market days.
Instead of using all calendar days between start and end in test
sources, use the trading calendar for test sources.

Needed for an incoming refactoring of market open and close,
where the opens and closes are indexed by market days.
2013-10-17 16:45:51 -04:00
Thomas Wiecki a66f45b598 MAINT: Moving yahoo loader from factory to utils. 2013-10-01 14:09:26 -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 6bbc131bbf MAINT: Compare datetime in test utils instead of integer.
Reduce dependency on date_utils, and improve legibility on failing
test.
2013-09-30 23:01:49 -04:00
John Ricklefs 191715a148 ENH: Add support for asynchronous commission events. 2013-08-26 14:18:32 -04:00