tws

Can you tell whether market data is delayed in TWS API?


I've enabled delayed market data, using:

  ibClient.ClientSocket.reqMarketDataType(4);

But then some of my market data will be delayed, and some will not be. Is there any way of telling which are delayed (and ideally by how much)?


Solution

  • When you receive the tickprice message take note of the field parameter, that tells you if it is delayed or live. Also register for MarketDataType events will give you another message as a header for the data type.

    Something like:

    public enum TickFields:int
    {
        BidPrice = 1,
        AskPrice = 2,
        LastPrice = 4,
        DelayedBid = 66,
        DelayedAsk = 67,
        DelayedLast = 68,
    }
    
    public enum MarketDataTypes:int
    {
        Unknown = 0,
        Live = 1,
        Frozen = 2,
        Delayed = 3,
        DelayedFrozen = 4,
    }
    

    The above constants are available at https://interactivebrokers.github.io/tws-api/tick_types.html and https://interactivebrokers.github.io/tws-api/market_data_type.html

    //in constructor
    ibClient.TickPrice += Recv_TickPrice;
    ibClient.MarketDataType += Recv_MarketDataType;
    
    
    private void Recv_TickPrice(TickPriceMessage msg)
    {
        switch((TickFields)msg.Field)
        {
            case TickFields.AskPrice:
            case TickFields.DelayedAsk:
                // do something with the ask price
                break;
            case TickFields.BidPrice:
            case TickFields.DelayedBid:
                // do something with the bid price
                break;
            default:
                return;
        }
    }
    
    private void Recv_MarketDataType(MarketDataTypeMessage msg)
    {
        Program.Log(LogLevel.Debug, "> Recv_MktDataType()", $"MarketDataType: {(MarketDataTypes)msg.MarketDataType}");
        switch((MarketDataTypes)msg.MarketDataType)
        {
            case MarketDataTypes.Live:
                break;
            case MarketDataTypes.Frozen:
                break;
            case MarketDataTypes.Delayed:
                break;
            case MarketDataTypes.DelayedFrozen:
                break;
            default:
                return;
        }
    }
    

    If I recall correctly a MarketDataTypeMessage is sent before each block of TickPriceMessages, so when TWS upgrades your reqMarketDataType(FrozenDelayed) to delayed (or live - if you are subscribed to that instruments' data) you should receive something along the lines of:

    MarketDataTypeMessage (Frozen)

    TickPriceMessage (multiple)

    MarketDataTypeMessage (Live/delayed)

    TickPriceMessage (multiple)

    Of course exactly what ticktypes you receive will depend on the genericTickList string entered into reqMktData.