Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem acquiring bid/ask data in real time

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problem acquiring bid/ask data in real time

    I've been developing my strategy with a requirement of accessing both bid and ask OHLC data. I do so by having my chart display the bid data and my strategy has the following statement:

    Code:
    AddDataSeries(Instrument.MasterInstrument.Name, BarsPeriod.BarsPeriodType, BarsPeriod.Value, MarketDataType.Ask);
    I then have a class for maintaining a Candle object containing the OHLC and datetime data, and I create one for both the bid and ask data. The data I'm collecting is of the most recent bar and I create these objects in OnBarUpdate only when IsFirstTickOfBar is true. So the Candle class sets the data with the following method:

    Code:
    public void set()
    {
        this.open = this.ns.Opens[this.barsIndex][this.barsAgo];
        this.high = this.ns.Highs[this.barsIndex][this.barsAgo];
        this.low = this.ns.Lows[this.barsIndex][this.barsAgo];
        this.close = this.ns.Closes[this.barsIndex][this.barsAgo];
        this.dateTime = this.ns.Times[this.barsIndex][this.barsAgo];
    }​
    where barsIndex = 0 for bid or 1 for ask and barsAgo = 1, defined in the constructor.

    Within my strategy I have the following method:

    Code:
    private void updateCandles()
    {
        if (BarsInProgress == 0)
        {
            this.yesterdaysBidCandle.set();
            this.yesterdaysBidCandle.print("Bid");
        }
    
        if (BarsInProgress == 1)
        {
            this.yesterdaysAskCandle.set();
            this.yesterdaysAskCandle.print("Ask");
        }
    }
    I initially run my program on historical data, which generates output like:

    Bid 10/22/2024 4:38:00 PM open 1.07985, high 1.07985, low 1.07985, close 1.07985
    Ask 10/22/2024 4:38:00 PM open 1.07995, high 1.07995, low 1.07995, close 1.07995

    ​Everything is beautiful until I start running in real time and executing my data analysis code that determines when to buy or sell and where to set stops to exit trades. I only process my analysis after BarsInProgress == 1. Most iterations through OnBarUpdate show output like the one above but on occasion I get situations where I see one but not the other bid and ask candles or one won't have the same timestamp as the other. Either situation kills my strategy.

    Any idea why this is happening and how I can ensure that I have both Candle objects properly defined with each iteration of OnBarUpdate within the IsFirstTickOfBar iterations before I run my analysis? It's critical I resolve the problem in order to properly run my strategy.




    #2
    Hello dweems,

    Bid and ask are separate series so in realtime they will have individual events which may not have the same timestamp. Generally you would reference the bid and series from the last event and use the most recent value, that is essentially what the GetCurrentBid and ask methods are doing.

    One other note is that AddDataSeries is not designed to be dynamic so the way you have coded that may fail to load data in some use cases. For the instrument you can use null in place of Instrument.MasterInstrument.Name to get rid of the dependence of Instrument, for the other variables I would suggest to hard code those to make sure AddDataSeries is always working correctly.

    Comment


      #3
      Hmmm. So what I have to have are the ask high and the bid low of the previous bar. GetCurrentBid() and GetCurrentAsk() can't give me those. Is there another way to acquire these two values?

      Comment


        #4
        Hello dweems,

        You can use the ask and bid series to access data for bars ago, however, as mentioned, those are separate events, so they will not always have the exact same timestamps in real-time. You may see cases where you have an ask event and a short time before the next bid event. In historical data, they will have the same timestamps based on how historical data is collected and processed.

        Comment


          #5
          So you're telling me there is no work around this issue?

          Comment


            #6
            Hello dweems,

            The data you are accessing in realtime comes from an exchange so those events will only happen as the ask or bid on the exhcnage change and send new data events. The way that works in realtime is based on how the events are happening right at that time so there is not a way to change that. If you need your logic to work in a certain way you would have to ome up with a plan on how you wanted to account for that in realtime and adjust your calculations.

            The high and low of the previous bar can always be accessed using High[1] or Low[1], the datapoint which you are getting will depend on the order of events and when you call that series. Just as a quick example if we use your code and assume the primary is the bid series and you are adding an ask series.

            Let's assume BarsInProgress 0 is called for the bar close, the 1 bars ago value you get for the primary series is going to be the previous bar. If you try to access the previous bar for the BIP1 series at that point, it's going to be whatever the last closed bar for BIP1 was. Because the ask and bid don't have to be called in a specific order, that could be the same timestamp as the previous bar for BIP0 or not. That depends on if the ask event happened near the same time to create a bar close for that time. If it did not, you might get 1 bar further back, which is the last closed bar value and the currently known data.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            116 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            61 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            40 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            44 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            82 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X