Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Strategy properties from indicator

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

    Accessing Strategy properties from indicator

    I have been able to access Strategy.Position, Strategy.Performance, and Strategy.BarsSinceEntry() from within my indicator when the indicator is called from Strategy.OnBarUpdate(). However, when this indicator is called by my strategy to plot the indicator on the chart (which apparently happens after Strategy.OnTerminate() is called), these properties and methods of Strategy are not available (although Strategy itself and a small subset of properties do still appear to be available).

    So, it appears that the indicator.OnBarUpdate() is being called twice, once to run the strategy and then again to draw the plot on the chart.

    My question is whether there is a way to gain full access to the Strategy object while the plot is being created. I can envision two different ways to accomplish this:
    1) Draw the plot from the instance of the indicator that was called during the execution of the strategy.
    2) Make the Strategy object persist until after the plots have been drawn.

    The reason that I want to do this is that I want to draw objects on the plot that are conditional on features like BarsSinceEntry() and MarketPosition so I can debug the operation of my strategy.

    Would you please suggest a way to accomplish my objective and let me know whether what I described is normal behavior or I have coded something incorrectly.

    Thanks,

    Mark




    #2
    Hello,

    Welcome to the NinjaTrader forums!

    Sharing this information between strategy and indicator is normally not available to do this and would take some unsupported methods to get this to work. Thus why you are running into issue doing this. It is best if you program the indicator and indicator plots directly in the strategy itself rather then trying to do it inside an indicator as there is no need to pass this to an indicator to do this.

    Please see this guide on how to add Strategy Plots which run in a strategy if you need them. Things like Text, Lines, Arrows, etc. will all work in a strategy just as it would an indicator.

    http://www.ninjatrader.com/support/f...ead.php?t=6651

    Let me know if I can be of further assistance.
    BrettNinjaTrader Product Management

    Comment


      #3
      Brett:

      Thanks for the quick response.

      I spent an enormous amount of time trying to do this from the strategy, which I agree would be the logical and easier way to approach it.

      However, I need to draw these objects in the indicator panel of the chart, rather than in the price panel. I have read many posts in which NT is adamant that in order to draw in a panel other than the price panel, you have to program it in the indicator. That is the reason that I shifted to that approach.

      Am I misunderstanding the situation?

      I agree with the many other users who have suggested that the simple solution to many problems would be to simply add a Panel parameter to each of the drawing functions so that you could just specify which panel to draw in when calling them from a strategy. Please add me as one more vote for that as an improvement.

      Comment


        #4
        You understand correctly.

        For your exact scenario you would need to do what your doing then.

        I will also submit your vote to this as well since we do keep track of this.

        Unfortunately in this case however you wouldn't have direct access to strategy state from the indicator.

        You would need to expose variables manually from the strategy that the indicator can then pick up and display.

        You would need to do this sample to do this which will work for strategies as well.

        http://www.ninjatrader.com/support/f...ead.php?t=4991

        Let me know if I can be of further assistance.
        BrettNinjaTrader Product Management

        Comment


          #5
          Are you wanting to use strategy plot in a secondary panel or drawingObjects?
          BrettNinjaTrader Product Management

          Comment


            #6
            Brett:

            Thanks for the suggestion. I tried it, but I had to change the code in order to move information from the strategy into the indicator (so that the drawing instructions conditional on the strategy can be drawn in the indicator panel of the chart). I was able to transfer information from the strategy to the indicator by calling the following in the strategy:

            SampleBoolSeries1().BullIndication.Set(true);

            This works (I can see the value of BullIndicator change in the debugger) when the indicator is called from OnBarUpdate() in the strategy. The indicator now includes the following code:

            if (bullIndication[1])
            {
            string strDrawingTag = "Bull " + CurrentBar.ToString();
            DrawArrowUp(strDrawingTag,
            true, 1, MACD(12, 26, 9).Avg[1] - TickSize, Color.Red);
            }

            Comment


              #7
              Brett:

              Thanks for the suggestion. I tried it, but I had to change the code in order to move information from the strategy into the indicator (so that the drawing instructions conditional on the strategy can be drawn in the indicator panel of the chart). I was able to transfer information from the strategy to the indicator by calling the following in the strategy:

              SampleBoolSeries1().BullIndication.Set(true);

              This works (I can see the value of BullIndicator change in the debugger) when the indicator is called from OnBarUpdate() in the strategy. The indicator now includes the following code:

              if (bullIndication[1])
              {
              string strDrawingTag = "Bull " + CurrentBar.ToString();
              DrawArrowUp(strDrawingTag,
              true, 1, MACD(12, 26, 9).Avg[1] - TickSize, Color.Red);
              }

              This statement executes (I can see it in the debugger) when the indicator is called from OnBarUpdate() in the strategy. However, there is no drawing on the indicator panel of the chart.

              The indicator gets called again after OnTerminate() and this seems to be when the indicator panel of the chart is drawn. However, after OnTerminate, bullIndication is never true again (I disabled it in the indicator so it is only set by the strategy), so DrawArrowUp never executes and nothing is drawn on the chart.

              It appears that two different instances of the indicator are created by NinjaTrader: one for use during execution of the strategy and a different instance for drawing the chart.

              The fundamental problem for me seems to be that by the time the instance that draws the chart is executed, the Strategy object has been partially destructed and it is not clear whether the first instance of the indicator still exists or how to access it.

              Is there a way to make the Strategy object persist until the chart is drawn? That would seem to be a simple (and very useful) solution to the problem.

              Alternatively, could the chart be drawn from the original indicator object? That would seem to make sense also.

              So I will try to restate my original question: Is there a way to draw in the indicator panel, conditional on information contained in the Strategy (such as MarketPosition)?

              I tried to attach the source files I modified from SampleBoolSeries. I am not sure whether that worked or not.

              Thanks again for your help.

              Comment


                #8
                Hello,

                Lets take a quick step back. I submitted your suggestion into development on your previous reply. They responded to me there is some unsupported NinjaScript that can be used to this inside the strategy.

                Please send me a note to support at ninjatrader dot com ATTN: Brett with this forum post references and I will send you the unsupported script from code that may be a better way for you to do this.

                I look forward to assisting you further.
                BrettNinjaTrader Product Management

                Comment


                  #9
                  Brett:

                  Thanks for the code you sent me. When I ran it none of the objects drawn from the strategy showed up in the chart. I eventually concluded that you ran the strategy from a Chart, whereas I have been running everything from the Strategy Analyzer. When I ran the code you sent (as well as the code I uploaded) from a Chart, everything worked as it should. Even my original problem (not being able to access the Strategy object from the indicator while the chart is being drawn) worked fine.

                  All of these problems seem to be consequences of the indicator being called a second time (after the strategy has finished and been destructed) in order to draw the panel for each indicator, when the strategy is called by Strategy Analyzer.

                  This seems to be a bug associated with the way the Strategy Analyzer draws charts. A simple fix might be to make the Strategy object persist until the chart has been drawn. If the structure of the program makes that unfeasible, an alternative simple fix would be to make a copy of the Strategy object (during OnTermination()) and store it in a calling object that will persist until the chart has been drawn. The OnStartUp() method of the indicator would then check whether Strategy == Null, and if it is, would redirect Strategy in the indicator to point to the copy of Strategy stored in the calling object (sorry, I am not yet familiar with the C# terminology for this). Either of these fixes should solve the problem I had that led to this thread. In essence, they should work as long as we stick with the approach that anything drawn in a panel other than the price panel needs to be drawn in the indicator.

                  I suppose the workaround for this until the bug is fixed is to call my strategies from Charts when I need to graphically debug a strategy. The frustrating part of this is that it seems very tedious to navigate to a trade that occurred six months ago in a 1 min Chart that is drawn from today backwards; I found that the workable solution in Strategy Analyzer was to set the date range to only the one or two days that I wanted to study for a specific trade. I will post a thread to the Charting forum to see whether there is a better way to navigate charts in NT. However, this workaround will always be a hassle because every time that I access Strategy within an indicator, I will have to test Strategy == Null and be prepared to skip some code in case the indicator is being run from Strategy Analyzer. Indicators are useless to me unless I can backtest and optimize them in strategies.

                  Thanks for your help.

                  Comment


                    #10
                    Originally posted by rmt81 View Post
                    ... it seems very tedious to navigate to a trade that occurred six months ago in a 1 min Chart that is drawn from today backwards; I found that the workable solution in Strategy Analyzer was to set the date range to only the one or two days that I wanted to study for a specific trade....

                    Thanks for your help.
                    You can do the same with a chart. Just set the range for the chart, in the same manner, to the time that you want to examine.
                    Last edited by koganam; 03-20-2011, 09:36 PM.

                    Comment


                      #11
                      Koganam:

                      Thanks for pointing that out to me. I had previously missed the option to set the ending date and number of days to load when requesting the chart in the first place.

                      Comment


                        #12
                        Thanks Koganam.
                        BrettNinjaTrader Product Management

                        Comment


                          #13
                          Originally posted by rmt81 View Post
                          Brett:

                          Thanks for the code you sent me. When I ran it none of the objects drawn from the strategy showed up in the chart. I eventually concluded that you ran the strategy from a Chart, whereas I have been running everything from the Strategy Analyzer. When I ran the code you sent (as well as the code I uploaded) from a Chart, everything worked as it should. Even my original problem (not being able to access the Strategy object from the indicator while the chart is being drawn) worked fine.

                          All of these problems seem to be consequences of the indicator being called a second time (after the strategy has finished and been destructed) in order to draw the panel for each indicator, when the strategy is called by Strategy Analyzer.

                          This seems to be a bug associated with the way the Strategy Analyzer draws charts. A simple fix might be to make the Strategy object persist until the chart has been drawn. If the structure of the program makes that unfeasible, an alternative simple fix would be to make a copy of the Strategy object (during OnTermination()) and store it in a calling object that will persist until the chart has been drawn. The OnStartUp() method of the indicator would then check whether Strategy == Null, and if it is, would redirect Strategy in the indicator to point to the copy of Strategy stored in the calling object (sorry, I am not yet familiar with the C# terminology for this). Either of these fixes should solve the problem I had that led to this thread. In essence, they should work as long as we stick with the approach that anything drawn in a panel other than the price panel needs to be drawn in the indicator.

                          I suppose the workaround for this until the bug is fixed is to call my strategies from Charts when I need to graphically debug a strategy. The frustrating part of this is that it seems very tedious to navigate to a trade that occurred six months ago in a 1 min Chart that is drawn from today backwards; I found that the workable solution in Strategy Analyzer was to set the date range to only the one or two days that I wanted to study for a specific trade. I will post a thread to the Charting forum to see whether there is a better way to navigate charts in NT. However, this workaround will always be a hassle because every time that I access Strategy within an indicator, I will have to test Strategy == Null and be prepared to skip some code in case the indicator is being run from Strategy Analyzer. Indicators are useless to me unless I can backtest and optimize them in strategies.

                          Thanks for your help.
                          Brett:

                          Koganam suggested how to restrict the date range of a chart when running the strategy from a chart.

                          Would you mind submitting this as a bug report?

                          Thanks.

                          Comment


                            #14
                            Hello,

                            The chart in the strategy analyzer is different as you can tell. I can forsee that this is this way to save resources when running exhaustive optimizer runs.

                            I will submit this into development. However since its unsupported NinjaScript I would count on it being added in a feature request and not a bug report.

                            I will repost with a tracking number for this shortly.

                            Thanks for your patience.
                            BrettNinjaTrader Product Management

                            Comment


                              #15
                              Hello,

                              So with your report, is this happening when you are using an indicator to get the strategy object information is this correct? Or are you doing everything in the strategy now and this is inside the strategy?

                              I look forward to assisting you further.
                              BrettNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by SmartArtsStudio, 05-15-2021, 10:46 AM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by minotti1, Today, 08:31 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post minotti1  
                              Started by Airwave, Today, 06:02 AM
                              2 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bottakemoney, Today, 12:10 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Started by JGRtrader, Yesterday, 10:54 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Working...
                              X