erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.8k stars 744 forks source link

I can't call ib_insync function on the flask server #266

Closed lovetrading10 closed 4 years ago

lovetrading10 commented 4 years ago

Hi, there. I am very interested in your project. I am going to integrate tradingview webhook and ib_insync. image image image Can you help me? I will wait for your favorable reply. Thanks.

goodboy commented 4 years ago

@SunHxiao you won't be able to do it.

flask uses it's own internal blocking/callback/threaded model and isn't compatible with asyncio which this project is built on. You'll have to wait for something like #262 to land or run a loop on your own in another thread and do inter-thread communication as suggested in that issue.

goodboy commented 4 years ago

@erdewit the "sync api" strikes again :popcorn:

lovetrading10 commented 4 years ago

Hi, @goodboy. Thanks for your reply and your advise. So how can I write the call function? Would you please inform me about that. Thank you!

goodboy commented 4 years ago

@SunHxiao in short I'd like to add an actual synchronous api to this project (currently there isn't really one) and that's what #262 is about. If you understand what we talk about in that issue then you might be able to do it on your own.

The bottom line is you'll need to start an the ib_insync.IB() object in another thread and then submit your requests to it in order to make it work with a callback system like flask routers.

This SO question might be helpful.

lovetrading10 commented 4 years ago

hi @goodboy. I appreciate your help. Would you please inform me how to call send_order1 function from /webhook with asyncio? Really sorry about it and very thanks. I am a beginner. So I don't know what do I have to do. Thanks.

lovetrading10 commented 4 years ago

Hi, @goodboy image I have fixed like this. At this time. I have gotten following errors. image

Can you help me?

goodboy commented 4 years ago

@SunHxiao hehe.

As I mentioned flask doesn't work with asyncio. So changing the route function into and async function will definitely not work.

Checkout the Stack Overflow question i provided. That's about the best guidance I can give.

Also, for what it's worth but not to deter you, the integration you're trying to do here is likely going to be a big hassle. See this thread: https://getsatisfaction.com/tradingview/topics/integrating-tradingview-with-interactive-brokerst

lovetrading10 commented 4 years ago

Hi, @goodboy I need to fix the issue soon. If you fix this issue, then I will pay you. Can you help me now? Looking forward for your favorable reply. And full marks

goodboy commented 4 years ago

@SunHxiao I have to be honest.

I am actively working on a project that will eventually make trading view effectively irrelevant and thus it would be a waste of both of our times for me to work on your integration. The best way forward for you is likely to try and understand the underlying problem and read that stack overflow question through thoroughly. Mostly likely an alternative web framework to flask is the easiest path forward.

Ask @erdewit though. He may have interest :wink:

lovetrading10 commented 4 years ago

I feel that if you add a few lines codes, then you can fix it soon. But it is difficult for me and take long time. Can you help me? It will never long time only 1~10mins rest your head few minuts. I god you can help me

lovetrading10 commented 4 years ago

Hi, @goodboy . Then last thing, would you please inform me how to use IB Web api? with reference url(github or website) Thanks.

goodboy commented 4 years ago

@SunHxiao sorry, I've never used it.

erdewit commented 4 years ago

The easiest solution is to use an asyncio-based alternative to Flask, such as Quart, Sanic, aiohttp or FastAPI, and use async endpoints.

See for example #251.