def verifyData(self): item = self.treeContracts.selection() if not item: disLog( getframeinfo(currentframe()).filename + ":" + sys._getframe().f_code.co_name + ":" + str(getframeinfo(currentframe()).lineno), "nothing selected") else: conID = self.treeContracts.item(item)['values'][0] symbol = 'T' + str(conID) oneStock: OneStock = self.gui.dbLite.getOneStock2(symbol) format2 = '%Y%m%d %H:%M:%S' i = 0 for item in oneStock.bars[:-1]: format1 = '%Y%m%d %H:%M:%S' d0 = dt.datetime.strptime(item.date, format2) d1 = dt.datetime.strptime(oneStock.bars[i + 1].date, format2) delta = (d1 - d0).total_seconds() if delta > 60: disLog( getframeinfo(currentframe()).filename + ":" + sys._getframe().f_code.co_name + ":" + str(getframeinfo(currentframe()).lineno), item.date, oneStock.bars[i + 1].date) i = i + 1
def Start(self): disLog( getframeinfo(currentframe()).filename + ":" + sys._getframe().f_code.co_name + ":" + str(getframeinfo(currentframe()).lineno), "start in watch") if self.activeStocks == None: self.activeStocks = self.gui.dbLite.getActive() #let's verify if the 1 min data is update in the last 60 minute otherwise don't start the watch mes = toMessage("CreatePorfolio", self.activeStocks) self.gui.toWat.put(mes) for item in self.activeStocks: dt = self.gui.dbLite.getMaxDateTime(item.contractID) FMT1 = '%Y%m%d %H:%M:%S' s1 = datetime.strftime(datetime.now(), FMT1) tdelta = datetime.strptime(s1, FMT1) - datetime.strptime(dt, FMT1) #we need to have download 1 minute less than 1 hours 3600 seconds #we set to 3400 because the download of 5 seconds can take some time if tdelta.seconds > 3000: print( "you don't have enough data on 1 minute, please download 1 minute first" ) return else: print('start sending to load 1 min', item.contractID) #toGui = toMessage('load1min', item.contractID) #self.gui.toGui.put(toGui) self.gui.load1min(item.contractID) time.sleep(1) time.sleep(3) print('I finish to upload 1 min') # mes = toMessage("start5sec", self.activeStocks) # self.gui.toWat.put(mes) #print(len(self.activeStocks),self.activeStocks[0].symbol) for item in self.activeStocks: toTWS = toMessage('Start5SecWatch', item) self.gui.toTws.put(toTWS) #break time.sleep(5) #we need time to be able to have continuous series for item in self.activeStocks: toTWS = toMessage('histnew5secOneTime', item) self.gui.toTws.put(toTWS)
def DispTest(self): # d=self.dbLite.getOneStock('T11') # convertToHigher(d,'5min') # convertToSameVolume(d[0:3],1500) d = self.gui.dbLite.getOneStock2('11') disLog( getframeinfo(currentframe()).filename + ":" + sys._getframe().f_code.co_name + ":" + str(getframeinfo(currentframe()).lineno), d.bars[1]) pass
def keyboardInterrupt(self): """Callback - User pressed Ctrl-C""" self.nKeybInt += 1 if self.nKeybInt == 1: msg = "Manual interruption!" logging.warn(msg) print(msg) self._onStop() else: msg = "Forced Manual interruption!" logging.error(msg) disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), msg) self.done = True
def checkMsg(self): try: while not self.toPre.empty(): msg: toMessage = self.toPre.get_nowait() if msg.purpose == "exit": self.exit = True if msg.purpose == "CreatePorfolio": self.createPortfolio(msg.obj) if msg.purpose == "stop5sec": print("i can display the portfolio in stop5sec") # tickid=msg.obj.contractID # # f = open(str(tickid) + "-5sec-final.txt", "a") # for item in self.oneportfolio.stocks[tickid].bars5sec: # f.write( # item.date + ' ' + str(item.open) + ' ' + str(item.high) + ' ' + str(item.low) + ' ' + str( # item.close) + ' ' + str(item.volume) + '\r\n') # f.close() # # f = open(str(tickid) + "-1min-final.txt", "a") # for item in self.oneportfolio.stocks[tickid].bars1min: # f.write( # item.date + ' ' + str(item.open) + ' ' + str(item.high) + ' ' + str(item.low) + ' ' + str( # item.close) + ' ' + str(item.volume) + '\r\n') # f.close() if msg.purpose == "5sec": self.do5sec(msg.obj) elif msg.purpose == "tickbytick": self.tickbytick(msg.obj) elif msg.purpose == "5secHist": print("5secHist") self.insert5sec(msg.obj) elif msg.purpose == "data1min": #this is data for 1 min self.data1min(msg.obj) elif msg.purpose == "timeframe": self.timeframe = msg.obj else: print("message unkown in checkMsg in WatchClasses") pass except queue.Empty: disLog( getframeinfo(currentframe()).filename + ":" + sys._getframe().f_code.co_name + ":" + str(getframeinfo(currentframe()).lineno), "empty") pass
def deleteContract(self): it = self.treeContracts.selection() if not it: print("nothing selected") else: self.treeContracts.delete(it) disLog( getframeinfo(currentframe()).filename + ":" + sys._getframe().f_code.co_name + ":" + str(getframeinfo(currentframe()).lineno), it[0]) self.gui.dbLite.deleteContract(it[0]) for item in (self.eCurrency, self.eSymbol, self.eExpire, self.cbExchange, self.cbSecType, self.cbBarSize, self.cbWhatToShow, self.cbActive, self.eOrderSize, self.eStopSize, self.eOrderExtra): item.delete(0, "end")
def winError(self, text:str, lastError:int): EWrapper.winError(text, lastError) disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), 'win error')
def onLoopIteration(self): # this is called from ClientClass try: msg:toMessage = self.toTws.get_nowait() #logging.info(f'GUI MESSAGE: {msg}') disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), 'msg.purpose on appclass ',msg.purpose) if msg.purpose=='histold': if(self.downloadHistInfo.get(msg.obj.contractID)==None): self.downloadHistInfo[msg.obj.contractID]=downloadHist()# need to be outside because we don't want to call many times self.downloadHistInfo[msg.obj.contractID].whatToDownload = 'histold' self.getHistOld(msg.obj) elif msg.purpose in ['histnew','histnewLevel1']: if (self.downloadHistInfo.get(msg.obj.contractID) == None): self.downloadHistInfo[msg.obj.contractID]=downloadHist() self.downloadHistInfo[msg.obj.contractID].whatToDownload=msg.purpose self.getHistNew(msg.obj) elif msg.purpose=='histnew5secOneTime': if (self.downloadHistInfo.get(msg.obj.contractID) == None): self.downloadHistInfo[msg.obj.contractID]=downloadHist() self.downloadHistInfo[msg.obj.contractID].whatToDownload='histnew5secOneTime' else: print('i got this contract id',msg.obj.contractID) self.getHistNew(msg.obj) elif msg.purpose == 'histstop': self.downloadHistInfo.pop(msg.obj.contractID) elif msg.purpose == 'histgap': #not implemented yet it may happen if a stock was download for a long period of time. pass elif msg.purpose=='marketdata': self.getMarketData(msg) elif msg.purpose=='watchStart': self.watchStart(msg) elif msg.purpose=='watchStop': self.watchStop(msg) elif msg.purpose=='Start5SecWatch': self.watchStart5Sec(msg) elif msg.purpose=='Stop5SecWatch': self.Stop5SecWatch(msg) elif msg.purpose=='cancelmarketdata': self.cancelMktData(self.idmarketdata) elif msg.purpose=='connect': #self.connect('127.0.0.1', 7496, 1) #live self.connect('127.0.0.1', 7497, 1) #paper elif msg.purpose=='disconnect': self.disconnect() elif msg.purpose=='PlaceOrder': #self.cancelOrder(msg.obj[0]) x=self.nextId print('order id=',x) self.placeOrder(x,msg.obj[1],msg.obj[2]) #self.cancelOrder(58) elif msg.purpose == 'exit': self.exit() elif msg.purpose == 'database': disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), msg.obj) self.dbLite=DB(msg.obj) if self.dbLite.conn != '': disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), 'i am connected to database') elif msg.purpose == 'bubu': disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), 'i am bubu from appclass') else: #logging.error(f'Unknown GUI message: {msg}') disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), "Unknown GUI messageXXXX") except queue.Empty: pass self.count = 0
def onStop(self): if self._file: self._file.close() #logging.info('Main logic stopped') disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), 'Main logic stopped')
def onStart(self): #logging.info('Main logic started') disLog(getframeinfo(currentframe()).filename+":"+sys._getframe().f_code.co_name+":"+str(getframeinfo(currentframe()).lineno), 'Main logic started')