数字货币高频盘口策略
策略描述:订阅数字货币WEBSOCKET盘口和成交信息,随价格变动一直挂买8单,只能有人砸市价单才能成交,成交后在卖1价格卖出,吃盘口毛刺和手续返佣。 所有逻辑均基于python 协程异步实现 ``
class ASYNC_STR(Strategy):
def __init__(self):
super(ASYNC_STR, self).__init__()
self.strategy = config.strategy
self.platform = config.platforms[0]["platform"]
self.account = config.platforms[0]["account"]
self.access_key = config.platforms[0]["access_key"]
self.secret_key = config.platforms[0]["secret_key"]
self.passphrase = config.platforms[0]["passphrase"]
self.symbols = config.platforms[0]["symbols"]
params = {
"strategy": self.strategy,
"platform": self.platform,
"symbols": self.symbols,
"account": self.account,
"access_key": self.access_key,
"secret_key": self.secret_key,
"passphrase": self.passphrase,
"enable_kline_update": False,
"enable_orderbook_update": True,
"enable_trade_update": False,
"enable_ticker_update": False,
"enable_order_update": True,
"enable_fill_update": True,
"enable_position_update": False,
"enable_asset_update": False,
"direct_kline_update": False,
"direct_orderbook_update": True,
"direct_trade_update": False,
"direct_ticker_update": False
}
self.grid = self.create_gateway(**params)
self.enable_timer()
self._count = 0
self.bids = None
self.asks = None
self.sell_order_id = None
self.buy_order_id = None
self.buy_price = None
self.sell_price = None
self.buy_status = None
self.sell_status = None
self.amount = None
self.step = 1
async def on_time(self):
self._count += 3
print("######################__heart beat__######################", self._count)
'''
策略写在回调函数中,开平仓等注意要加协程锁从而忽略多次得到的信息,先完成一次操作才能再得到相关信息
'''
@async_method_locker("ASYNC_STR.main.locker", False)
async def main(self):
print('#'*50)
if self.step == 1:
if self.buy_order_id:
if self.buy_price > self.bids[6][0] or self.buy_price < self.bids[8][0]:
s, e = await self.revoke_order(self.grid, self.symbols[0])
if e == None:
self.buy_order_id = None
self.buy_price = None
return
else:
return
price = self.bids[7][0]
amount = 0.1
s,e = await self.create_order(self.grid,self.symbols[0],ORDER_ACTION_BUY,price,amount)
if e == None:
self.buy_order_id = s
self.buy_price = price
await asyncio.sleep(0.5)
if self.step == 2:
if self.sell_order_id:
if self.sell_price > self.asks[0][0]:
s, e = await self.revoke_order(self.grid, self.symbols[0])
if e == None:
self.sell_order_id = None
self.sell_price = None
return
else:
return
price = self.asks[0][0]
if price < self.buy_price:
return
s,e = await self.create_order(self.grid,self.symbols[0],ORDER_ACTION_SELL,price,self.amount)
if e == None:
self.sell_order_id = s
self.sell_price = price
async def on_orderbook_update_callback(self, orderbook: Orderbook):
self.asks = orderbook.asks[:10]
self.bids = orderbook.bids[:10]
await self.main()
async def on_order_update_callback(self, order: Order):
logger.info("order:",order,caller=self)
if order.status == "FILLED" and order.action == "BUY":
print('挂买单已成交')
self.step = 2
self.buy_price = order.price
self.amount = order.quantity
elif order.status == "FILLED" and order.action == "SELL":
print('本次套利完成')
self.step = 1
self.sell_price = order.price
pro = float(self.sell_price) - float(self.buy_price)
logger.info("本次套利:",pro*self.amount*6.4,caller=self)
self.buy_price = None
self.buy_order_id = None
self.sell_price = None
self.sell_order_id = None
if __name__ == '__main__':
default_main(ASYNC_STR)
|