okcoinFuture = OKCoinFuture(okcoinRESTURL, apikey, secretkey) ''' print (u' 现货行情 ') print (okcoinSpot.ticker('btc_usd')) print (u' 现货深度 ') print (okcoinSpot.depth('btc_usd')) print (u' 现货历史交易信息 ') print (okcoinSpot.trades('btc_usd',0)) print (u'期货交易记录信息') print (okcoinFuture.future_trades('ltc_usd','this_week',0)) ''' print(u'期货分钟日线数据') print(okcoinFuture.future_kline('btc_usd', '1min', 'this_week', 0, 0)) print(u' it has done the job') print(okcoinSpot.kline('btc_usd', '1min', 0, 0)) #print (u' 用户现货账户信息 ') #print (okcoinSpot.userinfo()) #print (u' 现货下单 ') #print (okcoinSpot.trade('ltc_usd','buy','0.1','0.2')) #print (u' 现货批量下单 ') #print (okcoinSpot.batchTrade('ltc_usd','buy','[{price:0.1,amount:0.2},{price:0.1,amount:0.2}]')) #print (u' 现货取消订单 ') #print (okcoinSpot.cancelOrder('ltc_usd','18243073'))
#print (okcoinFuture.future_depth('btc_usd','this_week','6')) #print (u'期货交易记录信息') #print (okcoinFuture.future_trades('ltc_usd','this_week')) #print (u'期货指数信息') #print (okcoinFuture.future_index('ltc_usd')) #print (u'美元人民币汇率') #print (okcoinFuture.exchange_rate()) #print (u'获取预估交割价') #print (okcoinFuture.future_estimated_price('ltc_usd')) print(u'获取虚拟合约的K线信息') print(okcoinFuture.future_kline('ltc_usd', 'this_week', '1min', '50')) print(u'获取当前可用合约总持仓量') print(okcoinFuture.future_hold_amount('ltc_usd', 'this_week')) print(u'获取合约坐高买价和最低卖价格') print(okcoinFuture.future_price_limit('ltc_usd', 'this_week')) #print (u'获取全仓账户信息') #print (okcoinFuture.future_userinfo()) #print (u'获取全仓持仓信息') #print (okcoinFuture.future_position('ltc_usd','this_week')) #print (u'期货下单') #print (okcoinFuture.future_trade('ltc_usd','this_week','0.1','1','1','0','20'))
operateState = 'empty' # 初始仓位状态:空仓 directionList = ['rise', 'fall'] # 开单方向列表:多,空 operateDirection = 'rise' # 初始开单方向:多 orderId = '' # 合约单id try: buyPrice = float( okcoinFuture.future_ticker(tradePair, tradeCycle)['ticker']['buy']) sellPrice = float( okcoinFuture.future_ticker(tradePair, tradeCycle)['ticker']['sell']) except: restart_program() # 获取之前的k线:交易对、k线周期、合约类型、k线数量 kLinePre = okcoinFuture.future_kline(tradePair, kLineName, tradeCycle, str(klinePreSize)) print('Get History KLine DONE.') # 用于计算EMA、MA、ATR的价格列表 pricePre = [] for i in range(len(kLinePre)): a = float(kLinePre[i][4]) pricePre.append(a) # 记录此前的极端行情 priceKLine = [ pricePre[i] for i in range(len(pricePre)) if (i + 1) % kLineCycle == 0 ] # 按周期级别生成新价格表
class Kline(object): __my_future_client = None __my_spot_client = None __mydb = None def __init__(self, dbconfig, credential): self.__my_future_client = OKCoinFuture(credential['okcoinRESTURL'], credential['apikey'], credential['secretkey']) self.__my_spot_client = OKCoinSpot(credential['okcoinRESTURL'], credential['apikey'], credential['secretkey']) self.__mydb = MySQL(dbconfig['host'], dbconfig['user'], dbconfig['passwd'], dbconfig['db']) def collect_future_kline(self, symbol, type, contract_type): max_timestamp = self.__mydb.get_max_timestamp(symbol, type, 0, contract_type) hjson = self.__my_future_client.future_kline(symbol, type, contract_type, 0, max_timestamp) collected = 0 for i in range(0, len(hjson)): timestamp = hjson[i][0] open = hjson[i][1] high = hjson[i][2] low = hjson[i][3] close = hjson[i][4] amount = hjson[i][5] amount_in_coin = hjson[i][6] dateArray = datetime.datetime.fromtimestamp(timestamp / 1000) otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S") if timestamp > max_timestamp: self.__mydb.insert_kline(otherStyleTime, type, open, high, low, close, amount, symbol, 0, amount_in_coin, contract_type) collected = collected + 1 print(symbol + ' ' + type + ' ' + contract_type + ' future kline history' + ' is done! collected ' + str(collected) + ' records!') ##end of def def collect_spot_kline(self, symbol, type): spot = 1 since = 0 max_timestamp = self.__mydb.get_max_timestamp(symbol, type, spot) hjson = self.__my_spot_client.kline(symbol, type, since, max_timestamp) collected = 0 for i in range(0, len(hjson)): timestamp = hjson[i][0] open = hjson[i][1] high = hjson[i][2] low = hjson[i][3] close = hjson[i][4] amount = hjson[i][5] dateArray = datetime.datetime.fromtimestamp(timestamp / 1000) otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S") if timestamp > max_timestamp: self.__mydb.insert_kline(otherStyleTime, type, open, high, low, close, amount, symbol, spot) collected = collected + 1 print(symbol + ' ' + type + ' spot kline history' + ' is done! collected ' + str(collected) + ' records!')