Subscribe to Bar Events

Created at 12 Oct 2020, 22:01
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CT

ctid1412177

Joined 02.03.2020

Subscribe to Bar Events
12 Oct 2020, 22:01


Hi All,
I am trying to subscribe to multiple symbols for a timeframe of 1 hour.  I want the method to be called on each symbols bar event update.  The code is below. I am concerned that the method will be called once for the traded instrument and it will cycle through all the instruments in my "marketDataSymbolsProd" Symbol[], and will not give me the true close/open price.

 subscribeBarEvents();
 private void subscribeBarEvents()
        {
            foreach (var symbol in marketDataSymbolsProd)
            {
                if (OPTIMISE == false)
                {
                    var bars = MarketData.GetBars(timeFrame, symbol.Name);
                    bars.BarOpened += onNewBarOpen;
                }
            }
        }

 

  private void onNewBarOpen(BarOpenedEventArgs barOpened)
        {
            if (OPTIMISE == false)
            {
                if (mapCoef.ContainsKey(barOpened.Bars.SymbolName))
                {
                    if (Bars.Last(1).OpenTime.CompareTo(startTime) >= 0)
                    {
                        Statistical barUpdate = (Statistical)mapCoef[barOpened.Bars.SymbolName.ToString()];
                        //Print(barOpened.Bars.SymbolName.ToString() + ", onNewBarOpen, " + barOpened.Bars.OpenTimes.ToString());
                        barUpdate.candle.isNewBar = true;
                        updateMarketData(barOpened.Bars.SymbolName.ToString());
                        foreach (var symbol in marketDataSymbolsProd)
                        {
                            Statistical dataObj = (Statistical)mapCoef[symbol.Name];
                            //DEAL WITH ANY MARKET VENUE WHICH IS SHUT BUT WE STILL NEED TO PRICE OFF THE LAST PRICE.
                            if (symbol.MarketHours.IsOpened() == false && dataObj.candle.isNewBar == false)
                            {
                                string info = (Bars.Last(1).OpenTime.ToString() + "," + symbol.Name.ToString() + ", onNewBarOpen As MarketClosed, ");
                                Print(info);                                
                                debugList.Add(DateTime.Now.ToLongTimeString()+"," + interceptSymbol +","+ info);
                                dataObj.candle.isNewBar = true;
                                updateMarketData(symbol.Name);
                            }
                        }
                    }
                }
            }
        }

The idea of the code is that we can deal with an instrument and take its last price if it is shut for trading.  However, it would be good if I could rely on the code to update the method on every bar update event regardless if the market is open or closed and give me the lates open price for that bar.
 

Could someone please let me know if I am on the right path or I need to make a change in the code?

Thanks  

 


@ctid1412177
Replies

PanagiotisCharalampous
13 Oct 2020, 08:05

Hi there,

The OnBar method will be called as soon as the bar for the specific symbol has been created.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous