mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-24 13:00:57 +08:00
125 lines
4.3 KiB
Python
125 lines
4.3 KiB
Python
|
|
"""
|
|
Factory functions to prepare useful data for optimize tests.
|
|
|
|
Author: Thomas V. Wiecki (thomas.wiecki@gmail.com), 2012
|
|
"""
|
|
from datetime import timedelta
|
|
import pandas as pd
|
|
from copy import copy
|
|
from itertools import cycle
|
|
|
|
import zipline.protocol as zp
|
|
|
|
from zipline.utils.factory import get_next_trading_dt, create_trading_environment
|
|
from zipline.gens.tradegens import SpecificEquityTrades, DataFrameSource
|
|
from zipline.optimize.algorithms import BuySellAlgorithmNew
|
|
from zipline.lines import SimulatedTrading
|
|
from zipline.finance.trading import SIMULATION_STYLE
|
|
|
|
def create_updown_trade_source(sid, trade_count, trading_environment, base_price, amplitude):
|
|
"""Create the updown trade source. This source emits events with
|
|
the price going up and down by the same amount in each
|
|
iteration. The trade source is thus perfectly predictable. This is
|
|
used for a test case for the optimization code.
|
|
|
|
:Arguments:
|
|
sid : int
|
|
SID of stock to create.
|
|
trade_count : int
|
|
How many trade events to create (will also influence order count)
|
|
trading_environment : TradeEnvironment object
|
|
The trading environment to use (see zipline.factory.create_trading_environment)
|
|
base_price : int
|
|
The average price that each iteration will hover around.
|
|
amplitude : int
|
|
How much the price will go up and down each iteration.
|
|
|
|
:Returns:
|
|
source : SpecificEquityTrades
|
|
The trade source emitting up down events.
|
|
"""
|
|
price = base_price-amplitude/2.
|
|
|
|
cur = trading_environment.first_open
|
|
one_day = timedelta(days = 1)
|
|
|
|
#create iterator to cycle through up and down phases
|
|
change = cycle([1,-1])
|
|
prices = []
|
|
dts = []
|
|
for i in xrange(trade_count + 2):
|
|
cur = get_next_trading_dt(cur, one_day, trading_environment)
|
|
dts.append(cur)
|
|
prices.append(price)
|
|
|
|
price += change.next()*amplitude
|
|
|
|
df = pd.DataFrame(index=dts, data=prices, columns=[0])
|
|
|
|
return df
|
|
|
|
|
|
def create_predictable_zipline(config, offset=0):
|
|
"""Create a test zipline object as specified by config. The
|
|
zipline will use the UpDown tradesource which is perfectly
|
|
predictable.
|
|
|
|
Trade source parameters can be specified inside the config object.
|
|
|
|
:Trade source arguments:
|
|
config['sid'] : int
|
|
SID of stock to create.
|
|
config['amplitude'] : int (default 10)
|
|
How much the price will go up and down each iteration.
|
|
config['base_price'] : int (default 50)
|
|
The average price that each iteration will hover around.
|
|
config['trade_count'] : int (default 3)
|
|
How many trade events to create (will also influence order count)
|
|
|
|
If not specified, the BuySellAlgorithm is used by default. This
|
|
can be changed by setting config['algorithm'].
|
|
|
|
:Arguments:
|
|
offset : int (default 0)
|
|
The offset parameter specifies how much the BuySellAlgorithm will
|
|
order each iteration and is a negative quadratic centered around
|
|
0. Thus, any deviations from 0 will lead to less buy and sell
|
|
orders each iteration and ultimately to less compound returns.
|
|
simulate : bool (default True)
|
|
Whether to call .simulate(blocking=True) on the created zipline
|
|
argument.
|
|
|
|
:Returns:
|
|
zipline : class zipline
|
|
created zipline object
|
|
config : dict
|
|
the config dict used to create the zipline
|
|
"""
|
|
config = copy(config)
|
|
sid = config['sid']
|
|
# remove
|
|
amplitude = config.pop('amplitude', 10)
|
|
base_price = config.pop('base_price', 50)
|
|
trade_count = config.pop('trade_count', 3)
|
|
|
|
trading_environment = create_trading_environment()
|
|
source = create_updown_trade_source(sid,
|
|
trade_count,
|
|
trading_environment,
|
|
base_price,
|
|
amplitude)
|
|
|
|
if 'algorithm' not in config:
|
|
algorithm = BuySellAlgorithmNew(sid, 100, offset)
|
|
|
|
config['order_count'] = trade_count - 1
|
|
config['trade_count'] = trade_count
|
|
config['trade_source'] = source
|
|
config['environment'] = trading_environment
|
|
config['simulation_style'] = SIMULATION_STYLE.FIXED_SLIPPAGE
|
|
|
|
return algorithm, config
|
|
|
|
|