class Trades(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_trades(self,symbol,contract_type): hjson = self.__my_future_client.future_trades(symbol,contract_type) collected = 0 for i in range(0,len(hjson)): amount = float(hjson[i]['amount']) date = hjson[i]['date'] date = datetime.datetime.fromtimestamp(date) date = date.strftime("%Y-%m-%d %H:%M:%S") date_ms = hjson[i]['date_ms'] / 1000 date_ms = datetime.datetime.fromtimestamp(date_ms) date_ms = date_ms.strftime("%Y-%m-%d %H:%M:%S") price = float(hjson[i]['price']) tid = hjson[i]['tid'] trade_type = hjson[i]['type'] if self.__mydb.future_trade_exist(symbol, date_ms, tid, contract_type) == 0: self.__mydb.insert_trades(amount,date,date_ms,price,tid,trade_type,symbol,0,contract_type) collected = collected+1 print (symbol +' ' + contract_type +' future trade history' + ' is done! collected ' + str(collected) + ' records!') ##end of def collect_future_trades def collect_spot_trades(self,symbol): max_trade_id = self.__mydb.get_max_trade_id(symbol,1) hjson = self.__my_spot_client.trades(symbol,max_trade_id) collected = 0 for i in range(0,len(hjson)): amount = float(hjson[i]['amount']) date = hjson[i]['date'] date = datetime.datetime.fromtimestamp(date) date = date.strftime("%Y-%m-%d %H:%M:%S") date_ms = hjson[i]['date_ms'] / 1000 date_ms = datetime.datetime.fromtimestamp(date_ms) date_ms = date_ms.strftime("%Y-%m-%d %H:%M:%S") price = float(hjson[i]['price']) tid = hjson[i]['tid'] trade_type = hjson[i]['type'] if tid > max_trade_id: self.__mydb.insert_trades(amount,date,date_ms,price,tid,trade_type,symbol,1) collected = collected+1 print (symbol +' spot trade history' + ' is done! collected ' + str(collected) + ' records!') ##end of def collect_spot_trades
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') print u'获取全仓账户信息' print okcoinFuture.future_userinfo() print u'获取全仓持仓信息' print okcoinFuture.future_position('ltc_usd','this_week')
N1 = 350 nbdev = 2.5 # N1=55 # nbdev=2 stop_N = 2 entry_price = 0 last_entry_price = 0 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)