Category Archives: TWSLink

Python 3.6 wrapper for TWSLink

Python 3.6 wrapper for TWSLink available

  • 32-bit .pyd and 64-bit .pyd binaries included.
  • Visual Studion 2015 solution file included (should be usable with Visual Studio 2013,2015 Express)
  • A small python test script to place a MKT order included.

 

The project included (along with binaries) can be used to build custom Python TWSLink wrappers for all future Python versions 2.x and 3.x. It depends just on what library path provided for link settings (to the 2.x lib or 3.x lib) and what include path provided for the c++/general settings. So a few clicks only, when you like to create wrappers for future Python versions on your own.

 

View sample code
# file: sample.py

# -----------------------------------------------------------------------------------------
# This file demonstrates placing MSFT MKT order with TWSLink2 for python3 (tested with 3.5)
# -----------------------------------------------------------------------------------------

import twslink2pt
import time
import sys


# ----- Object creation -----
print ("Creating twslink2")
tws = twslink2pt.TWSLink2Wrap("",1)
# print ("    Created twslink2", tws)

# ----- conntect to TWS at standard port -----
host = "127.0.0.1" ;
print( "Connecting . . . \n")
tws.CONNECT(host,7496,5,100);

# --- get a contract uid for MSFT (do this only once per contract. save uid globally, if you need this contract more than once)
print ("Register MSFT\n")
uidMSFT = tws.REGISTER_CONTRACT("MSFT","STK","USD","SMART","NASDAQ","","",0.0,"",0,0.0) 
print ("uid contract MSFT=",uidMSFT,"\n")

# --- place the market order
if uidMSFT > 0:
    print ("Place buy market 100 MSFT\n")
    uidOrderMSFT = tws.PLACE_ORDER(uidMSFT,0,"BUY","MKT",100,0.0,0.0,"GTC",1,0) 
    print ("uid order MSFT=",uidOrderMSFT,"\n");
    tws.WAIT_FOR_FILLED(uidOrderMSFT,2000)
else:
    print("wrong contract specification");
print ("finished")



[ddownload id=”1196″ style=”link” text=”Download TWSLink Python 3.6 wrapper”]

C# class library for TWSLink

Using TWSLink in C#  projects is possible in 2 ways:

  1. Add Reference of the TWSLink COM module to your project. The function calls of TWSLink exported functions look a bit unusual because of their get/set prefix. [ddownload id=”409″ text=”Example for download” style=”link”].
  2. Add C# class libary of TWSLink to your C# project. Function calls look same as in other TWSLink projects.

The 2nd is what this post is about.

Aside the binaries, the package (download below) contains a solution with 3 projects to create the class library on your own:

  1. twslink2cs
    This generates a c# wrapper DLL for TWSLink along with c# class files to call this wrapper DLL exported functions
    from .NET
  2. twslink2cslib
    This is the final .NET class library for TWSLink using the c# class library files
    from project 1
  3. twslink2csapp
    A c# Form example application which demonstrate how to use the C# .NET class library
    -> add twslink2cslib as reference
    -> create variable of TWSLink2Wrap in your form:
    private TWSLink2Wrap _twsl;

The first 2 projects can be used to create your own handy c# library from TWSLink. This is only required when TWSLink changes its interface (add,remove, change function). The 3. project demonstrate how to use this handy library.

ToDo list how to update TWSLink C# class library when a new TWSLink version changed interface. 

  • rebuild twslink2cs
  • Copy twslink2cs.dll into windows/system32 folder
  • On 64 bit Windows, make sure to select the x64 build
    if you like to use 32 bit .NET apps, then also
    do the 32 bit build and copy resulting twslink2cs.dll
    to windws/syswow64 folder
  • Copy all .cs files generated to twslink2cslib project, because
    those files been changed, if TWSLink interface changed.
  • rebuild twslink2cslib
  • rebuild (adjust) your .net app having twslink2cslib as reference

[ddownload id=”915″ text=”Download C# class library for TWSLink” style=”link”]

Show sample code how to use class library

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace twslink2csapp
{
    
    public partial class Form1 : Form
    {
        private TWSLink2Wrap _twsl;

        public Form1()
        {
            InitializeComponent();
            test_connect();
        }
       
       
        #region test
        void    test_connect()
        {
            int ret=connect("",7496,0,2000);
        }
        #endregion

        #region twslink_instance
        private TWSLink2Wrap TL()
        // create a TWSLink COM instance
        {
            if (_twsl == null)
                _twsl = new TWSLink2Wrap("",1);
            return _twsl;
        }
        #endregion

        #region twslink_interface
        private int connect(string host,int port,int clientid,int timeout)
        {
            // connect to TWS or gateway;this will create a TWSLink instance and you can see the TWSLink tray icon in the taskbar
            int ret=TL().CONNECT(host,port,clientid,timeout);
            return ret;
        }
        #endregion

   
    }
}

Python 3.5 wrapper for TWSLink

Python 3.5 wrapper for TWSLink available

  • 32-bit .pyd and 64-bit .pyd binaries included.
  • Visual Studion 2015 solution file included (should be usable with Visual Studio 2013,2015 Express)
  • A small python test script to place a MKT order included.
  • Tested with Python 3.5.2   32- and 64 bit.

 

The project included (along with binaries) can be used to build custom Python TWSLink wrappers for all future Python versions 2.x and 3.x. It depends just on what library path provided for link settings (to the 2.x lib or 3.x lib) and what include path provided for the c++/general settings. So a few clicks only, when you like to create wrappers for future Python versions on your own.

 

View sample code
# file: sample.py

# -----------------------------------------------------------------------------------------
# This file demonstrates placing MSFT MKT order with TWSLink2 for python3 (tested with 3.5)
# -----------------------------------------------------------------------------------------

import twslink2pt
import time
import sys


# ----- Object creation -----
print ("Creating twslink2")
tws = twslink2pt.TWSLink2Wrap("",1)
# print ("    Created twslink2", tws)

# ----- conntect to TWS at standard port -----
host = "127.0.0.1" ;
print( "Connecting . . . \n")
tws.CONNECT(host,7496,5,100);

# --- get a contract uid for MSFT (do this only once per contract. save uid globally, if you need this contract more than once)
print ("Register MSFT\n")
uidMSFT = tws.REGISTER_CONTRACT("MSFT","STK","USD","SMART","NASDAQ","","",0.0,"",0,0.0) 
print ("uid contract MSFT=",uidMSFT,"\n")

# --- place the market order
if uidMSFT > 0:
    print ("Place buy market 100 MSFT\n")
    uidOrderMSFT = tws.PLACE_ORDER(uidMSFT,0,"BUY","MKT",100,0.0,0.0,"GTC",1,0) 
    print ("uid order MSFT=",uidOrderMSFT,"\n");
    tws.WAIT_FOR_FILLED(uidOrderMSFT,2000)
else:
    print("wrong contract specification");
print ("finished")



[ddownload id=”873″ style=”link” text=”Download TWSLink Python 3.5 wrapper”]