VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 420
声望: 181

1. 主要函数实现

from typing import List,Dict,Tuple,Optional,Sequence
from vnpy.trader.object import ContractData
from datetime import datetime,timedelta
import rqdatac as rq

def left_alphas(instr:str):
    """
    得到字符串左边的字符部分
    """
    ret_str = ''
    for s in instr:
        if s.isalpha():
            ret_str += s
        else:
            break
    return ret_str

def get_contract_kinds(contracts:List[ContractData]):
    """
    从合约列表中获取合约的品种和品种列表
    """
    kinds:Dict[str,List[ContractData]] = {}
    for contract in contracts:
        vt_symbol = contract.vt_symbol
        kind = left_alphas(vt_symbol)
        if kind not in kinds:
            kinds[kind] = [vt_symbol]
        else:
            if vt_symbol not in kinds[kind]:
                kinds[kind].append(vt_symbol)

    return kinds


def get_all_dominants(kinds:List[str], which_day:datetime=None):
    """ 主力合约排序  """
    if not which_day:
        which_day = datetime.today()-timedelta(days=3)

    dominants1 = []    
    for kind in kinds:    
        # 查询指定日期的合约品种的主力合约
        df = rq.futures.get_dominant(underlying_symbol=kind.upper(),start_date=which_day)
        vt_symbol = df.values[0]        
        # 查询主力合约在指定交易日的成交额度   
        df = rq.get_price(vt_symbol,frequency='1d',start_date=which_day,end_date=which_day)
        total_turnover = df['total_turnover'].values[0]

        dominants1.append({"kind":kind,"vt_symbol":vt_symbol,"total_turnover":total_turnover})

    # 按照成交额度降序排序  
    dominants = sorted(dominants1,key=lambda x:x["total_turnover"],reverse=True)
    return dominants

2. 找当前市场的所有正在交易的合约品种及主力合约

在vnpy\trader\ui\main_window.py中增加一个菜单,然后为其增加下面的处理函数:

    def query_contracts(self) -> None:  # hxxjava add
        """
        Query contracts test.
        """
        oms_engine:SamEngine = self.main_engine.get_engine('oms')
        contracts = oms_engine.contracts.values()
        # for row,contract in enumerate(contracts):
        #     print(f"【{row}】   {contract}")


        contract_kinds = get_contract_kinds(contracts)
        print("==============全市场品种及在交易合约列表========================")
        for kind,vt_symbols in contract_kinds.items():
            print(f"kind={kind} vt_symbols={vt_symbols}")

        kinds = [kind for kind in contract_kinds]
        dominants = get_all_dominants(kinds)
        print("=====================全市场主力合约排序========================")        
        for dominant in dominants:
            print(f"{dominant}")

3. 测试结果:

3.1 全市场品种及在交易合约列表

==============全市场品种及在交易合约列表========================
kind=CF vt_symbols=['CF109.CZCE', 'CF105.CZCE', 'CF107.CZCE', 'CF101.CZCE', 'CF103.CZCE', 'CF011.CZCE']
kind=TA vt_symbols=['TA109.CZCE', 'TA105.CZCE', 'TA104.CZCE', 'TA107.CZCE', 'TA101.CZCE', 'TA106.CZCE', 'TA108.CZCE', 'TA102.CZCE', 'TA103.CZCE', 'TA012.CZCE', 'TA011.CZCE', 'TA010.CZCE']
kind=SR vt_symbols=['SR109.CZCE', 'SR105.CZCE', 'SR107.CZCE', 'SR101.CZCE', 'SR103.CZCE', 'SR011.CZCE']
kind=CY vt_symbols=['CY109.CZCE', 'CY105.CZCE', 'CY104.CZCE', 'CY107.CZCE', 'CY101.CZCE', 'CY106.CZCE', 'CY108.CZCE', 'CY102.CZCE', 'CY103.CZCE', 'CY012.CZCE', 'CY011.CZCE', 'CY010.CZCE']
kind=FG vt_symbols=['FG109.CZCE', 'FG105.CZCE', 'FG104.CZCE', 'FG107.CZCE', 'FG101.CZCE', 'FG106.CZCE', 'FG108.CZCE', 'FG102.CZCE', 'FG103.CZCE', 'FG012.CZCE', 'FG011.CZCE', 'FG010.CZCE']
kind=sc vt_symbols=['sc2109.INE', 'sc2106.INE', 'sc2303.INE', 'sc2101.INE', 'sc2309.INE', 'sc2102.INE', 'sc2107.INE', 'sc2104.INE', 'sc2306.INE', 'sc2105.INE', 'sc2011.INE', 'sc2108.INE', 'sc2212.INE', 'sc2209.INE', 'sc2103.INE', 'sc2112.INE', 'sc2010.INE', 'sc2206.INE', 'sc2203.INE', 'sc2012.INE']
kind=ag vt_symbols=['ag2103.SHFE', 'ag2102.SHFE', 'ag2104.SHFE', 'ag2101.SHFE', 'ag2105.SHFE', 'ag2106.SHFE', 'ag2108.SHFE', 'ag2109.SHFE', 'ag2107.SHFE', 'ag2011.SHFE', 'ag2010.SHFE', 'ag2012.SHFE']
kind=al vt_symbols=['al2103.SHFE', 'al2102.SHFE', 'al2104.SHFE', 'al2101.SHFE', 'al2105.SHFE', 'al2106.SHFE', 'al2108.SHFE', 'al2109.SHFE', 'al2107.SHFE', 'al2011.SHFE', 'al2010.SHFE', 'al2012.SHFE']
kind=au vt_symbols=['au2104.SHFE', 'au2102.SHFE', 'au2106.SHFE', 'au2011.SHFE', 'au2110.SHFE', 'au2108.SHFE', 'au2012.SHFE', 'au2010.SHFE']
kind=bu vt_symbols=['bu2203.SHFE', 'bu2010.SHFE', 'bu2011.SHFE', 'bu2206.SHFE', 'bu2102.SHFE', 'bu2209.SHFE', 'bu2101.SHFE', 'bu2112.SHFE', 'bu2109.SHFE', 'bu2103.SHFE', 'bu2106.SHFE', 'bu2012.SHFE']
kind=cu vt_symbols=['cu2103.SHFE', 'cu2102.SHFE', 'cu2104.SHFE', 'cu2101.SHFE', 'cu2105.SHFE', 'cu2106.SHFE', 'cu2108.SHFE', 'cu2109.SHFE', 'cu2107.SHFE', 'cu2011.SHFE', 'cu2010.SHFE', 'cu2012.SHFE']
kind=hc vt_symbols=['hc2103.SHFE', 'hc2102.SHFE', 'hc2104.SHFE', 'hc2101.SHFE', 'hc2105.SHFE', 'hc2106.SHFE', 'hc2108.SHFE', 'hc2109.SHFE', 'hc2107.SHFE', 'hc2011.SHFE', 'hc2010.SHFE', 'hc2012.SHFE']
kind=ni vt_symbols=['ni2103.SHFE', 'ni2102.SHFE', 'ni2104.SHFE', 'ni2101.SHFE', 'ni2105.SHFE', 'ni2106.SHFE', 'ni2108.SHFE', 'ni2109.SHFE', 'ni2107.SHFE', 'ni2011.SHFE', 'ni2010.SHFE', 'ni2012.SHFE']
kind=pb vt_symbols=['pb2103.SHFE', 'pb2102.SHFE', 'pb2104.SHFE', 'pb2101.SHFE', 'pb2105.SHFE', 'pb2106.SHFE', 'pb2108.SHFE', 'pb2109.SHFE', 'pb2107.SHFE', 'pb2011.SHFE', 'pb2010.SHFE', 'pb2012.SHFE']
kind=rb vt_symbols=['rb2103.SHFE', 'rb2102.SHFE', 'rb2104.SHFE', 'rb2101.SHFE', 'rb2105.SHFE', 'rb2106.SHFE', 'rb2108.SHFE', 'rb2109.SHFE', 'rb2107.SHFE', 'rb2010.SHFE', 'rb2011.SHFE', 'rb2012.SHFE']
kind=ru vt_symbols=['ru2103.SHFE', 'ru2104.SHFE', 'ru2101.SHFE', 'ru2105.SHFE', 'ru2106.SHFE', 'ru2108.SHFE', 'ru2109.SHFE', 'ru2107.SHFE', 'ru2010.SHFE', 'ru2011.SHFE']
kind=sn vt_symbols=['sn2103.SHFE', 'sn2102.SHFE', 'sn2104.SHFE', 'sn2101.SHFE', 'sn2105.SHFE', 'sn2106.SHFE', 'sn2108.SHFE', 'sn2109.SHFE', 'sn2107.SHFE', 'sn2010.SHFE', 'sn2011.SHFE', 'sn2012.SHFE']
kind=sp vt_symbols=['sp2103.SHFE', 'sp2102.SHFE', 'sp2104.SHFE', 'sp2101.SHFE', 'sp2105.SHFE', 'sp2106.SHFE', 'sp2108.SHFE', 'sp2109.SHFE', 'sp2107.SHFE', 'sp2010.SHFE', 'sp2011.SHFE', 'sp2012.SHFE']
kind=wr vt_symbols=['wr2103.SHFE', 'wr2102.SHFE', 'wr2104.SHFE', 'wr2101.SHFE', 'wr2105.SHFE', 'wr2106.SHFE', 'wr2108.SHFE', 'wr2109.SHFE', 'wr2107.SHFE', 'wr2010.SHFE', 'wr2011.SHFE', 'wr2012.SHFE']
kind=zn vt_symbols=['zn2103.SHFE', 'zn2102.SHFE', 'zn2104.SHFE', 'zn2101.SHFE', 'zn2105.SHFE', 'zn2106.SHFE', 'zn2108.SHFE', 'zn2109.SHFE', 'zn2107.SHFE', 'zn2010.SHFE', 'zn2011.SHFE', 'zn2012.SHFE']
kind=PM vt_symbols=['PM103.CZCE', 'PM105.CZCE', 'PM101.CZCE', 'PM107.CZCE', 'PM109.CZCE', 'PM011.CZCE']
kind=RI vt_symbols=['RI103.CZCE', 'RI105.CZCE', 'RI101.CZCE', 'RI107.CZCE', 'RI109.CZCE', 'RI011.CZCE']
kind=RM vt_symbols=['RM103.CZCE', 'RM105.CZCE', 'RM101.CZCE', 'RM107.CZCE', 'RM108.CZCE', 'RM109.CZCE', 'RM011.CZCE']
kind=SF vt_symbols=['SF103.CZCE', 'SF105.CZCE', 'SF104.CZCE', 'SF101.CZCE', 'SF106.CZCE', 'SF107.CZCE', 'SF108.CZCE', 'SF109.CZCE', 'SF102.CZCE', 'SF012.CZCE', 'SF011.CZCE', 'SF010.CZCE']
kind=SM vt_symbols=['SM103.CZCE', 'SM105.CZCE', 'SM104.CZCE', 'SM101.CZCE', 'SM106.CZCE', 'SM107.CZCE', 'SM108.CZCE', 'SM109.CZCE', 'SM102.CZCE', 'SM012.CZCE', 'SM011.CZCE', 'SM010.CZCE']
kind=WH vt_symbols=['WH103.CZCE', 'WH105.CZCE', 'WH101.CZCE', 'WH107.CZCE', 'WH109.CZCE', 'WH011.CZCE']
kind=a vt_symbols=['a2103.DCE', 'a2105.DCE', 'a2107.DCE', 'a2101.DCE', 'a2109.DCE', 'a2011.DCE']
kind=b vt_symbols=['b2103.DCE', 'b2105.DCE', 'b2104.DCE', 'b2107.DCE', 'b2106.DCE', 'b2101.DCE', 'b2108.DCE', 'b2109.DCE', 'b2102.DCE', 'b2012.DCE', 'b2011.DCE', 'b2010.DCE']
kind=bb vt_symbols=['bb2103.DCE', 'bb2105.DCE', 'bb2104.DCE', 'bb2107.DCE', 'bb2106.DCE', 'bb2101.DCE', 'bb2108.DCE', 'bb2109.DCE', 'bb2102.DCE', 'bb2012.DCE', 'bb2011.DCE', 'bb2010.DCE']
kind=c vt_symbols=['c2103.DCE', 'c2105.DCE', 'c2107.DCE', 'c2101.DCE', 'c2109.DCE', 'c2011.DCE']
kind=cs vt_symbols=['cs2103.DCE', 'cs2105.DCE', 'cs2107.DCE', 'cs2101.DCE', 'cs2109.DCE', 'cs2011.DCE']
kind=fb vt_symbols=['fb2103.DCE', 'fb2105.DCE', 'fb2104.DCE', 'fb2107.DCE', 'fb2106.DCE', 'fb2101.DCE', 'fb2108.DCE', 'fb2109.DCE', 'fb2102.DCE', 'fb2012.DCE', 'fb2011.DCE', 'fb2010.DCE']
kind=i vt_symbols=['i2103.DCE', 'i2105.DCE', 'i2104.DCE', 'i2107.DCE', 'i2106.DCE', 'i2101.DCE', 'i2108.DCE', 'i2010.DCE', 'i2109.DCE', 'i2102.DCE', 'i2012.DCE', 'i2011.DCE']
kind=j vt_symbols=['j2103.DCE', 'j2105.DCE', 'j2104.DCE', 'j2107.DCE', 'j2106.DCE', 'j2101.DCE', 'j2108.DCE', 'j2010.DCE', 'j2109.DCE', 'j2102.DCE', 'j2012.DCE', 'j2011.DCE']
kind=jm vt_symbols=['jm2103.DCE', 'jm2105.DCE', 'jm2104.DCE', 'jm2107.DCE', 'jm2106.DCE', 'jm2101.DCE', 'jm2108.DCE', 'jm2010.DCE', 'jm2109.DCE', 'jm2102.DCE', 'jm2012.DCE', 'jm2011.DCE']
kind=l vt_symbols=['l2103.DCE', 'l2105.DCE', 'l2104.DCE', 'l2107.DCE', 'l2101.DCE', 'l2106.DCE', 'l2108.DCE', 'l2010.DCE', 'l2109.DCE', 'l2102.DCE', 'l2012.DCE', 'l2011.DCE']
kind=m vt_symbols=['m2103.DCE', 'm2105.DCE', 'm2107.DCE', 'm2101.DCE', 'm2108.DCE', 'm2109.DCE', 'm2012.DCE', 'm2011.DCE']
kind=p vt_symbols=['p2103.DCE', 'p2105.DCE', 'p2104.DCE', 'p2107.DCE', 'p2101.DCE', 'p2106.DCE', 'p2108.DCE', 'p2010.DCE', 'p2109.DCE', 'p2102.DCE', 'p2012.DCE', 'p2011.DCE']
kind=pp vt_symbols=['pp2103.DCE', 'pp2105.DCE', 'pp2104.DCE', 'pp2107.DCE', 'pp2101.DCE', 'pp2106.DCE', 'pp2108.DCE', 'pp2010.DCE', 'pp2109.DCE', 'pp2102.DCE', 'pp2012.DCE', 'pp2011.DCE']
kind=v vt_symbols=['v2103.DCE', 'v2105.DCE', 'v2109.DCE', 'v2104.DCE', 'v2107.DCE', 'v2101.DCE', 'v2106.DCE', 'v2108.DCE', 'v2010.DCE', 'v2102.DCE', 'v2012.DCE', 'v2011.DCE']
kind=y vt_symbols=['y2103.DCE', 'y2105.DCE', 'y2109.DCE', 'y2107.DCE', 'y2101.DCE', 'y2108.DCE', 'y2012.DCE', 'y2011.DCE']
kind=IC vt_symbols=['IC2012.CFFEX', 'IC2103.CFFEX', 'IC2010.CFFEX', 'IC2011.CFFEX']
kind=IF vt_symbols=['IF2012.CFFEX', 'IF2103.CFFEX', 'IF2010.CFFEX', 'IF2011.CFFEX']
kind=IH vt_symbols=['IH2012.CFFEX', 'IH2103.CFFEX', 'IH2010.CFFEX', 'IH2011.CFFEX']
kind=ZC vt_symbols=['ZC105.CZCE', 'ZC109.CZCE', 'ZC106.CZCE', 'ZC104.CZCE', 'ZC102.CZCE', 'ZC103.CZCE', 'ZC107.CZCE', 'ZC108.CZCE', 'ZC101.CZCE', 'ZC011.CZCE', 'ZC010.CZCE', 'ZC012.CZCE']
kind=eg vt_symbols=['eg2106.DCE', 'eg2105.DCE', 'eg2101.DCE', 'eg2102.DCE', 'eg2104.DCE', 'eg2010.DCE', 'eg2103.DCE', 'eg2107.DCE', 'eg2108.DCE', 'eg2011.DCE', 'eg2012.DCE', 'eg2109.DCE']
kind=jd vt_symbols=['jd2106.DCE', 'jd2105.DCE', 'jd2101.DCE', 'jd2102.DCE', 'jd2104.DCE', 'jd2010.DCE', 'jd2103.DCE', 'jd2107.DCE', 'jd2108.DCE', 'jd2011.DCE', 'jd2012.DCE', 'jd2109.DCE']
kind=fu vt_symbols=['fu2103.SHFE', 'fu2101.SHFE', 'fu2109.SHFE', 'fu2102.SHFE', 'fu2107.SHFE', 'fu2104.SHFE', 'fu2106.SHFE', 'fu2105.SHFE', 'fu2011.SHFE', 'fu2108.SHFE', 'fu2012.SHFE', 'fu2010.SHFE']
kind=AP vt_symbols=['AP105.CZCE', 'AP107.CZCE', 'AP101.CZCE', 'AP103.CZCE', 'AP012.CZCE', 'AP011.CZCE', 'AP010.CZCE']
kind=JR vt_symbols=['JR105.CZCE', 'JR107.CZCE', 'JR101.CZCE', 'JR109.CZCE', 'JR103.CZCE', 'JR011.CZCE']
kind=LR vt_symbols=['LR105.CZCE', 'LR011.CZCE', 'LR101.CZCE', 'LR107.CZCE', 'LR109.CZCE', 'LR103.CZCE']
kind=MA vt_symbols=['MA105.CZCE', 'MA104.CZCE', 'MA101.CZCE', 'MA106.CZCE', 'MA107.CZCE', 'MA108.CZCE', 'MA109.CZCE', 'MA102.CZCE', 'MA103.CZCE', 'MA012.CZCE', 'MA011.CZCE', 'MA010.CZCE']
kind=OI vt_symbols=['OI105.CZCE', 'OI101.CZCE', 'OI107.CZCE', 'OI109.CZCE', 'OI103.CZCE', 'OI011.CZCE']
kind=eb vt_symbols=['eb2010.DCE', 'eb2011.DCE', 'eb2012.DCE', 'eb2101.DCE', 'eb2102.DCE', 'eb2103.DCE', 'eb2104.DCE', 'eb2105.DCE', 'eb2106.DCE', 'eb2107.DCE', 'eb2108.DCE', 'eb2109.DCE']
kind=rr vt_symbols=['rr2010.DCE', 'rr2011.DCE', 'rr2012.DCE', 'rr2101.DCE', 'rr2102.DCE', 'rr2103.DCE', 'rr2104.DCE', 'rr2105.DCE', 'rr2106.DCE', 'rr2107.DCE', 'rr2108.DCE', 'rr2109.DCE']
kind=pg vt_symbols=['pg2011.DCE', 'pg2012.DCE', 'pg2101.DCE', 'pg2102.DCE', 'pg2103.DCE', 'pg2104.DCE', 'pg2105.DCE', 'pg2106.DCE', 'pg2107.DCE', 'pg2108.DCE', 'pg2109.DCE']
kind=T vt_symbols=['T2103.CFFEX', 'T2106.CFFEX', 'T2012.CFFEX']
kind=TF vt_symbols=['TF2103.CFFEX', 'TF2106.CFFEX', 'TF2012.CFFEX']
kind=TS vt_symbols=['TS2103.CFFEX', 'TS2106.CFFEX', 'TS2012.CFFEX']
kind=RS vt_symbols=['RS107.CZCE', 'RS108.CZCE', 'RS109.CZCE', 'RS011.CZCE']
kind=UR vt_symbols=['UR107.CZCE', 'UR108.CZCE', 'UR109.CZCE', 'UR010.CZCE', 'UR011.CZCE', 'UR012.CZCE', 'UR101.CZCE', 'UR102.CZCE', 'UR103.CZCE', 'UR104.CZCE', 'UR105.CZCE', 'UR106.CZCE']

3.2 全市场主力合约排序

=====================全市场主力合约排序========================
{'kind': 'ag', 'vt_symbol': 'AG2012', 'total_turnover': 214143898665.0}
{'kind': 'IF', 'vt_symbol': 'IF2010', 'total_turnover': 115089726720.0}
{'kind': 'IC', 'vt_symbol': 'IC2010', 'total_turnover': 108709168960.0}
{'kind': 'au', 'vt_symbol': 'AU2012', 'total_turnover': 97275079200.0}
{'kind': 'i', 'vt_symbol': 'I2101', 'total_turnover': 70141287600.0}
{'kind': 'T', 'vt_symbol': 'T2012', 'total_turnover': 66779643250.0}
{'kind': 'ru', 'vt_symbol': 'RU2101', 'total_turnover': 62175496100.0}
{'kind': 'y', 'vt_symbol': 'Y2101', 'total_turnover': 62036693940.0}
{'kind': 'p', 'vt_symbol': 'P2101', 'total_turnover': 60139173280.0}
{'kind': 'OI', 'vt_symbol': 'OI2101', 'total_turnover': 58055810880.0}
{'kind': 'cu', 'vt_symbol': 'CU2011', 'total_turnover': 53450265650.0}
{'kind': 'FG', 'vt_symbol': 'FG2101', 'total_turnover': 45607836000.0}
{'kind': 'm', 'vt_symbol': 'M2101', 'total_turnover': 35834947080.0}
{'kind': 'rb', 'vt_symbol': 'RB2101', 'total_turnover': 35004834540.0}
{'kind': 'j', 'vt_symbol': 'J2101', 'total_turnover': 32930215200.0}
{'kind': 'IH', 'vt_symbol': 'IH2010', 'total_turnover': 28645460100.0}
{'kind': 'sc', 'vt_symbol': 'SC2011', 'total_turnover': 28389715200.0}
{'kind': 'zn', 'vt_symbol': 'ZN2011', 'total_turnover': 25126207275.0}
{'kind': 'fu', 'vt_symbol': 'FU2101', 'total_turnover': 24160222910.0}
{'kind': 'ZC', 'vt_symbol': 'ZC2011', 'total_turnover': 21100200000.0}
{'kind': 'pp', 'vt_symbol': 'PP2101', 'total_turnover': 20908030425.0}
{'kind': 'AP', 'vt_symbol': 'AP2101', 'total_turnover': 20720715330.0}
{'kind': 'bu', 'vt_symbol': 'BU2012', 'total_turnover': 20684842140.0}
{'kind': 'TF', 'vt_symbol': 'TF2012', 'total_turnover': 20387290900.0}
{'kind': 'ni', 'vt_symbol': 'NI2011', 'total_turnover': 18114747280.0}
{'kind': 'SR', 'vt_symbol': 'SR2101', 'total_turnover': 17356500420.0}
{'kind': 'MA', 'vt_symbol': 'MA2101', 'total_turnover': 17291912680.0}
{'kind': 'CF', 'vt_symbol': 'CF2101', 'total_turnover': 14776293750.0}
{'kind': 'jd', 'vt_symbol': 'JD2011', 'total_turnover': 14047527750.0}
{'kind': 'c', 'vt_symbol': 'C2101', 'total_turnover': 13462882980.0}
{'kind': 'RM', 'vt_symbol': 'RM2101', 'total_turnover': 12374485580.0}
{'kind': 'al', 'vt_symbol': 'AL2011', 'total_turnover': 12072076375.0}
{'kind': 'l', 'vt_symbol': 'L2101', 'total_turnover': 11536233225.0}
{'kind': 'TS', 'vt_symbol': 'TS2012', 'total_turnover': 9449540000.0}
{'kind': 'TA', 'vt_symbol': 'TA2101', 'total_turnover': 9398714690.0}
{'kind': 'eg', 'vt_symbol': 'EG2101', 'total_turnover': 8941426960.0}
{'kind': 'pg', 'vt_symbol': 'PG2011', 'total_turnover': 8926672280.0}
{'kind': 'sp', 'vt_symbol': 'SP2012', 'total_turnover': 7528736540.0}
{'kind': 'v', 'vt_symbol': 'V2101', 'total_turnover': 7187423175.0}
{'kind': 'a', 'vt_symbol': 'A2101', 'total_turnover': 6813036460.0}
{'kind': 'hc', 'vt_symbol': 'HC2101', 'total_turnover': 6807265320.0}
{'kind': 'jm', 'vt_symbol': 'JM2101', 'total_turnover': 4624123680.0}
{'kind': 'sn', 'vt_symbol': 'SN2012', 'total_turnover': 4067929330.0}
{'kind': 'pb', 'vt_symbol': 'PB2011', 'total_turnover': 3304319225.0}
{'kind': 'SM', 'vt_symbol': 'SM2101', 'total_turnover': 2529065430.0}
{'kind': 'cs', 'vt_symbol': 'CS2101', 'total_turnover': 2402811690.0}
{'kind': 'b', 'vt_symbol': 'B2011', 'total_turnover': 2338601160.0}
{'kind': 'eb', 'vt_symbol': 'EB2101', 'total_turnover': 1968052130.0}
{'kind': 'UR', 'vt_symbol': 'UR2101', 'total_turnover': 1476473040.0}
{'kind': 'CY', 'vt_symbol': 'CY2101', 'total_turnover': 707752500.0}
{'kind': 'SF', 'vt_symbol': 'SF2101', 'total_turnover': 507498280.0}
{'kind': 'rr', 'vt_symbol': 'RR2012', 'total_turnover': 505222820.0}
{'kind': 'fb', 'vt_symbol': 'FB2010', 'total_turnover': 38542900.0}
{'kind': 'WH', 'vt_symbol': 'WH2101', 'total_turnover': 1927700.0}
{'kind': 'LR', 'vt_symbol': 'LR2011', 'total_turnover': 346200.0}
{'kind': 'RS', 'vt_symbol': 'RS2011', 'total_turnover': 264500.0}
{'kind': 'wr', 'vt_symbol': 'WR2103', 'total_turnover': 0.0}
{'kind': 'PM', 'vt_symbol': 'PM2105', 'total_turnover': 0.0}
{'kind': 'RI', 'vt_symbol': 'RI2101', 'total_turnover': 0.0}
{'kind': 'bb', 'vt_symbol': 'BB2107', 'total_turnover': 0.0}
{'kind': 'JR', 'vt_symbol': 'JR2101', 'total_turnover': 0.0}
Administrator
avatar
加入于:
帖子: 4547
声望: 325

接下来就可以本地生成主力合约了嘛

Member
avatar
加入于:
帖子: 420
声望: 181

用Python的交易员 wrote:

接下来就可以本地生成主力合约了嘛

是呀,也可以实现自动换约。

Member
avatar
加入于:
帖子: 104
声望: 5

第一步骤,主要函数实现,放在那个文件夹下?

Member
avatar
加入于:
帖子: 420
声望: 181

ahren wrote:

第一步骤,主要函数实现,放在那个文件夹下?

  1. 第一步骤的主要函数实现可以放在你认为合适的文件夹下,如:vnpy\usertools\myutility.py中
  2. 在你需要调用这些函数的文件的开头这样引入myutility.py的函数:
    from vnpy.usertools.myutility import get_contract_kinds,get_all_dominants
  3. 然后参照步骤二来使用get_contract_kinds(),get_all_dominants()函数,来获取当前市场正在交易的品种和这些品种中的主力合约。我这里是把它们print了一下,你怎么使用就是另外一回事了。
Member
avatar
加入于:
帖子: 104
声望: 5

借鉴您的方法,直接从rqdata抓取主力合约列表以及相关open,close,high等数据,现在要实现高低开提示,需要获取当日open价格,好像rqdata get_price 是获取历史价格,那么怎么获取所有主力合约当日开盘价呢?比如今天是周六,get_latest_trading_date,在通过get_price对于有夜盘的品种无法获取到周五夜盘开盘价格

Member
avatar
加入于:
帖子: 420
声望: 181

ahren wrote:

借鉴您的方法,直接从rqdata抓取主力合约列表以及相关open,close,high等数据,现在要实现高低开提示,需要获取当日open价格,好像rqdata get_price 是获取历史价格,那么怎么获取所有主力合约当日开盘价呢?比如今天是周六,get_latest_trading_date,在通过get_price对于有夜盘的品种无法获取到周五夜盘开盘价格

rqdata的主力合约列表是根据历史日行情比较后得到的,当日行情没有接受是无法得到的,不存在你所说的取主力合约当日open价格的问题。而且在一个月中一个品种的某个合约一旦成为主力合约,即使其后有段时间交易金额或者持仓量不再是最大,也不一定会不再是主力合约了,它会有一定的连续性。具体的可以见米筐的接口文档。
不同的软件或机构对主力合约的选择和延续规则可能不一样,例如文华的主力合约定义规则和米筐的可能是不一样的。但是大致是差不多的。他们的目的就是找到市场中参与方最多的、流动性最好的交易合约。

Member
avatar
加入于:
帖子: 104
声望: 5

是的,主力合约计算方法,文华是按照持仓量,成交量同时最大时候取的,米筐是按照持仓量倍数取主力合约~无论按照那种方法,我需要的是上一个交易日主力合约在下一个交易日的开盘价,比较上一交易日收盘与次日开盘作为跳空依据;看了下米筐的get_tick可能是要权限才能获取,如果通过vnpy直接获取所有期货标的开盘价,试了下脚本联ctp后,get_all_contracts(),结果是空的,目前还不知问题在哪儿

Member
avatar
加入于:
帖子: 420
声望: 181
  1. 米筐的账户有tick权限的与没有权限的费用差别一个2~3万另一个是3000,而且即使有tick权限,主力合约也只有上一个交易日的收盘价,因为今天的还没有结束它不会更新这个数据。
  2. 主力合约都给你找到了,你找到主力合约的今日第一个分钟bar的开盘价,这个不就是你要的开盘价吗?
Member
avatar
加入于:
帖子: 104
声望: 5

hxxjava wrote:

  1. 米筐的账户有tick权限的与没有权限的费用差别一个2~3万另一个是3000,而且即使有tick权限,主力合约也只有上一个交易日的收盘价,因为今天的还没有结束它不会更新这个数据。
  2. 主力合约都给你找到了,你找到主力合约的今日第一个分钟bar的开盘价,这个不就是你要的开盘价吗?

从ctatemplate中获取单一合约开盘价没问题,想独立出脚本获取所有主力合约开盘价,还没搞明白,感觉应该先获取某个引擎,连接ctp,在遍历主力合约获取开盘价
description按照脚本策略这么加载,打印出来 合约信息,tick数据都为None

Member
avatar
加入于:
帖子: 420
声望: 181

你可能是没有连接CTP吧?

正确的步骤:
0、赋值ctp接口的设置:

ctp_setting = {
    "用户名": "xxxxx",
    "密码": "*****",
    "经纪商代码": "jjjjj",
    "交易服务器": "ip:port",
    "行情服务器": "ip:port",
    "产品名称": "name",
    "授权编码": "auth_code",
    "产品信息": ""
}

1、创建EventEngine类型的消息引擎event_engine
2、创建MainEngine类型的主引擎main_engine = MainEngine(event_engine)
3、main_engine.add_gateway(CtpGateway)
4、main_engine.connect(ctp_setting, "CTP")
5、创建ScriptEngine类型script_engine = ScriptEngine(main_engine,event_engine)
6、run(script_engine)

Member
avatar
加入于:
帖子: 104
声望: 5

感谢!!!按照上述方法获取了engin.get_contract, engin.get_tick数据;问题在于开始连接ctp没获取到数据可能是连接后需要sleep下,后来直接打开vnstation启动脚本策略(就是上图),感觉vntrader连接了ctp,也就去掉了加载各种引擎和连接ctp

Member
avatar
加入于:
帖子: 420
声望: 181

已经开盘了就不要从CTP接口获取主力合约的的开盘价,从米筐接口get_price1分钟的就可以啦,夜盘是日内的第一个bar的open_price,白盘9:00的那一个bar的,无需订阅,简单。

Member
avatar
加入于:
帖子: 104
声望: 5

hxxjava wrote:

已经开盘了就不要从CTP接口获取主力合约的的开盘价,从米筐接口get_price1分钟的就可以啦,夜盘是日内的第一个bar的open_price,白盘9:00的那一个bar的,无需订阅,简单。

实现了,从米筐接口要省事儿很多,谢啦!

Member
avatar
加入于:
帖子: 8
声望: 1

用Python的交易员 wrote:

接下来就可以本地生成主力合约了嘛

hxxjava wrote:

用Python的交易员 wrote:

接下来就可以本地生成主力合约了嘛

是呀,也可以实现自动换约。

请教一下,如何实现自动换约?

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】