Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to ignore a secondary data series in OnRender?

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

    How to ignore a secondary data series in OnRender?

    I have this code:

    Code:
    foreach (var data in MyDataDictionary.Values)
    {
    if (data.OpenTime >= Bars.GetTime(ChartBars.FromIndex) && data.CloseTime <= Bars.GetTime(ChartBars.ToIndex))


    It has render lines and rectangles after that. It works fine when not using real-time data, only displaying
    the objects when they're within the chart time,

    but as soon as I turn on real-time data, they all disappear.
    I do have a secondary day chart on the indicator.

    I have a theory that it's disappearing because of the daily data series,
    because
    Print("FromIndex Time: " + Bars.GetTime(ChartBars.FromIndex));
    provides lots of different times when not connected to real-time data, like:

    FromIndex Time: 2/01/2024 8:50:00 am
    FromIndex Time: 2/01/2024 8:55:00 am
    FromIndex Time: 2/01/2024 9:05:00 am
    FromIndex Time: 2/01/2024 9:10:00 am
    FromIndex Time: 2/01/2024 9:20:00 am
    FromIndex Time: 2/01/2024 9:25:00 am
    FromIndex Time: 2/01/2024 9:30:00 am
    FromIndex Time: 2/01/2024 9:35:00 am
    FromIndex Time: 2/01/2024 9:40:00 am​

    but when connected to real-time, it only gives
    FromIndex Time: 28/12/2023 5:00:00 pm
    FromIndex Time: 28/12/2023 5:00:00 pm
    FromIndex Time: 28/12/2023 5:00:00 pm
    FromIndex Time: 28/12/2023 5:00:00 pm
    FromIndex Time: 28/12/2023 5:00:00 pm

    what's the correct code to add so it ignores the daily secondary data series?

    Thank you!​

    #2
    I'm not sure I fully understand the sort of problem you are having because I don't know what your chart looks like e.g. whether the secondary series has bars between the other bars or wide gaps, etc. But, I know one thing right away - in OnRender, you should not be referring to Bars but rather to ChartBars, in order to get the bars that are being visually displayed relative to the indicator in question (the one running OnRender). You're looking at ChartBars.FromIndex, but then getting that index number from Bars rather than ChartBars, which has you already down the wrong path.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello davydhnz,

      Thank you for your post.

      The best practice for restricting OnRender() to the visible chart bars is to use the following loop:
      Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
      {
        // restricting this loop to only the ChartBars.From/ToIndex limits the loop to only what is visible on the chart
        for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
        {
          Print(ChartControl.GetSlotIndexByX(barIndex));
        }
      }​
      This is listed in the Performance Practices on the following page:


      More info about FromIndex and ToIndex may be found at the following links:



      You are only printing the value of Bars.GetTime(ChartBars.FromIndex). What if you use the loop from the snippet above and instead use GetValueAt() to get a value, such as from the Time series or the Close price?

      "but as soon as I turn on real-time data, they all disappear."
      Are there any errors on the Log tab of the Control Center after enabling real-time data? If so, what do the errors report?

      I look forward to your reply.
      Emily C.NinjaTrader Customer Service

      Comment


        #4
        Thank you both for you replies.

        I wasn't able to get any errors, but did confirm a solution.

        Based on what you shared, I thought, maybe I should get away from

        Bars.GetTime(ChartBars.FromIndex) and Bars.GetTime(ChartBars.ToIndex))
        and try just ChartBars.FromIndex and ChartBars.ToIndex

        and also change data.OpenTime and data.CloseTime
        to Bars.GetBar(data.OpenTime) and Bars.GetBar(data.CloseTime)


        that didn't work. Then I looked around online how to do Bars.GetBar only on the primary data series and deliberately suppress the secondary daily series,
        and that worked!

        Here's the functional code:
        BarsArray[0].GetBar(data.OpenTime) >= ChartBars.FromIndex && BarsArray[0].GetBar(data.CloseTime) <= ChartBars.ToIndex)

        Thank you both for pointing me in the right direction

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,406 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        98 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        8 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        160 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        9 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X