Commit Graph

148 Commits

Author SHA1 Message Date
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
Joe Jevnik fdf4e9b737 PERF: Makes get_datetime not make a copy.
datetime.datetime objects are immutable.
2015-02-17 11:50:20 -05: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 52f78fcbc7 restricted list trading control added. 2015-02-05 13:56:45 -05:00
Thomas Wiecki c734f23102 BUG: kwargs.pop() requires default argument. 2015-02-03 12:20:45 +01:00
Thomas Wiecki 9bdc2c8734 BUG: Support algo_filename being None. Fixes #480. 2015-02-03 11:36:00 +01:00
fawce 9ccb2c571f added cumulative risk measures to the
datapanel produced from perf frames.
2015-01-22 22:31:40 -05:00
Thomas Wiecki f474ee7eed BUG: Various functions were missing from the API. 2014-12-31 09:43:35 +01:00
Thomas Wiecki d0eba3d1ca DOC: Fix typo in TradingAlgorithm doc string. 2014-12-26 13:13:53 +01:00
Thomas Wiecki 8f3671bf24 DOC: Fix typo in TradingAlgorithm doc string. 2014-12-26 13:12:52 +01:00
fawce cd976ee2dd Merge pull request #449 from quantopian/bug_5089
expand get_environment
2014-12-12 17:42:58 -05:00
Scott Sanderson 51e43e3d61 BUG: Use string_types instead of basestring for py3 compat. 2014-12-09 11:17:28 -05:00
Scott Sanderson a701d0c47c DEV: Explicitly convert string to timezones in get_datetime. 2014-12-09 11:17:28 -05:00
Scott Sanderson 44f993c64e BUG: Use astimezone instead of tz_convert.
Makes get_datetime agnostic as to whether its input is a datetime or a
Timestamp.  (astimezone is an alias to tz_convert when the input is a
Timestamp).
2014-12-09 11:17:28 -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 e56457c671 ENH: Makes breakpoints work in algoscripts started with the run_algo.py
script.

Adds an option kwarg to TradingAlgorithm named 'algo_filename' that
represents the file where the algoscript came from (if any). The
run_algo.py script will pass this argument with the value passed to the
'-f' flag. The default name is '<string>' to represent that the script
is coming from a string in python and not a file. This matches the
behavior of exec and the python convention for compiling code objects.
2014-11-10 10:14:56 -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
Joe Jevnik f8f7f2fc4c ENH: Allows history to be dynamic and grow the container at runtime.
Previously, all specs had to be pre-allocated by using the 'add_history'
function. This is now no longer required and instead serves as a hint to
the HistoryContainer to pre-allocate the space for the given spec.

History can grow by increasing the length for a frequency, adding a
frequency, or adding a field. It can grow with any combination of
these.

HistoryContainer now is aware of the data_frequency of the algorithm,
and no longer uses the daily_at_midnight flag; instead, this is the
default behavior.
2014-11-03 15:57:44 -05:00
Scott Sanderson affeb2afbc DEV: Add tz kwarg to get_datetime. 2014-10-23 00:59:50 -04: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
Joe Jevnik 615228b272 ENH: Changes the default time_rule for schedule_function to market_open() 2014-10-20 17:17:34 -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
Scott Sanderson 97e4e5e94d MAINT: In _create_generator only overwrite perf_tracker when it's None. 2014-09-29 14:40:02 -04:00
Thomas Wiecki 011f072fe2 MAINT: Add assertion for incompatible history freq specification. Supplying 1m frequency to history but only providing daily source data resulted in an odd non-descript exception. This adds a specific check and raises a more informative exception. 2014-09-17 15:04:37 +02:00
Richard Frank 3784ed4ba9 ENH: A TradingAlgorithm method called before each trading day 2014-09-03 18:07:51 -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
Scott Sanderson 3e978d0ebb MAINT: Remove unused methods from TradingAlgorithm.
Removes unused method `raw_positions` and `raw_orders` from `TradingAlgorithm`.
These methods existed to help support features in the Quantopian codebase,
which no longer use the existing interface.
2014-08-13 13:00:03 -04:00
llllllllll 90405b1c99 MAINT: Adds symbol to the list of api_methods. 2014-08-06 18:18:56 -04:00
Delaney Granizo-Mackenzie 62c9b96674 Merge pull request #372 from quantopian/moved-perf-tracker-assignment
BUG: Put initialization of perf_tracker back in __init__
2014-07-21 13:48:22 -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
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 0176279404 MAINT: Refactor AlgorithmSimulator.transform.
Breaks out the main snapshot processing loop into its own function, and does
some minor variable renaming-shuffling.

Adds `TradingAlgorithm.on_dt_changed`, a function to be called when the
simulation dt changes, prior to processing any events.

There should be no difference in behavior as a result of this change.
2014-07-18 15:04:20 -04:00
Thomas Wiecki 6de4d32ee1 ENH: Change open and close to midnight if using daily input data. 2014-07-16 15:16:57 +02:00
Eddie Hebert 4980928394 MAINT: Remove redundant perf_tracker member.
The perf_tracker member of TradingMember was set to None twice
during __init__.

Remove one of those instances.
2014-07-12 00:06:09 -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 49eaeeb6ae BUG: Apply integer truncation to order amounts earlier in the pipeline.
Truncate non-integer order amounts in `TradingAlgorithm.order` instead of
`Blotter.order`.  This fixes an issue where non-integer orders coming out of
order_value can spuriously trigger a `LongOnly` trading guard.

Example:

sid.price == 2.0
order_value(sid, 5) -> order(sid, 2.5) -> truncated to order(sid, 2.0)
order_value(sid, -5) -> order(sid, -2.5) -> LongOnlyViolation b/c 2.0 - 2.5 < 0
2014-06-09 11:42:43 -04:00
David Edwards 7547ef535a MAINT: Removed unused keyword arg
Deleted keyword arg include_open_orders, it was left over from working
on handling open orders.
2014-06-04 20:08:52 +02:00
David Edwards 243c0348f3 MAINT: Refactored target order methods
Refactored the target order methods to separate their logic from the
other order methods.

This makes order_target the only target method that calls order()
directly.

order_target_value is passed to order_target instead of order_value.

order_target_percent is passed to order_target_value rather than
order_value.

This simplified the code and decouples the logic of target orders from
the other order methods. This allows the target order methods to be
developed independently from order_value and order_percent.
2014-06-04 20:08:52 +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 ecd9bff0d6 PERF/BUG: Make the portfolio property call updated_portfolio.
Make the portfolio property on TradingAlgorithm call `updated_portfolio`
internally.  This prevents needless recomputation of the portfolio between
calls to `handle_data`, and also prevents issues where the portfolio object
could be unexpectedly modified in place in the body of a `handle_data` call.

Noteworthy finding in the course of investigating this bug:

If you modify a Python dictionary while iterating over it, the language will
only throw an exception if the size of the dictionary changes between loop
iterations; this means that you can do:
```
x = {1:1, 2:2, 3:3}
for k in x:
    old_val = x[k]
    del x[k]
    x[f(k)] = old_val
    print k
```
and you'll only get an error if f(k) is already a key in the dictionary.
This can lead to bizarre/nondeterministic behavior in the key iterator.
2014-05-27 11:20:13 -04:00
Scott Sanderson c3075f0ece ENH: Add a classmethod to TradingAlgorithm to get all API methods. 2014-05-14 11:24:33 -04:00