TWSLink Python 3.5 wrapper code sample

# 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")