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 demo下单 #52

Closed 18728476237 closed 3 years ago

18728476237 commented 3 years ago

新春快乐! 我通过Kline获取到实时的close数据,然后我设置close到达某个值时,进行买入多或者空的操作,应该调用哪个函数来实现这个逻辑?进一步来说,是调用huobi_swap_markey.py中的process_trade(self, data)来实现吗?这个函数感觉只是在打印实时的交易数据呢?谢谢!

ad-long commented 3 years ago

可以参考这个:alpha/platforms/huobi_usdt_swap_cross_trade.py line 385: result, error = await self._rest_api.create_orders({"orders_data": orders_data})

foonsun commented 3 years ago

参考这里的批量下单函数就可以:https://github.com/hbdmapi/huobi_futures_Python/blob/master/examples/huobi_swap/strategy.py#L148

18728476237 commented 3 years ago

我在python demo项目的example/huobi_swap/strategy.py的on_event_kline_update(self, kline: Kline)函数中引用 alpha/platforms/huobi_swap_trade.py中的create_order()函数如下: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 symbol = "BTC-USD" result, error = await HuobiSwapTrade.create_order(self, action, symbol ,price, quantity, direction, offset, lever_rate, order_price_type, client_order_id)

        print("新订单返回的result:%s,error:%s" % (result, error))

打印result和error:result:None,error:order type error

18728476237 commented 3 years ago

如果改变函数传参如下: order_type = ORDER_TYPE_MARKET 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 symbol = "BTC-USD" result, error = await HuobiSwapTrade.create_order(HuobiSwapTrade,action, price, quantity, order_type, client_order_id,symbol ,price,direction, offset,lever_rate, order_price_type) print("新订单返回的result:%s,error:%s" % (result, error)) 则打印:AttributeError: type object 'HuobiSwapTrade' has no attribute '_rest_api'

foonsun commented 3 years ago

async def create_order(self, contract_code, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id=None):

需要根据以上方法来填参,如下例子调用:

order_nos, error = await self.trader.rest_api.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id) if error: logger.error(self.strategy, "create future order error! error:", error, caller=self) else: logger.info(self.strategy, "create future orders success:", order_nos, caller=self)

foonsun commented 3 years ago

HuobiSwapTrade 需要先根据例子的init 先做实例化,才能调用。

18728476237 commented 3 years ago

async def create_order(self, contract_code, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id=None):

这个函数定义在哪个文件里边?

------------------ 原始邮件 ------------------ 发件人: "hbdmapi/huobi_futures_Python" <notifications@github.com>; 发送时间: 2021年2月15日(星期一) 下午3:26 收件人: "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 demo下单 (#52)

async def create_order(self, contract_code, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id=None):

需要根据以上方法来填参,如下例子调用:

order_nos, error = await self.trader._rest_api.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id) if error: logger.error(self.strategy, "create future order error! error:", error, caller=self) else: logger.info(self.strategy, "create future orders success:", order_nos, caller=self)

— 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/master/alpha/platforms/huobi_swap_api.py#L175

18728476237 commented 3 years ago

你上一封邮件写到: order_nos, error = await self.trader._rest_api.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id) 我在example/huobi_swap/strategy.py中先import HuobiSwapRestAPI 类,然后原样调用上面这个函数,报错: AttributeError: 'Trade' object has no attribute '_rest_api'

await 后面的 self.trader._rest_api. 是什么意思?create_order()这个函数不是类HuobiSwapRestAPI的一个方法吗? 我改成order_nos, error = await  HuobiSwapRestAPI.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id)却仍然报错: File "/tmp/pycharm_project_13/alpha/platforms/huobi_swap_api.py", line 204, in create_order     success, error = await self.request("POST", uri, body=body, auth=True) AttributeError: 'str' object has no attribute 'request'

谢谢!

------------------ 原始邮件 ------------------ 发件人: "hbdmapi/huobi_futures_Python" <notifications@github.com>; 发送时间: 2021年2月15日(星期一) 下午3:26 收件人: "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 demo下单 (#52)

async def create_order(self, contract_code, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id=None):

需要根据以上方法来填参,如下例子调用:

order_nos, error = await self.trader._rest_api.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id) if error: logger.error(self.strategy, "create future order error! error:", error, caller=self) else: logger.info(self.strategy, "create future orders success:", order_nos, caller=self)

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

18728476237 commented 3 years ago

order_nos, error = await self.trader._rest_api.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id) 我在example/huobi_swap/strategy.py中先import HuobiSwapRestAPI 类,然后原样调用上面这个函数,报错: AttributeError: 'Trade' object has no attribute '_rest_api'

await 后面的 self.trader._rest_api. 是什么意思?create_order()这个函数不是类HuobiSwapRestAPI的一个方法吗? 我改成order_nos, error = await HuobiSwapRestAPI.create_order(“BTC-USD”, price, quantity, direction, offset, lever_rate, order_price_type, client_order_id)却仍然报错: File "/tmp/pycharm_project_13/alpha/platforms/huobi_swap_api.py", line 204, in create_order success, error = await self.request("POST", uri, body=body, auth=True) AttributeError: 'str' object has no attribute 'request'