def api_get(self, method, params={}): # 现货API if self.account: apikey, secretkey = get_account_key("okex", self.account) okcoinSpot = OKCoinFuture(okcoinRESTURL, apikey, secretkey) if method == "future_userinfo_4fix": api_do = "okcoinSpot.%s()" % (method) return eval(api_do) elif method == "future_position_4fix": return okcoinSpot.future_position_4fix(params["symbol"], params["contractType"], params["type"]) elif method == "trade": return okcoinSpot.future_trade(params["symbol"], params["contractType"], params["price"], params["amount"], params["type"], params["match_price"], params["lever_rate"]) elif method == "ticker": return okcoinSpot.future_ticker(params["symbol"], params["contractType"]) elif method == "order_info": return okcoinSpot.future_orderinfo(params["symbol"], params["contractType"], params["id"]) elif method == "cancelOrder": return okcoinSpot.future_cancel(params["symbol"], params["contractType"], params["id"])
#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 (u' 现货订单信息查询 ') #print (okcoinSpot.orderinfo('ltc_usd','18243644')) #print (u' 现货批量订单信息查询 ') #print (okcoinSpot.ordersinfo('ltc_usd','18243800,18243801,18243644','0')) #print (u' 现货历史订单信息查询 ') #print (okcoinSpot.orderHistory('ltc_usd','0','1','2')) print(u' 期货行情信息') print(okcoinFuture.future_ticker('btc_usd', 'this_week')) #print (u' 期货市场深度信息') #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'))
for period in period_list: time.sleep(100) ans = pd.DataFrame() data_n = price_to_df(symbol, period, frequency, count) ans['close'] = data_n['close'].apply(np.float) ans['sma5'] = tbs.SMA(np.array(ans['close']), 10) ans['sma20'] = tbs.SMA(np.array(ans['close']), 40) #考夫曼均线系统 #ans['AMA1'] = okcoinFuture.KAMA(ans['close'],10,4,30) #ans['AMA2'] = okcoinFuture.KAMA(ans['close'],20,8,60) #ans.plot(figsize=[40,40]) #print(ans) if (a > 10): print('listening') a = 0 else: a = a + 1 price = okcoinFuture.future_ticker(symbol, period)['ticker']['last'] # 五单位均线,向上突破 20 单位均线 开仓做多。 if ans.loc[count - 1, 'sma5'] > ans.loc[count - 1, 'sma20'] and ans.loc[ count - 3, 'sma5'] < ans.loc[count - 3, 'sma20']: print('open_long') okcoinFuture.close_all_position([symbol], [period]) okcoinFuture.open_long(symbol, period, price, 1, 0, 20) elif ans.loc[count - 1, 'sma5'] < ans.loc[count - 1, 'sma20'] and ans.loc[ count - 3, 'sma5'] > ans.loc[count - 3, 'sma20']: okcoinFuture.close_all_position([symbol], [period])
leverFullCoin = initialCoin * leverRate # 满仓可开资金 = 本金 * 杠杆 useLeverCoin = useCoin * leverRate # 持仓量 = 开仓币数 * 杠杆 useAmount = 0 # 已开合约张数 addCoin = 0 # 加仓币数 addAmount = 0 # 加仓合约张数 handFee = 0.001 # 手续费:0.05% operatePrice = 0 # 开仓价格 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)):
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 u' 现货订单信息查询 ' print okcoinSpot.orderinfo('ltc_usd','18243644') print u' 现货批量订单信息查询 ' print okcoinSpot.ordersinfo('ltc_usd','18243800,18243801,18243644','0') print u' 现货历史订单信息查询 ' print okcoinSpot.orderHistory('ltc_usd','0','1','2') print u' 期货行情信息' print okcoinFuture.future_ticker('ltc_usd','this_week') print u' 期货市场深度信息' print okcoinFuture.future_depth('ltc_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')
entry_atr = 0 units = 0 while True: time.sleep(1) try: print(u'------') print(u'当季期货行情信息') trades = okcoinFuture.future_trades('btc_usd', contract_type) klines = okcoinFuture.kline('btc_usd', kline_type, contract_type, 1001) future_userinfo_4fix = okcoinFuture.future_userinfo_4fix() future_position_4fix = okcoinFuture.future_position_4fix( 'btc_usd', contract_type, 1) future_order_info = okcoinFuture.future_orderinfo( 'btc_usd', contract_type, '-1', '2', 1, 45) future_ticker = okcoinFuture.future_ticker('btc_usd', contract_type) if future_userinfo_4fix == '': continue if future_position_4fix == '': continue if future_order_info == '': continue future_userinfo_4fix_json = json.loads(future_userinfo_4fix) future_position_4fix_json = json.loads(future_position_4fix) future_order_info_json = json.loads(future_order_info) holding = future_position_4fix_json['holding'] orders = future_order_info_json['orders'] rights = future_userinfo_4fix_json['info']['btc']['rights'] print(future_userinfo_4fix_json['info']['btc']) inputs = { 'times':
okcoinRESTURL = 'www.okex.com' #请求注意:国内账号需要 修改为 www.okcoin.cn #期货API okcoinFuture = OKCoinFuture(okcoinRESTURL,apikey,secretkey) okcoinSpot = OKCoinSpot(okcoinRESTURL,apikey,secretkey) #Load Spot last/bid/ask #print (u' 期货市场深度信息') #Fut_Data =okcoinFuture.future_depth('btc_usd','this_week','6') #Fut_Ask=Fut_Data.get("asks") #print (Fut_Ask) #Load next week fut spot/bid/last print (u' 期货行情信息') while True: try: Fut_Data_Next_Quarter_BTC = okcoinFuture.future_ticker('btc_usd','quarter') Fut_Data_Next_Quarter_LTC = okcoinFuture.future_ticker('ltc_usd','quarter') LTC_Index = okcoinFuture.future_index('ltc_usd') BTC_Index = okcoinFuture.future_index('btc_usd') USDT_Index = okcoinFuture.exchange_rate() LTC_Fut_Pos = json.loads(okcoinFuture.future_position('ltc_usd','quarter')).get('holding')[0].get('sell_amount') BTC_Fut_Pos = json.loads(okcoinFuture.future_position('btc_usd','quarter')).get('holding')[0].get('sell_amount') Total_Pos = json.loads(okcoinFuture.future_userinfo()) BTC_Spot_OKCoin = okcoinSpot.ticker('btc_usdt').get('ticker').get('last') LTC_Spot_OKCoin = okcoinSpot.ticker('ltc_usdt').get('ticker').get('last') #USDT_Spot_OKCoin except: time.sleep(3) continue else:
# 全仓持仓信息 holding = json.loads(okcoinFuture.future_position( 'btc_usd', 'quarter'))['holding'][0] buy_available = holding['buy_available'] sell_available = holding['sell_available'] # 在没有挂单同时也没有持仓的情况下就要退出 if not len(orders) and buy_available == 0 and sell_available == 0: sys.exit() # 平多仓 if category == 'duo' and buy_available > 0: amount = buy_available # 获取当前价格参数 current_usd_price = okcoinFuture.future_ticker( 'btc_usd', 'quarter')['ticker']['last'] current_price = round(float(rate) * float(current_usd_price), 1) print(current_price) plan(trend, current_price, price, amount, okcoinFuture, '3', orders) # 平空仓 elif category == 'kong' and sell_available > 0: amount = sell_available # 获取当前价格参数 current_usd_price = okcoinFuture.future_ticker( 'btc_usd', 'quarter')['ticker']['last'] current_price = round(float(rate) * float(current_usd_price), 1) print(current_price)