AutoTrader now provides an external interface to allow external programs or a simple web browser to view market data and submit orders to TWS without having to know the TWS API or installing any TWS API client software. If you can write programs that can make http connections, then you can submit orders to TWS using this AutoTrader TWS Http Interface.
The format of the http command is as follows:
http://localhost:7400/<command>?<key1>=<value1>&<key2>=<value2>& ... &<keyn>=<valuen>
The URL is made up of a command, followed by ?, then a series of key/value pairs separated by & that are pertinent to the command. For security reasons, only connections from the same machine where AutoTrader is running is allowed.
The followsing is a list of commands currently supported:
Command |
Description |
setSymbol |
To switch the current symbol in AutoTrader. The symbol must be setup in AutoTrader. |
getQuotes |
To get the quotes for the specified symbol. Symbol must be defined in AutoTrader. If the symbol is not the current symbol, a setSymbol command is performed first. |
reqMktData |
To receive market data for the requested symbol. The data will start streaming to this connection until the TWS connection is terminated or you terminate the connection. |
placeOrder |
To submit an order to TWS. There is an option to make the order passthru, or to make AutoTrader manage the position once the order is filled. The Order Status is returned as a response by this command. If a managed position is already open, you can send another placeOrder in the same direction as the current position to scale in. |
reverse |
To reverse the current position. |
ClosePosition |
To close a position using default values set in AutoTrader. |
The format of the parameters for the commands is key/value pairs. You can list the parameters in any order. The following are key/value pairs that you can use.
Key |
Description |
Commands |
Required |
symbol |
The symbol of the security you want to use for the command. | setSymbol |
yes |
getQuotes |
yes |
||
reqMktData |
yes |
||
placeOrder |
yes |
||
secType |
FUT, STK or OPT | reqMktData |
yes |
currency |
USD,EUR | reqMktData | yes |
exchange |
Exchange for the security, must be SMART for stocks. | reqMktData |
yes |
expiry |
Contract expiration. Format is yyyymm. | reqMktData |
no |
strike |
Strike price for options. | reqMktData |
no |
right |
Put or Call. | reqMktData |
no |
action |
Buy or Sell | placeOrder |
yes |
qty |
Specify the quantity | placeOrder |
no |
orderType |
Mkt, Lmt, Stp, StpLmt , BuyAtBid, BuyAtAsk, SellAtBid, SellAtAsk (last four types are valud for non passthru orders only) | placeOrder |
no |
lmtPrice |
The limit price for limit and stop limit orders | placeOrder |
no |
auxPrice |
The trigger price for stop orders | placeOrder |
no |
oca |
If you are submitting multiple orders that are OCA, specify a string for this key. | placeOrder |
no |
passthru |
true or false, default to false if not specified. | placeOrder |
no |
Below are some example commands. Click on them to try it out. Make sure AutoTrader is connected to TWS DEMO Account and not to your live account.
AutoTrader currently displays NQ data. You want to switch it to ES. Send this command.
http://localhost:7400/setSymbol?symbol=ES
To get quotes for ES, send this command.
http://localhost:7400/getQuotes?symbol=ES
To get quotes for another symbol that is not the current symbol in AutoTrader, send this command.
http://localhost:7400/getQuotes?symbol=NQ
Request market data for any symbol, even those not defined in AutoTrader. If you submit this command from a browser, the market data will continuously display in your browser until you close the browser or disconnect TWS.
http://localhost:7400/reqMktData?symbol=ZN&secType=FUT&exchange=ECBOT&expiry=200406¤cy=USD
To place an order that will be managed by AutoTrader
http://localhost:7400/placeOrder?symbol=ES&action=BUY&qty=1&orderType=MKT&passthru=false
http://localhost:7400/placeOrder?symbol=ES&action=BUY&qty=1&orderType=BuyAtAsk&passthru=false
http://localhost:7400/placeOrder?symbol=ES&action=SELL&qty=1&orderType=MKT&passthru=false
http://localhost:7400/placeOrder?symbol=ES&action=SELL&qty=1&orderType=SellAtBid&passthru=false
To reverse a position managed by AutoTrader
http://localhost:7400/reverse
To close a position managed by AutoTrader
http://localhost:7400/closePosition
To place an order that will not be managed by AutoTrader (pass thru)
http://localhost:7400/placeOrder?symbol=ES&secType=FUT&exchange=GLOBEX&expiry=200406¤cy=USD&action=BUY&qty=1&orderType=MKT&passthru=true http://localhost:7400/placeOrder?symbol=ES&secType=FUT&exchange=GLOBEX&expiry=200406¤cy=USD&&action=SELL&qty=1&orderType=MKT&passthru=true