def opt_tick_req_single_code(self,index,option_code,queryTime): self.client.opt_req_next_code = False order_id = index*100000 + 1 while not self.client.opt_req_next_code and not self.client.process_done: time_recorder = 0 self.client.reqHistoricalTicks(order_id, ContractSamples.OptionWithLocalSymbol(option_code), queryTime.strftime("%Y%m%d %H:%M:%S"), "", 1000, "TRADES", 1, True, []) time.sleep(10) while not self.client.process_done: if self.client.opt_req_next_code: print('next code...............') break elif self.client.opt_req_continue: queryTime = self.client.lasttime order_id += 1 self.client.opt_req_continue = False print('continue...............') break else: time.sleep(2) time_recorder += 1 if time_recorder > 90: #3分钟内没有返回内容,则重新提交请求 fw = open('data.txt', 'a') fw.write(option_code+' '+str(order_id)+' '+str(queryTime)+'\n') fw.close() self.client.opt_req_next_code = True self.client.tick_num = 1 break print(datetime.datetime.now(), 'sleeping.................') print(datetime.datetime.now())
def historicalDataRequests_req_opt_Seconds(self): for index in self.stock_code_map.keys(): queryTime = datetime.datetime(2018, 1, 16, 16, 00) endday = queryTime - timedelta(days=180) while queryTime > endday: if self.client.process_done: break else: stock_code = self.stock_code_map[index] print('Start to get', stock_code, str(index), str(queryTime)) self.client.reqHistoricalData(index, ContractSamples.OptionWithLocalSymbol(stock_code), queryTime.strftime("%Y%m%d %H:%M:%S"), "1800 S", "1 secs", "TRADES", 1, 1, False, []) time.sleep(11) print('Finish query', stock_code + str(queryTime)) if queryTime.hour == 10 and queryTime.minute == 0: if queryTime.isoweekday() == 1: queryTime -= timedelta(days=2,hours=18) else: queryTime -= timedelta(hours=18) else: queryTime -= timedelta(seconds=1800) print('request done')
def opt_tick_req_single_code(self,index,option_code,query_Time): queryTime = query_Time while not self.client.opt_req_next_code and not self.client.process_done: self.client.reqHistoricalTicks(index, ContractSamples.OptionWithLocalSymbol(option_code), queryTime.strftime("%Y%m%d %H:%M:%S"), "", 1000, "TRADES", 1, True, []) time.sleep(15) while not self.client.process_done: if self.client.opt_req_next_time: if queryTime.isoweekday() == 1: queryTime -= timedelta(days=3) else: queryTime -= timedelta(days=1) queryTime = datetime.datetime.combine(queryTime.date(),query_Time.time()) print(queryTime) self.client.opt_req_next_time = False print('next time...............') break elif self.client.opt_req_continue: queryTime = self.client.lasttime self.client.opt_req_continue = False print('continue...............') break else: time.sleep(0.5) print('sleeping.................')
def mktData_req_opt(self): for index in self.stock_code_map.keys(): stock_code = self.stock_code_map[index] print('Start to get', stock_code, str(index)) self.client.reqMktData(index, ContractSamples.OptionWithLocalSymbol(stock_code), "100,101,104,106,233,236,258", False, False, []) print('Finish query', stock_code) print('request done')
def historicalDataRequests_req_HeadTimestamp(self): # Requesting historical data # ! [reqHeadTimeStamp] for index in self.stock_code_map.keys(): if self.client.process_done: break else: stock_code = self.stock_code_map[index] print('Start to get', stock_code, str(index)) self.client.reqHeadTimeStamp(index, ContractSamples.OptionWithLocalSymbol(stock_code), "TRADES", 0, 1) time.sleep(10) print('Finish query', stock_code)
def optionsOperations_req(self): # ! [reqsecdefoptparams] # self.client.reqSecDefOptParams(1, "IBM", "", "STK", 8314) # self.client.reqContractDetails(210, ContractSamples.OptionForQuery()) # self.client.reqMktData(1002, ContractSamples.USStockAtSmart('AAPL'), "", False, False, []) #OptionForQuery() # self.client.reqHistoricalData(1, ContractSamples.OptionForQuery(), # "20180224 16:00:00", "900 S", "1 secs", "TRADES", 1, 1, False, []) # self.client.reqHistoricalTicks(0, ContractSamples.OptionWithLocalSymbol("AAPL 180420C00180000"), # "20171124 09:30:00", "", 1000, "TRADES", 1, True, []) # self.opt_tick_req_single_code(0, "AAPL 180420C00180000", datetime.datetime(2017,11,22,9,30)) self.client.reqHeadTimeStamp(1, ContractSamples.OptionWithLocalSymbol("AAPL 190118C00135000"), "TRADES", 0, 1) # self.client.reqMktData(1000, ContractSamples.OptionWithLocalSymbol("AAPL 180420C00180000"), "100,101,104,106,233,236,258", False, False, []) # ! [reqsecdefoptparams] print('request done!!')
def paper_trade(self): my_client = MongoClient('10.12.0.30', 27017) my_db = my_client['research'] my_col = my_db['optionsonar'] data = my_col.find({}, {'_id': 0}) data_pd = pd.DataFrame(list(data)) start_num = len(data_pd) order_done = False while not self.client.process_done and not order_done: if self.client.nextValidOrderId is not None: self.client.placeOrder(self.client.nextOrderId(), ContractSamples.OptionWithLocalSymbol('AAPL 180420C00180000'), OrderSamples.LimitOrder("BUY", 3, 0.1)) order_done = True else: time.sleep(1) print('wait for placing order') print('place order done')
def option_minutes_req(self): client = MongoClient('127.0.0.1', 27017) my_db = client.option_data_us_mins index_continue = 2647 time_continue = 0 if index_continue == -1: for index in self.stock_code_map.keys(): if self.client.process_done: break else: stock_code = self.stock_code_map[index] my_db[stock_code].create_index([('date', ASCENDING), ('option_code', ASCENDING) ]) self.client.reqContractDetails( index, ContractSamples.OptionForQuery(stock_code)) time.sleep(30) print('Asked for ', str(stock_code)) while not self.client.req_opt_contract_end and not self.client.process_done: print('waiting for req_opt_contract_end Done') time.sleep(1) print(self.client.option_code_map) else: temp = pd.read_csv('option_code_map.csv') self.client.option_code_map = temp['option_code'].values.tolist() if index_continue < 0: index_continue = 0 option_code_map = self.client.option_code_map queryTime = datetime.datetime(2017, 11, 24, 23, 59, 59) queryTime += timedelta(days=7 * time_continue) while queryTime < datetime.datetime( 2018, 3, 19, 23, 59, 59) and not self.client.process_done: for index in range(index_continue, len(option_code_map)): if self.client.process_done: break order_id = index * 100000 + time_continue option_code = option_code_map[index] time_recorder = 0 self.client.queryTime = queryTime.strftime("%Y%m%d %H:%M:%S") self.client.reqHistoricalData( order_id, ContractSamples.OptionWithLocalSymbol(option_code), queryTime.strftime("%Y%m%d %H:%M:%S"), "5 D", "1 min", "TRADES", 1, 1, False, []) time.sleep(random.randint(8, 15)) while not self.client.process_done: if self.client.opt_req_next_code: self.client.opt_req_next_code = False print('next code...............') break else: time.sleep(1) time_recorder += 1 if time_recorder > 120: # 2分钟内没有返回内容,则跳过 fw = open('data.txt', 'a') fw.write(option_code + ', ' + str(order_id) + ', ' + str(queryTime) + '\n') fw.close() self.client.opt_req_next_code = True # break print(datetime.datetime.now(), " ", order_id, ' sleeping.................') index_continue = 0 time_continue += 1 queryTime += timedelta(days=7)