您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么進行針對vnpy的不同期貨品種行情數據清理,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
vnpy自帶的行情清理功能較為簡單,只是在清除非交易時段,沒有考慮周六日;而且只是籠統給了一個最大時間交易范圍,像股指期貨沒有夜盤,螺紋鋼晚上11點就結束,但是默認只是結束在凌晨兩點半這個最大交易時間。 所以寫了一個方法,按照不同品種,更細致的清理。
可以直接把這個方法插入\DataRecording\runDataCleaning.py, 然后替代原來方法。也可以自己另外調用。
# ---------------------------------------------------------------------- def cleanDataAdv(dbName, collectionName, start): """清洗數據""" #新的靜態數據 # 這里以商品期貨為例 MORNING_START = time(9, 0) MORNING_REST = time(10, 15) MORNING_RESTART = time(10, 30) MORNING_END = time(11, 30) AFTERNOON_START = time(13, 30) AFTERNOON_END = time(15, 0) NIGHT_START = time(21, 0) NIGHT_END = time(2, 30) #股指期貨 STOCK_FUTURE = ["IC", "IF", "IH"] MORNING_START_STOCK = time(9, 30) AFTERNOON_START_STOCK = time(13,0) AFTERNOON_END_STOCK = time(15, 0) #晚上11點結束交易,不全,請自行維護 PM11CLOSE_FUTURE = ['rb','ru','bu','hc','sp'] NIGHT_END_11 = time(23, 00) #晚上11點半結束交易,不全,請自行維護,大連只有一位標志,所以帶1 PM1130CLOSE_FUTURE = ['FG','MA','SR','TA','RM','OI','CF','CY','ZC','i1','j1','m1','p1','y1'] NIGHT_END_1130 = time(23, 30) #凌晨1點半結束交易,不全,請自行維護 AM1CLOSE_FUTURE = ['cu','pd','al','zn'] NIGHT_END_AM1 = time(1, 00) print(u'\n清洗數據庫:%s, 集合:%s, 起始日:%s' % (dbName, collectionName, start)) mc = MongoClient('localhost', 27017) # 創建MongoClient cl = mc[dbName][collectionName] # 獲取數據集合 d = {'datetime': {'$gte': start}} # 只過濾從start開始的數據 cx = cl.find(d) # 獲取數據指針 for data in cx: # 獲取時間戳對象 dt = data['datetime'].time() # 默認需要清洗 cleanRequired = True ####如果是股指期貨,這沒有上午休息和夜盤,9點半到11點半,下午1點到下午三點,周六日無行情 if collectionName[:2] in STOCK_FUTURE: if data['datetime'].weekday() is not (5 or 6): if ((MORNING_START_STOCK <= dt < MORNING_END) or (AFTERNOON_START_STOCK <= dt < AFTERNOON_END_STOCK)): cleanRequired = False ####如果是11點結束,則周六日無行情 elif collectionName[:2] in PM11CLOSE_FUTURE: if data['datetime'].weekday() is not (5 or 6): if ((MORNING_START <= dt < MORNING_REST) or (MORNING_RESTART <= dt < MORNING_END) or (AFTERNOON_START <= dt < AFTERNOON_END) or ( NIGHT_START <= dt <NIGHT_END_11)): cleanRequired = False ####如果是11點半結束,則周六日無行情 elif collectionName[:2] in PM1130CLOSE_FUTURE: if data['datetime'].weekday() is not (5 or 6): if ((MORNING_START <= dt < MORNING_REST) or (MORNING_RESTART <= dt < MORNING_END) or (AFTERNOON_START <= dt < AFTERNOON_END) or (NIGHT_START <= dt < NIGHT_END_1130)): cleanRequired = False ####如果是1點結束, elif collectionName[:2] in AM1CLOSE_FUTURE: # 如果在交易事件內,則為有效數據,無需清洗 if data['datetime'].weekday() is not 6: if ((MORNING_START <= dt < MORNING_REST) or (MORNING_RESTART <= dt < MORNING_END) or (AFTERNOON_START <= dt < AFTERNOON_END) or (dt >= NIGHT_START) or (dt < NIGHT_END_AM1)): cleanRequired = False else: # 如果在交易事件內,則為有效數據,無需清洗 if data['datetime'].weekday() is not 6: if ((MORNING_START <= dt < MORNING_REST) or (MORNING_RESTART <= dt < MORNING_END) or (AFTERNOON_START <= dt < AFTERNOON_END) or (dt >= NIGHT_START) or (dt < NIGHT_END)): cleanRequired = False # 如果需要清洗 if cleanRequired: print(u'刪除無效數據,時間戳:%s' % data['datetime']) cl.delete_one(data) print(u'清洗完成,數據庫:%s, 集合:%s' % (dbName, collectionName))
上述就是小編為大家分享的怎么進行針對vnpy的不同期貨品種行情數據清理了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。