def __init__(self): self.logger = setup_logger() test = False api_key = os.getenv('API_KEY') api_secret = os.getenv('API_SECRET') test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' self.logger.info('APIKEY: %s' % api_key) if test: url = test_url else: url = product_url self.cli = bitmex(test=test, api_key=api_key, api_secret=api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=["XBTUSD", self.contract_name], api_key=api_key, api_secret=api_secret) # init redis client self.redis_cli = redis.Redis(host='35.230.143.112', password='******', port=6379, decode_responses=True) self.last_sms_time = 0
def __init__(self): self.logger = setup_logger() test = False api_key = 'dbS7FklMUz4A0Ftf_0eb-khj' api_secret = 'UGbHj7ucCrz1xz5slMhPPAV72wemdXxxMk4J2OS_73foWObM' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url else: url = product_url self.cli = bitmex(test=test, api_key=api_key, api_secret=api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=[self.contract_name], api_key=api_key, api_secret=api_secret) #init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # test reids self.redis_cli.sadd(self.filled_order_set, 'test orderid') # threading lock self._value_lock = threading.Lock() self.unfilled_sell_list = '' self.unfilled_buy_list = ''
def __init__(self): self.logger = setup_logger() test = False self.api_key = 'kVfKITnQdJEzEC2sKYlVr9mM' self.api_secret = 'joccPUd5_DwOd3CDL1lSq_prKDxxM6oRQCmu7aALcw_6KWCi' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url self.filled_order_set = 'filled_order_set2' self.setting_ht = 'grid_setting_hash2' self.api_key = 'hDZPEs8ECXv9A1OfBysNIySo' self.api_secret = '_KkD8sZiG8T1rhNRqN2EvnGu9C4lJXoQHi6v4lj6eVyNxAM4' else: url = product_url self.cli = bitmex(test=test, api_key=self.api_key, api_secret=self.api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=self.contract_names, api_key=self.api_key, api_secret=self.api_secret) # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # # threading lock # self._value_lock = threading.Lock() self.logger.info('从redis同步参数') self.price_dist = int(self.redis_cli.hget(self.setting_ht, 'price_dist')) self.profit_dist = int(self.redis_cli.hget(self.setting_ht, 'profit_dist')) self.init_position = int(self.redis_cli.hget(self.setting_ht, 'init_position')) self.final_position = int(self.redis_cli.hget(self.setting_ht, 'final_position')) self.unit_amount = int(self.redis_cli.hget(self.setting_ht, 'unit_amount')) self.unfilled_buy_list = 'unfilled_buy_list' self.unfilled_sell_list = 'unfilled_sell_list' self.logger.info('同步委托列表') self.redis_cli.ltrim(self.unfilled_buy_list, 1, 0) self.redis_cli.ltrim(self.unfilled_sell_list, 1, 0) for o in self.get_unfilled_orders({'orderQty': self.unit_amount, 'ordStatus': 'New'}): redis_item = {'orderID': o['orderID'], 'side': o['side'], 'price': o['price'], 'orderQty': o['orderQty'] } if o['side'] == 'Buy': self.redis_insert_buy(self.unfilled_buy_list, redis_item) else: self.redis_insert_sell(self.unfilled_sell_list, redis_item) self.logger.info('同步完毕')
def __init__(self): self.logger = setup_logger() test = False api_key = 'kVfKITnQdJEzEC2sKYlVr9mM' api_secret = 'joccPUd5_DwOd3CDL1lSq_prKDxxM6oRQCmu7aALcw_6KWCi' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url else: url = product_url self.cli = bitmex(test=test, api_key=api_key, api_secret=api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=[self.contract_name], api_key=api_key, api_secret=api_secret) # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # threading lock self._value_lock = threading.Lock() self.unfilled_sell_list = '' self.unfilled_buy_list = '' self.logger.info('从redis同步参数') if self.redis_cli.llen(self.redis_fragment_list) > 0: fm = json.loads(self.redis_cli.lindex(self.redis_fragment_list, -1)) self.logger.info(fm) self.open_price = fm['open_price'] self.price_dist = fm['price_dist'] self.profit_dist = fm['profit_dist'] self.init_position = fm['init_position'] self.final_position = fm['final_position'] self.unit_amount = fm['unit_amount'] self.unfilled_buy_list = fm['buy_list_name'] self.unfilled_sell_list = fm['sell_list_name'] else: self.logger.info('当前redis为空')
def __init__(self): self.logger = setup_logger() test = False api_key = 'RbKv_8cp9-EM5sPKhM2-tcIh' api_secret = '6VEYWM7x6Cg5Uo3iTNAgD997tvgYT4711eXqTXgU6dj7cCoB' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url else: url = product_url self.cli = bitmex(test=test, api_key=api_key, api_secret=api_secret) # self.ws = BitMEXWebsocket(endpoint=url, # symbols=[self.contract_name], # api_key=api_key, # api_secret=api_secret) # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # threading lock self._value_lock = threading.Lock() self.logger.info('从redis同步参数') if self.redis_cli.llen(self.redis_fragment_list) > 0: last_fm = json.loads( self.redis_cli.lindex(self.redis_fragment_list, -1)) self.logger.info(last_fm) self.open_price = last_fm['open_price'] self.price_dist = last_fm['price_dist'] self.profit_dist = last_fm['profit_dist'] self.init_position = last_fm['init_position'] self.final_position = last_fm['final_position'] self.unit_amount = last_fm['unit_amount'] self.unfilled_buy_list = 'unfilled_buy_list_1' self.unfilled_sell_list = 'unfilled_sell_list_1' else: self.logger.info('当前redis为空')
def __init__(self): self.logger = setup_logger() # init redis client test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' print(self.if_test) if self.if_test: print('TEST.') url = test_url self.api_key = 'YaZ6c81UNsKVCW2eh87a7OeL' self.api_secret = '4lursf1Lk5DBrl7M28hJTBsxMiVeBIhnNyciL_glYQDPCJdy' else: url = product_url self.cli = bitmex(test=self.if_test, api_key=self.api_key, api_secret=self.api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=self.contract_names, api_key=self.api_key, api_secret=self.api_secret)
def __init__(self): self.logger = setup_logger() test = False api_key = 'dbS7FklMUz4A0Ftf_0eb-khj' api_secret = 'UGbHj7ucCrz1xz5slMhPPAV72wemdXxxMk4J2OS_73foWObM' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url else: url = product_url self.cli = bitmex(test=test, api_key=api_key, api_secret=api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=[self.contract_name], api_key=api_key, api_secret=api_secret) # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # threading lock self._value_lock = threading.Lock() self.unfilled_sell_list = '' self.unfilled_buy_list = '' self.logger.info('从redis同步参数') if self.redis_cli.llen(self.redis_fragment_list) > 0: fm = json.loads(self.redis_cli.lindex(self.redis_fragment_list, -1)) self.logger.info(fm) self.open_price = fm['open_price'] self.price_dist = fm['price_dist'] self.profit_dist = fm['profit_dist'] self.init_position = fm['init_position'] self.final_position = fm['final_position'] self.unit_amount = fm['unit_amount'] self.unfilled_buy_list = fm['buy_list_name'] self.unfilled_sell_list = fm['sell_list_name'] else: self.logger.info('当前redis为空')
def __init__(self): self.logger = setup_logger() test = False api_key = 'vj708HQhWkv1JbTM9y_LI-Xn' api_secret = 'lWPOhvhY-yn-HAIo7k3mnjR7pijJJJQAKjTKtioQ_K1Wq3vf' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url else: url = product_url self.cli = bitmex(test=test, api_key=api_key, api_secret=api_secret) self.ws = BitMEXWebsocket( endpoint=url, symbols=[self.contract_1['name'], self.contract_2['name']], api_key=api_key, api_secret=api_secret) # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # threading lock self._value_lock = threading.Lock()
def __init__(self): self.logger = setup_logger() # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) self.logger.info('从redis同步参数') self.price_dist = int( self.redis_cli.hget(self.setting_ht, 'price_dist')) self.profit_dist = int( self.redis_cli.hget(self.setting_ht, 'profit_dist')) self.init_position = int( self.redis_cli.hget(self.setting_ht, 'init_position')) self.final_position = int( self.redis_cli.hget(self.setting_ht, 'final_position')) self.unit_amount = int( self.redis_cli.hget(self.setting_ht, 'unit_amount')) self.if_test = int(self.redis_cli.hget(self.setting_ht, 'if_test')) self.api_key = self.redis_cli.hget(self.setting_ht, 'api_key') self.api_secret = self.redis_cli.hget(self.setting_ht, 'api_secret') #test = False #self.api_key = '-ycA8-LJUWi58YNRiqTnBTjn' #self.api_secret = '4BYMXW9vTv97Fx6W2j9zSw3atTE-GVxiEsobFaZZlqJtHLH8' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' print(self.if_test) if self.if_test: print('TEST.') url = test_url self.filled_order_set = 'filled_order_set_test' self.setting_ht = 'grid_setting_hash_test' self.api_key = 'YaZ6c81UNsKVCW2eh87a7OeL' self.api_secret = '4lursf1Lk5DBrl7M28hJTBsxMiVeBIhnNyciL_glYQDPCJdy' else: url = product_url self.cli = bitmex(test=self.if_test, api_key=self.api_key, api_secret=self.api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=self.contract_names, api_key=self.api_key, api_secret=self.api_secret) # # threading lock # self._value_lock = threading.Lock() self.logger.info('同步委托列表') self.unfilled_buy_list = 'unfilled_buy_list' self.unfilled_sell_list = 'unfilled_sell_list' self.redis_cli.ltrim(self.unfilled_buy_list, 1, 0) self.redis_cli.ltrim(self.unfilled_sell_list, 1, 0) for o in self.get_unfilled_orders({ 'orderQty': self.unit_amount, 'ordStatus': 'New' }): redis_item = { 'orderID': o['orderID'], 'side': o['side'], 'price': o['price'], 'orderQty': o['orderQty'] } if o['side'] == 'Buy': self.redis_insert_buy(self.unfilled_buy_list, redis_item) else: self.redis_insert_sell(self.unfilled_sell_list, redis_item) self.logger.info('同步完毕')
# -*- coding: utf-8 -*- #from bitmex_websocket import BitMEXWebsocket from time import time from six import with_metaclass from bitmex_rest import bitmex import json from datetime import datetime, date, timedelta from dateutil.tz import tzutc import re api_key = 'FlvujwcHMC85oPzfML4RgAKY' api_secret = 't5gttC4Wywcm-SDuizVeHY9KRWeWdbPoEOjwmM3uKctS40a6' rest_cli = bitmex(False, None, api_key, api_secret) def get_trade_info(orderID): filter = { 'orderID' : orderID } execCost = 0 execComm = 0 trade_info = rest_cli.Execution.Execution_getTradeHistory(reverse=True, filter=json.dumps(filter)).result() for o in trade_info[0]: #orderId= o['orderID'] #timestamp = o['transactTime'] execCost += o['execCost'] execComm += o['execComm'] orderQty = o['orderQty'] price = o['price'] #side = o['side']
def __init__(self): self.logger = setup_logger() test = True self.api_key = 'kVfKITnQdJEzEC2sKYlVr9mM' self.api_secret = 'joccPUd5_DwOd3CDL1lSq_prKDxxM6oRQCmu7aALcw_6KWCi' test_url = 'https://testnet.bitmex.com/api/v1' product_url = 'https://www.bitmex.com/api/v1' if test: url = test_url self.filled_order_set = 'filled_order_set2' self.setting_ht = 'grid_setting_hash2' self.api_key = 'W6sK1OHR6eiS60ri4ITwH3Aq' self.api_secret = 'ipr3Vkq5800x3yrIImocfzqVJhDHsfgGXSoN0-ZaivbIuIw-' else: url = product_url self.cli = bitmex(test=test, api_key=self.api_key, api_secret=self.api_secret) self.ws = BitMEXWebsocket(endpoint=url, symbols=self.contract_names, api_key=self.api_key, api_secret=self.api_secret) # init redis client self.redis_cli = redis.Redis(host='localhost', port=6379, decode_responses=True) # threading lock # self._value_lock = threading.Lock() # 同步redis数据 self.logger.info('从redis同步参数') # self.price_dist = int( self.redis_cli.hget(self.setting_ht, 'price_dist')) self.profit_dist = int( self.redis_cli.hget(self.setting_ht, 'profit_dist')) self.init_position = int( self.redis_cli.hget(self.setting_ht, 'init_position')) self.final_position = int( self.redis_cli.hget(self.setting_ht, 'final_position')) self.unit_amount = int( self.redis_cli.hget(self.setting_ht, 'unit_amount')) self.unfilled_buy_list = [] self.unfilled_sell_list = [] self.backup_buy_order_0 = [] self.backup_buy_order_1 = [] self.backup_sell_order_0 = [] self.backup_sell_order_1 = [] # for o in self.get_unfilled_orders({'ordStatus': 'New'}): item = { 'orderID': o['orderID'], 'side': o['side'], 'symbol': o['symbol'] } if o['orderQty'] != self.unit_amount: if o['side'] == 'Buy' and o['symbol'] == self.contract_names[0]: self.backup_buy_order_0.append(item) elif o['side'] == 'Buy' and o['symbol'] == self.contract_names[ 1]: self.backup_buy_order_1.append(item) elif o['side'] == 'Sell' and o[ 'symbol'] == self.contract_names[0]: self.backup_sell_order_0.append(item) else: self.backup_sell_order_1.append(item) else: if o['side'] == 'Buy': self.unfilled_buy_list.append(o['orderID']) else: self.unfilled_sell_list.append(o['orderID']) print(len(self.unfilled_buy_list)) print(self.unfilled_buy_list) print(len(self.unfilled_sell_list)) print(self.unfilled_sell_list) self.logger.info('同步完毕') t = threading.Thread(target=self.monitor_backup_order) t.daemon = True t.start() self.logger.debug("Started thread")