mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-04 07:50:08 +08:00
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)
```
This commit is contained in:
committed by
Thomas Wiecki
parent
38e8d5214d
commit
dd37a49f2f
@@ -45,11 +45,14 @@ from zipline.test_algorithms import (
|
||||
TestOrderAlgorithm,
|
||||
TestOrderInstantAlgorithm,
|
||||
TestOrderPercentAlgorithm,
|
||||
TestOrderPercentAlgorithmPercentOf,
|
||||
TestOrderStyleForwardingAlgorithm,
|
||||
TestOrderValueAlgorithm,
|
||||
TestRegisterTransformAlgorithm,
|
||||
TestTargetAlgorithm,
|
||||
TestTargetAlgorithm_NonInt,
|
||||
TestTargetPercentAlgorithm,
|
||||
TestTargetPercentAlgorithmPercentOf,
|
||||
TestTargetValueAlgorithm,
|
||||
SetLongOnlyAlgorithm,
|
||||
SetMaxPositionSizeAlgorithm,
|
||||
@@ -303,6 +306,9 @@ class TestTransformAlgorithm(TestCase):
|
||||
self.panel_source, self.panel = \
|
||||
factory.create_test_panel_source(self.sim_params)
|
||||
|
||||
self.df_large = pd.concat([self.df] * 10, 1)
|
||||
self.df_large.columns = range(10)
|
||||
|
||||
def test_source_as_input(self):
|
||||
algo = TestRegisterTransformAlgorithm(
|
||||
sim_params=self.sim_params,
|
||||
@@ -374,6 +380,7 @@ class TestTransformAlgorithm(TestCase):
|
||||
AlgoClasses = [TestOrderAlgorithm,
|
||||
TestOrderValueAlgorithm,
|
||||
TestTargetAlgorithm,
|
||||
TestTargetAlgorithm_NonInt,
|
||||
TestOrderPercentAlgorithm,
|
||||
TestTargetPercentAlgorithm,
|
||||
TestTargetValueAlgorithm]
|
||||
@@ -384,6 +391,16 @@ class TestTransformAlgorithm(TestCase):
|
||||
)
|
||||
algo.run(self.df)
|
||||
|
||||
AlgoClasses2 = [
|
||||
TestOrderPercentAlgorithmPercentOf,
|
||||
TestTargetPercentAlgorithmPercentOf]
|
||||
|
||||
for AlgoClass in AlgoClasses2:
|
||||
algo = AlgoClass(
|
||||
sim_params=self.sim_params,
|
||||
)
|
||||
algo.run(self.df_large)
|
||||
|
||||
def test_order_method_style_forwarding(self):
|
||||
|
||||
method_names_to_test = ['order',
|
||||
|
||||
Reference in New Issue
Block a user