Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions wtpy/CodeHelper.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import re


class CodeHelper:

@staticmethod
def isStdChnFutOptCode(stdCode:str) -> bool:
def isStdChnFutOptCode(stdCode: str) -> bool:
pattern = re.compile("^[A-Z]+.[A-z]+\\d{4}.(C|P).\\d+$")
if re.match(pattern, stdCode) is not None:
return True

return False

@staticmethod
def stdCodeToStdCommID(stdCode:str) -> str:
def stdCodeToStdCommID(stdCode: str) -> str:
ay = stdCode.split(".")
if not CodeHelper.isStdChnFutOptCode(stdCode):
return ay[0] + "." + ay[1]
Expand All @@ -24,4 +25,4 @@ def stdCodeToStdCommID(stdCode:str) -> str:
elif exchg == 'CFFEX':
return exchg + "." + pid
else:
return exchg + "." + pid + '_o'
return exchg + "." + pid + '_o'
95 changes: 47 additions & 48 deletions wtpy/ContractMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@

from .ProductMgr import ProductMgr, ProductInfo


class ContractInfo:

def __init__(self):
self.exchg:str = '' #交易所
self.code:str = '' #合约代码
self.name:str = '' #合约名称
self.product:str = '' #品种代码
self.stdCode:str = '' #标准代码
self.exchg: str = '' # 交易所
self.code: str = '' # 合约代码
self.name: str = '' # 合约名称
self.product: str = '' # 品种代码
self.stdCode: str = '' # 标准代码

self.isOption:bool = False # 是否期权合约
self.underlying:str = '' # underlying
self.strikePrice:float = 0 # 行权价
self.underlyingScale:float = 0 # 放大倍数
self.isCall:bool = True # 是否看涨期权
self.isOption: bool = False # 是否期权合约
self.underlying: str = '' # underlying
self.strikePrice: float = 0 # 行权价
self.underlyingScale: float = 0 # 放大倍数
self.isCall: bool = True # 是否看涨期权

self.openDate:int = 19000101 # 上市日期
self.expireDate:int = 20991231 # 到期日
self.openDate: int = 19000101 # 上市日期
self.expireDate: int = 20991231 # 到期日

self.longMarginRatio:float = 0 # 多头保证金率
self.shortMarginRatio:float = 0 # 空头保证金率
self.longMarginRatio: float = 0 # 多头保证金率
self.shortMarginRatio: float = 0 # 空头保证金率


class ContractMgr:

def __init__(self, prodMgr:ProductMgr = None):
def __init__(self, prodMgr: ProductMgr = None):
self.__contracts__ = dict()
self.__underlyings__ = dict() # 期权专用
self.__products__ = dict() # 期权专用
self.__underlyings__ = dict() # 期权专用
self.__products__ = dict() # 期权专用
self.__prod_mgr__ = prodMgr

def load(self, fname:str):
'''
def load(self, fname: str):
"""
从文件加载品种信息
'''
"""
f = open(fname, 'rb')
content = f.read()
f.close()
Expand Down Expand Up @@ -71,8 +72,8 @@ def load(self, fname:str):
cInfo.shortMarginRatio = float(cObj["shortmarginratio"])

if "product" in cObj:
cInfo.product = cObj["product"]
#股票标准代码为SSE.000001,期货标准代码为SHFE.rb.2010
cInfo.product = cObj["product"]
# 股票标准代码为SSE.000001,期货标准代码为SHFE.rb.2010
if cInfo.code[:len(cInfo.product)] == cInfo.product:
month = cInfo.code[len(cInfo.product):]
if len(month) < 4:
Expand Down Expand Up @@ -107,7 +108,7 @@ def load(self, fname:str):
if "option" in cObj:
oObj = cObj["option"]
cInfo.isOption = True
cInfo.isCall = (int(oObj["optiontype"])==49)
cInfo.isCall = (int(oObj["optiontype"]) == 49)
cInfo.underlying = oObj["underlying"]
cInfo.strikePrice = float(oObj["strikeprice"])
cInfo.underlyingScale = float(oObj["underlyingscale"])
Expand All @@ -120,61 +121,59 @@ def load(self, fname:str):

self.__underlyings__[stdUnderlying].append(cInfo.stdCode)

def getContractInfo(self, stdCode:str, uDate:int = 0) -> ContractInfo:
'''
def getContractInfo(self, stdCode: str, uDate: int = 0) -> ContractInfo:
"""
获取合约信息
@stdCode 合约代码,格式如SHFE.rb.2305
'''
"""
if stdCode not in self.__contracts__:
return None
cInfo:ContractInfo = self.__contracts__[stdCode]

cInfo: ContractInfo = self.__contracts__[stdCode]
if uDate != 0 and (cInfo.openDate > uDate or cInfo.expireDate < uDate):
return None

return cInfo

def getTotalCodes(self, uDate:int = 0) -> list:
'''
def getTotalCodes(self, uDate: int = 0) -> list:
"""
获取全部合约代码列表
@uDate 交易日, 格式如20210101
'''
"""
codes = list()
for code in self.__contracts__:
cInfo:ContractInfo = self.__contracts__[code]
if uDate == 0 or (cInfo.openDate <= uDate and cInfo.expireDate >= uDate):
cInfo: ContractInfo = self.__contracts__[code]
if uDate == 0 or (cInfo.openDate <= uDate <= cInfo.expireDate):
codes.append(self.__contracts__[code].stdCode)
return codes
def getCodesByUnderlying(self, underlying:str, uDate:int = 0) -> list:
'''

def getCodesByUnderlying(self, underlying: str, uDate: int = 0) -> list:
"""
根据underlying读取合约列表
@underlying 格式如CFFEX.IM2304
@uDate 交易日, 格式如20210101
'''
"""
ret = list()
if underlying in self.__underlyings__:
codes = self.__underlyings__[underlying]
for code in codes:
cInfo:ContractInfo = self.__contracts__[code]
if uDate == 0 or (cInfo.openDate <= uDate and cInfo.expireDate >= uDate):
cInfo: ContractInfo = self.__contracts__[code]
if uDate == 0 or (cInfo.openDate <= uDate <= cInfo.expireDate):
ret.append(self.__contracts__[code].stdCode)

return ret
def getCodesByProduct(self, stdPID:str, uDate:int = 0) -> list:
'''

def getCodesByProduct(self, stdPID: str, uDate: int = 0) -> list:
"""
根据品种代码读取合约列表
@stdPID 品种代码, 格式如SHFE.rb
@uDate 交易日, 格式如20210101
'''
"""
ret = list()
if stdPID in self.__products__:
codes = self.__products__[stdPID]
for code in codes:
cInfo:ContractInfo = self.__contracts__[code]
if uDate == 0 or (cInfo.openDate <= uDate and cInfo.expireDate >= uDate):
cInfo: ContractInfo = self.__contracts__[code]
if uDate == 0 or (cInfo.openDate <= uDate <= cInfo.expireDate):
ret.append(self.__contracts__[code].stdCode)
return ret


Loading