¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: Simple programs


uberquant
 

There's an project on github that does (almost) all of this, implemented in C++ and uses XML to return values.It can work either cygwin under Windows (note Powershell has a nice XML parser) or linux. I found it a great resource to help me figure out what I wanted to do with the API and still use it for certain batch tasks (e.g. download historical data and as a secondary monitor of important account values)

It could be good as a solution to what you're looking for or as a nice example of good practice.

It sounds like what you're doing isn't super sensitive to timing, but you're mre looking for something to help you, so you can set transmit=0 and then dispatch the orders via TWS by hand (when you make sure it's not making 100 of the same order or applying a crazy order size)


Submit some order would look like this (can substitute different order types and then specifiy auxiliary prices or limit prices as appropriate) :
<?xml version="1.0"?>
<TWSXML>
<request type="place_order">
<query orderId="501">
<contract symbol="IBKR" secType="STK" expiry="" currency="USD" exchange="SMART"/>
<order action="SELL" totalQuantity="500" orderType="MKT" transmit="0"/>
</query>
</request>
<request type="place_order">
<query orderId="502">
<contract symbol="AMZN" secType="STK" expiry="" currency="USD" exchange="SMART"/>
<order action="BUY" totalQuantity="300" orderType="MKT" transmit="0"/>
</query>
</request>
<request type="place_order">
<query orderId="503">
<contract symbol="IBM" secType="STK" expiry="" currency="USD" exchange="SMART"/>
<order action="SELL" totalQuantity="900" orderType="MKT" transmit="0"/>
</query>
</request>
</TWSXML>

You can wait for the return value in an XML and then parse it. Don't forget to watch for pacing if you are inputting dozens of orders at a time. Also don't forget the orderID must be in strictly increasing order across time.

To get account values and positions there's a snapshot ability. In powershell for example

Function TWSDO ($port)
{
? ? ? $poo=& ?C:\utils\twsdo.exe -h localhost -p $port --get-account?
? ? ? Return $poo
}

? ? ? ? ?$res=TWSDO($arr[$i])
? ? ? ? ?$x=[xml]$res[0..($res.Length-2)]
? ? ? ? ?$acct = ($x.TWSXML.request.response.AccVal | ? {$_.key -eq "AccountCode"}).val
? ? ? ? ?$nlvxx = $x.TWSXML.request.response.AccVal | ? {$_.key -eq "NetLiquidation" -and $_.accountName -eq $acct}?
? ? ? ? ?$nlv=$nlvxx.val -as [double]
? ? ? ? ?$imronxx = $x.TWSXML.request.response.AccVal | ? {$_.key -eq "FullInitMarginReq" -and $_.accountName -eq $acct}
? ? ? ? ?$imron=$imronxx.val -as [double]

To get historical data you can make a request as like this

<?xml version="1.0"?>
<TWSXML>
? <request type="historical_data">
? ? <query endDateTime="20160101 23:59:59 GMT" durationStr="6 D" barSizeSetting="1 min" whatToShow="ASK" useRTH="1" formatDate="1">
? ? ? <reqContract conId="43645865" symbol="IBKR" secType="STK" exchange="SMART"?primaryExchange="NASDAQ" currency="USD" localSymbol="IBKR" tradingClass="NMS"/>
? ? </query>
? </request>
</TWSXML>

It won't do for getting the current price, but that is relatively easy to put together, I think this fellow's tutorial swill get you that quicly enough?

?




Join [email protected] to automatically receive all group messages.