Running with Excel
TWSLink
' check first if prior
registered
Ret = Cells(Row, ColUID).Value
If Ret > 0 Then
' cancel market data
Ret = TwsLink.REQ_MARKET_DATA(Ret, 0, "",0)
Cells(Row, ColLocSym).Value = ""
End If
Ret = TL().REGISTER_CONTRACT(Cells(Row, 2).Value, Cells(Row, 3).Value,
Cells(Row, 4).Value, Cells(Row, 5).Value, "", Cells(Row, 7).Value, "", 0, "", 0)
Cells(Row, ColUID).Value = Ret
If Ret > 0 Then
' save row for uid
Ret2 = TL().SETLONGVAR(CStr(Ret), Row)
Running with Trade Station
If MarketPosition = 0 Then begin
If Sig < 0 Then begin { section 1.1 }
Sell ("MR_SE") This Bar on Close;
if Timeout = 0 then begin
Print("pos
before short: "+NumToStr(GET_POSITIONS(UidContract,""),0));
CUSTOMCOMMENT("Enter Short",0);
{ place sell
order, dont transmit to IB}
Oid1=PLACE_ORDER(UidContract,0,"SELL","MKT",OrderSize,0.0,0.0,"GTC",0,-1,"","",-1);
Running with eSignal EFS
function
register_Option_Contracts(symbol,sectype,exchange,expiry,right,strike) {
debugPrintln("************Excuting register_Option_Contracts()*********");
// register contract
OPT_CALL_ID = twslink.call("REGISTER_CONTRACT", symbol, sectype, "USD", "NYSE",
"", expiry,right, strike, "",0);
debugPrintln("************ Option CONTRACT_ID = " + OPT_CALL_ID);
// check if contract has been accepted
contract_accepted = twslink.call("WAIT_FOR_ACCEPTED",OPT_CALL_ID,5000);
// ok
if(contract_accepted > 0)
{
debugPrintln("TWS accepted OPT_Call_ID="+OPT_CALL_ID);
}
// pending
else if(contract_accepted >= -1)
{
debugPrintln("Pending accpet OPT_Call_ID="+OPT_CALL_ID);
}
// no ok. check your contract spec again
else
{
Error_TWS_Rejected_Contract();
ret = 0;
}
Running with AmiBroker
// Create a TWSLink instance
TWSLink = CreateStaticObject("twslink.comif");
// init the DLL (Optional)
TWSLink.INITDLL(2,"twslink.log",6,3,3,4000);
// register microsoft Contract for trading
ID_MSFT = TWSLink.REGISTER_CONTRACT("MSFT", "STK","USD","SMART","","","",0.0,"",0);
// connect to TWS
TWSLink.CONNECT("127.0.0.1", 7496, 4, 5000);
// place order and transmit to IB
OidID = TWSLink.BUY_LMT(ID_MSFT, 0,20.77, 100, "GTC", 1);
// just wait one second to demonstrate the impact
TWSLink.WAITDLL(1000);
// modify order (here prior returned OidID provided) and transmit to IB
OidID = TWSLink.BUY_LMT(ID_MSFT, OidID ,21.77, 100, "GTC", 1);
Running with MatLab
if calllib('twslink', 'WAIT_FOR_TICK',-1,20000)
> 0
ticktype = 0;
% process all avaiable ticks
while ticktype >= 0
ticktype = calllib('twslink', 'GET_TICK',uid,-1);
switch ticktype
case DF_ASK
current =
calllib('twslink', 'GET_MARKET_DATA',uid,ticktype,0,1)
pipDiff =
tcifhelper(lastEntry,current,minTick);
% go long (since
last entry, market moved n pips down)
if pipDiff <
-triggerPips
action = 'BUY';
Running with Perl
# get current positions
$pos=$tws->GET_CONTRACT_VAL($ContractUID,8,"","");
$PositionsTable{$ContractUID}=$pos;
# --- pull the next quote from stack ---
# -----------------------------------------------------
# NOTE: last paremeter is 1, what means get latest tick
# and reset this event.
# -----------------------------------------------------
my $primarytype = $tws->GET_TICK($ContractUID,1) ;
if($primarytype >= 0)
{
# update bid, last, ask on every tick (regardless which type
of tick)
$bid = $tws->GET_MARKET_DATA($ContractUID,1,0,1) ;
$ask = $tws->GET_MARKET_DATA($ContractUID,2,0,1) ;
Running with Python
elif next_size > 0:
oids_pips[idofcontract] = tws.PLACE_ORDER(idofcontract,0,"BUY","MKT",next_size,0.0,0.0,"GTC",1,0,"","",-1);
o_states[idofcontract]=tws.WAIT_FOR_ORDER_STATUS(oids_pips[idofcontract],9,1500,12);
print str(ident_dict[idofcontract]).ljust(48) + "++ BUY ++ "
+ str(next_size);
# cancel order if not filled within 1.5 seconds
if o_states[idofcontract] <> 9:
tws.CANCEL_ORDER(oids_pips[idofcontract],0,"");
print "cancel order...";
Running with Wealth-Lab
var UID : integer;
var lib: ComVariant;
const MONEY = 10000.0;
const BIG_NUMBER = 9999;
const FORCE_BUY = 0.0;
//this will initialize twslink
procedure TWSLink_Init();
begin
var iret : integer;
lib := CreateOleObject('twslink.comif');
iret := lib.INITDLL(2, 'c:\twslinkdll_wl.log', 6, 5, 3,
4000);
iret := lib.CONNECT('127.0.0.1', 7496, 4, 5000);
end;
//mintick of contract
function get_mintick(contractuid : integer): float;
begin
var i : integer;
var mintick : float;
mintick := lib.GET_CONTRACT_VAL(contractuid ,20,'',''); //20
is = 'MINTICK'
print('MINTICK of '+GetSymbol+' is: '+FloatToStr(mintick));
Result := mintick;
end;
Running with RUBY
# event market data tick
if eventcategory == "1"
# get context
uid = $tws.GET_EVENT_VAL(uidevent,4)
# price tick
if uid == "1"
# get id associated with event, which
is here an uid
uid = $tws.GET_EVENT_VAL(uidevent,2)
# get the ticktype
primarytype =
$tws.GET_EVENT_VAL(uidevent,3)
ShowData(uid.to_i,primarytype.to_i)
end
# other event
else
ShowEvent(uidevent,eventcategory)
end
Running with .NET
private void
UpdateBidDepStatistic(int uidcontract)
// update all bid-tick dependent statistics for this contract
{
int row = this.UID2Row(uidcontract);
if (row >= 0)
{
// subsequent bid ticks
int sbids = Double2Int(TL().get_GET_MARKET_DATA(uidcontract, 79, 0, 1), 0);
SourceGrid.Cells.Cell xcell = (SourceGrid.Cells.Cell)_MDData[row,
(int)ColIdx.enSEQ_BID];
if (xcell != null)
{
xcell.View =
G1.GetIntView(sbids);
Running with TCL
# we have an event...
if {$uidevent > 0} {
set eventcategory [tws GET_EVENT_VAL $uidevent 1]
# event market data tick
if {$eventcategory == "1"} {
# get context
set uid [tws GET_EVENT_VAL $uidevent
4]
# price tick
if {$uid == "1"} {