1 year ago

#353776

test-img

lenza

Only one Order get submitted on Interactive Brokers using ib_insync and sanic and Ngrok

After one order is automatically placed, no subsequent orders are placed until I restart the app. So for example, the above code would buy AAPL if there was a buy alert, but if I leave it running and a sell alert came in thereafter, it would not place that sell order. Only if I had restarted the app before then will the sell order have been received. I try to use ib.qualifyContracts(ticker_contract) and ib.sleep(1) in to order sequence as it suggest on line, but python fails with error.

I know you have to keep in mind the orderid, but i don't know how to develop this. Do you have some examples? Thank you very much

This Is My code :

#import librerie
import ibapi
from datetime import datetime
from sanic import Sanic
from sanic import response
from ib_insync import *
import json
import asyncio

#create sanic chiamato app
app = Sanic(__name__)
appib = None
#Create root /homepage
@app.route('/')
async def root(request):
    return response.text('online')



#ascolta per il sengale e immetti l'ordine
@app.route('/webhook', methods=['POST'])
async def webhook(request):

    if request.method == 'POST':
        #controllo reconnect  IB
        #await checkIfReconnect()
        #analisi del segnale
        data = request.json
        print(data)
        price = float (data["price"])
        #price = 175.56
           
        # definizione tipo di ordine
        if data["position"] == 'LONG':
            #order = appib.bracketOrder ('BUY',10,limitPrice=price,takeProfitPrice=price*1.02,stopLossPrice=price*0.99,account=appib.wrapper.accounts[0]) #immissione ordine dell account di default o primario
            order = appib.bracketOrder('BUY',10,price,price*1.02,price*0.99,account=appib.wrapper.accounts[0])
            contract = Stock(data["Ticker"],'SMART','USD')    
            for ord in order:
                appib.placeOrder(contract,ord)
                
            return response.text('Buy-ok')        
        
        #if data["position"] == 'SHORT':
        #   order = MarketOrder('SELL',10,account=appib.wrapper.accounts[0]) #immissione ordine dell account di default o primario
        
        
        #contract = Crypto(data['symbol'][0:3],'PAXOS',data['symbol'][4:6])
        #for ord in order:
        #appib.placeOrder(contract,order)

I found on Web this Soultion add appib.sleep(1) in the cycle For:

contract = Stock(data["Ticker"],'SMART','USD')    
                for ord in order:
                    appib.placeOrder(contract,ord)
                    appib.sleep(1)

But gives me the following error:

 .Traceback (most recent call last):
  File "handle_request", line 83, in handle_request
    class Sanic(BaseSanic, metaclass=TouchUpMeta):
  File "c:\Users\Anto\OneDrive - Oficina IS\Anto-Pers\Interactive Brokers-Python\2Tradingview-bot-interBroker-lenza.py", line 42, in webhook
    appib.sleep(1)
  File "C:\Users\Anto\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\ib_insync\util.py", line 378, in sleep
    run(asyncio.sleep(secs))
  File "C:\Users\Anto\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\ib_insync\util.py", line 332, in run
    result = loop.run_until_complete(task)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 623, in run_until_complete
    self._check_running()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 583, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

python

ngrok

tradingview-api

interactive-brokers

sanic

0 Answers

Your Answer

Accepted video resources