def run(self): data = self.task.data db = mysql_db.Mysql() cursor = db._cursor actions = self.task.action.split('/') market = actions[1] chs = self.task.data['ch'].split('.') symbol = chs[1] records = data['tick']['data'] for record in records: date = time.strftime("%Y%m%d", time.localtime()) datetime = time.strftime("%Y%m%d%H%M%S", time.localtime()) sql = "INSERT INTO `trade` (`market`, `symbol`, `price`,`amount`,`time`,`direction`,`trade_id`,`create_date`,`create_time`) VALUES ('%s', '%s', '%G', '%G', '%d', '%s', '%d','%s','%s')" % ( market, symbol, record['price'], record['amount'], record['ts'], record['direction'], record['id'], date, datetime) try: cursor.execute(sql) db.commit() except BaseException as e: logging.error(record) logging.error(e) db.rollback() db.dispose()
def update_kline_data(self, market, symbol, period, record, db): # 连接MySQL数据库 db = mysql_db.Mysql() cursor = db._cursor date = time.strftime("%Y%m%d", time.localtime()) datetime = time.strftime("%Y%m%d%H%M%S", time.localtime()) s_uuid = ''.join(str(uuid.uuid1()).split('-')) sql = "INSERT INTO `kline` (`id`, `market`, `symbol`,`period`, `kid`,`count`,`amount`,`open`,`close`,`low`,`high`,`vol`,`create_date`,`create_time`) VALUES ('%s', '%s', '%s', '%s', '%d', '%d', '%G', '%G', '%G','%G','%G','%G','%s','%s')" % ( s_uuid, market, symbol, period, record['id'], record['count'], record['amount'], record['open'], record['close'], record['low'], record['high'], record['vol'], date, datetime) try: querySQL = "delete from kline where market='%s' and symbol='%s' and period='%s' and kid='%d'" % ( market, symbol, period, record['id']) cursor.execute(querySQL) except BaseException as e: logging.error(record) logging.error(e) try: cursor.execute(sql) db.commit() except BaseException as e: logging.error(record) logging.error(e) #db.rollback() return True
def run(self): data = self.task.data # 连接MySQL数据库 db = mysql_db.Mysql() cursor = db._cursor actions = self.task.action.split('/') market = actions[1] chs = self.task.data['ch'].split('.') symbol = chs[1] period = chs[3] if actions[2] == 'ws': record = data['tick'] self.update_kline_data(market, symbol, period, record, db) ctx = context.get_context() if ctx != None: tick_task = {'market': market, 'dtype': 'kline', 'symbol': symbol, 'did': record['id'], 'period': period} ctx['app'].pub_task( None, 'exbroker/kline/tick', '0', tick_task) else: for record in data['data']: ret = self.update_kline_data(market, symbol, period, record, db) db.dispose()
def run(self): first = True # 连接MySQL数据库 db = mysql_db.Mysql() cursor = db._cursor while True: sql_query = "SELECT * FROM market_schedule WHERE next_run_time < '%d'" % time.time( ) try: results = cursor.execute(sql_query) except BaseException as ex: logging.error(ex) break results = cursor.fetchall() for row in results: reqs = row['reqs'].split(',') for req in reqs: req_str = row['req_tpl'] % req if first and row['req_first_tpl'] != None: req_str = row['req_first_tpl'] % req req_data = json.loads(req_str) ev = events.Event(row['action'], str(1), req_data) logging.debug(row['action'] + ':' + req_str) if self.service.ctx != None: r = self.service.ctx['app'].evt_mng.pub_event(ev) next_time = time.time() + 60 if row['freq'] == 'd': next_time = time.time() + 60 * 60 * 24 sql_update = "update market_schedule set last_run_time=%d, next_run_time=%d where id='%s'" % ( time.time(), next_time, row['id']) try: cursor.execute(sql_update) db.commit() except BaseException as ex: logging.error(ex) db.rollback() break if first: first = False r = self.task_process() if r: break time.sleep(1) db.dispose()
def run(self): # 连接MySQL数据库 db = mysql_db.Mysql() cursor = db._cursor topic = self.task.data['dtype'] if self.task.data['dtype'] == 'kline': topic = topic + '.' + \ self.task.data['symbol'] + '.' + self.task.data['period'] sql_query = "SELECT * FROM strategy_tick WHERE market='%s' and topic='%s' and status='00'" % ( self.task.data['market'], topic) try: results = cursor.execute(sql_query) except BaseException as ex: logging.error(ex) db.dispose(0) return results = cursor.fetchall() for row in results: action = row['action'] logging.debug(action + ":" + row['data']) task_data = json.loads(row['data']) task_data['id'] = row['strategy_id'] task_data['uid'] = row['uid'] task_data['type'] = 'tick' task_data['market'] = self.task.data['market'] task_data['did'] = self.task.data['did'] ctx = context.get_context() if ctx != None: ctx['app'].pub_task(None, action, '0', task_data) sql_update = "update strategy_tick set last_run_time=%d where id='%s'" % ( time.time(), row['id']) try: cursor.execute(sql_update) db.commit() except BaseException as ex: logging.error(ex) db.rollback() break db.dispose()
def run(self): data = self.task.data return db = mysql_db.Mysql() cursor = db._cursor actions = self.task.action.split('/') market = actions[1] chs = self.task.data['ch'].split('.') symbol = chs[1] record = data['tick'] ktype = chs[3] date = time.strftime("%Y%m%d", time.localtime()) datetime = time.strftime("%Y%m%d%H%M%S", time.localtime()) sql = "INSERT INTO `depth` (`market`, `symbol`,`ktype`, `kid`,`count`,`amount`,`open`,`close`,`low`,`high`,`vol`,`create_date`,`create_time`) VALUES ('%s', '%s', '%s', '%d', '%d', '%G', '%G', '%G','%G','%G','%G','%s','%s')" % ( market, symbol, ktype, record['id'], record['count'], record['amount'], record['open'], record['close'], record['low'], record['high'], record['vol'], date, datetime) try: querySQL = "SELECT count(1) as num from kline where market='%s' and symbol='%s' and ktype='%s' and kid='%d'" % ( market, symbol, ktype, record['id']) cursor.execute(querySQL) row = cursor.fetchone() if row != None and row['num'] > 0: return except BaseException as e: logging.error(data) logging.error(e) try: cursor.execute(sql) db.commit() except BaseException as e: logging.error(data) logging.error(e) db.rollback() finally: db.dispose()
def order(self, market, symbol, amount, price): db = mysql_db.Mysql() s_uuid = ''.join(str(uuid.uuid1()).split('-')) cur_time = time.time() action = 'buy' vol = amount if amount < 0: action = 'sell' vol = 0 - amount attrs = { 'id': s_uuid, 'uid': self._uid, 'strategy_id': self.strategy_id, 'market': market, 'symbol': symbol, 'time': '%d' % cur_time, 'action': action, 'status': '00', 'price': '%G' % price, 'vol': '%G' % vol, 'amount': '%G' % (price * vol), 'did': '%d' % self.did, 'create_date': time.strftime("%Y%m%d", time.localtime(cur_time)), 'create_time': time.strftime("%Y%m%d%H%M%S", time.localtime(cur_time)), } try: db._insertDic('tx', attrs) logging.info( 'order stategy[%s] market[%s] symbol[%s] amount[%f] price[%s]' % (self.strategy_id, market, symbol, amount, price)) except BaseException as ex: logging.error(ex) db.dispose()
def run(self): # 连接MySQL数据库 db = mysql_db.Mysql() cursor = db._cursor last_sec = None while True: r = self.task_process() if r: break cur_time = time.localtime() cur_sec = time.strftime("%Y%m%d%H%M", cur_time) cur_min = time.strftime("%Y%m%d%H%M", cur_time) cur_day = time.strftime("%Y%m%d", cur_time) if last_sec != None and last_sec == cur_sec: time.sleep(1) continue sql_query = "SELECT * FROM strategy_schedule WHERE next_run_time < '%d' and status='00'" % time.time( ) try: results = cursor.execute(sql_query) except BaseException as ex: logging.error(ex) break results = cursor.fetchall() for row in results: action = row['action'] last_time = time.localtime(row['last_run_time']) if row['freq'] == 'd' and cur_time.tm_mon == last_time.tm_mon and cur_time.tm_mday == last_time.tm_mday: continue logging.debug(action + ":" + row['data']) did = time.mktime(time.strptime(cur_sec, '%Y%m%d%H%M')) if row['freq'] == 'd': did = time.mktime(time.strptime(cur_day, '%Y%m%d')) task_data = json.loads(row['data']) task_data['id'] = row['strategy_id'] task_data['uid'] = row['uid'] task_data['did'] = did next_time1 = time.strftime("%Y%m%d%H%M", time.localtime(time.time() + 60)) next_time = time.mktime(time.strptime(next_time1, '%Y%m%d%H%M')) if row['freq'] == 'd': next_time1 = time.strftime( "%Y%m%d", time.localtime(time.time() + 60 * 60 * 24)) next_time = time.mktime(time.strptime( next_time1, '%Y%m%d')) if self.service.ctx != None: self.service.ctx['app'].pub_task(None, action, '0', task_data) sql_update = "update strategy_schedule set last_run_time=%d, next_run_time=%d where id='%s'" % ( time.time(), next_time, row['id']) try: cursor.execute(sql_update) db.commit() except BaseException as ex: logging.error(ex) db.rollback() break last_sec = cur_sec db.dispose()