C++ Tutorial: Create an API
Open Microsoft Visual Studio 2010 Professional Edition, then click File > New > Project.
In New Project dialog, select Visual C++ from the list of Installed Templates on the left, then select MFC Application. Type HelloIBMFC as the project name in the Name field, then click the Browse button and choose a location for the project on your computer. Click OK.
The MFC Applicat
ion Wizard starts.
The next screen in the wizard lets you choose the kind of application you want to create. For this tutorial, make the following selections: Application Type = Dialog based Project style = MFC standard Use of MFC = Use MFC as a shared DLL. -> Click Next. The next screen in the wizard lets you choose some additional User Interface options. For this tutorial, disable all of these options by deselecting all check boxes, then click Finish.
The result will be our empty project showing an application with a single dialog. This simple user interface will be the shell of your sample application.
To prepare the user interface
So far in this tutorial, you have created a simple dialog box that contains two buttons, labeled OK and Cancel. Change the captions to Get Data and Close as follows:
Click each button, then type the new label as the value of Caption in the Properties section of the Resource View, shown below:
2. Next, you need to add an area in which to display market data. To do this, open the Toolbox either by using the toolbox tab on the left side of the screen or by selecting View > Toolbox from the menu.
- In the Toolbox dialog, select the List Box component, then drag and drop the component into the application’s dialog and resize it so that it resembles the image below.
3. Save the project.
To add the API source files
Find and copy the Shared and src directories from the source/CppClient directory in the API installation directory to your application directory.
When you view the contents of your application directory after copying the directories, it should look something like this:
Next, you need to tell Visual Studio to include both directories in your project. Open the Property Pages for your project by selecting Project -> HelloIBMFC Properties from the Visual Studio menu.
In the Properties dialog, navigate to Configuration Properties > C/C++ > Additional Include Directories > Edit.
2. Add the Shared and src directories that you just copied to your application directory earlier.
3. Click OK, and then save the project.
4. Finally, before you can start coding, you need to add the client socket’s implementation classes to the project. In the Solution Explorer, right-click the project and select Add -> Existing Item from the popup menu.
5. Navigate to the previously copied src directory and select the files EClientSocket.cpp and EClientSocketBase.cpp, then click Add. (To prevent these errors, open the Property Pages by selecting Project -> HelloIBMFC Properties from the Visual Studio menu, then change the Character Set to <inherit from parent or project defaults)
6. Save the project.
To implement the EWrapper interface
In the dialog’s header file (HelloIBMFCDlg.h), include the EWrapper.h header file.
Inherit the EWrapper class.
The completed code for this step in the tutorial is shown below.
To add the code required to connect to TWS
Save all files.
This is all the code you need to connect to the TWS or IB Gateway. When you click the Get Data button, your application will connect to TWS and receive some events, most importantly, the next valid order id, which we will display in the next step.
To display information from TWS
1. Right-click the HelloIBMFC project in the Solution Explorer, then click Add > Existing Item from the menu.
2. Navigate to the API's Shared directory and select the file HScrollListBox.cpp, then click Add.In the file HelloIBMFCDlg.h, add a CHScrollListBox member variable to our main dialog just as we did with the EClient class:
To request market data from TWS
In the file HelloIBMFCDlg.h, add a Contract member variable: