def get_soc_wicc_day_prices(): # 2018-09-12 今天取前百日(本来想取200, 但是soc和wicc对usdt的交易没这么多数据)的数据算出 k = 9.0902; n = 0.0468 # soc_kline = HuobiService.get_kline("socusdt", "1day", "100") ''' 2018-09-18 11:11 今天取百日数据算的 k = 6.9950; n = 0.1150; 用这组数据回测收益较低, 所以这里取106日的数据试试 2018-09-18 106日的数据算出来 k = 9.1002; n = 0.0458 ''' days = 106 soc_kline = HuobiService.get_kline("socusdt", "1day", "%d" % days) soc_day_prices = [] for soc_bean in soc_kline['data']: soc_day_prices.append(float(soc_bean['close'])) print soc_day_prices common_utils.write_json("soc_day_prices.txt", soc_day_prices, list) time.sleep(5) wicc_kline = HuobiService.get_kline("wiccusdt", "1day", "%d" % days) wicc_day_prices = [] for wicc_bean in wicc_kline['data']: wicc_day_prices.append(float(wicc_bean['close'])) print wicc_day_prices common_utils.write_json("wicc_day_prices.txt", wicc_day_prices, list)
def on_minute(): global kline, usdt, buythresh, sellupthresh, selldownthresh Timer(60, on_minute).start() curtime = int(time()) # 更新价格 try: for symbol in kline: klinedata = HuobiService.get_kline(symbol, '1min', 1)['data'][0] kline[symbol]['time'] = klinedata['id'] kline[symbol]['high'] = klinedata['high'] kline[symbol]['low'] = klinedata['low'] kline[symbol]['open'] = klinedata['open'] kline[symbol]['close'] = klinedata['close'] print symbol, kline[symbol], kline[symbol] if curtime - klinedata['id'] > 120: print 'Time different from server:', symbol coinid = symbol.replace('usdt', '') # 如果购买则更新maxprice if coinid in usdtcoins and usdtcoins[coinid]['buy']: if klinedata['high'] > usdtcoins[coinid]['maxprice']: usdtcoins[coinid]['maxprice'] = klinedata['high'] except Exception as e: print 'Error in get_kline', e.message return # 更新当前货币量 try: balancedata = get_balance() for item in balancedata: if item['type'] == 'trace' and ( item['currency']) in usdtcoins: # 不算冻结资金 usdtcoins[item]['balance'] = float(item['balance']) except Exception as e: pass # 检测哪些币要卖 buyable_symbols = [] for symbol in kline: # 策略2 => 1分钟内涨幅降幅超过各自门限的多个币 均摊 optype = 'none' # none buy sell if symbol.endswith('usdt') and symbol != 'usdt': coinid = symbol.replace('usdt', '') op = kline[symbol]['open'] # 开盘价 cp = kline[symbol]['close'] # 收盘价 bp = usdtcoins[coinid]['buyprice'] mp = usdtcoins[coinid]['maxprice'] if cp / op > buythresh: # 涨到一定程度则入手 buyable_symbols.append(symbol) #elif bp > 0.0 and (cp / bp > sellupthresh or cp / bp < selldownthresh): # 已经入手且降到一定程度则出手 # optype = 'buy-market' elif mp > 0.0 and cp / mp < selldownthresh: # 市价卖出,清除数据 sell(usdtcoins[coinid]['balance'], symbol) usdtcoins[coinid] = { 'buyprice': 0.0, 'maxprice': 0.0, 'balance': 0.0, 'optype': 'none' } # 更新卖出后的usdt数量 #需要等待吗? usdtamount = 0.0 try: balancedata = get_balance() for item in balancedata: coinid = item['currency'] if item['type'] == 'trace' and coinid in usdtcoins: # 不算冻结资金 usdtcoins[item]['balance'] = float(item['balance']) if coinid == 'usdt': usdtamount = float(item['balance']) except Exception as e: pass if usdtamount > 1.0 and len(buyable_symbols) > 0: # 如果有可买货币则进行对冲 splitamount = usdtamount / len(buyable_symbols) for symbol in buyable_symbols: coinid = symbol.replace('usdt', '') buy(splitamount, symbol) usdtcoins[coinid]['buy'] = True