Skip to content

Commit

Permalink
update instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuizi7 committed Dec 19, 2018
1 parent 0126c4b commit 8261987
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion rqalpha/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def all_instruments(type=None, date=None):

result = env.data_proxy.all_instruments(types, dt)
if types is not None and len(types) == 1:
return pd.DataFrame([i._ins_dict for i in result])
return pd.DataFrame([i.__dict__ for i in result])

return pd.DataFrame(
[
Expand Down
62 changes: 28 additions & 34 deletions rqalpha/model/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import datetime

import six
import numpy as np

from rqalpha.environment import Environment
Expand All @@ -40,91 +39,86 @@ def _fix_date(ds, dflt):
__repr__ = property_repr

def __init__(self, dic):
self._ins_dict = dic
self.__dict__ = dic

if "listed_date" in dic:
self._ins_dict["listed_date"] = self._fix_date(dic["listed_date"], self.DEFAULT_LISTED_DATE)
self.__dict__["listed_date"] = self._fix_date(dic["listed_date"], self.DEFAULT_LISTED_DATE)
if "de_listed_date" in dic:
self._ins_dict["de_listed_date"] = self._fix_date(dic["de_listed_date"], self.DEFAULT_DE_LISTED_DATE)
if "maturity_date" in self._ins_dict:
self._ins_dict["maturity_date"] = self._fix_date(dic["maturity_date"], self.DEFAULT_DE_LISTED_DATE)
self.__dict__["de_listed_date"] = self._fix_date(dic["de_listed_date"], self.DEFAULT_DE_LISTED_DATE)
if "maturity_date" in self.__dict__:
self.__dict__["maturity_date"] = self._fix_date(dic["maturity_date"], self.DEFAULT_DE_LISTED_DATE)

if 'contract_multiplier' in dic:
if np.isnan(self.contract_multiplier):
raise RuntimeError("Contract multiplier of {} is not supposed to be nan".format(self.order_book_id))

def __getattr__(self, item):
try:
return super(Instrument, self).__getattribute__("_ins_dict")[item]
except KeyError:
raise AttributeError(item)

@property
def order_book_id(self):
"""
[str] 股票:证券代码,证券的独特的标识符。应以’.XSHG’或’.XSHE’结尾,前者代表上证,后者代表深证。
期货:期货代码,期货的独特的标识符(郑商所期货合约数字部分进行了补齐。例如原有代码’ZC609’补齐之后变为’ZC1609’)。
主力连续合约UnderlyingSymbol+88,例如’IF88’ ;指数连续合约命名规则为UnderlyingSymbol+99
"""
return self._ins_dict["order_book_id"]
# print(self.__dict__)
return self.__dict__["order_book_id"]

@property
def symbol(self):
"""
[str] 股票:证券的简称,例如’平安银行’。期货:期货的简称,例如’沪深1005’。
"""
return self._ins_dict["symbol"]
return self.__dict__["symbol"]

@property
def round_lot(self):
"""
[int] 股票:一手对应多少股,中国A股一手是100股。期货:一律为1。
"""
return self._ins_dict["round_lot"]
return self.__dict__["round_lot"]

@property
def listed_date(self):
"""
[datetime] 股票:该证券上市日期。期货:期货的上市日期,主力连续合约与指数连续合约都为 datetime(1990, 1, 1)。
"""
return self._ins_dict["listed_date"]
return self.__dict__["listed_date"]

@property
def de_listed_date(self):
"""
[datetime] 股票:退市日期。期货:交割日期。
"""
return self._ins_dict["de_listed_date"]
return self.__dict__["de_listed_date"]

@property
def type(self):
"""
[sty] 合约类型,目前支持的类型有: ‘CS’, ‘INDX’, ‘LOF’, ‘ETF’, ‘FenjiMu’, ‘FenjiA’, ‘FenjiB’, ‘Future’
"""
return self._ins_dict["type"]
return self.__dict__["type"]

@property
def exchange(self):
"""
[str] 交易所。股票:’XSHE’ - 深交所, ‘XSHG’ - 上交所。期货:’DCE’ - 大连商品交易所, ‘SHFE’ - 上海期货交易所,
’CFFEX’ - 中国金融期货交易所, ‘CZCE’- 郑州商品交易所
"""
return self._ins_dict["exchange"]
return self.__dict__["exchange"]

@property
def market_tplus(self):
"""
[int] 合约卖出和买入操作需要间隔的最小交易日数,如A股为 1
"""
return self._ins_dict["market_tplus"]
return self.__dict__["market_tplus"]

@property
def sector_code(self):
"""
[str] 板块缩写代码,全球通用标准定义(股票专用)
"""
try:
return self._ins_dict["sector_code"]
return self.__dict__["sector_code"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'sector_code' ".format(self.order_book_id)
Expand All @@ -136,7 +130,7 @@ def sector_code_name(self):
[str] 以当地语言为标准的板块代码名(股票专用)
"""
try:
return self._ins_dict["sector_code_name"]
return self.__dict__["sector_code_name"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'sector_code_name' ".format(self.order_book_id)
Expand All @@ -148,7 +142,7 @@ def industry_code(self):
[str] 国民经济行业分类代码,具体可参考“Industry列表” (股票专用)
"""
try:
return self._ins_dict["industry_code"]
return self.__dict__["industry_code"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'industry_code' ".format(self.order_book_id)
Expand All @@ -160,7 +154,7 @@ def industry_name(self):
[str] 国民经济行业分类名称(股票专用)
"""
try:
return self._ins_dict["industry_name"]
return self.__dict__["industry_name"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'industry_name' ".format(self.order_book_id)
Expand All @@ -172,7 +166,7 @@ def concept_names(self):
[str] 概念股分类,例如:’铁路基建’,’基金重仓’等(股票专用)
"""
try:
return self._ins_dict["concept_names"]
return self.__dict__["concept_names"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'concept_names' ".format(self.order_book_id)
Expand All @@ -184,7 +178,7 @@ def board_type(self):
[str] 板块类别,’MainBoard’ - 主板,’GEM’ - 创业板(股票专用)
"""
try:
return self._ins_dict["board_type"]
return self.__dict__["board_type"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'board_type' ".format(self.order_book_id)
Expand All @@ -197,7 +191,7 @@ def status(self):
‘PreIPO’ - 发行配售期间, ‘FailIPO’ - 发行失败(股票专用)
"""
try:
return self._ins_dict["status"]
return self.__dict__["status"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'status' ".format(self.order_book_id)
Expand All @@ -210,7 +204,7 @@ def special_type(self):
‘PT’ - 代表该股票连续3年收入为负,将被暂停交易, ‘Other’ - 其他(股票专用)
"""
try:
return self._ins_dict["special_type"]
return self.__dict__["special_type"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'special_type' ".format(self.order_book_id)
Expand All @@ -222,7 +216,7 @@ def contract_multiplier(self):
[float] 合约乘数,例如沪深300股指期货的乘数为300.0(期货专用)
"""
try:
return self._ins_dict["contract_multiplier"]
return self.__dict__["contract_multiplier"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'contract_multiplier' ".format(self.order_book_id)
Expand All @@ -234,7 +228,7 @@ def margin_rate(self):
[float] 合约最低保证金率(期货专用)
"""
try:
return self._ins_dict["margin_rate"]
return self.__dict__["margin_rate"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'margin_rate' ".format(self.order_book_id)
Expand All @@ -246,7 +240,7 @@ def underlying_order_book_id(self):
[str] 合约标的代码,目前除股指期货(IH, IF, IC)之外的期货合约,这一字段全部为’null’(期货专用)
"""
try:
return self._ins_dict["underlying_order_book_id"]
return self.__dict__["underlying_order_book_id"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'underlying_order_book_id' ".format(self.order_book_id)
Expand All @@ -258,7 +252,7 @@ def underlying_symbol(self):
[str] 合约标的代码,目前除股指期货(IH, IF, IC)之外的期货合约,这一字段全部为’null’(期货专用)
"""
try:
return self._ins_dict["underlying_symbol"]
return self.__dict__["underlying_symbol"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'underlying_symbol' ".format(self.order_book_id)
Expand All @@ -270,7 +264,7 @@ def maturity_date(self):
[datetime] 期货到期日。主力连续合约与指数连续合约都为 datetime(2999, 12, 31)(期货专用)
"""
try:
return self._ins_dict["maturity_date"]
return self.__dict__["maturity_date"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'maturity_date' ".format(self.order_book_id)
Expand All @@ -282,7 +276,7 @@ def settlement_method(self):
[str] 交割方式,’CashSettlementRequired’ - 现金交割, ‘PhysicalSettlementRequired’ - 实物交割(期货专用)
"""
try:
return self._ins_dict["settlement_method"]
return self.__dict__["settlement_method"]
except (KeyError, ValueError):
raise AttributeError(
"Instrument(order_book_id={}) has no attribute 'settlement_method' ".format(self.order_book_id)
Expand Down
Binary file not shown.

0 comments on commit 8261987

Please sign in to comment.