Skip to content

Commit

Permalink
ENH: Renamed to batch_order and added batch_order_target_percent
Browse files Browse the repository at this point in the history
  • Loading branch information
richafrank committed Nov 30, 2016
1 parent 74384ff commit a59b678
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
16 changes: 9 additions & 7 deletions tests/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
TestTargetAlgorithm,
TestTargetPercentAlgorithm,
TestTargetValueAlgorithm,
TestBatchTargetPercentAlgorithm,
SetLongOnlyAlgorithm,
SetAssetDateBoundsAlgorithm,
SetMaxPositionSizeAlgorithm,
Expand Down Expand Up @@ -905,14 +906,15 @@ def test_data_frequency_setting(self):
self.assertEqual(algo.sim_params.data_frequency, 'minute')

@parameterized.expand([
(TestOrderAlgorithm,),
(TestOrderValueAlgorithm,),
(TestTargetAlgorithm,),
(TestOrderPercentAlgorithm,),
(TestTargetPercentAlgorithm,),
(TestTargetValueAlgorithm,),
('order', TestOrderAlgorithm,),
('order_value', TestOrderValueAlgorithm,),
('order_target', TestTargetAlgorithm,),
('order_percent', TestOrderPercentAlgorithm,),
('order_target_percent', TestTargetPercentAlgorithm,),
('order_target_value', TestTargetValueAlgorithm,),
('batch_order_target_percent', TestBatchTargetPercentAlgorithm,),
])
def test_order_methods(self, algo_class):
def test_order_methods(self, test_name, algo_class):
algo = algo_class(
sim_params=self.sim_params,
env=self.env,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_order_batch_matches_multi_order(self):
(self.asset_25, i * 100, LimitOrder(i * 100 + 1)),
]

order_batch_ids = blotter1.order_batch(order_arg_lists)
order_batch_ids = blotter1.batch_order(order_arg_lists)
order_ids = []
for order_args in order_arg_lists:
order_ids.append(blotter2.order(*order_args))
Expand Down
37 changes: 36 additions & 1 deletion zipline/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
try:
# optional cython based OrderedDict
from cyordereddict import OrderedDict
except ImportError:
from collections import OrderedDict
from copy import copy
import operator as op
import warnings
Expand All @@ -32,6 +37,7 @@
itervalues,
string_types,
viewkeys,
viewvalues,
)

from zipline._protocol import handle_non_market_minutes
Expand Down Expand Up @@ -1915,7 +1921,7 @@ def order_target_percent(self, asset, target,
----------
asset : Asset
The asset that this order is for.
percent : float
target : float
The desired percentage of the porfolio value to allocate to
``asset``. This is specified as a decimal, for example:
0.50 means 50%.
Expand Down Expand Up @@ -1968,6 +1974,35 @@ def _calculate_order_target_percent_amount(self, asset, target):
target_amount = self._calculate_order_percent_amount(asset, target)
return self._calculate_order_target_amount(asset, target_amount)

@api_method
@disallowed_in_before_trading_start(OrderInBeforeTradingStart())
def batch_order_target_percent(self, weights):
"""Place orders towards a given portfolio of weights.
Parameters
----------
weights : collections.Mapping[Asset -> float]
Returns
-------
order_ids : pd.Series[Asset -> str]
See Also
--------
:func:`zipline.api.order_target_percent`
"""
order_args = OrderedDict()
for asset, target in iteritems(weights):
if self._can_order_asset(asset):
amount = self._calculate_order_target_percent_amount(
asset, target,
)
amount, style = self._calculate_order(asset, amount)
order_args[asset] = (asset, amount, style)

order_ids = self.blotter.batch_order(viewvalues(order_args))
return pd.Series(data=order_ids, index=order_args)

@error_keywords(sid='Keyword argument `sid` is no longer supported for '
'get_open_orders. Use `asset` instead.')
@api_method
Expand Down
2 changes: 1 addition & 1 deletion zipline/finance/blotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def order(self, sid, amount, style, order_id=None):

return order.id

def order_batch(self, order_arg_lists):
def batch_order(self, order_arg_lists):
"""Place a batch of orders.
Parameters
Expand Down
10 changes: 9 additions & 1 deletion zipline/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,17 @@ def handle_data(self, data):
"Orders not filled at current price."

self.sale_price = data.current(sid(0), "price")
self.order_target_percent(self.sid(0), .002)
self._order(self.sid(0), .002)
self.ordered = True

def _order(self, asset, target):
return self.order_target_percent(asset, target)


class TestBatchTargetPercentAlgorithm(TestTargetPercentAlgorithm):
def _order(self, asset, target):
return self.batch_order_target_percent({asset: target})


class TestTargetValueAlgorithm(TradingAlgorithm):
def initialize(self):
Expand Down

0 comments on commit a59b678

Please sign in to comment.