def ltcData(): print u'enter ltcData Collection' dbLink = db.db_control() try: ltcData = requests.get(r'http://api.huobi.com/staticmarket/ltc_kline_015_json.js',timeout=5) handlerltc = json.loads(ltcData.text) handlerltc = handlerltc[-200:] xBxisData = [] ltcData = [] for ltc in handlerltc: timeData = ltc[0][8:12] timeData = timeData[0:2]+':'+timeData[2:] xBxisData.append(timeData) ltc = ltc[1:5] h = ltc[1]; l = ltc[2]; c = ltc[3]; ltc[1] = c; ltc[2] = l; ltc[3] = h; ltcData.append(ltc) dbLink.insert('ltcData',time.strftime('%Y%m%d%H%M%S',time.localtime()),encode_publicData(str(xBxisData)),ltcData) except BaseException as e: print u'network wrong',e return count = len(dbLink.select('ltcData')) if ltcData else 0 if count >= 3: dbLink.delete('ltcData',Time=(min(dbLink.select('ltcData')))[0])
def main(): while True: print 'enter privateDataCollection' dbLink = db.db_control() users = dbLink.select('user') print users for user in users: if len(user[4]) == 32 and len(user[5]) == 32: now = datetime.now() print 'now:', now.strftime('%Y%m%d-%H:%M') try: personalH = pH.personalHandler(user[4], user[5]) ProfitRate = personalH.ProfitRate(user[1]) net_asset = personalH.net_asset total = personalH.total bombprice = personalH.BombPrice() dbLink.insert( 'privateData', user[1], time.strftime('%Y%m%d%H%M%S', time.localtime()), net_asset, ProfitRate, total, bombprice) except BaseException as e: print u'huobi api fxcking shit again!', e privateData = dbLink.select('privateData') count = len(dbLink.select('privateData')) if privateData else 0 if count >= 50: dbLink.delete('privateData', Time=(min(dbLink.select('privateData')))[0]) time.sleep(3)
def ltcDataReturn(): dbLink = db.db_control() ltcData = dbLink.select('ltcData') if ltcData: data = {'xBxisData':decode_publicData(str(ltcData[-1][1])), 'ltcData':ltcData[-1][2] } return data
def publicDataReturn(): #返回dealData,tickerltc,tickerbtc dbLink = db.db_control() publicData = dbLink.select('publicData') if publicData: data = {'time':publicData[-1][0], 'dealdata':decode_publicData(str(publicData[-1][1])), 'ticker_ltc':publicData[-1][2], 'ticker_btc':publicData[-1][3], 'ltcTradeVol':publicData[-1][4]} return data
def main(): print 'enter PublicData collection' dbLink = db.db_control() try: publicData = dbLink.select('publicData') ltc = requests.get( r'http://api.huobi.com/staticmarket/ticker_ltc_json.js', timeout=5) ticker_ltc = ltc.json()['ticker']['last'] ltcTradeVol = ltc.json()['ticker']['vol'] btc = requests.get( r'http://api.huobi.com/staticmarket/ticker_btc_json.js', timeout=5) ticker_btc = btc.json()['ticker']['last'] dealltcData = requests.get( r'http://api.huobi.com/staticmarket/detail_ltc_json.js', timeout=5) dealdata = dealltcData.json() dealdata = encode_publicData(str(dealdata['trades'][0])) dbLink.insert('publicData', time.strftime('%Y%m%d%H%M%S', time.localtime()), dealdata, ticker_ltc, ticker_btc, ltcTradeVol) count = len(dbLink.select('publicData')) if publicData else 0 if count >= 5: dbLink.delete('publicData', Time=(min(dbLink.select('publicData')))[0]) ltcData = requests.get( r'http://api.huobi.com/staticmarket/ltc_kline_015_json.js', timeout=5) handlerltc = json.loads(ltcData.text) handlerltc = handlerltc[-200:] xBxisData = [] ltcData = [] for ltc in handlerltc: timeData = ltc[0][8:12] timeData = timeData[0:2] + ':' + timeData[2:] xBxisData.append(timeData) ltc = ltc[1:5] h = ltc[1] l = ltc[2] c = ltc[3] ltc[1] = c ltc[2] = l ltc[3] = h ltcData.append(ltc) dbLink.insert('ltcData', time.strftime('%Y%m%d%H%M%S', time.localtime()), encode_publicData(str(xBxisData)), ltcData) count = len(dbLink.select('ltcData')) if ltcData else 0 if count >= 3: dbLink.delete('ltcData', Time=(min(dbLink.select('ltcData')))[0]) except BaseException as e: print u'publicData colleciton fxcking the bolt shit wrong', e
def ProfitDataCollection(): while True: print 'enter ProfitDataColleciton' db = dbL.db_control() users = db.select('user') print users for user in users: if len(user[4]) == 32 and len(user[5]) == 32: now = datetime.now() print 'now:', now.strftime('%Y%m%d-%H:%M') str_time = now.strftime('%M') timeRun = [0, 15, 30, 45, 60, 0] if int(str_time) in timeRun: personalH = pH.personalHandler(user[4].encode('utf-8'), user[5].encode('utf-8')) profitData = db.select('profitData', uid=user[1]) setting = db.select('SETTING', UID=user[1]) count = max([item[0] for item in profitData ]) + 1 if profitData else 0 profit = personalH.profit if profit == 0: return db.insert('profitData', count, time.strftime('%Y%m%d%H%M%S'), setting[0][0], setting[0][0], profit) else: for n in timeRun: if timeRun[timeRun.index(n) - 1] < int(str_time) < n: print u'休息时间' print(n - int(str_time)) * 60 time.sleep((n - int(str_time)) * 60) personalH = pH.personalHandler( user[4].encode('utf-8'), user[5].encode('utf-8')) profitData = db.select('profitData', uid=user[1]) setting = db.select('SETTING', UID=user[1]) count = max([item[0] for item in profitData ]) + 1 if profitData else 0 profit = personalH.profit if profit == 0: return db.insert( 'profitData', count, time.strftime('%Y%m%d%H%M%S', time.localtime()), setting[0][0], setting[0][0], profit) else: pass time.sleep(60)
def main(): while True: print 'enter privateDataCollection' dbLink = db.db_control() users = dbLink.select('user') print users for user in users: if len(user[4]) == 32 and len(user[5]) == 32: now = datetime.now() print 'now:', now.strftime('%Y%m%d-%H:%M') try: personalH = pH.personalHandler(user[4], user[5]) ProfitRate = personalH.ProfitRate(user[1]) net_asset = personalH.net_asset total = personalH.total bombprice = personalH.BombPrice() str_now = time.strftime('%Y%m%d%H%M%S', time.localtime()) dbLink.insert('privateData', user[1], str_now, net_asset, ProfitRate, total, bombprice) dealOrders = personalH.dealOrder() if dealOrders: print dealOrders for order in dealOrders: # 'uid BLOB','time BLOB','order_id integer','order_time BLOB','last_processed_time BLOB','order_amount BLOB','order_price BLOB','type BLOB' now = time.strftime(r'%Y/%m/%d %H:%M:%S', time.localtime()) msg = now msg += u' 卖出' if int( order['type']) == 2 else u' 买入' msg += u' 已成交 %s %s' % (order['order_price'], order['order_amount']) print msg dbLink.insert('dealOrder', user[1], str_now, order['id'], order['order_time'], order['last_processed_time'], order['order_amount'], order['order_price'], order['type'], msg) # 'tradePenny','uid integer','BuyOrSell BLOB','Coin Blob','Amount BLOB','Price BLOB','Status Blob','msg BLOB','order_id BLOB' except BaseException as e: print u'huobi api fxcking shit again!:', e privateData = dbLink.select('privateData') count = len(dbLink.select('privateData')) if privateData else 0 if count >= 50: dbLink.delete('privateData', Time=(min(dbLink.select('privateData')))[0]) time.sleep(3)
def privateDataReturn(uid): dbLink = db.db_control() data = dbLink.select('privateData', uid=uid) if data: return data[-1] return
#!/usr/bin/env python # coding:utf-8 import tornado.web import db import os import sys import time sys.path.append(os.path.dirname(os.path.dirname(os.getcwd()))) from strategy import parameter as p from strategy.setting import total_money if __name__ == '__main__': db = db.db_control() p = p.parameter() #db.creatTable('SETTING','UID INTEGER PRIMARY KEY','TESTMONEY BLOB','TOTALMONEY BLOB','BUYCOUNT BLOB','overHighPrice BLOB','overLowPrice BLOB','COEFFICIENT BLOB','lastPrice BLOB','OneBuyCount integer') #db.insert('SETTING', 0,700,700,1,27,25,0.985,0,1) #db.insert('SETTING', 1,0,0,1,27,25,0.985,0,1) #db.delete('SETTING') d1 = {'PriceDict':{0:3,1:6,2:7,3:11,4:20,5:21}} d2 = {'UID':1} db.update('SETTING',d1,d2) d = db.select('SETTING') print d