mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-07 11:10:36 +08:00
ENH: Adds tick_size and renames futures multiplier
This commit is contained in:
@@ -649,7 +649,7 @@ class TestTransformAlgorithm(TestCase):
|
||||
cls.env = TradingEnvironment()
|
||||
cls.env.write_data(equities_identifiers=[0, 1, 133])
|
||||
|
||||
futures_metadata = {0: {'contract_multiplier': 10}}
|
||||
futures_metadata = {0: {'multiplier': 10}}
|
||||
cls.futures_env = TradingEnvironment()
|
||||
cls.futures_env.write_data(futures_data=futures_metadata)
|
||||
|
||||
@@ -1916,7 +1916,7 @@ class TestFutureFlip(TestCase):
|
||||
def test_flip_algo(self):
|
||||
metadata = {1: {'symbol': 'TEST',
|
||||
'end_date': self.days[3],
|
||||
'contract_multiplier': 5}}
|
||||
'multiplier': 5}}
|
||||
self.env.write_data(futures_data=metadata)
|
||||
|
||||
algo = FutureFlipAlgo(sid=1, amount=1, env=self.env,
|
||||
|
||||
@@ -280,7 +280,8 @@ class TestFuture(TestCase):
|
||||
notice_date=pd.Timestamp('2014-01-20', tz='UTC'),
|
||||
expiration_date=pd.Timestamp('2014-02-20', tz='UTC'),
|
||||
auto_close_date=pd.Timestamp('2014-01-18', tz='UTC'),
|
||||
contract_multiplier=500
|
||||
tick_size=.01,
|
||||
multiplier=500
|
||||
)
|
||||
cls.future2 = Future(
|
||||
0,
|
||||
@@ -311,7 +312,8 @@ class TestFuture(TestCase):
|
||||
in reprd)
|
||||
self.assertTrue("auto_close_date=Timestamp('2014-01-18 00:00:00+0000'"
|
||||
in reprd)
|
||||
self.assertTrue("contract_multiplier=500" in reprd)
|
||||
self.assertTrue("tick_size=0.01" in reprd)
|
||||
self.assertTrue("multiplier=500" in reprd)
|
||||
|
||||
def test_reduce(self):
|
||||
reduced = self.future.__reduce__()
|
||||
@@ -323,7 +325,8 @@ class TestFuture(TestCase):
|
||||
self.assertTrue('notice_date' in dictd)
|
||||
self.assertTrue('expiration_date' in dictd)
|
||||
self.assertTrue('auto_close_date' in dictd)
|
||||
self.assertTrue('contract_multiplier' in dictd)
|
||||
self.assertTrue('tick_size' in dictd)
|
||||
self.assertTrue('multiplier' in dictd)
|
||||
|
||||
from_dict = Future.from_dict(dictd)
|
||||
self.assertTrue(isinstance(from_dict, Future))
|
||||
|
||||
@@ -2551,8 +2551,8 @@ class TestPositionTracker(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = TradingEnvironment()
|
||||
futures_metadata = {3: {'contract_multiplier': 1000},
|
||||
4: {'contract_multiplier': 1000}}
|
||||
futures_metadata = {3: {'multiplier': 1000},
|
||||
4: {'multiplier': 1000}}
|
||||
cls.env.write_data(equities_identifiers=[1, 2],
|
||||
futures_data=futures_metadata)
|
||||
|
||||
|
||||
@@ -855,7 +855,7 @@ class TradingAlgorithm(object):
|
||||
return 0
|
||||
|
||||
if isinstance(asset, Future):
|
||||
value_multiplier = asset.contract_multiplier
|
||||
value_multiplier = asset.multiplier
|
||||
else:
|
||||
value_multiplier = 1
|
||||
|
||||
|
||||
+17
-11
@@ -37,7 +37,7 @@ cimport numpy as np
|
||||
# IMPORTANT NOTE: You must change this template if you change
|
||||
# Asset.__reduce__, or else we'll attempt to unpickle an old version of this
|
||||
# class
|
||||
CACHE_FILE_TEMPLATE = '/tmp/.%s-%s.v4.cache'
|
||||
CACHE_FILE_TEMPLATE = '/tmp/.%s-%s.v5.cache'
|
||||
|
||||
cdef class Asset:
|
||||
|
||||
@@ -228,7 +228,8 @@ cdef class Future(Asset):
|
||||
cdef readonly object notice_date
|
||||
cdef readonly object expiration_date
|
||||
cdef readonly object auto_close_date
|
||||
cdef readonly float contract_multiplier
|
||||
cdef readonly object tick_size
|
||||
cdef readonly float multiplier
|
||||
|
||||
def __cinit__(self,
|
||||
int sid, # sid is required
|
||||
@@ -242,13 +243,15 @@ cdef class Future(Asset):
|
||||
object auto_close_date=None,
|
||||
object first_traded=None,
|
||||
object exchange="",
|
||||
float contract_multiplier=1):
|
||||
object tick_size="",
|
||||
float multiplier=1):
|
||||
|
||||
self.root_symbol = root_symbol
|
||||
self.notice_date = notice_date
|
||||
self.expiration_date = expiration_date
|
||||
self.auto_close_date = auto_close_date
|
||||
self.contract_multiplier = contract_multiplier
|
||||
self.root_symbol = root_symbol
|
||||
self.notice_date = notice_date
|
||||
self.expiration_date = expiration_date
|
||||
self.auto_close_date = auto_close_date
|
||||
self.tick_size = tick_size
|
||||
self.multiplier = multiplier
|
||||
|
||||
def __str__(self):
|
||||
if self.symbol:
|
||||
@@ -259,7 +262,8 @@ cdef class Future(Asset):
|
||||
def __repr__(self):
|
||||
attrs = ('symbol', 'root_symbol', 'asset_name', 'exchange',
|
||||
'start_date', 'end_date', 'first_traded', 'notice_date',
|
||||
'expiration_date', 'auto_close_date', 'contract_multiplier')
|
||||
'expiration_date', 'auto_close_date', 'tick_size',
|
||||
'multiplier')
|
||||
tuples = ((attr, repr(getattr(self, attr, None)))
|
||||
for attr in attrs)
|
||||
strings = ('%s=%s' % (t[0], t[1]) for t in tuples)
|
||||
@@ -284,7 +288,8 @@ cdef class Future(Asset):
|
||||
self.auto_close_date,
|
||||
self.first_traded,
|
||||
self.exchange,
|
||||
self.contract_multiplier,))
|
||||
self.tick_size,
|
||||
self.multiplier,))
|
||||
|
||||
cpdef to_dict(self):
|
||||
"""
|
||||
@@ -295,7 +300,8 @@ cdef class Future(Asset):
|
||||
super_dict['notice_date'] = self.notice_date
|
||||
super_dict['expiration_date'] = self.expiration_date
|
||||
super_dict['auto_close_date'] = self.auto_close_date
|
||||
super_dict['contract_multiplier'] = self.contract_multiplier
|
||||
super_dict['tick_size'] = self.tick_size
|
||||
super_dict['multiplier'] = self.multiplier
|
||||
return super_dict
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import sqlalchemy as sa
|
||||
# Define a version number for the database generated by these writers
|
||||
# Increment this version number any time a change is made to the schema of the
|
||||
# assets database
|
||||
ASSET_DB_VERSION = 0
|
||||
ASSET_DB_VERSION = 1
|
||||
|
||||
|
||||
def generate_asset_db_metadata(bind=None):
|
||||
@@ -120,7 +120,8 @@ def _futures_contracts_schema(metadata):
|
||||
sa.Column('notice_date', sa.Integer, nullable=False),
|
||||
sa.Column('expiration_date', sa.Integer, nullable=False),
|
||||
sa.Column('auto_close_date', sa.Integer, nullable=False),
|
||||
sa.Column('contract_multiplier', sa.Float),
|
||||
sa.Column('multiplier', sa.Float),
|
||||
sa.Column('tick_size', sa.Float),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -60,7 +60,8 @@ _futures_defaults = {
|
||||
'notice_date': None,
|
||||
'expiration_date': None,
|
||||
'auto_close_date': None,
|
||||
'contract_multiplier': 1,
|
||||
'tick_size': None,
|
||||
'multiplier': 1,
|
||||
}
|
||||
|
||||
# Default values for the exchanges DataFrame
|
||||
|
||||
@@ -156,8 +156,7 @@ class PositionTracker(object):
|
||||
self._position_exposure_multipliers[sid] = 1
|
||||
if isinstance(asset, Future):
|
||||
self._position_value_multipliers[sid] = 0
|
||||
self._position_exposure_multipliers[sid] = \
|
||||
asset.contract_multiplier
|
||||
self._position_exposure_multipliers[sid] = asset.multiplier
|
||||
# Futures auto-close timing is controlled by the Future's
|
||||
# auto_close_date property
|
||||
self._insert_auto_close_position_date(
|
||||
|
||||
@@ -343,7 +343,7 @@ class TestOrderValueAlgorithm(TradingAlgorithm):
|
||||
|
||||
multiplier = 2.
|
||||
if isinstance(self.sid(0), Future):
|
||||
multiplier *= self.sid(0).contract_multiplier
|
||||
multiplier *= self.sid(0).multiplier
|
||||
|
||||
self.order_value(self.sid(0), data[0].price * multiplier)
|
||||
|
||||
@@ -391,7 +391,7 @@ class TestOrderPercentAlgorithm(TradingAlgorithm):
|
||||
if isinstance(self.sid(0), Future):
|
||||
self.target_shares += np.floor(
|
||||
(.001 * self.portfolio.portfolio_value) /
|
||||
(data[0].price * self.sid(0).contract_multiplier)
|
||||
(data[0].price * self.sid(0).multiplier)
|
||||
)
|
||||
|
||||
|
||||
@@ -439,7 +439,7 @@ class TestTargetValueAlgorithm(TradingAlgorithm):
|
||||
self.target_shares = np.round(20 / data[0].price)
|
||||
if isinstance(self.sid(0), Future):
|
||||
self.target_shares = np.round(
|
||||
20 / (data[0].price * self.sid(0).contract_multiplier))
|
||||
20 / (data[0].price * self.sid(0).multiplier))
|
||||
|
||||
|
||||
class FutureFlipAlgo(TestAlgorithm):
|
||||
|
||||
@@ -388,7 +388,7 @@ def make_future_info(first_sid,
|
||||
'start_date': start_date_func(month_begin),
|
||||
'notice_date': notice_date_func(month_begin),
|
||||
'expiration_date': notice_date_func(month_begin),
|
||||
'contract_multiplier': 500,
|
||||
'multiplier': 500,
|
||||
})
|
||||
return pd.DataFrame.from_records(contracts, index='sid').convert_objects()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user