def get_kline_column_names(exchange_name): if exchange_name == BINANCE_SPOT_EXCHANGE_NAME: return BinanceExchange.get_kline_column_names() elif exchange_name == BINANCE_MARGIN_EXCHANGE_NAME: return BinanceMargin.get_kline_column_names() elif exchange_name == OKEX_SPOT_EXCHANGE_NAME: return OkexExchange.get_kline_column_names() else: return None
def create_exchange(exchange_name): if exchange_name == BINANCE_SPOT_EXCHANGE_NAME: return BinanceExchange(debug=True) elif exchange_name == BINANCE_MARGIN_EXCHANGE_NAME: return BinanceMargin(debug=True) elif exchange_name == OKEX_SPOT_EXCHANGE_NAME: return OkexExchange(debug=True) else: return None
def __init__(self, instance_id, config): super().__init__(instance_id, config, DB_ORDERS_NAME) exchange = config["exchange"] if exchange == "binance": self.__exchange = BinanceExchange(debug=True) elif exchange == "okex": self.__exchange = OkexExchange(debug=True) else: print("Wrong exchange name: %s" % exchange) exit(1)
def __init__(self, instance_id, config, db_orders_name): self.instance_id = instance_id self.config = config self.db_orders_name = db_orders_name self._db = md.MongoDB(mongo_user, mongo_pwd, db_name, db_url) self._db.ensure_index(db_orders_name, [("instance_id",1),("symbol",1)]) self.can_buy_time = None exchange = config["exchange"] if exchange == "binance": self.kline_column_names = BinanceExchange.get_kline_column_names() elif exchange == "okex": self.kline_column_names = OkexExchange.get_kline_column_names()
def __init__(self, instance_id, config, db_orders_name=None): self.instance_id = instance_id self.config = config exchange = config["exchange"] if exchange == "binance": self.kline_column_names = BinanceExchange.get_kline_column_names() elif exchange == "okex": self.kline_column_names = OkexExchange.get_kline_column_names() self.md_db = md.MongoDB(mongo_user, mongo_pwd, exchange, db_url) self.td_db = md.MongoDB(mongo_user, mongo_pwd, "xquant", db_url) self.value = 100 if db_orders_name: self.db_orders_name = db_orders_name self.td_db.ensure_index(db_orders_name, [("instance_id", 1), ("symbol", 1)]) self.can_open_time = None self.tp_cc = {"base_open": 0}
db.ensure_index(collection, [("open_time",1)], unique=True) # 注意,下面代码有隐患,在上午8点前取1d、12h时,最后的一个是不完整的,后续再整改 if args.r: start_time, end_time = split_time_range(args.r) else: # 续接db中最后一条记录,至今天之前 klines = db.find_sort(collection, {}, 'open_time', -1, 1) if len(klines) > 0: start_time = (datetime.fromtimestamp(klines[0]["open_time"]/1000) + interval).replace(hour=0,minute=0,second=0,microsecond=0) else: start_time = None end_time = datetime.now().replace(hour=0,minute=0,second=0,microsecond=0) if args.m == "binance": exchange = BinanceExchange(debug=True) if not start_time: start_time = BinanceExchange.start_time else: print("market data source error!") exit(1) size = 1000 tmp_time = start_time while tmp_time < end_time: print(tmp_time, end=" ") if (tmp_time + size * interval) > end_time: batch = int((end_time - tmp_time)/interval) else: batch = size
parser.add_argument('-t', help='target coin') parser.add_argument('-d', help='direction') parser.add_argument('-a', help='amount') args = parser.parse_args() # print(args) if not (args.b and args.t and args.d and args.a): parser.print_help() exit(1) base_coin = args.b target_coin = args.t direction = args.d amount = float(args.a) exchange = BinanceExchange() symbol = "%s_%s" % (target_coin, base_coin) klines = exchange.get_klines_1day(symbol, 1) cur_price = float(klines[-1][4]) book = exchange.get_order_book(symbol, 1000) # print(book) if direction == 'buy': orders = book['asks'] else: orders = book['bids'] deal = 0 cost = 0 for order in orders:
#!/usr/bin/python3 import sys sys.path.append('../') import argparse from exchange.binanceExchange import BinanceExchange if __name__ == "__main__": parser = argparse.ArgumentParser(description='account') parser.add_argument('-e', help='exchange') args = parser.parse_args() # print(args) if not (args.e): parser.print_help() exit(1) if args.e == "binance": exchange = BinanceExchange(debug=True) else: print("exchange error!") exit(1) account = exchange.get_account() print("account info: %s" % account)
#!/usr/bin/python3 import sys sys.path.append('../') import argparse from exchange.binanceExchange import BinanceExchange import common.xquant as xq if __name__ == "__main__": parser = argparse.ArgumentParser(description='account') parser.add_argument('-e', help='exchange') parser.add_argument('-s', help='symbol (btc_usdt)') parser.add_argument('-p', help='price') parser.add_argument('-a', help='amount') args = parser.parse_args() # print(args) if not (args.e and args.s and args.p and args.a): parser.print_help() exit(1) if args.e == "binance": exchange = BinanceExchange(debug=True) else: print("exchange error!") exit(1) exchange.send_order(xq.DIRECTION_LONG, xq.OPEN_POSITION, xq.ORDER_TYPE_LIMIT, args.s, float(args.p), float(args.a))
time_range = args.r.split(",") start_time = datetime.strptime(time_range[0], "%Y-%m-%d") end_time = datetime.strptime(time_range[1], "%Y-%m-%d") if args.k == "1min": collection = "kline_%s" % args.s tick_time = start_time td = timedelta(minutes=1) elif args.k == "1day": collection = "kline_1day_%s" % args.s tick_time = start_time + timedelta(hours=8) td = timedelta(days=1) else: exit(1) exchange = BinanceExchange(debug=True) db = md.MongoDB(mongo_user, mongo_pwd, db_name, db_url) klines = db.find( collection, { "open_time": { "$gte": int(start_time.timestamp()) * 1000, "$lt": int(end_time.timestamp()) * 1000 } }) i = 0 miss_start = None miss_count = 0 while tick_time < end_time: ts = int(tick_time.timestamp() * 1000)
#!/usr/bin/python3 import sys sys.path.append('../') import argparse from exchange.binanceExchange import BinanceExchange if __name__ == "__main__": parser = argparse.ArgumentParser(description='trade') parser.add_argument('-e', help='exchange') parser.add_argument('-s', help='symbol (btc_usdt)') args = parser.parse_args() # print(args) if not (args.e and args.s): parser.print_help() exit(1) if args.e == "binance": exchange = BinanceExchange(debug=True) else: print("exchange error!") exit(1) trades = exchange.get_trades(args.s) print("trades: %s" % trades)
args = parser.parse_args() # print(args) if not (args.b and args.t and args.s and args.e): parser.print_help() exit(1) base_coin = args.b target_coin = args.t start_time = int(time.mktime(time.strptime(args.s, "%Y-%m-%d"))) * 1000 end_time = int(time.mktime(time.strptime(args.e, "%Y-%m-%d"))) * 1000 print("start time:", start_time, args.s) print("end time:", end_time, args.e) db = md.MongoDB(mongo_user, mongo_pwd, db_name, db_url) exchange = BinanceExchange(debug=True) symbol = creat_symbol(base_coin=base_coin, target_coin=target_coin) if args.k == "1min": interval = 60 * 1000 collection = "kline_%s" % symbol elif args.k == "1day": interval = 24 * 60 * 60 * 1000 collection = "kline_1day_%s" % symbol else: exit(1) print("collection: ", collection) size = 1000 tmp_time = start_time