def __saveQuotesToDB(self, listQuotes): dataForQuery = [] for i in range(0, len(listQuotes)): dataForQuery.append( (MySQLdb.TimestampFromTicks(listQuotes[i]['timeStamp']), listQuotes[i]['provider'], listQuotes[i]['currencyPair'], 'BID', listQuotes[i]['bid']['quote'][0]['volume'], listQuotes[i]['bid']['quote'][0]['price'], MySQLdb.TimestampFromTicks(listQuotes[i]['timeStampMDL']))) dataForQuery.append( (MySQLdb.TimestampFromTicks(listQuotes[i]['timeStamp']), listQuotes[i]['provider'], listQuotes[i]['currencyPair'], 'OFFER', listQuotes[i]['offer']['quote'][0]['volume'], listQuotes[i]['offer']['quote'][0]['price'], MySQLdb.TimestampFromTicks(listQuotes[i]['timeStampMDL']))) self.cursor.executemany(self.confObj.queryInsertQuotes, dataForQuery) self.adapterMySQL.disconnect()
def __handlerQuotes(self): while True: time.sleep(1) #handler quotes if self.__listQuotes.qsize( ) >= self.confObj.countQuotesForOneOrder: listQuotes = [] for i in range(0, self.confObj.countQuotesForOneOrder): listQuotes.append(self.__listQuotes.get()) if i == self.confObj.countQuotesForOneOrder - 1: order = self.__gen.generate_data('Order') order['timeStamp'] = MySQLdb.TimestampFromTicks( int(order['timeStamp'] / 1000)) order['currencyPair'] = listQuotes[ self.confObj.countQuotesForOneOrder - 1]['currencyPair'] if order['tradeType'] == 0: order['filledPrice'] = listQuotes[ self.confObj.countQuotesForOneOrder - 1]['bid']['quote'][0]['price'] order['filledVolume'] = listQuotes[ self.confObj.countQuotesForOneOrder - 1]['bid']['quote'][0]['volume'] elif order['tradeType'] == 1: order['filledPrice'] = listQuotes[ self.confObj.countQuotesForOneOrder - 1]['offer']['quote'][0]['price'] order['filledVolume'] = listQuotes[ self.confObj.countQuotesForOneOrder - 1]['offer']['quote'][0]['volume'] self.__listOrders.put(order) self.__saveQuotesToDB(listQuotes) #handler orders if self.__listOrders.qsize() >= self.confObj.countOrdersForSave: listOrders = [] for i in range(0, self.confObj.countOrdersForSave): listOrders.append(self.__listOrders.get()) self.__saveOrdersToDB(listOrders)
def db_TimestampFromTicks(*args, **kwargs): return MySQLdb.TimestampFromTicks(*args, **kwargs)
def transsync(): cursor.execute("SELECT lastblock from btc_synclog WHERE type = 'trans' and status = 'S' order by timestamp desc LIMIT 0,1") result = cursor.fetchone() if result is not None: lastblock = result[0] else: lastblock = '*' try: res = ag.listsinceblock(lastblock, btc_minconf) for tx in res['transactions']: cursor.execute("DELETE FROM btc_trans where txid=%s", tx['txid']) if 'fee' not in tx: vtxfee = '0' else: vtxfee = tx['fee'] cursor.execute("INSERT INTO btc_trans(type,user,amount,fee,address,confirm,txid,timestamp)VALUES (%s,%s,%s,%s,%s,%s,%s,%s)", [tx['category'], tx['account'], tx['amount'],vtxfee, tx['address'], tx['confirmations'], tx['txid'], MySQLdb.TimestampFromTicks(tx['time'])]) updateuser(tx['account']) if len(res['transactions']) > 0: cursor.execute("DELETE FROM btc_synclog WHERE lastblock = %s",res['lastblock']) cursor.execute("INSERT INTO btc_synclog(type,lastblock,status,message)VALUES ('trans',%s,'S',%s)", [res['lastblock'], len(res['transactions'])]) logger.info(str(len(res['transactions']))+ ' transactions synced.') except Exception as inst: cursor.execute("INSERT INTO btc_synclog(type,status,message)VALUES ('trans','F',%s)", "Err:{0}{1}".format(type(inst),inst.args)) logger.error('TransSync Error'+ str(type(inst))+ str(inst.args))