Commit Graph

145 Commits

Author SHA1 Message Date
fawce 34f1dd783a STY: Tweak comments in performance to match rest of file. 2013-04-30 17:19:09 -04:00
fawce 427ea8d4ca ENH: Change simulation loop to use benchmarks as simulation 'clock'.
Refactor PerformanceTracker, Blotter, and AlgorithmSimulator to
work with handling the end of a bar at the AlgorithmSimulator level
instead of within PerformanceTracker.

- PerforamnceTracker and Blotter are longer generators,
  both provide functions to process events instead.
- AlgorithmSimulator calls each from within the loop running
  over the data generator.
- Change test_perf_tracker utility to be compatible with change
  away from PerformanceTracker as a generator.

Has the effect of:
- Fixing the timing of order emission.
- Allow minutely emission of benchmarks, which was prevented
  by the extra grouping previously caused by Blotter.

Minutely emission also depends on work for streaming benchmarks
through performance and risk at a minute granularity.
2013-04-25 17:16:35 -04:00
Eddie Hebert d31303b86c ENH: Add basis for minute rate emission of performance.
- Create different benchmark containers in performance
  depending on emission rate.
- Add a minute close method which updates algorithm and
  benchmark returns, and calculates the risk metrics
  depending on those methods.
- Provide fake 0.0 values for annualized metrics like
  sharpe, sortino, and information, until we figure out
  how they should be treated in the context of minutely
  calculation.

*NOTE* This does not fully work without the changes to the
simulation loop by @fawce
2013-04-25 16:49:38 -04:00
Eddie Hebert fd6c71286d MAINT: Use sim_params for risk metrics init.
Prepare for adding emission_rate in risk metrics logic.
2013-04-25 15:30:34 -04:00
Eddie Hebert d067f13ba8 MAINT: Use a fake progress value for minute performance.
Eventually should to either return None or remove
progress completely, but in the meantime, return a
constant of 1.0 for progress of minute emissions.

Also, factor out the daily calculation into a property
instead of calculating during process.
2013-04-25 14:28:33 -04:00
Eddie Hebert 8937ac1f41 MAINT: Generate perfomance message only once per bar for minute mode.
Instead of creating a set of perf messages for each event during minute
emission mode, only include the messages on the last event in the bar.

Should cut down on calculations/serialization as well, as work towards
doing more 'end of bar' logic for minute benchmarks.
2013-04-22 17:37:32 -04:00
fawce 3811df78b9 BUG: Fix grouping of events streamed through blotter.
To fix the grouping of events so that (dt, events) ordering
is preserved, the tracking of order states needs to change
in the following way.

Change how order keeps track of dates:
- Change order's dt field to reflect modified date.
- Add a created field.

Change how performance keeps track of orders by:
- Map dt to transactions
- Map dt to orders
- Map order ids to keep track of updated orders.
2013-04-22 16:46:28 -04:00
fawce bc95c3a62e BUG: Fix emission of order updates.
The emission of order updates from the blotter were incorrect,
and subsequently, performance.

Previously, only the first action of the order was emitted,
fix so that all status updates are emitted.
2013-04-18 16:08:44 -04:00
Richard Frank d487401989 BUG: Perf tracker should emit perf messages only for TRADE events 2013-04-15 16:57:33 -04:00
Eddie Hebert 9099d301f3 ENH: Stream benchmark returns as events.
Instead of creating a list of benchmarks in the risk module,
stream benchmarks through the system as events, starting from the
algorithm generator.

Works towards more easily setting arbritrary pricing data as
a a benchmark, as well as working towards live minutely benchmarks.
2013-04-15 11:43:13 -04:00
Eddie Hebert 6210467bec MAINT: Use pd.Series for benchmarks and algorithm returns in risk.
Instead of lists, use pd.Series, so that memory is preallocated.
2013-04-15 11:37:21 -04:00
Richard Frank 2dbafd5162 BUG: Zero out the microsecond attribute of datetimes
wherever we zero out the second attribute.  Otherwise, we can be
off by some microseconds from midnight, etc.
2013-04-15 10:44:44 -04:00
Eddie Hebert 35f57ada3e ENH: Send transactions and orders as standalone events.
- Add transaction and order types
- Move TransactionSimulator from trading.py to tradesimulation.py
  (only used by other members of the tradesimulation module)
- Make Transaction an independent event, like dividend
- Add Blotter class.
- Flatten the transaction events to be independent of trade bar events
- Make orders into events that reach performance (need to add
handling)
- Issue IDs to orders and tracking each transaction's order id.
- Make volume share slippage fill orders independently, rather than
  aggregating them into a single transaction.
- Perf tracker holds orders, serializes them with transactions.
- Order state defined and maintained by order class.
- Minutely emission of orders based on last_modified date.
2013-04-14 18:59:57 -04:00
Eddie Hebert 6a3c35c0fd BUG: Ensure that correct dates are emitted during entire minute rate.
Also, fix double emission of performance results with the last minute.

Change the perf tracker unit tests so that it doesn't rely on an
'extra' event triggering emission.
Unlike daily, minute emission now emits at the end of the bar in
the PerformanceTracker.transform instead of waiting for the next event.
2013-04-11 15:42:07 -04:00
Eddie Hebert d21b500db6 ENH: Emit a rollup of day's performance in minutely emission mode.
During minute emissions, it is still helpful to have a final daily
performance result, analogous to what would be the final packet in
a daily emitted backtest, so that all transactions, etc. are contained
in one place.
2013-04-10 16:20:44 -04:00
Eddie Hebert e03d51f0bc BUG: Fix extra minutely performance period during minute performance.
Prevent an extra performance result with the timestamp of the midnight of
the day from being emitted.

Fix by setting the `saved_dt` value with the dt of the first event,
before entering into the main performance loop, otherwise a performance
result with a midnight timestamp and data from just the first event is
emitted.
2013-04-10 10:51:39 -04:00
Eddie Hebert 5a7039ab93 BUG: Move minutely performance period end time forward in time.
The end time of the performance period during minutely emission
should move forward with the events' dt, not be static.
2013-04-10 10:51:39 -04:00
Eddie Hebert 90fa2a8a4e MAINT: Stop including progess field with minute performance result.
As currently implemented, progress doesn't currently make sense with
minutely results. Dropping the field from the results so should help
reduce some noise.
2013-04-09 11:02:00 -04:00
Eddie Hebert 5422970d13 BUG: Stop intraday performance from emitting all transactions.
The intraday performance results were emitting all transactions
for the entire day up to that point, instead of the desired transaction
list for the current timestamp.

Add a `dt` parameter to the `to_dict` method of PerformancePeriod so
that the transactions are limited to a specific datetime.
When the parameter is `None`, a todays_performance object will
function as previously with returning all transactions for the day.
 # Please enter the commit message for your changes. Lines starting
2013-04-05 13:55:04 -04:00
Eddie Hebert 7679e5a581 ENH: Wires minutely emission of data from performance tracker.
Wires up performance tracker so that when `emission_rate` is set
to `minute`, the performance packets are sent out every minute,
instead of once per day.

Please note, the performance packets that are generated are not
ready for prime time consumption, this patch is merely a step towards
hooking up the ability to inspect minute data.

Known issues:
- The packets do not currently include risk information.
  Since we need to consider how this affects the denominators
  of the risk calculations.
2013-03-27 16:58:56 -04:00
Eddie Hebert 41d3c72627 MAINT: Removes unused currentValue method on Position object.
This method became unused when vectorizing position totals.
2013-03-26 22:33:19 -04:00
Eddie Hebert 6f1cbcbc4f MAINT: Moves internal state variables in performance tracker.
Slight refactoring of grouping the tracking variables in the
PerformanceTracker together.

So that it's easier to see which are config members and which are
members used to track internal state.
2013-03-25 12:44:45 -04:00
Eddie Hebert 832c93134f MAINT: Removes comment referreing to removed started_at member. 2013-03-20 22:18:30 -04:00
Eddie Hebert 5dc449ba19 MAINT: Changes boolean check for snapshot existence in performance.
Small tweak to check for existence of elements using built-in
boolean of lists, instead of checking for `len`.
2013-03-20 13:19:39 -04:00
Eddie Hebert 75049fdd15 MAINT: Removes unused started_at member from performance tracker. 2013-03-20 13:05:38 -04:00
Eddie Hebert 95a9b7b3c2 MAINT: Updates docstring for performance tracker class. 2013-03-20 11:27:57 -04:00
Eddie Hebert 8bf4c60169 MAINT: Removes unused member from performance tracker class.
`last_dict` is not referenced elsewhere.
2013-03-20 10:46:59 -04:00
fawce dba86153d2 ENH: added a CUSTOM datasource type for custom data.
- perf modified to let non-performance related events flow through.
- changes to support streaming non-trading data through batch transforms
and for mixing in sids with just custom data.
- allowing CUSTOM events to flow through to transforms.
- Added logic to maintain pre-specified sid filter.
2013-03-19 11:39:23 -04:00
fawce 045773264b ENH: Adds a flag for optionally not serializing positions.
So that both computational and memory overhead is reduced,
this turns off serializing positions for cumulative performance.

Positions were essentially being doubled up by being stored
in both cumalative and daily.
2013-03-08 15:06:41 -05:00
Eddie Hebert a4e6520137 MAINT: Reverses polarity on keep transactions default.
So that transactions are kept by default.

This prepares for the addition of the serialize flag added by
@fawce.

Setting the default to True, so that the flags will be aligned.
2013-03-08 15:00:12 -05:00
Richard Frank ebdb5429aa MAINT: Moved DailyReturn to protocol module to break circular references
and removed code that solved that same problem with conditional imports.
2013-03-01 16:05:39 -05:00
fawce a4a4d38a73 TradingEnvironment allows the specification of a benchmark index and a local timezone for the exchange. This commit adds tests to verify the TradingEnvironment properly handles London Stock Exchange index, FTSE.
- added LSE reference rrules calendar (thanks to Edward Johns)
    - added tests to verify LSE environment matches rrule calendar
    - added a test to verify global environment behavior can be set.
    - moved DailyReturn class to trading to eliminate circularity from
    risk <-> trading.
    - updated TradingEnvironment to be a context manager. This allows users
    to run algorithms in individually isolated environments in one python
    process. This is useful for managing multiple algorithms in a single
    ipython notebook.
    - added comments to explain behavior and useage of the global environment
2013-02-18 10:24:32 -05:00
fawce 2c7355a0dc Refactoring of TradingEnvironment to isolate the global state: index symbol and exchange timezone. Parameters that define the simulation (start, end, and capital base) were put in a new class, SimulationParameters.
Global state for the financial simulation environment is accessed through the
zipline.finance.trading module, which now contains a module variable:
environment.

Parameters are passed into an algorithm as a keyword argument, sim_params.
SimulationParameters creates a trading day index for the test period that
can be used to find trading days, calculate distance between trading days,
and other common operations. The sim params index is just selected from the
global state.

================

Details:

    - adding delorean to the requirements.
    - made index symbol a parameter for loading the benchmark data. changed
    messagepack storage to be symbol specific.
    - ported risk, performance, algorithm, transforms, batch transforms
    and associated tests to use simulation parameters and global environment
    - factory and sim factory use global state and sim params
    - factory method parameter names now reflect the class expected
2013-02-18 10:24:32 -05:00
fawce 3ae02281da Fixed bugs in the sequence of dividend payment calculations. Previously, we were using midnight of the current trading day in market close. That meant that we were "rewinding" the clock, and then checking the ex_date and pay_date. As a result, we were delaying payments by one day.
With this patch, on the close of markets we "fast forward" to midnight of the
next trading day and calculate the dividend payments. This patch assumes that
the dividend dates are all at midnight UTC.
2013-02-15 22:52:38 -05:00
fawce 31b528e8dd Implemented dividend costs for short positions.
Based on user feedback in Quantopian forums:
https://www.quantopian.com/posts/total-return-slash-dividends
2013-02-06 23:34:14 -05:00
fawce 817ed88e38 Adds dividends to performance tracking.
Algorithm returns and the risk calculations that depend on them now include
cash dividends. This commit does _not_ provide an API for user algorithms to
access dividends.

PerformanceTracker expects the dividend data to arrive as events, similar to
the way that Trades arrive. Dividends are expected to have adjusted payment
amounts that are inline with adjusted trades.

PerformanceTracker maintains state of all the unpaid dividends in the position
objects held in PerformancePeriod. Dividend objects contain all the relevant
dates (declared, ex, payment) as well as net and gross amounts. Dividends are
removed from the list as they are paid. Cash flow is not incremented until the
payment day. This creates the possibility of a dividend being owed but not
paid or realized before the end of a test. For example, a dividend with an
ex_date of today may have a pay date 2 weeks in the future. Right now the
algorithm does not receive any credit for unpaid dividends.

Tests cover buying/selling around the ex_date and payment_date, and checking
that the performance calculated is as expected.
2013-02-06 16:39:39 -05:00
Eddie Hebert d5a0446f7b Moves slippage transactions off of ndict.
So that the datatype is unique.
2013-01-22 20:55:24 -05:00
Eddie Hebert 65138fbceb Uses numpy.dot instead numpy.vdot to calculate positions value.
Since the position amount and price ndarrays are one dimensional
and use real numbers, we do not need the overhead of the extra
case handling provided by numpy.vdot, which comes at a cost of
performance.

With thanks to @jlowin, for pointing out the better fit of numpy.dot.
2013-01-16 11:38:37 -05:00
Eddie Hebert 018ac67966 Uses vdot and numpy arrays for position totals.
Gets almost 100x speed up over iterating over the values and
summing up the values in Python.

Farms out the work to numpy and atlas by using the vector dot
product of the amounts and last sale prices.

Adds some wiring of keeping track of an index into the numpy arrays
for each position, so that value can be overwritten as events update
those amounts and sale prices.
2013-01-14 21:47:14 -05:00
Eddie Hebert e7405d04ad Rolls over existing PerformancePeriod.
Instead of doing the rollover by creating a new PerformancePeriod,
introduces a `rollover` method that resets the values that need
to be fresh in a new period, and moves the ending values to starting
values, and leaves positions intact.

This isn't a major runtime improvement in of itself, but it does
allow us to more easily keep track of position values from period
to period, which other improvements will use.
2013-01-14 21:47:13 -05:00
Eddie Hebert 34d577d3d7 Recycles objects for positions.
Instead of creating a new ndict for each position on every event,
we change the values in the object that held the previous position.

The creation of new objects on each event was incurring too much
overhead.

Changes the position type returned by performance module.

For improved speed, changes from ndict to a simple Python object,
since the cost of setting ndict values is too expensive for the
number of times that positions are returned.

Also, changes the containing type of the positions to be dictionary
with the __missing__ overloaded, instead of the ndict that had that
behavior, to reduce the penalty of using ndicts.
2013-01-12 15:37:18 -05:00
Eddie Hebert 1ddfadf5b4 Recycles the portfolio container to be passed to handle_data.
The creation of a new portfolio ndict on each call of handle_data
was creating a very high performance overhead.

Instead, we use the same the portfolio object for each event,
and replace the values contained within.
2013-01-12 15:35:49 -05:00
Eddie Hebert ca9fdcfe84 Uses a Portfolio object instead of an ndict.
Gains some performance by using a 'regular' object instead of
an ndict.

Also, directly sets up the values that we return, instead of going in
between with __core_dict and then removing values.

In it's entirety performanc.as_portfolio is the current
highest bottleneck, working on reducing time spent in that function.
2013-01-11 14:50:00 -05:00
Eddie Hebert fc03e80cdf Removes done message.
Instead of checking for 'DONE' on each call uses generators
builtin StopIteration for signalling the end of input.
2013-01-07 12:06:31 -05:00
Eddie Hebert f7e4f57425 Enables performance messages on days that have no trades.
Previously, on days that were trading days, but there with no
event data to process for that day, performance metrics were
not emitted, since the handling was based on having an event
trigger the daily performance metric.

Handled by grouping together performance messages, on market open,
for all days since the last market close.

Also, changes perf_tracker unit test to simulate missing data.

Taken from @richafrank's branch handling the same case.
2012-12-28 11:43:31 -05:00
Eddie Hebert a8413e1cc2 Adds reprs for PerformanceTracker and TradingEnvironment.
For debugging in the REPL.
2012-12-27 18:26:55 -05:00
Eddie Hebert f54881cd08 Changes tests from using an ndict for trades to an Event object.
When run over large amounts of data the use of ndict's gets and sets
become a large bottleneck, around 1/5th of the CPU time is spent
in ndict's __setattr__, __getattr__, etc.

By switching to an object for an event,
we reduce the penalty significantly.

Removes asserts that check for event being an ndict, as well as those
that assume a certain behavior of the __contains__ method for events.
2012-12-21 14:31:40 -05:00
Richard Frank 095f2dd65b Date bookkeeping fixes in perf and risk
Issues appeared when we were close to the end of our
historical data.

Yielding DONE event with both perf and risk messages now
2012-12-12 15:23:26 -05:00
Richard Frank 4d41070585 Fix for slippage time getting out of sync with algo.
Moved grouping by date earlier in the pipeline of generators,
prior to any date-dependent state getting involved.  Grouping
pulls from the pipeline until the start of the next group,
which is in the next day.  The effect of grouping after
slippage but before handle_data is that slippage and the algo
are out of sync by a transaction.
2012-11-27 13:38:50 -05:00
Eddie Hebert 0617e53d69 Upgrades flake8 from 1.5 -> 1.6
Also, removes flake8 ignores, since the warnings that were
at odds with eachother now work.
2012-11-19 12:49:09 -05:00