?> QuantXpress – Deliver high performance automated trading and connectivity solutions



Exchange Adapter Development


BlitzTrader provides comprehensive framework to enable the quick development and integration to use a particular liquidity source such as exchanges, ECN and broker systems in addition to a broad set of a pre-built connectivity adapter for a direct use. The adapter framework is used to connect Blitz Trading System with external liquidity provider and responsible for the communication between Blitz Trading System and Exchange Adapter.
The framework provides following features
-FIX components library to develop connectivity adapters using the industry standard Financial Information eXchange (FIX) messaging protocol.
-Define a mechanism to import Instrument class of any asset type
-Provides interface, APIs for Order and Execution messaging
-Manages the session state event of adapter

Implementation Consideration

This section defines the details of how to create an exchange connectivity adapter plugin. Your main adapter implementation class must derived from core base class .AdapterBase (QX.Base.Core.Adapter namespace) provided by the framework. By implementing override methods of base class, you can bridge the communication mechanism between BlitzTrader OMS and native implementation of your adapter class.
Adapter implementation class decorated by attribute AdapterBaseAttribute defining the unique identifier represented by Guid, Name, the adapter type (Trading or Market Data), the Exchange Segment enum and the description. Here is explanation of some important override function provided for your adapter implementation class
Initialize This function is notified only once during the lifetime of the adapter instance before a start of the adapter. This is a place where you can initialize your adapter level variables. During initialize state the adapter state is Disconnected (with counterparty)
Start This function is notified by framework to start the adapter. This is a place where a adapter implementation class establish a connection and create a valid session with counterparty system to transact trading business messages. The valid adapter state after start process is successful is Logged.
Stop This function is notified by framework to stop the adapter. Adapter implementation class terminate the counterparty session here and notify to framework on session disconnected state.
ProcessAdapterOutboundMessage This function is notified to get an event of order related request message such as new order, modification and cancellation request. This information is converted into counterparty messaging format and transact with them on a establish session.
GetReadyToTradeState This function is notified by framework immediately after the successful Logged state triggered by Start. Here framework needs to ask adapter implementation class to confirm on ReadyToTrade State. ReadyToTrade state is final good state of adapter to routes and transact trade related message with counterparty system.
Implement your adapter implementation according to following example:
 
[AdapterBaseAttribute("{D56EB250-021A-4898-BE68-268802FC2296}",
                      "IB",
                       AdapterType.Trading,
                       ExchangeSegment.IB,
                      "The adapter provides the Interactive and MarketData connectivity towards IB TWS")]
public class IBMarketDataNativeAdapter : AdapterBase
{ 
   private NSEInitDefaults _initDefaults = null;
    ............................
    ............................

    protected override void Initialize()
    {
       ............................................
      .............................................
        _initDefaults = new NSEInitDefaults(base.AdapterBaseInterface.ConfigurationFile.FullName, 0);
    }
    public override void Start()
    {
        ...........................................................
        base.SetAdapterState(SessionState.LoggedIn);    
   }
    public override void Stop()
    {
    }
    public override void DisposeAdapter()
    {
    }
    public override void ProcessAdapterOutboundMessage(BlitzMessage outboundMessage)
    {
        switch (outboundMessage.MessageCode)
        {
                case BlitzMessageCode.NewOrder:
                    ProcessNewOrderMessage(outboundMessage as NewSingleOrderRequestMessage);
                    break;
                case BlitzMessageCode.OrderModification:
                    ProcessOrderModificationMessage(outboundMessage as OrderModificationRequestMessage);
                    break;
                case BlitzMessageCode.OrderCancellation:
                    ProcessOrderCancellationMessage(outboundMessage as OrderCancelRequestMessage);
                    break;
                 case BlitzMessageCode.NewMultiLeg:
                     ProcessMultilegOrderMessage(outboundMessage as NewMultiLegOrderRequestMessage);
                     break;
                  case BlitzMessageCode.NewSpreadOrder:
                     ProcessSpreadOrderMessage(outboundMessage as NewSpreadOrderRequestMessage);
                     break;
                default:
                    FileLogger.WriteLine("Outbound message not supported by Adapter.");
                    break;
            }
    }
    protected override string GetSessionIdentifer()
    {
        //Your custom session identifer
        return "IB->" + AdapterConfig.Instance.ClientID;
    }
    protected override void GetReayToTradeState()
    {
    
         base.SetAdapterState(SessionState.ReadyToTrade);
     } 
}
After compiling your implementation of .NET adapter, place output assembly to application Adapter directory.

Manage Execution Report

Exchange sends real-time trade information notification called as Execution Report for orders sent to them.
The following message types are covered by adapter API Execution Reports:
-Confirm the receipt of an order.
-Confirm changes to an existing order (i.e. accept, cancel requests and cancel replace requests).
-Update order status changes.
-Update trade information of working orders (Filled/Partially Filled).
-Update Rejection of orders.
Exchange relayed native order execution report needs to be translated and communicated to Blitz OMS.

API to create a new order acceptance message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreateOrderAcceptedMessage(long exchangeInstrumentID,
                                                                                            uint clientOrderID,
                                                                                            string exchangeOrderID,
                                                                                            string exchangeExecutionID,
                                                                                            long transactTime,
                                                                                            long transactTimeUTC,
                                                                                            long orderAcceptedTransactTime);

API to create a new order rejection message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreateOrderRejectedMessage (long exchangeInstrumentID,
                                                                                             uint clientOrderID,
                                                                                             string exchangeOrderID,
                                                                                             string exchangeExecutionID,
                                                                                             long transactTime,
                                                                                             long transactTimeUTC,
                                                                                             string rejectReason);

API to create a order cancel rejection message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreateOrderCancelRejectMessage(long exchangeInstrumentID,
                                                                                                uint clientOrderID,
                                                                                                string exchangeOrderID,
                                                                                                string exchangeExecutionID,
                                                                                                long transactTime,
                                                                                                long transactTimeUTC,
                                                                                OrderCancelOrReplaceRejectType replaceRejectType,
                                                                                string rejectReason);

API to create a order modification rejection message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreateOrderReplaceRejectMessage(
                  long exchangeInstrumentID, 
                  uint clientOrderID,
                  string exchangeOrderID,
                  string exchangeExecutionID,
                  long transactTime,
                  long transactTimeUTC,
                  OrderCancelOrReplaceRejectType replaceRejectType,
                  string rejectReason);

API to create a partial fill trade report message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreatePartialFillReportMessage(
                  long exchangeInstrumentID, 
                  uint clientOrderID,
                  string exchangeOrderID,
                  string exchangeExecutionID,
                  long transactTime,
                  long transactTimeUTC,
                  uint tradedQuantity,
                  double tradedPrice);

API to create a stop order triggered report message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreatePartialFillReportMessage(
                  long exchangeInstrumentID, 
                  uint clientOrderID,
                  string exchangeOrderID,
                  string exchangeExecutionID,
                  long transactTime,
                  long transactTimeUTC);


API to create a complete fill trade report message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreateFilledReportMessage(
                  long exchangeInstrumentID, 
                  uint clientOrderID,
                  string exchangeOrderID,
                  string exchangeExecutionID,
                  long transactTime,
                  long transactTimeUTC,
                  uint tradedQuantity,
                  double tradedPrice);

API to create a order cancelled report message
 
ExecutionReportMessage executionReport =  ExecutionReportMessage.CreateOrderCanceledMessage(
                  long exchangeInstrumentID, 
                  uint clientOrderID,
                  string exchangeOrderID,
                  string exchangeExecutionID,
                  long transactTime,
                  long transactTimeUTC);

Execution report message communicated to BlitzOMS with following API
 base.ProcessAdapterInboundMessage(executionReport);

File Logger

 
FileLogger.WriteLine(LogType.Error, base.AdapterExchangeSegment + " Adapter is not initialized .");

Adapter Custom Command

 
base.CreateActionCommand(new Guid("AEEA965A-DCDE-4387-B071-88B2AFCB8345"),
                                  "Upload Instruments",
                                   new ActionCommandFieldInfo[] { },
                                   ExecuteActionCommandUploadInstrument);


base.CreateActionCommand(new Guid("A199361D-1B6C-4C80-BFEE-231395353D68"),
                                  "Display Adapter Health", 
                                   new ActionCommandFieldInfo[] { },
                                   DisplayAdapterHealthStatus);

...................................
...................................

private void ExecuteActionCommandUploadInstrument(ActionCommandFieldInfo[] inputFields)
{
}

private void DisplayAdapterHealthStatus(ActionCommandFieldInfo[] inputFields)
 {
     string adapterHealthStatus = string.Format("AdapterState: {0},
                                                 InvitationCount: {1}, 
                                                 TotalInvitationCount:{2}, 
                                                 TotalAckCount: {3}.",
                                                 AdapterBaseInterface.SessionState,
                                                _interactiveSession.GetInvitationCount(),
                                                _interactiveSession.GetTotalInvitationCount(),
                                                _interactiveSession.GetTotalAcknowledgePacketCount());

              RaiseAlert(AlertType.Info, adapterHealthStatus);
 }

Import Instruments

Importing a Instrument is a efficient way to add Instruments of specific type in the BlitzTrader system.
Exchange Adapter base class provides a handle to Blitz Instrument Persistence that can be used in your implementation class to persist all instrument supported by the adapter. The Instrument list can be obtained through your internal function which understands the proprietary mechanism of the exchange to get an Instrument List. Most exchange provides a ASCII file with the details of tradable instrument in their proprietary format. In this case, your code must parse the file to retrieve relevant information to create a Instrument object of specific type
BlitzTrader import mechanism consolidates and persist the entire instrument list from all configured adapters and allow system to have an access of Instruments.
 
Instrument[] availableInstruments = GetNSEFOInstrumentList();
.....
.....
// Persisting the Instrument List in BlitzTrader. It overrides the existing entry 
InstrumentPersistence.AddInstruments(ExchangeSegment.NSEFO, availableInstruments);

Pseudo code snippet of GetNSEFOInstrumentList() function
Instrument[] GetNSEFOInstrumentList()
{
    ConcurrentBag instrumentList = new ConcurrentBag();
    .......
    .......

    Instrument instrument = new Futures(........);
    instrumentList.Add(instrument);

    .......

    instrument = new Options(........);
    instrumentList.Add(instrument);

    .......

    instrument = new Equity(........);
    instrumentList.Add(instrument);

    .......
    return instrumentList.ToArray();
}

The Instrument import process can triggered using an custom command supported by Adapter. BlitzTrader administrator can use the command to manually execute the command to refresh the latest Instrument into the system.