ENH: Renamed to batch_order and added batch_order_target_percent

This commit is contained in:
Richard Frank
2016-12-20 11:58:05 -05:00
parent 74a3247892
commit 8ea3226a5c
5 changed files with 58 additions and 12 deletions
+37 -1
View File
@@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import Iterable
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
@@ -33,6 +38,7 @@ from six import (
itervalues,
string_types,
viewkeys,
viewvalues,
)
from zipline._protocol import handle_non_market_minutes
@@ -1916,7 +1922,7 @@ class TradingAlgorithm(object):
----------
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%.
@@ -1969,6 +1975,36 @@ class TradingAlgorithm(object):
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]
The unique identifiers for the orders that were placed.
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