我以编程方式将订单发送到Bitmex,我想使用asyncio监视并连续检查是否已满足这些订单中的任何一个。一旦完成订单,我需要将完成交易的详细信息返回到我的脚本,然后再下一个订单。
我不确定即使这是正确的方法,但是选择了此路线后,我遇到了2个错误:
1)我无法停止循环,从而导致服务器崩溃,并且
2)当它没有崩溃时,我收到一条错误消息,提示我需要将一个函数传递给另一个函数的ID无法下标。
订单成功放置如下:
import ccxt
import asyncio
import ccxt.async_support as ccxt
def place_order():
bitmex = ccxt.bitmex ({
'apiKey' : 'my_key',
'secret' : 'my_secret'
})
if 'test' in bitmex.urls:
bitmex.urls['api'] = bitmex.urls['test']
data = request.args.get("payload")
result = data.split(",")
symbol = result[0]
type = 'limit'
side = result[1]
amount = result[2]
price = result[3]
offset = result[4]
params = {'execInst': 'ParticipateDoNotInitiate'}
order = bitmex.create_order(symbol, type, side, amount, price, params)
id = order["info"]["orderID"]
symbol = order["info"]["symbol"]
fetch_order(id, offset)
return id
现在的问题是,我收到一条错误消息,指出“ id” 无法下标,这会阻止 fetch_order()函数正常运行。我必须传递ID,否则fetch_order()函数将无法从Bitmex获取数据。
fetch_order()函数如下:
async def fetch_order(id, offset):
bitmex = ccxt.bitmex ({
'apiKey' : 'SaZhyZ9U9qqKxJ-3rUewDgGS',
'secret' : 'W3xvrTs45RIWOTgzAJjATEUQa4wgTv0_4GbZK3MGBy7IQJdk'
})
if 'test' in bitmex.urls:
bitmex.urls['api'] = bitmex.urls['test']
if bitmex.has['fetchOrder']:
bitmex.fetch_order(id)
asyncio.get_event_loop().run_until_complete(bimex.fetch_order(id))
status = order["info"]["status"]
amount = order["info"]["orderQty"]
remaining = order["info"]["remaining"]
if status == 'closed':
symbol = order["info"]["symbol"]
side = order["info"]["side"]
price = order["info"]["price"]
if side == 'buy':
type = 'limit'
side = 'sell'
price = price + offset
params = {'execInst': 'ParticipateDoNotInitiate'}
order = bitmex.create_order(symbol, type, side, amount, price, params)
else:
type = 'limit'
side = 'buy'
price = price + offset
params = {'execInst': 'ParticipateDoNotInitiate'}
order = bitmex.create_order(symbol, type, side, amount, price, params)
return 'OK'
我已经尝试在bitmex.fetch_order(id)行之前放置await,但这也会引发错误信息。
所以问题是: 1)使用asyncio我如何使这项工作?我在哪里构造不正确?
2)假设这是正确的方法,如何传递id参数?
3)完成工作后,如何停止它?
谢谢!