hbdmapi / huobi_futures_Python

An Asynchronous Event-driven High-frequency Trading System,huobi future,huobi coin margined swap, huobi usdt margined swap included.
MIT License
267 stars 113 forks source link

在 huobi_swap example 中 下单 #53

Closed 18728476237 closed 3 years ago

18728476237 commented 3 years ago

在 example/huobi_swap/strategy.py 中import alpha/platforms/huobi_swap_trade 的HuobiSwapTrade 类,然后在 example/huobi_swap/strategy.py 的函数 on_event_kline_update(self, kline: Kline) 中调用 HuobiSwapTrade 类的create_order()方法: order_type = ORDER_TYPE_MARKET symbol = "BTC-USD" action = ORDER_ACTION_BUY price = 41000.1 quantity = 100 direction = "buy" offset = "open" lever_rate = 20 order_price_type = "optimal_20" client_order_id = None order_nos, error = await HuobiSwapTrade.create_order(HuobiSwapTrade, action, price, quantity, order_type, client_order_id, symbol, direction, offset, lever_rate, order_price_type) 报错: AttributeError: type object 'HuobiSwapTrade' has no attribute '_rest_api' E [2021-02-15 16:37:02,515] Task exception was never retrieved future: <Task finished coro=<MyStrategy.on_event_kline_update() done, defined at /tmp/pycharm_project_13/examples/huobi_swap/strategy.py:157> exception=AttributeError("type object 'HuobiSwapTrade' has no attribute '_rest_api'",) created at /tmp/pycharm_project_13/alpha/tasks.py:65> source_traceback: Object created at (most recent call last):

foonsun commented 3 years ago

1、上面的是报错没有rest_api,说明HuobiSwapTrade没有初始化成功,检查api key等没有配置,参考example的初始化过程,强烈推荐先使用example去运行,先跑通例子再去做DIY:

https://github.com/hbdmapi/huobi_futures_Python/blob/master/examples/huobi_swap/strategy.py#L80

2、create_order与examples中对create_orders调用类似,如果create_orders运行成功之后,再去尝试调用create_order方法:https://github.com/hbdmapi/huobi_futures_Python/blob/master/examples/huobi_swap/strategy.py#L148

使用create_order进行传参数下单。

async def create_order(self, action, price, quantity, order_type=ORDER_TYPE_LIMIT, client_order_id=None, *args, **kwargs):

一个简易例子:


                            # 卖出开空1张,限价单方式。
                            price = 100000
                            quantity = -1
                            action = ORDER_ACTION_SELL
                            new_price = str(price) 
                            order_no, error = await self.trader.create_order(action, new_price, quantity, ORDER_TYPE_LIMIT, lever_rate=1)
                            if error:
                                logger.error(self.strategy,"create future order error! error:", error, caller=self)
                            else:
                                logger.info(self.strategy,"create sell open order:", order_no, caller=self)

3、参考create_order调用过程:

https://github.com/hbdmapi/huobi_futures_Python/blob/master/alpha/platforms/huobi_swap_trade.py#L270

18728476237 commented 3 years ago

请查看附件,_rest_api应该初始化成功了?

------------------ 原始邮件 ------------------ 发件人: "hbdmapi/huobi_futures_Python" <notifications@github.com>; 发送时间: 2021年2月15日(星期一) 下午5:06 收件人: "hbdmapi/huobi_futures_Python"<huobi_futures_Python@noreply.github.com>; 抄送: "宇"<525082270@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [hbdmapi/huobi_futures_Python] 在 huobi_swap example 中 下单 (#53)

1、上面的是报错没有rest_api,说明HuobiSwapTrade没有初始化成功,检查api key等没有配置,参考example的初始化过程:

https://github.com/hbdmapi/huobi_futures_Python/blob/master/examples/huobi_swap/strategy.py#L80

2、create_order与examples中对create_orders调用类似,如果create_orders运行成功之后,再去尝试调用create_order方法:https://github.com/hbdmapi/huobi_futures_Python/blob/master/examples/huobi_swap/strategy.py#L148

使用create_order进行传参数下单。 async def create_order(self, action, price, quantity, order_type=ORDER_TYPE_LIMIT, client_order_id=None, *args, **kwargs):
一个简易例子:

卖出开空1张,限价单方式。 price = 100000 quantity = -1 action = ORDER_ACTION_SELL new_price = str(price) order_no, error = await self.trader.create_order(action, new_price, quantity, ORDER_TYPE_LIMIT, lever_rate=1) if error: logger.error(self.strategy,"create future order error! error:", error, caller=self) else: logger.info(self.strategy,"create sell open order:", order_no, caller=self)

3、参考create_order调用过程:

https://github.com/hbdmapi/huobi_futures_Python/blob/master/alpha/platforms/huobi_swap_trade.py#L270

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

foonsun commented 3 years ago

下单请求打印: https://github.com/hbdmapi/huobi_futures_Python/blob/401fc1564ea6e56c1586099c7f52c828001f9917/alpha/utils/request.py#L75

在config.json中,将debug的level设置为"DEBUG",可以打印出请求的原始log信息