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 differentiate between Strategy "Is On Chart" vs."In Simulation (Invisible)"?

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

    How to differentiate between Strategy "Is On Chart" vs."In Simulation (Invisible)"?

    Hello,

    When inside of the OnBarUpdate() method of an NT8 Strategy, is there a way to determine if the Strategy is not being viewed in a chart, i.e. it is currently running in an invisible back-test or optimization, and is not being viewed in either a realtime chart or a chart in the back-test results section?

    I am asking because I want my back-tests and optimizations to run as fast as possible by running all the non-visual buy and sell logic, but not running all the drawing code that I am doing in OnBarUpdate(). Note that my logic can't go in the OnRender() section. Also, I want those visuals to appear even on back-test results charts. Also, even if the NT8 methods like Draw.Dot(...) don't draw when not in a chart, I am still doing lots of calculations before I call Draw.Dot(...) and those calculations have no purpose other than to figure out what dot to draw. It is these calculations that I don't want to run if the strategy is not on a chart.

    Thank you in advance,

    EquityTrader

    #2
    Hello,

    Thank you for the question.

    In general, checking if the ChartControl object is null or not should be sufficient to know if there is a Chart present. If the ChartControl is null, you can prevent the visual logic from occurring.

    if(ChartControl == null)

    I am unsure of any other property that would specifically let you know where the strategy is applied currently. It would only have the option to run as a Background item from the control center, Visually from a chart or in a Backtest/historical setting.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_Jesse,

      Thank you for the quick response.

      Unfortunately, ChartControl==null on the chart in the back-test results chart screen that shows all the trades.

      Is there any test I can apply that will return "true" if the strategy is in either the back-test results chart or the live charts?


      Thank you again,

      EquityTrader
      Last edited by EquityTrader; 06-16-2017, 05:19 PM.

      Comment


        #4
        Hi NinjaTrader_Jesse, or anybody who can help,

        I asked a question below over a day ago and haven't received an answer so thought I would bump this forum thread up.

        Does anyone know the answer to my question in my last thread?

        Thank you in advance,

        EquityTrader

        Comment


          #5
          EquityTrader,

          If I understand your question correctly, you hope to improve the speed of calculating your Strategy specifically by not running any of your drawing code.

          You may find post #9 & #10 in the thread How to best structure indicators which are computationally intensive instructive.
          From koganam:
          ... in NT7, drawing objects checked to be sure that they were being applied to a chart, or they did not run their code.
          NinjaTrader_PatrickH confirmed this approach applies in NT8.

          Shannon

          Comment


            #6
            Hello EquityTrader,

            Thank you for your patience.

            ChartControl will only return when loaded on a chart window. The Chart Display of the Strategy Analyzer does not trigger ChartControl as it is not the Chart window.

            There is not an option that would state whether the Chart Display of the Strategy Analyzer was being viewed for the strategy.

            Please let me know if you have any questions.

            Comment


              #7
              Thank you to NinjaTrader_PatrickH and Shansen for shedding light on this.

              In response to Shansen's post:

              From koganam:
              ... in NT7, drawing objects checked to be sure that they were being applied to a chart, or they did not run their code.

              NinjaTrader_PatrickH confirmed this approach applies in NT8.
              It is great that Draw.XYZ(...) methods only run their code it they are actually on a live or back-test results chart (i.e. not when they are running as part of a back-test or optimization in memory).

              I have my own methods, similar to Draw.XYZ(...) methods that I also only want to run when on an actual chart.

              All I need to know is...

              What "if" condition does Draw.XYZ(...) methods use to determine if they should execute their code?

              This is the perfect "if" condition that I want to use.

              Thank you for all your help on this,

              EquityTrader

              Comment


                #8
                Originally posted by EquityTrader View Post
                Thank you to NinjaTrader_PatrickH and Shansen for shedding light on this.

                In response to Shansen's post:



                It is great that Draw.XYZ(...) methods only run their code it they are actually on a live or back-test results chart (i.e. not when they are running as part of a back-test or optimization in memory).

                I have my own methods, similar to Draw.XYZ(...) methods that I also only want to run when on an actual chart.

                All I need to know is...

                What "if" condition does Draw.XYZ(...) methods use to determine if they should execute their code?

                This is the perfect "if" condition that I want to use.

                Thank you for all your help on this,

                EquityTrader
                That would be the aforementioned
                Code:
                if (ChartControl == null)
                {
                //We are not on a chart, so do not run any of the code in this block
                //...
                }

                Comment


                  #9
                  koganam,

                  Thanks for your post (and actually for lots of your posts... I am a fan of your answers.)

                  With all due respect, that is not true for the special case of the back-test results charts. This couldn't be what the NT8 platform is using as its check to not execute the code in the Draw.XYZ(...) methods, for the simple reason that Draw.XYZ(...) methods actually draw shapes on the back-test results charts that show the buy and sell signals for a back-test on an instrument, yet ChartControl == null on these charts, most unfortunately.

                  Check out my initial question in my initial post, with its initial font highlighting:

                  When inside of the OnBarUpdate() method of an NT8 Strategy, is there a way to determine if the Strategy is not being viewed in a chart, i.e. it is currently running in an invisible back-test or optimization, and is not being viewed in either a realtime chart or a chart in the back-test results section?
                  Do you or does anybody know the answer to my original question?

                  Thank you very much,

                  EquityTrader

                  Comment


                    #10
                    Originally posted by EquityTrader View Post
                    koganam,

                    Thanks for your post (and actually for lots of your posts... I am a fan of your answers.)

                    With all due respect, that is not true for the special case of the back-test results charts. This couldn't be what the NT8 platform is using as its check to not execute the code in the Draw.XYZ(...) methods, for the simple reason that Draw.XYZ(...) methods actually draw shapes on the back-test results charts that show the buy and sell signals for a back-test on an instrument, yet ChartControl == null on these charts, most unfortunately.

                    Check out my initial question in my initial post, with its initial font highlighting:



                    Do you or does anybody know the answer to my original question?

                    Thank you very much,

                    EquityTrader
                    As I said in the thread that @Shansen referenced, I have not yet really delved into the innards of NT8. The answer that I gave you really is for NT7. Maybe I should shut up, but there is one more possibility that you can examine. That would be to use the StrategyBase.Category enum.

                    ref: http://ninjatrader.com/support/forum...27&postcount=1

                    After this, I really will shut up. I just do not know enough of the innards of NT8 yet. I know just enough to be dangerous.

                    Comment


                      #11
                      koganam,

                      The code you linked to is excellent, thanks! I have bookmarked it and will find a use for it soon for something for sure.

                      Unfortunately for me, this script, while it does what it is supposed to do, it doesn't differentiate between a strategy that is running as part of a back-test running in memory vs. a strategy that is running on a chart that displays the trades from a back-test. This is the same problem that the test for "ChartControl == null" suffers from.

                      Attn: NinjaTrader development team:

                      I still really need to know the answer to my original question. Could somebody from NinjaTrader please answer it (copied below for your convenience):

                      When inside of the OnBarUpdate() method of an NT8 Strategy, is there a way to determine if the Strategy is not being viewed in a chart, i.e. it is currently running in an invisible back-test or optimization, and is not being viewed in either a realtime chart or a chart in the back-test results section?

                      The answer to this will be valuable to anybody that wants to be able to do the same thing on either a live chart or a back-test chart, but wants to do something different if the back-test is simply running in memory as part of the NT8 back-testing engine and therefore not displayed on a chart.

                      Thank you in advance for your time and help with this,

                      EquityTrader

                      Comment


                        #12
                        I know that I said that I would shut up, but now I am really intrigued as I am going to have to do this myself once I start on NT8 in earnest.

                        So, if there is no real secret sauce in your strategy code, I was wondering if you would be willing to post the actual file or code so that I can see exactly what you are doing? Maybe you could just remove the calculations that you are doing and instead use a remark so we can see where they would have been, but leave in the drawings? Not sure, but that may be a viable possibility?

                        Comment


                          #13
                          koganam,

                          My strategy is one big entangled mess at the moment, but what I can do is show you the relevant code and you will see the problem right away. I should have done this much earlier:

                          Code:
                          protected override void OnBarUpdate()
                          {
                          	if (CurrentBar < BarsRequiredToTrade)
                          		return;
                          
                          [B][COLOR="Red"]	// I wish the line below could be called only for back-test charts and live charts, not during back-tests 
                          	// that are running invisibly in memory, because it is a waste of computer resources.
                          	SolidColorBrush myCustomSolidBrush = ComputationallyExpensiveMethodToGetACustomBrush();[/COLOR][/B]
                          
                          	Draw.Dot(this, GetNewUniqueIdForThisDrawingObject(), true, 0, High[0], myCustomSolidBrush, false);
                          }
                          I have been told that NinjaTrader doesn't wastefully execute the internals of Draw.Dot(...) which is fantastic.

                          My problem is that my method to get the SolidColorBrush, "ComputationallyExpensiveMethodToGetACustomBrush() ", is called on every OnBarUpdate of every back-test and every optimization run in memory (i.e. when they aren't even being displayed on the back-test chart.

                          My goal is to only call "ComputationallyExpensiveMethodToGetACustomBrush() " if the strategy is being viewed in any kind of chart, including a back-test results chart.

                          Unfortunately for me, 1) "ChartControl == null", and 2) "Category == Category.Backtest" for strategies running in either a back-test chart or running in memory as part of a large back-test, so I am unable to differentiate these two situations, so "ComputationallyExpensiveMethodToGetACustomBrush() " either runs on every OnBarUpdate() or on none at all.

                          Attn: NinjaTrader development team:

                          I still really need to know the answer to my original question. Could somebody from NinjaTrader please answer it (copied below for your convenience):

                          When inside of the OnBarUpdate() method of an NT8 Strategy, is there a way to determine if the Strategy is not being viewed in a chart, i.e. it is currently running in an invisible back-test or optimization, and is not being viewed in either a realtime chart or a chart in the back-test results section?

                          The answer to this will be valuable to anybody that wants to be able to do the same thing on either a live chart or a back-test chart, but wants to do something different if the back-test is simply running in memory as part of the NT8 back-testing engine and therefore not displayed on a chart.

                          Thank you in advance for your time and help with this,

                          EquityTrader

                          Comment


                            #14
                            I think that I understood the execution environments in which you wanted "ComputationallyExpensiveMethodToGetACustomBrush() " code to run or not run. What I was actually asking is how you have written your location filter that you say is not selectively running your code. In other words, would you care to show the way you are using "if (CurrentBar == null) ..." around the "SolidColorBrush myCustomSolidBrush = ComputationallyExpensiveMethodToGetACustomBrush(); " and "Draw.Dot"?

                            Thanks.
                            Last edited by koganam; 06-21-2017, 07:04 AM.

                            Comment


                              #15
                              koganam,

                              I am not using any location filter, because unfortunately neither "if (CurrentBar == null) ..." nor "if (Category == Category.Backtest) ..." solve the problem described in my previous posts.

                              This is precisely what I am trying to get an answer for. That is, what location filter can I use, since the two above don't solve my problem.

                              EquityTrader

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              171 responses
                              2,273 views
                              0 likes
                              Last Post QuantKey_Bruce  
                              Started by Irukandji, Yesterday, 02:53 AM
                              2 responses
                              17 views
                              0 likes
                              Last Post Irukandji  
                              Started by adeelshahzad, Today, 03:54 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post adeelshahzad  
                              Started by CortexZenUSA, Today, 12:53 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Working...
                              X