Search code examples
quickfixfix-protocolquickfixj

How to compose OrderCancelRequest in QuickFix/J


I am trying to create OrderCancelRequest using FIX.4.2 but confused with OrderID,OrigClOrdID and ClOrdID. I searched on the web but it was not clear to me. Please explain those parameters and provide snippet of code for OrderCancelRequest if possible.

Thanks in advance.


Solution

  • You wish to cancel an order you created with quickfix.fix42.NewOrderSingle. To send that message you had to assign it a unique quickfix.field.ClOrdID. For instance:

    String instructionId = createNewInstructionId( ); 
    quickfix.Message fixMessage = new quickfix.fix42.NewOrderSingle (
        new ClOrdID( instructionId ),
        new HandlInst( HandlInst.AUTOMATED_EXECUTION_ORDER_PUBLIC ), 
        new Symbol( symbol ),
        new Side( Side.BUY ),
        new TransactTime( ),
        new OrdType( OrdType.LIMIT )
    );
    // ...
    

    You need to store this instructionId for referencing in further messaging.

    If the counterparty accepts the instruction, it does so with an EXECUTION_REPORT message (OrdStatus.NEW). This execution report will contain an quickfix.Field.OrderID field, which is a unique identifier for the order as assigned by the broker (uniqueness within single trading day, or uniqueness across days for multi-day orders). Store this OrderID for use in later instructions (orderIdBroker).

    If you wish to cancel the order, you need to reference the instruction that created the order. The OrigClOrdID in this instance is the ClOrdID of the NewOrderSingle instruction that created the order. The ClOrdID field is a unique identifier for the cancel request (a new identifier you assign to the cancel request). If you wish (or the broker requires it) you can supply the OrderID you received from the broker:

    String orderInstructionId = getOrderInstructionId( );
    String cancelInstructionId = createNewInstructionId( ); 
    quickfix.Message fixMessage = new quickfix.fix42.OrderCancelRequest (
        new OrigClOrdID( orderInstructionId ),
        new ClOrdID( cancelInstructionId ),
        new Symbol( symbol ),
        new Side( Side.BUY ),
        new TransactTime( )
    );
    // If required, set the OrderID as assigned by the broker:
    String orderIdBroker = getOrderIdBroker( );
    fixMessage.setField( new OrderID( orderIdBroker ) );