mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-15 12:15:22 +08:00
Merge pull request #57 from quantopian/optimize_qexec
Minor refactoring of optimize code. Touches nothing else.
This commit is contained in:
+29
-59
@@ -14,6 +14,7 @@ from zipline.utils.logger import configure_logging
|
|||||||
|
|
||||||
from zipline.core.devsimulator import AddressAllocator, Simulator
|
from zipline.core.devsimulator import AddressAllocator, Simulator
|
||||||
from zipline.optimize.algorithms import BuySellAlgorithm
|
from zipline.optimize.algorithms import BuySellAlgorithm
|
||||||
|
from zipline.optimize.factory import create_predictable_zipline
|
||||||
from zipline.finance.trading import TradingEnvironment
|
from zipline.finance.trading import TradingEnvironment
|
||||||
from zipline.lines import SimulatedTrading
|
from zipline.lines import SimulatedTrading
|
||||||
from zipline.finance.trading import SIMULATION_STYLE
|
from zipline.finance.trading import SIMULATION_STYLE
|
||||||
@@ -53,20 +54,18 @@ class TestUpDown(TestCase):
|
|||||||
base_price = 50
|
base_price = 50
|
||||||
amplitude = 6
|
amplitude = 6
|
||||||
offset = 0
|
offset = 0
|
||||||
self.zipline_test_config['order_count'] = trade_count - 1
|
|
||||||
self.zipline_test_config['trade_count'] = trade_count
|
|
||||||
self.zipline_test_config['simulation_style'] = \
|
|
||||||
SIMULATION_STYLE.FIXED_SLIPPAGE
|
|
||||||
|
|
||||||
trading_environment = factory.create_trading_environment()
|
zipline, config = create_predictable_zipline(
|
||||||
source = create_updown_trade_source(sid,
|
self.zipline_test_config,
|
||||||
trade_count,
|
sid=sid,
|
||||||
trading_environment,
|
amplitude=amplitude,
|
||||||
base_price,
|
base_price=base_price,
|
||||||
amplitude
|
offset=offset,
|
||||||
|
trade_count=5,
|
||||||
|
simulate=False
|
||||||
)
|
)
|
||||||
|
|
||||||
prices = np.array([event.price for event in source.event_list])
|
prices = np.array([event.price for event in config['trade_source'].event_list])
|
||||||
max_price_idx = np.where(prices==prices.max())[0]
|
max_price_idx = np.where(prices==prices.max())[0]
|
||||||
min_price_idx = np.where(prices==prices.min())[0]
|
min_price_idx = np.where(prices==prices.min())[0]
|
||||||
self.assertTrue(np.all(max_price_idx % 2 == 1),
|
self.assertTrue(np.all(max_price_idx % 2 == 1),
|
||||||
@@ -82,15 +81,10 @@ class TestUpDown(TestCase):
|
|||||||
"Minimum price does not equal expected maximum price."
|
"Minimum price does not equal expected maximum price."
|
||||||
)
|
)
|
||||||
|
|
||||||
algo = BuySellAlgorithm(sid, 100, 0)
|
|
||||||
|
|
||||||
self.zipline_test_config['trade_source'] = source
|
|
||||||
self.zipline_test_config['algorithm'] = algo
|
|
||||||
self.zipline_test_config['environment'] = trading_environment
|
|
||||||
|
|
||||||
zipline = SimulatedTrading.create_test_zipline(**self.zipline_test_config)
|
|
||||||
zipline.simulate(blocking=True)
|
zipline.simulate(blocking=True)
|
||||||
|
|
||||||
|
algo = config['algorithm']
|
||||||
|
|
||||||
orders = np.asarray(algo.orders)
|
orders = np.asarray(algo.orders)
|
||||||
max_order_idx = np.where(orders==orders.max())[0]
|
max_order_idx = np.where(orders==orders.max())[0]
|
||||||
min_order_idx = np.where(orders==orders.min())[0]
|
min_order_idx = np.where(orders==orders.min())[0]
|
||||||
@@ -108,7 +102,6 @@ class TestUpDown(TestCase):
|
|||||||
"Algorithm did not sell when price was going to increase."
|
"Algorithm did not sell when price was going to increase."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_concavity_of_returns(self):
|
def test_concavity_of_returns(self):
|
||||||
"""verify concave relationship between free parameter and
|
"""verify concave relationship between free parameter and
|
||||||
returns in certain region around the max. Moreover,
|
returns in certain region around the max. Moreover,
|
||||||
@@ -121,10 +114,6 @@ class TestUpDown(TestCase):
|
|||||||
sid = 133
|
sid = 133
|
||||||
amplitude = 30
|
amplitude = 30
|
||||||
base_price = 50
|
base_price = 50
|
||||||
self.zipline_test_config['order_count'] = trade_count - 1
|
|
||||||
self.zipline_test_config['trade_count'] = trade_count
|
|
||||||
self.zipline_test_config['simulation_style'] = \
|
|
||||||
SIMULATION_STYLE.FIXED_SLIPPAGE
|
|
||||||
|
|
||||||
#test whether return-function is concave wrt repeats.
|
#test whether return-function is concave wrt repeats.
|
||||||
test_offsets = np.arange(-9, 9, 1.)
|
test_offsets = np.arange(-9, 9, 1.)
|
||||||
@@ -133,21 +122,16 @@ class TestUpDown(TestCase):
|
|||||||
|
|
||||||
compound_returns = np.empty(len(test_offsets))
|
compound_returns = np.empty(len(test_offsets))
|
||||||
ziplines = []
|
ziplines = []
|
||||||
for i, test_offset in enumerate(test_offsets):
|
for i, offset in enumerate(test_offsets):
|
||||||
trading_environment = factory.create_trading_environment()
|
zipline, config = create_predictable_zipline(
|
||||||
source = create_updown_trade_source(sid,
|
self.zipline_test_config,
|
||||||
trade_count,
|
sid=sid,
|
||||||
trading_environment,
|
amplitude=amplitude,
|
||||||
base_price,
|
base_price=base_price,
|
||||||
amplitude
|
offset=offset,
|
||||||
|
trade_count=trade_count,
|
||||||
|
simulate=True
|
||||||
)
|
)
|
||||||
|
|
||||||
algo = BuySellAlgorithm(sid, 100, test_offset)
|
|
||||||
self.zipline_test_config['algorithm'] = algo
|
|
||||||
self.zipline_test_config['trade_source'] = source
|
|
||||||
self.zipline_test_config['environment'] = trading_environment
|
|
||||||
zipline = SimulatedTrading.create_test_zipline(**self.zipline_test_config)
|
|
||||||
zipline.simulate(blocking=True)
|
|
||||||
ziplines.append(zipline)
|
ziplines.append(zipline)
|
||||||
compound_returns[i] = zipline.get_cumulative_performance()['returns']
|
compound_returns[i] = zipline.get_cumulative_performance()['returns']
|
||||||
|
|
||||||
@@ -177,29 +161,15 @@ class TestUpDown(TestCase):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def simulate(offset):
|
def simulate(offset):
|
||||||
#generate events
|
zipline, config = create_predictable_zipline(
|
||||||
trade_count = 3
|
self.zipline_test_config,
|
||||||
sid = 133
|
sid=133,
|
||||||
amplitude = 10
|
amplitude=10,
|
||||||
base_price = 50
|
base_price=50,
|
||||||
self.zipline_test_config['order_count'] = trade_count - 1
|
offset=offset,
|
||||||
self.zipline_test_config['trade_count'] = trade_count
|
trade_count=5,
|
||||||
self.zipline_test_config['simulation_style'] = \
|
simulate=True
|
||||||
SIMULATION_STYLE.FIXED_SLIPPAGE
|
|
||||||
trading_environment = factory.create_trading_environment()
|
|
||||||
source = create_updown_trade_source(sid,
|
|
||||||
trade_count,
|
|
||||||
trading_environment,
|
|
||||||
base_price,
|
|
||||||
amplitude
|
|
||||||
)
|
)
|
||||||
|
|
||||||
algo = BuySellAlgorithm(sid, 100, offset)
|
|
||||||
self.zipline_test_config['algorithm'] = algo
|
|
||||||
self.zipline_test_config['trade_source'] = source
|
|
||||||
self.zipline_test_config['environment'] = trading_environment
|
|
||||||
zipline = SimulatedTrading.create_test_zipline(**self.zipline_test_config)
|
|
||||||
zipline.simulate(blocking=True)
|
|
||||||
#function is getting minimized, so have to return negative cum returns.
|
#function is getting minimized, so have to return negative cum returns.
|
||||||
return -zipline.get_cumulative_performance()['returns']
|
return -zipline.get_cumulative_performance()['returns']
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ from zipline.utils.factory import get_next_trading_dt, create_trading_environmen
|
|||||||
from zipline.finance.sources import SpecificEquityTrades
|
from zipline.finance.sources import SpecificEquityTrades
|
||||||
from zipline.optimize.algorithms import BuySellAlgorithm
|
from zipline.optimize.algorithms import BuySellAlgorithm
|
||||||
from zipline.lines import SimulatedTrading
|
from zipline.lines import SimulatedTrading
|
||||||
|
from zipline.finance.trading import SIMULATION_STYLE
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
@@ -47,20 +49,27 @@ def create_updown_trade_source(sid, trade_count, trading_environment, start_pric
|
|||||||
return source
|
return source
|
||||||
|
|
||||||
|
|
||||||
def create_predictable_zipline(config, sid=133, amplitude=10, base_price=50, offset=0):
|
def create_predictable_zipline(config, sid=133, amplitude=10, base_price=50, offset=0, trade_count=3, simulate=True):
|
||||||
config = deepcopy(config)
|
#config = deepcopy(config)
|
||||||
trading_environment = create_trading_environment()
|
trading_environment = create_trading_environment()
|
||||||
source = create_updown_trade_source(sid,
|
source = create_updown_trade_source(sid,
|
||||||
config['trade_count'],
|
trade_count,
|
||||||
trading_environment,
|
trading_environment,
|
||||||
base_price,
|
base_price,
|
||||||
amplitude)
|
amplitude)
|
||||||
|
|
||||||
algo = BuySellAlgorithm(sid, 100, offset)
|
if 'algorithm' not in config:
|
||||||
config['algorithm'] = algo
|
config['algorithm'] = BuySellAlgorithm(sid, 100, offset)
|
||||||
|
|
||||||
|
config['order_count'] = trade_count - 1
|
||||||
|
config['trade_count'] = trade_count
|
||||||
config['trade_source'] = source
|
config['trade_source'] = source
|
||||||
config['environment'] = trading_environment
|
config['environment'] = trading_environment
|
||||||
zipline = SimulatedTrading.create_test_zipline(**config)
|
config['simulation_style'] = SIMULATION_STYLE.FIXED_SLIPPAGE
|
||||||
zipline.simulate(blocking=True)
|
|
||||||
|
|
||||||
return zipline
|
zipline = SimulatedTrading.create_test_zipline(**config)
|
||||||
|
|
||||||
|
if simulate:
|
||||||
|
zipline.simulate(blocking=True)
|
||||||
|
|
||||||
|
return zipline, config
|
||||||
|
|||||||
Reference in New Issue
Block a user