请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365770 新帖:2

【建议与研究分享】关于期货盘口逐笔明细(tick数据)的研究

蜡笔小新炒外汇发表于:6 月 20 日 00:00回复(1)

所有函数¶

import pandas as pd
from jqdata import *
import warnings
warnings.filterwarnings("ignore")

def get_future_tick_stat(ticker,date):
    """
    ticker = "RB1905.XSGE"
    date = "2019-02-15"
    获得tick数据的标记
    """
    #获得前一个交易日
    pre_day = str(get_trade_days(start_date=None, end_date=date, count=2)[0])
    start_time = pre_day+' 20:00:00'
    end_time = date + ' 15:30:00'
    
    #获得tick数据  
    field = ['time','current','volume','money','position','a1_p','a1_v','b1_p','b1_v']
    data = get_ticks(ticker,start_dt=start_time,end_dt=end_time,count=None,fields=field)
    data = pd.DataFrame(data)
    data.columns = ['time','lastPrice','volume','value','openInterest','askPrice1','askVolume1','bidPrice1','bidVolume1']
    
    #对成交量按升序排列,因为郑州交易所的数据可能有错,同一秒的两个数据有颠倒情况(如FG905,20190121等等)导致再手为负
    data = data.sort_values(by='volume')
    
    #现手、增仓计算
    data['现手']=data['volume']-data['volume'].shift(1)
    data['增仓']=data['openInterest']-data['openInterest'].shift(1)
    
    #------集合竞价的现手与增仓
    data.loc[0,'现手']=data.loc[0,'volume']
    #前一天的持仓量
    temp = get_extras('futures_positions', ticker, start_date=pre_day, end_date=date, df=True).iloc[0,0]
    #集合竞价增仓
    data.loc[0,'增仓']=data.loc[0,'openInterest']-temp
    
    
    #筛选非0tick
    data_need=data[(data['volume']>0)&(data['现手']!=0)]
    data_need = data_need.reset_index(drop=True)

    #----------主买、主卖的判断
    #昨天收盘价
    price =  get_price(ticker, start_date=None, end_date=date, frequency='daily', fields=['open','close'], skip_paused=False, fq=None, count=2)
    preClosePrice = price.iloc[0,1]
    openPrice = price.iloc[1,0]
    
    #集合竞价成交主买主卖的判断
    flag=[]
    if openPrice>preClosePrice:
        flag.append(1)
    else:
        flag.append(-1)
        
    #所有tick主买主卖
    data_need['主动买/卖'] = flag+[1 if data_need['lastPrice'][i]>=data_need['askPrice1'][i-1] else -1 for i in range(1,len(data_need))]

    #----------买卖性质
    temp=[]
    for i in range(len(data_need)):
        if data_need['现手'][i]==abs(data_need['增仓'][i]):
            if data_need['增仓'][i]>0:
                temp.append('双开')
            else:
                temp.append('双平')
        else:
            if data_need['主动买/卖'][i]==1:
                if data_need['增仓'][i]>0:
                    temp.append('多开')
                elif data_need['增仓'][i]<0:
                    temp.append('空平')
                else:
                    temp.append('多换')
            else:
                if data_need['增仓'][i]>0:
                    temp.append('空开')
                elif data_need['增仓'][i]<0:
                    temp.append('多平')
                else:
                    temp.append('空换')               
    data_need['性质']=temp
    #当笔tick的成交金额
    temp2 = data_need['value']-data_need['value'].shift(1)
    temp2 = temp2.tolist()[1:]
    data_need['现值'] = [data_need.loc[0,'value']] + temp2
    
    result = data_need[['time','lastPrice','volume','value','现手','现值','增仓','主动买/卖','性质']]
    return result

def get_8stat(result,big_order=0):
    """
    根据tick数据,计算八项统计,默认统计全部
    """
    total_volume = sum(result['现手'])
    duo_kai = sum(result[(result['性质']=='多开')&(result['现手']>=big_order)]['现手'])/total_volume
    kon_pin = sum(result[(result['性质']=='空平')&(result['现手']>=big_order)]['现手'])/total_volume

    kon_kai = sum(result[(result['性质']=='空开')&(result['现手']>=big_order)]['现手'])/total_volume
    duo_pin = sum(result[(result['性质']=='多平')&(result['现手']>=big_order)]['现手'])/total_volume

    duo_huan = sum(result[(result['性质']=='多换')&(result['现手']>=big_order)]['现手'])/total_volume
    kon_huan = sum(result[(result['性质']=='空换')&(result['现手']>=big_order)]['现手'])/total_volume

    shuang_kai = sum(result[(result['性质']=='双开')&(result['现手']>=big_order)]['现手'])/total_volume
    shuang_pin = sum(result[(result['性质']=='双平')&(result['现手']>=big_order)]['现手'])/total_volume
    
    all_ratio = duo_kai+kon_pin+kon_kai+duo_pin+duo_huan+kon_huan+shuang_kai+shuang_pin
    
    #多开-空开
    duo_minus_kon_kai = duo_kai-kon_kai
    #空平-多平
    kon_minus_duo_pin = kon_pin-duo_pin
    #多-空
    duo_minus_kon = duo_minus_kon_kai+kon_minus_duo_pin
    
    stat_result = pd.DataFrame([duo_kai,kon_pin,kon_kai,duo_pin,duo_huan,kon_huan,shuang_kai,shuang_pin,all_ratio,duo_minus_kon_kai,kon_minus_duo_pin,duo_minus_kon],\
                              index=['多开','空平','空开','多平','多换','空换','双开','双平','总计','多开-空开','空平-多平','多-空'],columns=['占比'])
    return stat_result.T

def get_1day1ticker(ticker,date,big_order=0):
    """
    获得某天一个合约的统计
    """
    result = get_future_tick_stat(ticker,date)
    temp = get_8stat(result,big_order=big_order)
    return temp


def get_1day1ticker_wenhuaBigStat(ticker,date):
    """
    获得某天一个合约的统计,文华大单(大于前一日单笔成交量的5倍定义为大单)
    """
    #前一天成交量
    pre_day = str(get_trade_days(start_date=None, end_date=date, count=2)[0])
    pre_vol = get_price(ticker, start_date=pre_day, end_date=pre_day, frequency='daily', fields='volume', fq=None).iloc[0,0]
    result = get_future_tick_stat(ticker,date)  
    big_order = pre_vol/len(result)*5
    temp = get_8stat(result,big_order=big_order)
    return temp


def get_1day1ticker_main(str1,date,big_order=0):
    """
    获得某天一个合约的统计,只需要输入标的名称,如'RB'
    """
    ticker = get_dominant_future(str1,date)
    result = get_future_tick_stat(ticker,date)
    temp = get_8stat(result,big_order=big_order)
    return temp

def get_ticker(date):
    """
    输入日期,返回当天主力合约代码ticker,及合约简称
    持仓量大于1万手
    """
    opid_th = 10000
    #获得主力合约
    a = get_all_securities(types=['futures'])
    cond = ['主力合约' in x for x in a.display_name]
    main = a[cond].index.tolist()  #9999
    main1 = [x[:-9] for x in main] #AU,仅含标的字母
    main2 = [get_dominant_future(x,date) for x in main1]
    main2_ = [x for x in main2 if x!='']
    #持仓量
    main2_opid = get_extras('futures_positions', main2_, start_date=date, end_date=date, df=True).T
    need_ticker = main2_opid[main2_opid[date]>opid_th].index.tolist()
    return need_ticker

#----------------------
def get_oneDay_stat(date,big_order=0):
    """
    获得某天的八项统计结果
    """
    ticker_data = get_ticker(date)
    all_ticker_stats = pd.DataFrame()
    for i in range(len(ticker_data)):
        ticker = ticker_data[i]
        result = get_future_tick_stat(ticker,date)
        temp = get_8stat(result,big_order)  
        temp.index = [ticker]
        all_ticker_stats = all_ticker_stats.append(temp)
    all_ticker_stats = all_ticker_stats.sort_values(by='多开-空开',ascending=False)    
    return all_ticker_stats

def get_headtail(date,n=5,big_order=0):
    """
    计算某天做多做空前N名
    """
    data = get_oneDay_stat(date,big_order=big_order)
    temp1 = data.head(n)
    temp2 = data.tail(n)
    result = temp1.append(temp2)
    result.index = pd.MultiIndex.from_arrays([['做多']*n+['做空']*n,result.index.tolist()])
    return result

def get_nDay_stas(ticker,startDate,endDate,big_order=0):
    """
    返回某个合约-某段时间的统计结果
    """
    tradeDay = get_trade_days(start_date=startDate, end_date=endDate)
    tradeDay = [str(x) for x in tradeDay]
    result = pd.DataFrame()
    for date in tradeDay:
        temp = get_1day1ticker(ticker,date,big_order=big_order)
        temp.index = [date]
        result = result.append(temp)
    return result

if __name__ == '__main__':
    from datetime import datetime
    t0 = datetime.now() 
    date = '2019-05-20'
    #all_data = get_oneDay_stat(date,big_order=0)
    print('用时:{}'.format(datetime.now()-t0))
用时:0:00:00.000008

一:计算某一天所有主力合约的分笔统计结果¶

时间大约2分钟

from datetime import datetime
t0 = datetime.now() 
all_data1 = get_oneDay_stat('2019-06-19',big_order=0)
print('用时:{}'.format(datetime.now()-t0))
all_data1
用时:0:01:46.656766
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
多开 空平 空开 多平 多换 空换 双开 双平 总计 多开-空开 空平-多平 多-空
FG1909.XZCE 0.277831 0.167096 0.204753 0.185158 0.042176 0.043623 0.042875 0.036488 1.0 0.073077 -0.018062 0.055015
TA1909.XZCE 0.271779 0.215334 0.211863 0.251488 0.016759 0.015909 0.009759 0.007107 1.0 0.059916 -0.036154 0.023762
I1909.XDCE 0.268119 0.228779 0.213435 0.246787 0.012096 0.015362 0.010666 0.004755 1.0 0.054683 -0.018008 0.036676
SR1909.XZCE 0.251656 0.188773 0.200302 0.233358 0.041798 0.036543 0.026664 0.020907 1.0 0.051354 -0.044586 0.006768
SF1909.XZCE 0.212141 0.145646 0.163601 0.194530 0.058205 0.075472 0.092396 0.058008 1.0 0.048541 -0.048884 -0.000343
CU1908.XSGE 0.215512 0.196875 0.170382 0.200779 0.056625 0.057241 0.044880 0.057707 1.0 0.045130 -0.003904 0.041226
AL1908.XSGE 0.221110 0.196067 0.182513 0.186411 0.062431 0.046148 0.052218 0.053101 1.0 0.038597 0.009656 0.048253
SP1909.XSGE 0.225395 0.182079 0.193136 0.188540 0.067119 0.065554 0.054907 0.023269 1.0 0.032259 -0.006461 0.025799
CF1909.XZCE 0.250480 0.218143 0.224692 0.233746 0.023140 0.021787 0.014556 0.013457 1.0 0.025787 -0.015604 0.010184
MA1909.XZCE 0.240901 0.214504 0.219507 0.237063 0.026137 0.026217 0.026134 0.009537 1.0 0.021394 -0.022559 -0.001164
FU1909.XSGE 0.205731 0.256111 0.187716 0.266044 0.023963 0.027499 0.013554 0.019381 1.0 0.018015 -0.009933 0.008082
CY2001.XZCE 0.211165 0.158691 0.194404 0.182944 0.074094 0.075736 0.052475 0.050491 1.0 0.016762 -0.024253 -0.007492
BU1912.XSGE 0.224509 0.238916 0.210648 0.236516 0.027191 0.025368 0.019937 0.016914 1.0 0.013861 0.002400 0.016261
V1909.XDCE 0.215635 0.243934 0.207824 0.155288 0.053904 0.035988 0.049045 0.038383 1.0 0.007811 0.088646 0.096458
CS1909.XDCE 0.220631 0.151490 0.214027 0.191845 0.043440 0.072173 0.068095 0.038299 1.0 0.006604 -0.040355 -0.033752
PP1909.XDCE 0.218290 0.225899 0.214895 0.235118 0.029613 0.033752 0.021156 0.021277 1.0 0.003395 -0.009219 -0.005824
JM1909.XDCE 0.172875 0.195984 0.170512 0.187230 0.065424 0.074515 0.070050 0.063410 1.0 0.002363 0.008753 0.011117
ZC1909.XZCE 0.169623 0.217192 0.170511 0.233489 0.057281 0.054189 0.046245 0.051470 1.0 -0.000889 -0.016298 -0.017186
RU1909.XSGE 0.211962 0.199702 0.212929 0.242804 0.043209 0.038992 0.027323 0.023080 1.0 -0.000967 -0.043101 -0.044068
RB1910.XSGE 0.218611 0.258765 0.220471 0.261906 0.012227 0.013185 0.008167 0.006669 1.0 -0.001860 -0.003141 -0.005001
AU1912.XSGE 0.192200 0.207506 0.194365 0.235125 0.046765 0.050874 0.033425 0.039738 1.0 -0.002165 -0.027619 -0.029784
T1909.CCFX 0.174211 0.158590 0.179034 0.195843 0.078582 0.074880 0.066832 0.072028 1.0 -0.004822 -0.037253 -0.042076
ZN1908.XSGE 0.213123 0.215089 0.219667 0.220926 0.038297 0.037796 0.024475 0.030627 1.0 -0.006544 -0.005836 -0.012380
AG1912.XSGE 0.184787 0.211180 0.191745 0.212836 0.055871 0.061350 0.033457 0.048775 1.0 -0.006958 -0.001656 -0.008614
CJ1912.XZCE 0.212283 0.243259 0.219628 0.231289 0.029139 0.030026 0.019074 0.015302 1.0 -0.007345 0.011970 0.004625
M1909.XDCE 0.228296 0.238270 0.237501 0.250827 0.012772 0.014205 0.009650 0.008479 1.0 -0.009205 -0.012557 -0.021762
JD1909.XDCE 0.182541 0.196402 0.193988 0.187606 0.067066 0.067748 0.060951 0.043699 1.0 -0.011447 0.008796 -0.002651
EG1909.XDCE 0.188337 0.223944 0.201196 0.239534 0.047590 0.046225 0.024434 0.028739 1.0 -0.012860 -0.015590 -0.028450
A1909.XDCE 0.179664 0.179405 0.193851 0.213777 0.057716 0.066695 0.054711 0.054181 1.0 -0.014187 -0.034373 -0.048560
J1909.XDCE 0.217929 0.208404 0.234536 0.210283 0.037060 0.040538 0.037038 0.014213 1.0 -0.016607 -0.001879 -0.018486
RM1909.XZCE 0.216697 0.240374 0.236640 0.244174 0.018622 0.019731 0.011781 0.011983 1.0 -0.019943 -0.003800 -0.023743
IC1906.CCFX 0.095454 0.200768 0.116051 0.281822 0.069569 0.104302 0.043083 0.088950 1.0 -0.020597 -0.081054 -0.101651
TF1909.CCFX 0.176049 0.078864 0.197557 0.115242 0.082581 0.136219 0.143388 0.070101 1.0 -0.021508 -0.036378 -0.057886
HC1910.XSGE 0.184851 0.235370 0.206739 0.212344 0.040322 0.048804 0.031488 0.040081 1.0 -0.021888 0.023026 0.001138
NI1908.XSGE 0.208357 0.194700 0.230865 0.227827 0.038979 0.053124 0.021410 0.024738 1.0 -0.022508 -0.033127 -0.055635
OI1909.XZCE 0.205479 0.200153 0.228020 0.239681 0.032328 0.041530 0.025091 0.027719 1.0 -0.022541 -0.039528 -0.062069
P1909.XDCE 0.200531 0.183089 0.224927 0.218723 0.047646 0.042390 0.035868 0.046826 1.0 -0.024396 -0.035633 -0.060029
PB1907.XSGE 0.124368 0.180540 0.151238 0.168977 0.084489 0.108009 0.064319 0.118061 1.0 -0.026871 0.011563 -0.015308
AP1910.XZCE 0.195978 0.219601 0.223271 0.229822 0.038279 0.046867 0.022932 0.023250 1.0 -0.027293 -0.010221 -0.037515
IH1906.CCFX 0.078188 0.152883 0.108690 0.247667 0.095803 0.136380 0.072532 0.107856 1.0 -0.030503 -0.094783 -0.125286
IF1906.CCFX 0.103827 0.213213 0.138562 0.298789 0.062110 0.090724 0.039538 0.053237 1.0 -0.034735 -0.085576 -0.120311
SM1909.XZCE 0.169241 0.187648 0.212028 0.178032 0.049330 0.075531 0.060397 0.067793 1.0 -0.042787 0.009616 -0.033171
L1909.XDCE 0.184876 0.249074 0.227727 0.182113 0.043079 0.041799 0.031020 0.040312 1.0 -0.042851 0.066961 0.024110
Y1909.XDCE 0.171366 0.214181 0.225003 0.240470 0.041064 0.041524 0.030702 0.035690 1.0 -0.053638 -0.026289 -0.079926
SN1909.XSGE 0.087449 0.077935 0.144256 0.111235 0.095704 0.198405 0.138380 0.146635 1.0 -0.056807 -0.033301 -0.090108
B1908.XDCE 0.106194 0.138474 0.188909 0.197711 0.062944 0.115509 0.091365 0.098894 1.0 -0.082715 -0.059237 -0.141953
C1909.XDCE 0.129073 0.230351 0.221395 0.254042 0.034779 0.046663 0.027248 0.056448 1.0 -0.092322 -0.023691 -0.116013

当然,你可以根据衍生的指标多开-空开来指导交易¶

get_headtail('2019-06-18',n=5,big_order=0)
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
多开 空平 空开 多平 多换 空换 双开 双平 总计 多开-空开 空平-多平 多-空
做多 TA1909.XZCE 0.287008 0.202943 0.211709 0.235573 0.018375 0.018445 0.014904 0.011043 1.0 0.075300 -0.032630 0.042670
AU1912.XSGE 0.235419 0.151219 0.170162 0.206918 0.063547 0.065594 0.048869 0.058272 1.0 0.065257 -0.055699 0.009558
SM1909.XZCE 0.210820 0.142616 0.177630 0.239896 0.059556 0.066878 0.049198 0.053406 1.0 0.033189 -0.097281 -0.064091
I1909.XDCE 0.236516 0.245354 0.203387 0.251157 0.018901 0.019050 0.013033 0.012602 1.0 0.033129 -0.005803 0.027326
ZC1909.XZCE 0.222026 0.229889 0.190567 0.171711 0.050972 0.051479 0.042191 0.041164 1.0 0.031459 0.058178 0.089636
做空 C1909.XDCE 0.171007 0.211103 0.230024 0.183470 0.051309 0.059982 0.042031 0.051073 1.0 -0.059017 0.027634 -0.031383
AP1910.XZCE 0.193283 0.207871 0.258300 0.233836 0.032412 0.039801 0.019218 0.015280 1.0 -0.065017 -0.025965 -0.090982
CY2001.XZCE 0.134596 0.222090 0.216568 0.177818 0.051529 0.067869 0.073163 0.056367 1.0 -0.081972 0.044272 -0.037700
V1909.XDCE 0.163216 0.211459 0.252779 0.199556 0.040155 0.052938 0.039813 0.040084 1.0 -0.089564 0.011904 -0.077660
B1908.XDCE 0.075534 0.132526 0.193739 0.255874 0.048020 0.080592 0.111557 0.102158 1.0 -0.118205 -0.123349 -0.241554

二:某段时间某个品种的分笔统计数据¶

ticker = 'RB1910.XSGE'
startDate = '2019-06-01'
endDate = '2019-06-19'
result = get_nDay_stas(ticker,startDate,endDate,big_order=0)
result
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
多开 空平 空开 多平 多换 空换 双开 双平 总计 多开-空开 空平-多平 多-空
2019-06-03 0.220147 0.231802 0.247251 0.247068 0.015904 0.018893 0.010992 0.007943 1.0 -0.027104 -0.015265 -0.042369
2019-06-04 0.229223 0.251816 0.234397 0.225439 0.019445 0.016460 0.013757 0.009463 1.0 -0.005174 0.026377 0.021203
2019-06-05 0.214473 0.240942 0.241051 0.237470 0.018354 0.022030 0.015283 0.010398 1.0 -0.026578 0.003472 -0.023106
2019-06-06 0.223883 0.256367 0.241113 0.240207 0.012624 0.012552 0.007742 0.005513 1.0 -0.017230 0.016160 -0.001070
2019-06-10 0.244607 0.237009 0.262524 0.212585 0.013007 0.014990 0.011028 0.004250 1.0 -0.017917 0.024424 0.006507
2019-06-11 0.235004 0.259200 0.220558 0.255416 0.010170 0.010685 0.005594 0.003373 1.0 0.014446 0.003784 0.018230
2019-06-12 0.217540 0.251908 0.225938 0.262167 0.014259 0.012612 0.008525 0.007051 1.0 -0.008398 -0.010260 -0.018657
2019-06-13 0.223058 0.247854 0.243731 0.243575 0.013089 0.013541 0.008727 0.006425 1.0 -0.020673 0.004279 -0.016393
2019-06-14 0.224816 0.234635 0.242755 0.250808 0.015643 0.013746 0.010415 0.007182 1.0 -0.017939 -0.016173 -0.034112
2019-06-17 0.238204 0.220400 0.272555 0.211813 0.017580 0.018667 0.013210 0.007572 1.0 -0.034351 0.008587 -0.025764
2019-06-18 0.240158 0.250762 0.232639 0.219087 0.016786 0.017445 0.014880 0.008243 1.0 0.007519 0.031676 0.039195
2019-06-19 0.218611 0.258765 0.220471 0.261906 0.012227 0.013185 0.008167 0.006669 1.0 -0.001860 -0.003141 -0.005001

全部回复

0/140

量化课程

    移动端课程