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

Access to High, Low, Open, Close, Time series out of OnBarUpdate

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

    Access to High, Low, Open, Close, Time series out of OnBarUpdate

    Hello
    I am trying to access OHLC, Time data series out of OnBarUpdate , but it seems these data are invalid as most of time I get index out of range exception
    so as general rule is there anyway to access these data on other modules like OnRender or OnMouseDown when user click on any bar on chart? how I should loop into these data series when user click on specific bar, for example when user click on a bar, I want to check if this bar is lower than previous day low, how I can loop into High/Low data series out of OnBarUpdate (in this case in OnMouseDown handler)

    #2
    to make it more clear I attach a sample code I have written, I have tried to read indicator value for 46 Bars ago once in OnBarUpdate and once in OnRender
    value read in OnBarUpdate is written on top right, and exception I have gotten when trying to read in OnRender function is printed on top left

    Code:
            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                if (!dataIsReady) return;
                try
                {
                    if (CurrentBar>46)
                    {
                        double obv=Mavis()[46];
                        Draw.TextFixed(this, "Test", obv.ToString(), TextPosition.TopLeft);
                    }
                }
                catch (Exception ex)
                {
                    Draw.TextFixed(this, "Test", CurrentBar.ToString()+", "+ex.Message, TextPosition.TopLeft);
                }
            }
            
            protected override void OnBarUpdate()
            {
                try
                {
                    if (CurrentBar>46)
                    {
                        value=Mavis()[46];
                        Draw.TextFixed(this, "Test2", value.ToString(), TextPosition.TopRight);
                    }
                }
                catch (Exception ex)
                {
                }
            }
    ​
    flag dataIsReady is set to true on OnStateChange when State=DataLoaded
    else if (State == State.DataLoaded)
    {
    dataIsReady=true;
    }


    Click image for larger version

Name:	image.png
Views:	42
Size:	46.8 KB
ID:	1296944

    Comment


      #3
      Hello tidicofx,

      I would not suggest trying to use drawing objects from OnRender, the code you have shown would be best suited for OnBarUpdate. OnRender is used for custom rendering where you write your own rendering code. You can see an example of that here: https://ninjatrader.com/support/help...htsub=onrender

      OnRender is not a NinjaScript bar processing method so you would need to know the specific indexes of the bars to get values from OnRender. You can use a series GetValueAt method to get prices based on an index. I would suggest looking at the sample in the following page. When using OnRender you should only try to access bar data for currently visible bars to avoid delaying OnRender, that makes it more efficient.



      JesseNinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello tidicofx,

        I would not suggest trying to use drawing objects from OnRender, the code you have shown would be best suited for OnBarUpdate. OnRender is used for custom rendering where you write your own rendering code. You can see an example of that here: https://ninjatrader.com/support/help...htsub=onrender

        OnRender is not a NinjaScript bar processing method so you would need to know the specific indexes of the bars to get values from OnRender. You can use a series GetValueAt method to get prices based on an index. I would suggest looking at the sample in the following page. When using OnRender you should only try to access bar data for currently visible bars to avoid delaying OnRender, that makes it more efficient.


        hello, than you for replying
        I sent that code just as sample, so I need to know if Data series are accessible out of OnBarUpdate or not
        here is practical reason why I need them out of OnBarUpdate
        suppose I want to write a tool that is going to trigger an action when user press a specific key
        this action is to scan last 4days and check for specific conditions based on indicator values/and market data
        this is not something that I can do in OnBarUpdate or maybe I dont know how it is possible, because trigger comes from OnKeyDown module for example
        the sample code I have written above was to just show that data access is different when I try samething in OnBarUpdate and out of it

        Comment


          #5
          Hello tidicofx,

          The link I provided shows how to access a data series outside of OnbarUpdate in areas like OnRender. You need the specific index of the bar to access its value.

          Using OnRender is not a good use case for a DrawingTool. The code you provided would be suited to be used in OnBarUpdate because DrawingTools expect to be used from OnBarUpdate with a BarsAgo. TextFixed does not use a BarsAgo but can still be used from OnBarUpdate to make sure it does not use more resources than necessary because OnRender is called very frequently.

          If you are writing code that happens based on a key press your code should be inside the key event handler and not OnRender. When using key events you also need to use TriggerCustomEvent and surround the code in the handler so you can access series values using a BarsAgo. TriggerCustomEvent should not be used from OnRender that will cause lagging.



          Code:
          TriggerCustomEvent(o =>
          {
             //your code here
          }, null);


          JesseNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello tidicofx,

            The link I provided shows how to access a data series outside of OnbarUpdate in areas like OnRender. You need the specific index of the bar to access its value.

            Using OnRender is not a good use case for a DrawingTool. The code you provided would be suited to be used in OnBarUpdate because DrawingTools expect to be used from OnBarUpdate with a BarsAgo. TextFixed does not use a BarsAgo but can still be used from OnBarUpdate to make sure it does not use more resources than necessary because OnRender is called very frequently.

            If you are writing code that happens based on a key press your code should be inside the key event handler and not OnRender. When using key events you also need to use TriggerCustomEvent and surround the code in the handler so you can access series values using a BarsAgo. TriggerCustomEvent should not be used from OnRender that will cause lagging.



            Code:
            TriggerCustomEvent(o =>
            {
            //your code here
            }, null);

            yes using TriggerCustomEvent makes it work without problem
            thank you for your reply

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by poplagelu, Today, 05:00 AM
            0 responses
            3 views
            0 likes
            Last Post poplagelu  
            Started by fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,407 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  
            Working...
            X