加密做市商

2024-06-01 08:02:17 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,这里是stack overflow社区

我目前正在学习如何通过解决问题来编写代码,这些问题实际上对我作为交易员的工作有所帮助。这一直进展顺利,尽管现在我被一个看似简单的解决方案困住了。。一个简单的新手错误

因此,我在代码的顶部有一个变量spread。我将其更改为更小的值5,初始值为25

问题是当我运行代码时,购买订单仍然是当前价格的25

buyprice = price - spread

下面是代码,提前感谢您的帮助

# Simplified Deribit Market Maker. Please adjust for your own use.

import asyncio
import websockets
import json

#dollar spread
spread = 5

refresh_token = ""
expires_in = 0
access_token = ""

msg = \
{
  "jsonrpc" : "2.0",
  "id" : 42,
  "method" : "public/auth",
  "params" : {
    "grant_type" : "client_credentials",
    "client_id" : "###",
    "client_secret" : "###"
  }
}

async def call_api(msg):
   async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
       await websocket.send(msg)
       
       while websocket.open:
           response = json.loads(await websocket.recv())
           #print(response)
           if "result" in response:
             result = response["result"]
             #print(result)
             if "access_token" in result:
               access_token = result["access_token"]
               #print(access_token)
               msg1 = {"jsonrpc": "2.0","method": "private/subscribe", "id": 42, "params": {"access_token":access_token, "channels": ["user.orders.BTC-PERPETUAL.raw"]} }
               await websocket.send(json.dumps(msg1))
           if "params" in response:
             params = response["params"]
             if "data" in params:
               data = params["data"]
               #print(data)
               order_state = data["order_state"]
               order_type = data["order_type"]
               if order_state == "filled" and order_type != "stop_limit":
                 price = data["price"]
                 direction = data["direction"]
                 qty = data["amount"]
                 msg2=msg1
                 if direction == "buy":
                   sellprice = price + spread 
                   msg2 = {"jsonrpc": "2.0","method": "private/sell", "id": 42, "params": {"access_token":access_token, "instrument_name" : "BTC-PERPETUAL", "amount" : qty, "type" : "limit","price":sellprice,"post_only":True} }
                 if direction == "sell":
                   buyprice = price - spread
                   msg2 = {"jsonrpc": "2.0","method": "private/buy", "id": 42, "params": {"access_token":access_token, "instrument_name" : "BTC-PERPETUAL", "amount" : qty, "type" : "limit","price":buyprice,"post_only":True} }
                 await websocket.send(json.dumps(msg2))
           
loop = asyncio.get_event_loop()
loop.run_until_complete(call_api(json.dumps(msg)))
loop.run_forever()

Tags: intokenidjsondataifaccessresponse