Search code examples
pythonapiconsolepycharminteractive-brokers

Python Console not allowing me to write a new line of code. PyCharm


I am very new to Python, most of my code is done in R. In this code I am pulling data from Interactive Brokers API. The code is finished with 0 errors, however, the 3 green arrows in the console disappear and I am unable to write anything in the console or execute code in the script. Lastly, I hit the green debugging icon on the bottom left corner and now I am getting this "connecting to console" that does not go away.

I have an interpreter running on this project (python 3.7).

What I would like to see is the code to run, and after it is finished allow me to write code in the console and execute new code in the script as per usual. I have decided not to put in my Interactive brokers code as I believe it is redundant. If you would like to see it, I will upload it. Thank you.

enter image description here

Below is the interactive brokers code. The code comes directly from one of their sample scripts. I have decided to set the app.reqMktData on line 30 to True, True so I only receive a snapshot. I still receive the same problem.

import numpy as np
import pandas as pd
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
    def error(self, reqId, errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def tickPrice(self, reqId, tickType, price, attrib):
        print("Tick Price. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Price:", price, end=" ")

    def tickSize(self, reqId, tickType, size):
        print("Tick Size. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Size:", size, end=" ")

def main():
    app = TestApp()
    app.connect("127.0.0.1", 4001, 0)

    contract = Contract()
    contract.symbol = "AAPL"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    app.reqMarketDataType(4)
    app.reqMktData(1, contract, "", True, True, [])

    app.run()

if __name__ == "__main__":
    main()

When I put the run.app above the reqmkt data, it just keeps spinning without returning anything. Thanks


Solution

  • As Daniel said, this is a common problem that is a result of the threading from the API getting stuck open after your program returns. You won't be able to run your program and then continue to call the API in the console after. To solve the connecting to console issue, you need to disconnect from the API in your script by calling app.disconnect() before you return.

    Also, consider adding these lines to your TestApp object:

    thread = Thread(target=self.run)
    thread.start()
    
    setattr(self, "_thread", thread)
    

    This will start the thread for you, and you no longer need to call app.run()

    You will need to import the Thread class from the Threading module - from Threading import Thread

    This blog can help you with the implementation of the other classes and using queues to receive the callbacks from the API. https://qoppac.blogspot.com/2017/03/interactive-brokers-native-python-api.html