212 Commits

Author SHA1 Message Date
Jonathan Kamens fd0530d889 MAINT: Flake8 2015-05-01 11:03:01 -04:00
Jonathan Kamens 557e6a3009 Every setup_logger needs a teardown_logger 2015-05-01 07:38:42 -04:00
Jonathan Kamens 5c546b4f6c TST: overwrite_sim_params=False in tests when using list of sources
When specifying a list of sources to algorithm.py:run in a unit test,
specify overwrite_sim_params=False to suppress the unnessary
UserWarning.
2015-04-30 15:30:19 -04:00
Brian Fink 8baf52fe01 ENH: Add max leverage account guard 2015-04-10 15:19:54 -04:00
Eddie Hebert 314b22656f MAINT: Remove left over simple transform code.
Remove pieces that are no longer used now that the simple transforms are
wrappers around history via the SIDData object.

Move window length related pieces into batch_transform, since the rest
of the utils module is no longer used.
2015-03-30 14:18:39 -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
Thomas Wiecki 760bbced73 Revert "ENH Allow order_percent to work with various market values"
This reverts commit dd37a49f2f as it lead to different algo behavior we have to investigate first.
2015-02-17 20:27:59 +01:00
Jeremiah Lowin dd37a49f2f ENH Allow order_percent to work with various market values
Currently, `order_percent()` and `order_target_percent()` both operate as a percentage of `self.portfolio.portfolio_value`. This PR lets them operate as percentages of other important MVs.

(also adds `context.get_market_value()`, which enables this functionality)

For example:
```python
order_percent('AAPL', 0.5)

order_percent('AAPL', 0.5, percent_of='cash')

order_target_percent('MSFT', 0.1, percent_of='shorts')

tech_stocks = ('AAPL', 'MSFT', 'GOOGL')
tech_filter = lambda p: p.sid in tech_stocks
for stock in tech_stocks:
    order_target_percent(stock, 1/3, percent_of_fn=tech_filter)
```
2015-02-12 14:15:20 +01:00
fawce 909b412e9b modified do not order guard to take an iteratble or a container
container allows for dynamic restrictions, necessary for a
point in time implementation of the restricted list.
2015-02-05 13:56:46 -05:00
fawce 52f78fcbc7 restricted list trading control added. 2015-02-05 13:56:45 -05:00
fawce 34647ec6c4 added tests to confirm the full environment returns as expected. 2014-12-08 16:47:11 -05:00
fawce 0411627169 Modified get_environment to provide multiple fields. 2014-12-07 08:26:13 -05:00
Joe Jevnik e3d8b1034e ENH: Replaces the simple transforms with history calls. Switches
transforms to quantopian syntax.

Adds the sid attribute to the siddata so it is aware of which security
it represents.
2014-11-17 13:16:30 -05:00
Joe Jevnik ca59abcc43 ENH: Makes schedule_function work in daily mode. 2014-11-06 10:49:49 -05:00
Joe Jevnik 93429d69f5 BUG: HistoryContainer creation at runtime did not work as intended. 2014-11-05 18:31:11 -05:00
Scott Sanderson affeb2afbc DEV: Add tz kwarg to get_datetime. 2014-10-23 00:59:50 -04:00
Brian Fink 50c5b73a7b add account object to context 2014-10-10 17:10:45 -04:00
Joe Jevnik df234f516c BUG: Fixes various bugs with the event manager:
- NotHalfDay only worked at midnight
- week_(start|end) were actually month_(start|end)
- Removes check_args from api.
- Default offset of 30mins for market_(open|close)
2014-10-09 14:01:56 -04:00
Joe Jevnik 3c37704a5b ENH: Adds a new api method schedule_function.
schedule_function takes a date rule, a time rule, and a function and
will call the function, passing context and data only when the two rules
fire. This allows for code that is conditional to the datetime of the
algo.

This is implemented internally with `Event` objects which are pairings
of `EventRule`s and callbacks.

handle_data becomes a special event with a rule that always fires. This
makes the logic for handling events more complete and compact.
2014-10-06 13:42:36 -04:00
llllllllll 7563124547 ENH: Adds a new api_method called get_environment so that users may
check if their algorithm is running in zipline or on Quantopian.
2014-08-14 11:48:19 -04:00
Delaney Granizo-Mackenzie 97c88c3c30 BUG: Put initialization of perf_tracker back in __init__
The initialization of perf_tracker had been moved from __init__
in TradingAlgorithm to _create_generator. This caused perf_tracker
to not be ready when portfolio requested it. portfolio was consequently
not ready for access in init. portfolio can now be accessed in init
again, assuming valid sim_params are passed. Otherwise it will be
available in handle_data() after _create_generator() is called.
2014-07-21 12:17:07 -04:00
Delaney Granizo-Mackenzie 9b01d78f5d ENH: Implemented AUTO_INITIALIZE feature.
Created a new flag in TradingAlgorithm that enables subclasses to
decide if they want to handle setting self.initialized = True.
Before it was the responsibility of an overriding subclass to set
initalized = True. This was causing problems because it's easy to
forget this. Now it is the responsibility of TradingAlgorithm
unless explicity stated otherwise.
2014-07-08 14:03:53 -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
Delaney Granizo-Mackenzie c3169f60cd ENH: Added dynamic name functionality to record() API function.
Added the ability to pass *args before the **kwargs so that positional
arguments of the form name, value can be recorded.
2014-06-30 14:09:09 -04:00
Thomas Wiecki f8e84ecd48 MAINT: Remove unused annualizer code. 2014-06-30 17:28:02 +02: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
Scott Sanderson 0338dd73e1 ENH: Filter out empty lists from get_open_orders.
Filter out empty lists from `get_open_orders` so that we have consistent
behavior between the case where a user has never placed an order and the case
where the user has placed an order but it has been executed or cancelled.

A nice side-effect, which was the impetus for this change, is that you can
check if you have any open orders by doing:

```
len(get_open_orders()) == 0
```

Also adds a test for the behavior of `get_open_orders`, which was previously
lacking.
2014-05-29 13:04:53 -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
Scott Sanderson e3faf10dee MAINT: Add tests for UnsupportedOrderParameters.
Add a test case in test_algorithms to verify that appropriate exceptions are
thrown if an algorithm makes a call to the order api with a stop/limit price
and a style.
2014-04-22 23:22:21 -04:00
Scott Sanderson f1fafc34ce ENH: Add style parameters to order API helper methods.
Add `style` parameter to order_value, order_percent, order_target,
order_target_percent, and order_target_value methods.  The style parameter is
forwarded to the underlying call to `order`.
2014-04-22 23:22:21 -04:00
Scott Sanderson 574415f434 MAINT: Adding tests for Blotter handling of limit and stop orders.
Adds a test algorithm that tries to buy with very high limit prices/very low
stop prices and tries to sell with very low limit prices/very high stop prices.
2014-04-21 16:34:59 -04:00
Scott Sanderson 6412742799 MAINT: Clean up unused data in test. 2014-04-21 16:32:54 -04:00
twiecki e261438d01 ENH: Adapt history() to work on zipline. 2014-04-10 15:59:26 -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 4860a966b3 REL: Update copyright year on all files changed since the new year. 2014-03-07 22:31:41 -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
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
Thomas Wiecki 65637b9430 ENH: Add option of instantly filling orders. 2013-10-01 20:30:01 -04:00
Thomas Wiecki b1fdebfb7c TST: Added tests for new order methods. 2013-08-08 15:55:08 -04:00
Thomas Wiecki 940ddd22d3 TST: Added testing of order timings and price. 2013-07-17 19:56:55 -04:00
Eddie Hebert c6cbc9f8e2 TST: Explicitly define the data frequency of record incr test. 2013-04-25 01:12:58 -04:00
Eddie Hebert ccc6cd892b BUG: Ensure algorithm tests use test simulation parameters.
To pass sim_params to TradingAlgorithm using kwargs is required.
When just passing sim_params as an arg, it was ignored.
2013-04-09 16:47:25 -04:00
Eddie Hebert 8997b4f68c MAINT: Remove parameters from test that tests lack of parameters.
The `test_multi_source_as_input_no_start_end` test is designed
to test for a lack of simulation parameters, so make that they are
not passed.

Was unnoticed because sim_params passed as an arg are dropped.
2013-04-09 16:46:55 -04:00
Eddie Hebert 57db5bc17c BUG: Fix start and end dates of simulation parameters used in tests.
The start and end of the simulation parameters should be 'normalized'
i.e. midnight timestamped.
However, the algorithm tests were using the timestamp of the
first and last trade, which were in market times,
i.e. 9:30 AM and 4:00 PM EST.

Fix passing the sim_params that is used to create the trade_history,
instead of having the sim_params inferred from the source.

(Also may want to consider fixing the logic that infers the date
range from the sources provided.)

Also, add a `num_days` option to `factory.create_simulation_parameters`
so that the a date range that covers the desired number of days is covered.
Since the default sim_params were covering a year, while the test only
supplies 4 values, causing an alignment issue with the record test,
since a years worth of results were returned, but there were only 4 events.
2013-04-09 15:11:43 -04:00
Eddie Hebert 58af62f18d REL: Update copyright on all files touched since end of 2012.
s/Copyright 2012/Copyright 2013/
2013-04-05 14:28:15 -04:00
fawce 1a85781170 updated run method to use sim_params. 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
Eddie Hebert 81337d1306 Adds the ability to record variables.
Takes the value set for a variable on handle_data and records it,
e.g.:
```
    def initialize(self):
        self.incr = 0
        self.record_variables(['incr'])

    def handle_data(self, data):
        self.incr += 1
```

Would record a variable of `incr`.

Emits the recorded variables as part of the daily performance.

This batch combins work from:
Thomas Wiecki <thomas.wiecki@gmail.com> (@twiecki)
fawce <fawce@quantopian.com> (@fawce)
2013-01-31 08:16:54 -05:00
Thomas Wiecki 4615b29713 REF: Renamed granularity to data_frequency. 2012-12-30 11:50:46 -05:00