I am trying to learn about this API and there is a detail that is confusing me a lot. I am not used to work with classes and interfaces and I don't understand one detail.
In this API there are two classes that are very important: EClient and EWrapper. EClient is used to place orders and send requests. EWrapper is used to process the messages received from the API.
In all books, websites, tutorials... they create a new class that inherits those two classes. What I don't understand is the code used in the init function is different in the different sources. How is this possible?
Let me show what I mean.
Source 1, their videos on Youtube, this is how they create a new class (TestApp) that inherits the EClient and EWrapper classes:
https://www.youtube.com/watch?v=dzOilFBDmJI&list=PL71vNXrERKUpPreMb3z1WGx6fOTCzMaH1&index=4
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class TestApp (EClient, EWrapper):
def __init__ (self):
EClient.__init__(self, self)
I have two questions about this:
1 Why are they using self two times? (self, self)
2 Why is that they don't use EWrapper init function?
Something like this:
EWrapper.__init__(self, self)
Source 2, a book "Algorithmic Trading with Interactive Brokers (Python and C++)".
Here is how the create the new class:
class SubmitOrder (EClient, EWrapper):
def __init__ (self, addr, port, clientId):
EWrapper.__init__(self)
EClient.__init__(self, self)
I have 2 questions:
3 Why is that here they call the EWrapper init function?
4 Why is that there are two self (self, self) for EClient and one self (self) for EWrapper.
Source 3, their very own documentation:
https://interactivebrokers.github.io/tws-api/client_wrapper.html
First they say you have to implement the EWrapper interface with this code:
class TestWrapper(wrapper.EWrapper):
But after the : there is nothing, so here is my question.
5 What code should I use to implement the EWrapper interface? They talk about interfaces instead of classes. I have read an interface is similar to a class but defined methods are abstract, you are suposed to override the methods. I understand that you don't override methods in EClient but you have to with EWrappper. But I have no idea on how to "Implement the EWrapper interface". Unfortunately their code looks incomplete in the very first step of something that should be in every programm.
After that, they explain you have to create an EClient class, and you have to use the EWrapper here so you can receive messages:
class TestClient(EClient):
def __init__(self, wrapper):
EClient.__init__(self, wrapper)
Here are my question:
6 Why do they use wrapper instead of EWrapper? As far as I know wrapper is the file where the EWrapper class is stored.
Finally they use TestWrapper and TestClient to create the new class. Here is their code:
class TestApp(TestWrapper, TestClient):
def __init__(self):
TestWrapper.__init__(self)
TestClient.__init__(self, wrapper=self)
Here are my questions:
7 Why is that some init functions have one self, and others two?
I see here that the second "self" in the TestClient is equal to the wrapper. And that makes some sense because of their explanation:
"To use EClientSocket, first it may be necessary to implement the IBApi.EWrapper interface as part of its constructor parameters so that the application can handle all returned messages. Messages sent from TWS as a response to function calls in IBApi.EClientSocket require a EWrapper implementation so they can processed to meet the needs of the API client."
This shows the second self is equal to wrapper, and that makes me remember code from Source 1:
EClient.__init__(self, self)
The second self is not equal to wrapper, so this is my last question:
8 How can this code work without explicitly equaling self to wrapper?
As you can see I am super lost with this, hopefully someone could explain a little bit this.
Thanks for your time.