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

Strategy driven indicator plot

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

    Strategy driven indicator plot

    I have a strategy(myStrategy) which passes a variable(=realized PnL) to a Plot in an indicator(myIndicator).

    Purpose of this is to visualize the strategy's PnL in panel 1 of the chart.

    code in initialize section of indicator:
    Code:
     
    Add(new Plot(new Pen(Color.White, 2), "PnL"));
    code in strategy initialize()
    Code:
     
    myIndicator().Panel = 1; // indicator on panel 1
    Add(myIndicator());
    code in strategy onBarUpdate()
    Code:
     
    myIndicator().PnL.Set(Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit);
    the setup above seems to work fine, however I would like to access the plotted values from within the indicator to apply some logic so that they plot a green line above zero and a red line below zero.
    In the current setup, if I print the PnL value in the indicator, I get the current Price instead of the correct PnL value.

    I understand that a plot doesn't contain any actual values like a dataseries does, but is it possible to read these plotted values in the indicator ?
    What would be the correct procedure to do this ?

    Marco

    #2
    Hi marcow,

    You can access data from an indicator, by a strategy. But not the other way.

    You can create both plots from the indicator then access them from the strategy and set them to the colors you mentioned.
    TimNinjaTrader Customer Service

    Comment


      #3
      Tim,

      I understand that the normal route is that a strategy reads indicator data.
      But since the indicator is plotting the PnL-data from the strategy correctly, I guess it must have access to the PnL values of the strategy some way or the other ?

      Is it possible to put the values of plotted data of the indicator in a dataseries in that indicator ? I mean, if the indicator plots data, it has to know what values to plot, doesn't it ?

      Comment


        #4
        Hi marcow,

        If you plotting data from the indicator, say something like....

        Plot1.Set(Close[0] - Close[1]);

        you are creating a dataseries, which you can access the data with something like...

        double variable1 = Plot1[0];
        TimNinjaTrader Customer Service

        Comment


          #5
          the problem is that I'm plotting data from the strategy; the strategy passes data to the indicator, which in turn plots a graph in panel1. which works fine (please see my first post)

          this means the following code is in the strategy (PnL is a variable which stores realized Profit/Loss)
          Code:
           
          myIndicator().Plot1.Set(PnL)
          when I put
          Code:
          double variable1 = Plot1[0];
          in the indicator and print to output screen the value of variable1 I get the current price, and not the value of PnL from the strategy which it is correctly plotting at that very moment. Why is that ?

          Comment


            #6
            Hi marcow,

            Understood, you will need to set the calculations for, set the plot for, and access the values from the indicator. To see the correct values, this is the correct way of accessing the values, instead of uses the method where "the strategy passes data to the indicator"

            If you need to calculate the values from within the strategy, you can plot from within the strategy with the following method - http://www.ninjatrader.com/support/f...ead.php?t=6651
            Last edited by NinjaTrader_Tim; 05-10-2010, 02:05 PM.
            TimNinjaTrader Customer Service

            Comment


              #7
              thank you Tim,

              to isolate the issue, I stripped down the indicator and strategy from the link you provided me in the previous post.(SampleStrategyPlot)

              indicator StrategyPlot:

              Code:
               
              {
              [COLOR=dimgray]/// <summary>[/COLOR]
              [COLOR=dimgray]/// Empty placeholder indicator for creating plots in NinjaScript Strategies[/COLOR]
              [COLOR=dimgray]/// </summary>[/COLOR]
              [COLOR=dimgray][Description("Empty placeholder indicator for creating plots in NinjaScript Strategies")][/COLOR]
              [COLOR=dimgray]public class StrategyPlot : Indicator[/COLOR]
              [COLOR=dimgray]{[/COLOR]
              [COLOR=dimgray]#region Variables[/COLOR]
              [COLOR=dimgray]// Wizard generated variables[/COLOR]
              [COLOR=dimgray]private int id = 1; // Default setting for Id[/COLOR]
              [COLOR=dimgray]// User defined variables (add any user defined variables below)[/COLOR]
              [COLOR=dimgray]#endregion[/COLOR]
              [COLOR=dimgray]/// <summary>[/COLOR]
              [COLOR=dimgray]/// This method is used to configure the indicator and is called once before any bar data is loaded.[/COLOR]
              [COLOR=dimgray]/// </summary>[/COLOR]
              protected override void Initialize()
              {
              Add(new Plot(Color.Orange, PlotStyle.Line, "Strategy Plot"));
              CalculateOnBarClose = true;
              Overlay = false;
              PriceTypeSupported = false;
              }
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              [COLOR=red]// How to print the value of plots[0] ?[/COLOR]
              [COLOR=red]Print(Plot[0]);[/COLOR]
              }
              #region Properties
              [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
              [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
              public DataSeries Plot
              {
              get { return Values[0]; }
              }
              [Description("")]
              [Category("Parameters")]
              public int Id
              {
              get { return id; }
              set { id = Math.Max(0, value); }
              }
              #endregion
              }
              }

              strategy SampleStrategyPlot:

              Code:
               
              [COLOR=dimgray]{[/COLOR]
              [COLOR=dimgray]///<summary>[/COLOR]
              [COLOR=dimgray]/// Reference sample demonstrating how to plot from within a strategy[/COLOR]
              [COLOR=dimgray]///</summary>[/COLOR]
              [COLOR=dimgray][Description("Reference sample demonstrating how to plot from within a strategy")][/COLOR]
              [COLOR=dimgray]publicclass SampleStrategyPlot : Strategy[/COLOR]
              [COLOR=dimgray]{[/COLOR]
              [COLOR=dimgray]#region Variables[/COLOR]
              [COLOR=dimgray]#endregion[/COLOR]
              [COLOR=dimgray]///<summary>[/COLOR]
              [COLOR=dimgray]/// This method is used to configure the strategy and is called once before any strategy method is called.[/COLOR]
              [COLOR=dimgray]///</summary>[/COLOR]
              protectedoverridevoid Initialize()
              {
              CalculateOnBarClose = true;
               
              Add(StrategyPlot(0));
               
              StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
              StrategyPlot(0).PanelUI = 2;
               
              }
              ///<summary>
              /// Called on each bar update event (incoming tick)
              ///</summary>
              protectedoverridevoid OnBarUpdate()
              {
              [COLOR=red]StrategyPlot(0).Value.Set(High[0]*2);[/COLOR]
              }
              #region Properties
              #endregion
              }
              please take a look at the code of the indicator and strategy marked in red.
              When I print the values of Plot[0] in the indicator, I get the current/last price and not the plotted value (High[0]*2) from the strategy. Why is that ?
              How can an indicator plot a value correctly, but cannot print the same value to the output screen ?

              Of course my objective is not to print the value, but in the indicator to assign the plotted value to a variable for further processing.

              Comment


                #8
                marcow,

                To clarify, you cannot access the indicator values. You are setting it from the strategy and as such the indicator does not actually have access to it. This means any logic you want to apply needs to be done directly from the strategy prior to you setting the value to the indicator.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I found a little unconventional solution so I can do access strategy values in the indicator:
                  In the strategy I "encrypt" the values needed in the indicator in the Y coordinates of a dummy line, which then are passed to the indicator.

                  in the strategy
                  Code:
                   
                  myIndicator().Lines[0].Value = variable1
                  and in the indicator:
                  Code:
                   
                  variable1 = Lines[0].Value
                  this way variable1 will be passed to myIndicator() as the lines[0] Y-value.
                  myIndicator can now readout the Y-value of line[0] , which equals the variable from the strategy.

                  Now I can put the logic for drawregion/line colors in the indicator itself, which I think is where it should be.


                  thanks for the support !

                  Marco
                  Last edited by marcow; 05-11-2010, 11:55 AM.

                  Comment


                    #10
                    Hi , this looks awesome.... could you please tell me more about the implementation ... anything more than these two lines ?

                    Did you add anything as parameter (variable1?) ... I am trying to reproduce this but not seem to work so I am probably missing something .......


                    Thanks a lot !
                    Last edited by gabga100; 05-18-2010, 07:06 PM.

                    Comment


                      #11
                      Originally posted by marcow View Post
                      I found a little unconventional solution so I can do access strategy values in the indicator:
                      In the strategy I "encrypt" the values needed in the indicator in the Y coordinates of a dummy line, which then are passed to the indicator.

                      in the strategy
                      Code:
                       
                      myIndicator().Lines[0].Value = variable1
                      and in the indicator:
                      Code:
                       
                      variable1 = Lines[0].Value
                      this way variable1 will be passed to myIndicator() as the lines[0] Y-value.
                      myIndicator can now readout the Y-value of line[0] , which equals the variable from the strategy.

                      Now I can put the logic for drawregion/line colors in the indicator itself, which I think is where it should be.


                      thanks for the support !

                      Marco


                      Hey does this work ? could you give more details (the entire sample code) ??? thanks a lot

                      Comment


                        #12
                        hello Gabga100,

                        How does this work ?
                        The idea is to define a (transparent) line to be displayed in the indicator from within the strategy. The Y coordinates of the line, the " Lines[0].Value " bit hold the variable you want to pass to the indicator.
                        In the indicator you then readout the Y-coordinate of that same line, and there you have your variable from the strategy. That's it....


                        So in strategy:
                        Code:
                        myIndicator().Lines[0].Value = 99
                        and in the indicator:
                        Code:
                        variable1 = Lines[0].Value
                        variable1 will now equal 99.

                        Please take a look at this, it contains some crucial information on this topic, esspecially the sample zip at the bottom.
                        When running a strategy on a chart you may find the need to plot values onto a chart. If these values are internal strategy calculations that are difficult to migrate to an indicator, you can use the following technique to achieve a plot. NinjaTrader 8 With NinjaTrader 8 we introduced strategy plots which provide the ability


                        If it still doesn't work, please post your (simplified) strategy and indicator. I'll have a look.

                        Marco

                        Comment


                          #13
                          Hello,


                          Thank you for the sample... I already had seen the strategyplot implementation but your appraoch seemed more interesting as if you apss a value directly from indicator you ight eb able to see teh plot in the backtest chart ( were you able to acheive that ?)

                          If yes do you use any Value.Set command or particular parameter in the indicator code ?


                          Thanks a lot !

                          Comment


                            #14
                            and in the indicator:
                            Code:
                            variable1 = Lines[0].Value
                            variable1 will now equal 99.


                            Do you put that in the variables section or in onbarupdate section ?

                            Comment


                              #15
                              variable1 = Lines[0].Value is placed in the OnBarUpdate() section.

                              I used variable1 as an example, replace variable1 with any other variable in your indicator.

                              If you want a plot of the variable1, please use Value.Set(variable1);

                              Although I've not tested this with a plot ( I use the variable passed fom the strategy to drive some logic in the indicator which controls color settings of a "drawregion", used to fill a Profit/Loss graph green or red.)

                              The plot should be visible in backtesting, why not ?




                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              240 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              384 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Started by oviejo, Today, 12:28 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post oviejo
                              by oviejo
                               
                              Working...
                              X