Skip to content

Commit

Permalink
Merge pull request #55 from ricequant/feature/commission_margin_acces…
Browse files Browse the repository at this point in the history
…s_way

now rqalpha can get future commission and margin from data source if …
  • Loading branch information
wh1100717 committed Mar 7, 2017
2 parents c2efde7 + 75248bd commit de564fa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions rqalpha/data/data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from ..model.bar import BarObject
from ..model.snapshot import SnapshotObject
from ..utils.datetime_func import convert_int_to_datetime
from ..const import HEDGE_TYPE


class DataProxy(InstrumentMixin, TradingDatesMixin):
Expand Down Expand Up @@ -146,3 +147,6 @@ def current_snapshot(self, order_book_id, frequency, dt):

def available_data_range(self, frequency):
return self._data_source.available_data_range(frequency)

def get_future_info(self, order_book_id, hedge_type=HEDGE_TYPE.SPECULATION):
return self._data_source.get_future_info(order_book_id, hedge_type)
21 changes: 21 additions & 0 deletions rqalpha/execution_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from .utils.i18n import gettext as _
from .utils.exception import CustomException, patch_user_exc
from .utils import get_upper_underlying_symbol
from .utils.default_future_info import DEFAULT_FUTURE_INFO


class ContextStack(object):
Expand Down Expand Up @@ -150,3 +152,22 @@ def get_current_close_price(cls, order_book_id):
ExecutionContext.config.base.frequency,
ExecutionContext.calendar_dt
).last

@classmethod
def get_future_commission_info(cls, order_book_id, hedge_type):
try:
return ExecutionContext.data_proxy.get_future_info(order_book_id, hedge_type)
except NotImplementedError:
underlying_symbol = get_upper_underlying_symbol(order_book_id)
return DEFAULT_FUTURE_INFO[underlying_symbol][hedge_type.value]

@classmethod
def get_future_margin(cls, order_book_id):
try:
return ExecutionContext.data_proxy.get_future_info(order_book_id)['long_margin_ratio']
except NotImplementedError:
return ExecutionContext.data_proxy.instruments(order_book_id).margin_rate

@classmethod
def get_future_info(cls, order_book_id, hedge_type):
return ExecutionContext.data_proxy.get_future_info(order_book_id, hedge_type)
10 changes: 10 additions & 0 deletions rqalpha/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ def available_data_range(self, frequency):
"""
raise NotImplementedError

def get_future_info(self, order_book_id, hedge_type):
"""
获取期货合约手续费、保证金等数据
:param str order_book_id: 合约名
:param HEDGE_TYPE hedge_type: 枚举类型,账户对冲类型
:return: dict
"""
raise NotImplementedError


class AbstractBroker(with_metaclass(abc.ABCMeta)):
"""
Expand Down
3 changes: 1 addition & 2 deletions rqalpha/model/commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def __init__(self, multiplier, hedge_type=HEDGE_TYPE.SPECULATION):
def get_commission(self, trade):
order = trade.order
order_book_id = order.order_book_id
underlying_symbol = get_upper_underlying_symbol(order_book_id)
info = DEFAULT_FUTURE_INFO[underlying_symbol][self.hedge_type.value]
info = ExecutionContext.get_future_commission_info(order_book_id, self.hedge_type)
commission = 0
if info['commission_type'] == COMMISSION_TYPE.BY_MONEY:
contract_multiplier = ExecutionContext.get_instrument(order.order_book_id).contract_multiplier
Expand Down
4 changes: 2 additions & 2 deletions rqalpha/model/margin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ def __init__(self, multiplier):
self.multiplier = multiplier

def cal_margin(self, order_book_id, side, value):
instrument = ExecutionContext.get_instrument(order_book_id)
return self.multiplier * value * instrument.margin_rate
margin_rate = ExecutionContext.get_future_margin(order_book_id)
return self.multiplier * value * margin_rate

0 comments on commit de564fa

Please sign in to comment.