X TRADER API Tips and Tricks - Trading Technologies · 2019-02-08 · X_TRADER API Tips and Tricks...
Transcript of X TRADER API Tips and Tricks - Trading Technologies · 2019-02-08 · X_TRADER API Tips and Tricks...
X_TRADER API Tips and Tricks- A course by Trading Technologies -
Intended Audience
Prior Development Experience with C++, C#, or Visual Basic
Familiar with XTAPI
Familiar with Trading Application logic
This is not an Introduction!
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
XTAPI Overview
XTAPI is a collection of objects and interfaces that provide access to the TT environment through custom applications.
XTAPI objects provide real-time access to Market and Implied Prices, including Depth
Send, Modify, and Cancel Orders
Risk Administration and Back Office tasks
Market Browsing and Contract Specifications
Receive and process Fill records
XTAPI Overview
XTAPI uses Microsoft’s COM technology
Any COM-enabled language can be used to develop XTAPI applications Examples: C++, Visual Basic
.Net languages are compatible but not native Examples: C#, Visual Basic.Net
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
XTAPI is STA
XTAPI is a Single-Threaded Apartment (STA) model.
If you are able to develop your application with the STA model, you should. Because XTAPI is STA, you will not pay any marshalling or thread context switching penalties.
TT does not recommend the Multi Threaded Apartment (MTA) Model because your application will incur thread context switching with each method call to XTAPI.
Avoiding the COM Barrier
MTA
Application
COM
STA
XTAPI
A thread context switch will occur whenever a method or property of XTAPI is called from your MTA application.
Multi-Threaded Applications
To develop a multi-threaded XTAPI application, you should still select the STA model. Create a single thread that will act as the entry point to XTAPI, and have all other threads in your application access XTAPI through this single thread
XTAPIXTAPI Access
Thread
User Worker Thread
User Worker Thread
User Worker Thread
Get Properties
The Get() method causes to XTAPI allocate a VARIANT and return it to your application for extraction.
4 VARIANTS have been created!
1
23
4
Avoiding the COM Barrier
To optimize your code, condense the number of VARIANT objects that need to be created
Optimally, we would need only a single VARIANT object to be created, with each of our data
members contained within it
Compound Get Properties
This optimization is accomplished by using Compound Get Properties
Compound Get Properties request multiple XTAPI properties within a single method call.
This eliminates the need for XTAPI to repeatedly allocate individual VARIANT
objects
Example: Compound Get Properties
A single VARIANT is created to hold all of the property values. Data extraction into typed values occurs in native code.
1
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Ticks != Points != X_TRADER Display
XTAPI provides specific data types for many properties that allow you to request values in different formats.
Let XTAPI handle the data conversions.
Price Formats
X_TRADER String Format
Ticks
Currency
Delta
Data Types
Integer
Decimal
String
Bad Example
The following code snippet shows the extraction of the Best Bid value in X_TRADER string format, and then converts that price to a Tick Value.
This requires the user to add their own data conversion code
Use XTAPI to do your data conversion
Specifies Ticks Format
Using Ticks is recommended
XTAPI natively works with prices in Ticks
Modifying your application to use tick prices will eliminate the need to convert data to and from different formats both in your application as well as in XTAPI
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Inside Market vs Depth Updates
To subscribe for Inside Market updates: OnNotifyUpdate
To subscribe for Depth updates: OnNotifyDepthUpdate
Depth + Inside Market
OnNotifyUpdate
OnNotifyDepthUpdate
A market depth update will include any inside market updates
Depth vs. Inside Market Example
• Assume you are writing an automated Market Making application.• Per your Market Making agreement with the
Exchange, your quoting orders must be within 2 levels of the Inside Market
• When the Inside Market moves, you may want to re-quote your order. The decision is based on liquidity• If there is sufficient liquidity, you can quote at the
Inside Market
• If there is not, you want to quote up to 2 levels away, in an attempt to avoid getting filled
Depth vs IM Example…
Based on the application requirements, you need only to take an action if the Inside Market values change
Therefore you can subscribe to Inside Market changes and only request Depth when needed
This will result in fewer event callbacks, increasing application performance
Depth vs IM Example…
Based on the market making strategy, orders will be placed in the market:
Summary of Application Logic
When the Inside Market changes, the OnNotifyUpdate event will be fired. Within your event handler method, request Market Depth
The application subscribes to the OnNotifyUpdate event in order to receive a notification when the Inside Market values change
If…Else if… Then….
Level 0 quantity >= 50 Place order at Level 0
Level 0 + Level 1 >= 50 Place order at Level 1
Neither of the above are true Place order at Level 2
Depth vs IM Example…
This will result in significantly fewer callbacks. It also only gives you a depth snapshot when there has been a change to the Inside Market, eliminating superfluous notifications of outside quoting, and improving the overall efficiency of the application 3 Price Levels
on either side of the Market
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Filter Updates for the values you want
When using an Instrument Notify (TTInstrNotify) object to receiveevent callbacks, you will receive the event when any of the instrument’s propertieschange
As an optimization, you can set a Notify Filter that will only fire the event callbackswhen the specified properties change
This is accomplished by using the UpdateFilter property
Now only changes to the Last Traded Price, the Best Bid, or the Best Ask will result in the event being fired.
Back to the Depth vs IM Example…
Our Market Making example can be further optimized. Currently we are receiving the OnNotifyUpdate event when any of the Inside Market values change (Price, Quantity, High, Settle, etc…)
By setting a Notify Filter, we can limit the event callbacks to fire only when the Inside Market Price changes, ignoring any other updates
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Rapid Fill Delivery
When Rapid Fill Delivery is OFF (Default)
Order Server Fill Server
Exchange
XTAPI
Rapid Fill Delivery
When Rapid Fill Delivery is ON
Order Server Fill Server
Exchange
XTAPI
XTAPI accepts the first fill it receives
Rapid Fill Delivery Caveats
XTAPI’s Order Sets listen to Fill notifications from the Order Server, regardless of the Rapid Fill Delivery setting. This can lead to “in flight” issues.
For example, assume you have Rapid Fill Delivery set to TRUE, and you have a single Order Set containing two working orders.
Your application receives a Full Fill Record from the Fill Server. In your event handler method, you query for the number of orders contained within the Order Set.
The Order Set (may) report back that it contains 2 orders. This seems like an error because your Fully Filled order should no longer exist.
Rapid Fill Delivery Caveats…
It’s not an error.
The Order Set will eventually correct itself – when the Order Server processes the Fill Record and sends the notification to your application. This “in flight” issue must be considered when deciding whether to utilize Rapid Fill Delivery.
Rapid Fill Delivery Caveats…
These properties will not be available from the Fill Record when Rapid Fill Delivery is ON:
Ex:OrderNoOrderNo
FillKey
BrokerTec must use Rapid Fill Delivery
You will not receive fill notifications until after the Workup Phase is over unless you enable Rapid Fill
Delivery.
On BrokerTec, during the Workup Phase, the exchange will not send a Fill Notification until the Workup Phase is complete.The Fill records are held at the Fill Server. To get around this,turn on Rapid Fill Delivery, which will force the Fill Receipts to be sent from the Order Server.
Rapid Fill Delivery Usage Guidelines
Turn RFD On if… Turn RFD Off if…
Responding to Fills (e.g. hedging a spread) is the prime function of your application
Accurate Order Book management is the prime function of your application
In General…
The determination of whether to enable or disableRapid Fill Delivery should be based on the requirements of the application. There is no simple “Yes” or “No” answer.
Rapid Fill Delivery
Now that the costs and benefits of Rapid Fill Delivery
are clear, if you decide to enable them in your
application, here is how:
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Use XTAPI properties
Many users try to track market and order status values themselves.
XTAPI does it for you.
The following slides illustrate several Order Set properties that will provide accurate position
information.
Use XTAPI properties – Buy Position
NetPos – Current net position based on fillsBuyPos – Quantity bought based on fills
Example:
You have a single buy order in the market with an order quantity of 12. You receive a partial fill of 8 for the order
OrderSet.NetPos is 8OrderSet.BuyPos is 8OrderSet.SellPos is 0
Use XTAPI properties – Sell Position
NetPos – Current net position based on fillsSellPos – Quantity sold based on fills
Example:
You have a single sell order in the market with an order quantity of 18. You receive a partial fill of 11 for the order
OrderSet.NetPos is -11OrderSet.BuyPos is 0OrderSet.SellPos is 11
Combined Position Example
Example:
You submit a single sell order with an order quantity of 18. You receive a partial fill of 11 for the order
OrderSet.NetPos is -3OrderSet.BuyPos is 8OrderSet.SellPos is 11
You submit a single buy order with an order quantity of 12. You receive a partial fill of 8 for the order
OrderSet: BuyCnt / SellCnt
Example:
There are three buy orders in the market, with order quantities of 5, 12, and 17
There are two sell orders in the market, with order quantities of 2 and 8
OrderSet.BuyCnt = 3OrderSet.SellCnt = 2
BuyCnt – returns the number of individual buy ordersSellCnt – returns the number of individual sell orders
OrderSet: BuyWrk / SellWrk
Example:
There are three buy orders in the market, with order quantities of 5, 12, and 17
There are two sell orders in the market, with order quantities of 2 and 8
OrderSet.BuyWrk = 34OrderSet.SellWrk = 10
BuyWrk – returns the aggregate quantity of buy ordersSellWrk – returns the aggregate quantity of sell orders
Average Cost of Buys / Sells
To determine the average cost of your buy orderscontained within this Order Set, divide OrderSet.BuyTicksby OrderSet.BuyPos
To determine the average cost of your sell orderscontained within this Order Set, divide OrderSet.SellTicksby OrderSet.SellPos
Average Cost of Buys (in Ticks) = BuyTicks / BuyPosAverage Cost of Sells (in Ticks) = SellTicks / SellPos
This slide assumes that your Order Set contains orders and fills
for a single Instrument
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Implied Price Engine
To turn XTAPI’s Implied Engine ON, the Instrument object’s
CalculateTTImplieds property should be set to TRUE (1)
By default, XTAPI has the Implieds Engine turned OFF
By default, RTD has the Implieds Engine ON
Exchange vs TT Implieds
Certain exchanges (e.g. LIFFE) disseminate their own Implied prices. In this case, turning the XTAPI Implied Engine ON/OFFhas no impact.
XTAPI’s Implied Engine will calculate implied prices for markets when the Exchange does not disseminate its own implied prices.
Turning the Implied Engine ON or OFF will not affect Exchange-disseminated implieds prices, only XTAPI-generated implieds.
Implied Depth?
The XTAPI Implied Engine does not calculate Implied Depth.
If the Exchange disseminates Implied Depth, XTAPI will deliver this data to you.
Note that X_TRADER does not display Implied Depth. The only way to receive Exchange disseminated Implied Depth, in TT, is through XTAPI.
Direct versus Implied Price Fields
To view Direct Prices only:
To view Implied (TT Calculated or Disseminated ) Prices only:
Ask AskQty
BidQty Bid
IAsk IAskQty
IBidQty IBid
Merging Implieds
To view the “Best” Price, whether Direct or Implied:
MIAsk MIAskQty
MIBidQty MIBid
CalculateTTImplieds has no effect on Merging Implied and Direct quantities. However, only Exchange-disseminated Implied quantities will be merged if CalculateTTImplieds is OFF.
Merging Implieds Examples
•When the Implied Bid is the Best Bid:
MIBid = IBidMIBidQty = IBidQty
•When the Direct Bid is the Best Bid:
MIBid = BidMIBidQty = BidQty
•When Implied Bid and Direct Bid prices are equal:
MIBid = Bid MIBid = IBidMIBidQty = BidQty + IBidQty
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
InstrNotify
You can attach multiple Instruments to a single Instrument Notify object:
Now both instruments will use the same event handler
InstrNotify
Benefits Risks
Could result in less code
Single point of processing
Less events = less context switching
Processing one contract may lead to missed messages
Lots of IF statements and comparisons
OrderProfile
• Is Write-Only
•To query properties of a TTOrderProfile object, use GetLast instead of Get.
• The object’s properties are not fully created until the OrderProfileis converted to a TTOrderObj by calling TTOrderSet::SendOrder(myOrderProfile);
A TTOrderProfile object is used to populate the attributes of an order. A new order is created by calling the OrderSet.SendOrder() method, passing the Order Profile object as a parameter.
Some things to keep in mind about Order Profiles:
TTHotKeyNotify – The Lost Object
You can use the TTHotKeyNotify object to receive an event callback when a key combination is pressed. You can map XTAPI functionality to this event
For example, users can create a Hot Key that will fire an event when the Space Bar is pressed. This would be a quick and convenient way to implement a Delete All Orders panic button
WARNING! The Hot Key is registered with Windows. Therefore any use of the key outside the application will result in your application receiving the callback and possibly taking action.
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Order Set - Overview
•XTAPI allows the segregation of working orders and fills into logical groupings called Order Sets
•Properties such as P&L, Net Position, Buy Count, etc… are calculated at the Order Set level
•Order Status events fire from Order Sets
•There is always at least one Order Set, but a user can create more
Too many Order Sets
While there is no hard-coded limit to the number of Order Sets a user may create, you should understand that each Order Set comes with a cost.
When a Fill Record or other Update is received by XTAPI, it must evaluate each Order Set to determine if an Order Set Update notification is required to be sent to your application.
The more Order Sets that need to be evaluated, the more performance will be impacted.
Be conscious of the cost incurred with every Order Set that you create.
Using multiple Order Sets
May be advantageous because:
•You can logically group orders, such as buy orders in one order set and sell orders in another.
•For each Order Update, every order in the Order Set must be evaluated. By limiting the number of orders in each Order Set, you may experience performance gains.
Orders in Order Sets are Ordered
When requesting the list of orders contained within an Order Setthe returned array is already ordered such that the greater the index,the further away from the Inside Market.
Working orders within an Order Set are ordered such that Working Sells come before Working Buys
Orders in OrderSet are Ordered
If you have 10 working orders, and the first 5 are Sells, to get the Buy order closest to the inside market:
TTInstrObj myInstr = (TTInstrObj)m_TTOrderSet[5];
int workingSells = (int)m_TTOrderSet.get_Get(“WrkSell”);TTInstrObj myFirstBuy = (TTInstrObj)m_TTOrderSet[workingSells];
A simple example:
A more dynamic example:
Use XTAPI properties to determine the number of working Sell orders,And then offset the array to jump to the first Buy order:
Orders in OrderSet are Ordered
For a single order on either side of the market:
TTInstrObj mySell = (TTInstrObj)m_TTOrderSet[0];TTInstrObj myBuy = (TTInstrObj)m_TTOrderSet[1];
With only a single Buy and single Sell in the market, the Sell order will
always be the first in the array, and the Buy order the second.
TickPrice
Use this method to determine the true tick size
Starting Price
Increment Output Format
The “oneTick” string will now contain the price difference between 0 ticks and 1 tick, effectively giving you the contract’s tick size.
Iterating an Order Set
Warning! The Order Set can change while you are iterating
If you must iterate through the Order Set, use the .Net “foreach” instead of the C-style “for”
The “for” loop, relying on an index to move through a collection, must first load all of the collection members into memory. “foreach” implementsThe IEnumerable interface, which is a native construct in .Net allowing the iteration through a collection without having to load the contents of the collection in memory.
With foreach, you can catch a specific exception –System.InvalidOperationException – that will occur if the collection is modified during your processing.
Use Ticks to specify price
Internally, XTAPI will convert all prices to Ticks. Specifying a tick price for an order will save the time spent with a conversion.
Agenda1. XTAPI Overview
2. The COM Barrier
3. Data Conversions
4. Market Data Updates
5. Filters
6. Rapid Fill Delivery7. Properties
8. Implied Prices
9. Event Notifications
10. Order Sets
11. Series Keys
Series Keys
•Series keys are guaranteed to be valid only within a single exchange session
•If your application stores Series Keys, you should re-request the key at the beginning of each exchange session.
A Series Key is a numeric identifier for a Financial Instrument within the TT environment.
Notes:
Working with Series Keys
To request the series key, open an instrument by specifying theGateway / Product / Product Type / Contract.
Then request the Series Key from the Instrument object:
Working with Series Keys
You can also open an instrument with a Series Key, and then extract theGateway / Product / Product Type / Contract properties:
Example of a previously extracted Series Key