F# code <=> Tws.ocx <=> TWS <=> IB server.
Before we begin, you need to have an account with IB, which can be either a real account or a paper account. Once you have the account ready, here is the step-by-step guide to the console program.
Install TWS and API:
- Trade Workstation: please download and install a standalone version from here. You need to configure TWS to accept ActiveX clients by doing the following:
- Select Edit->Global Configuration
- Select API->Settings
- Check "Enable ActiveX and Socket Clients" and note down the port number specified at "Socket Port". By default, the port number is 7496.
- API: can be downloaded from here, which contains Tws.ocx.
Generate wrapper DLLs to expose Tws.ocx to .NET:
Assuming that the API has been installed at C:\TWS_API and you have Visual Studio loaded on your machine, you can then run Aximp.exe as follows:
C:\TWS_API\bin\ActiveX>aximp Tws.ocx
And you shall see the following 2 DLLs generated:
Generated Assembly: C:\TWS_API\bin\ActiveX\TWSLib.dll
Generated Assembly: C:\TWS_API\bin\ActiveX\AxTWSLib.dll
To make use of the ActiveX control, we need to add references to these 2 DLLs in the Visual Studio project. (Note that as the only thing we are trying to do here is connecting to and disconnecting from TWS, we need only AxTWSLib.dll. However, in following few blog posts, we will try to capture market data and submit trade orders, which will requires both DLLs.)
Write F# code to connect and disconnect:
Below you can see the code of our console program. There are some remarks about the code I would like to make:
- Tws.ocx is an ActiveX control which is meant to be added to a GUI container. To get the control initialized properly, a dummy Windows Form is created to host it, as you can see at the beginning of the main function. As our intention here is a console program, we don't display the form.
- To connect to TWS, we invoke the connect method, which takes 3 parameters: IP address, port number, client ID.
- IP address: to indicate where is the machine running TWS. Since I run TWS and the program on the same machine, there's no need to provide an IP address.
- port number: 7496 by default, unless you change it in the Global Configuration page of TWS.
- client ID: an integer used to identify this client connection. If you want to connect multiple clients to the same TWS instance at the same time, each client should be assigned a unique ID.
- Once TWS receives a connection request, a dialog pops up. Click on yes to accept the connection.
- To see whether the connection is successful, we check the serverVersion property of the control. If it's successful, we shall a positive serverVersion. Otherwise, we receive a zero.
------------------------------------------------------------------------
open AxTWSLib
open System
open System.Drawing
open System.Windows.Forms
[<EntryPoint; STAThread>]
let main _ =
// initialize TWS ActiveX Control tws1
let form1 = new Form(Text="Dummy Form")
let tws1 = new AxTws()
tws1.BeginInit()
form1.Controls.Add(tws1)
tws1.EndInit()
// connect to local TWS
tws1.connect("", 7496, 1)
printfn "server version = %d" tws1.serverVersion
// disconnect from TWS
if tws1.serverVersion > 0 then
tws1.disconnect()
0
------------------------------------------------------------------------
Any thoughts on the pros and cons of using the ActiveX control, versus importing the csharpapi dll directly into your F# project and implementing the various methods?
ReplyDeletehttps://www.interactivebrokers.com/en/software/api/apiguide/api/csharp_api.htm
Thank you for sharing this interesting post with us.
ReplyDeleteERP Development Melbourne
seo agency Melbourne
Custom software development Melbourne
aşk kitapları
ReplyDeleteyoutube abone satın al
cami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
instagram takipçi satın al
ReplyDeleteucuz takipçi
takipçi satın al
https://takipcikenti.com
https://ucsatinal.org
instagram takipçi satın al
https://perdemodelleri.org
https://yazanadam.com
instagram takipçi satın al
balon perdeler
petek üstü perde
mutfak tül modelleri
kısa perde modelleri
fon perde modelleri
tül perde modelleri
https://atakanmedya.com
https://fatihmedya.com
https://smmpaketleri.com
https://takipcialdim.com
https://yazanadam.com
yasaklı sitelere giriş
aşk kitapları
yabancı şarkılar
sigorta sorgula
https://cozumlec.com
word indir ücretsiz
tiktok jeton hilesi
rastgele görüntülü sohbet
fitness moves
gym workouts
https://marsbahiscasino.org
http://4mcafee.com
http://paydayloansonlineare.com