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

OnRender not updating after load

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

    OnRender not updating after load

    As an exercise in learning NinjaScript, I've used OnRender to draw a rectangle around the open and close of an extra data series I added for XLK. It renders fine but does not update as more bars form. Is the updating done from OnBarUpdate and not OnRender? If so, what part of my OnRender should be moved to OnBarUpdate? Here is the code:

    Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
    
       if (ChartBars.Bars == null || Bars.Instrument == null || CurrentBar < 1) {
        return;
       }
       base.OnRender(chartControl, chartScale);
       Vector2 start = new Vector2();
       Vector2 end = new Vector2();
       RectangleF		rect			= new RectangleF();
       double barWidth = chartControl.GetBarPaintWidth(chartControl.BarsArray[0]);
    
       for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++){
    
    	//This is the XLK symbol which is being added to the BarsArray   
    	double		    close					= BarsArray[1].GetClose(idx);
    	double			open					= BarsArray[1].GetOpen(idx);					
        int             x                       = chartControl.GetXByBarIndex(ChartBars, idx);
    
         // Rectangle
    	rect.X		= x - (float)barWidth * 0.5f;
    	rect.Y		= chartScale.GetYByValue(Math.Max((float)close, (float)open));
    	rect.Width	= (float)barWidth;
    	rect.Height	= Math.Abs(chartScale.GetYByValue((float)open) - chartScale.GetYByValue((float)close));
    	RenderTarget.FillRectangle(rect, close > open ? upCandleBrushDx:downCandleBrushDx);
    	RenderTarget.DrawRectangle(rect, borderBrushDx,1); 
      }
      }

    #2
    Hello swcooke,

    Thanks for the post.

    I am testing this concept here on my end. Your OnRender method did not work for me initially since you were iterating through the primary ChartBars objects and not the secondary series object added to the chart. I changed that to iterate through the series that was added second and I also added a DataSeries for the BarsArray object that it generates when you call AddDataSeries. You must run the test script on a 10-minute chart with a 1-minute series added second through the data series menu. Place the script within Documents\NinjaTrader 8\bin\Custom\Indicators and then compile

    Please see the attached script for an example.
    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I noticed this if statement and have a question:

      Code:
      if (ChartBars.Bars == null || Bars.Instrument == null || CurrentBars[1] < 1) {
        return;
      }
      My concern is the last OR which is CurrentBars[1] < 1. This seems to me that it is basically a condition that prevents the current bar from ever being drawn. Isn't this going to cause the last bar to never be drawn?

      Also, is there any way to copy the error messages that appear in the Log in Control Center or from the output of errors beneath the Editor? Would be nice to select that text or copy it somehow.

      Lastly, the file you provided compiles but does not render anything.I am trying to paint XLK data directly beneath the main instrument in a chart which is SPY. I really feel like my original idea was correct. I am iterating through the SPY bars and using that index to get the XLK Open and Close. This ensures that the bars I draw are always in sync with the SPY bars in the main pane. Using the XLK bars as the index does not lock them to the SPY bars. Does it?
      Last edited by swcooke; 06-27-2018, 10:05 AM.

      Comment


        #4
        Hello swcooke,

        Thanks for the follow-up.

        I fixed up the initial script. Mine is updating properly. See attached.

        See this page for instructions on importing:


        Unfortunately, there is not a way to copy compile messages from the editor. There exists a feature request to add this in a future update. I have added a vote to that feature for you.

        Please let me know if this does not resolve the issue.
        Attached Files
        Last edited by NinjaTrader_ChrisL; 06-27-2018, 10:57 AM. Reason: Fixed link.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Looks like you handed me back the original script I sent. I don't see any difference. Can you confirm what you changed? Anyway, using the original script, I am back to the original issue which is the last several bars stop painting or paint inaccurately. Then I get this error:
          Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


          Can you take a quick look at my original question?

          Comment


            #6
            Hello swcooke,

            Thanks for the reply.

            If you could kindly post the script you are working on so I can reproduce the issue that would help. Were you able to run the script that I just posted? It is, in fact, a copy paste from your original post. I am managing the SharpDX brushes in OnRenderTargetChanged. There might be an error in the way you are managing the SharpDX resources.

            https://ninjatrader.com/support/help...getchanged.htm - OnRenderTargetChanged

            I look forward to assisting further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Also, is there any way to copy the error messages that appear in the Log in Control Center or from the output of errors beneath the Editor? Would be nice to select that text or copy it somehow.


              Of course you can do this. Simply left click the message with your mouse so it is highlighted and then press CTRL C to copy it and CTRL V to paste it where you want it. Am I missing something here?

              Comment


                #8
                Chris,

                We are not getting anywhere here. I sent you the code in my first request. You copied and sent it back. You want me to copy and send again?

                Please just read my original request. Can you just run it and see for your self that it paints properly historically but not for bars as they develop? I am trying to troubleshoot why the bars that build in real time throw the error I provided to you and why they will not paint accurately.

                Comment


                  #9
                  My concern is the last OR which is CurrentBars[1] < 1. This seems to me that it is basically a condition that prevents the current bar from ever being drawn. Isn't this going to cause the last bar to never be drawn?
                  No. It is simply saying that if you have no bars or only 1 bar of your secondary series loaded then return.

                  Comment


                    #10
                    Hello swcooke,

                    Zeos is correct, you can select the compile message and copy it. The feature request is to add copy/paste options from the right-click menu.

                    The problem you say is occurring is the bars will not render in real time. The script that I just posted works. Have you tested this script? I see you are running a script with a different name. If you could post that full script I would be able to tell you what is wrong.

                    I look forward to your reply.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      In the example you gave, you are using:
                      Code:
                      AddDataSeries(BarsPeriodType.Minute, 1);
                      I am using:
                      Code:
                      AddDataSeries("XLK",BarsPeriodType.Minute, 1);
                      Doesn't yours add an additional series of the primary symbol while mine adds XLK? I have attached my source file. I think the issue is, I want to interate using the primary bars object and if that advances to the next bar before the additional bars object, I get the error. I need to be able to check if the XLK bars object exists before using the idx number from SPY. Let me know if you see the same thing. Let this run and it will error out.
                      Attached Files

                      Comment


                        #12
                        You need to check BarsInProgress...
                        Code:
                        protected override void OnBarUpdate() 
                        { 
                             // Check which Bars object is calling the OnBarUpdate() method 
                             if (BarsInProgress == 0) 
                             { 
                                  // A value of zero represents the primary Bars which is for example an ES 09-14 
                                  // 1 minute chart. 
                                  // Do something within the context of the 1 minute Bars here 
                             } 
                             else if (BarsInProgress == 1) 
                             { 
                                  // A value of 1 represents the secondary 5 minute bars added in OnStateChange() State.Configure 
                                  // Do something within the context of the 5 minute Bars, for example.
                             } 
                        }
                        I think you may want to revisit the structure and logic of your code to resolve this cleanly.

                        Comment


                          #13
                          Hello swcooke,

                          Thanks for your patience.

                          That is correct, the indexing is off because you are looping through the main chartbars object. I was able to get it to work by adding the XLK onto my chart and modifying the
                          FOR loop in OnRender:

                          for (int idx = chartControl.BarsArray[1].FromIndex; idx <= chartControl.BarsArray[1].ToIndex; idx++)

                          But this requires the additional series to be added to the chart itself. There is not a way to get the "ChartBars" for a series that is not rendered onto the chart. What needs to be figured out is how to get the "ChartBars" for the additional data series.

                          Looking at the Heiken Ashi Indicator for NinjaTrader 8 could be helpful:


                          Also, using the ChartControl facilities such as GetSlotIndexByTime(), GetXByBarIndex(), and GetSlotIndexByX() will help.

                          https://ninjatrader.com/support/help...properties.htm - ChartControl

                          If you have any additional questions along the way please let me know.
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            What would be a way to check if BarsArray[1] is actually an object that can be accessed? If I knew that, I could just keep my loop using the primary bars object and just put a conditional in there to return if the BarsArray[1][idx] is not an object yet for timing reasons. I tried this but it did not work:

                            Code:
                             for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++){
                              if(BarsArray[1][idx] == null){
                                return;
                              }
                              //do the rest
                            
                            }
                            I think I am on the right track but its not working. Any ideas would really be appreciated.

                            Comment


                              #15
                              I am looking at the reply from Zeos6 and wondering, why do I need an OnBarUpdate block at all if I am only custom rendering? Maybe I am missing something but this seems like it should work.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by TraderBCL, Today, 04:38 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post TraderBCL  
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              105 views
                              0 likes
                              Last Post martin70  
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              606 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by KenneGaray, Today, 03:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post KenneGaray  
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              471 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Working...
                              X