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

    #16
    Plotting



    Ok, actually I have tried what you say but no value was appearing so this is why I asked if you suceeded, I will post a sample code later for you to review......... thank you

    Comment


      #17
      I've modified the SampleStrategyPlot examples a bit to further explain the idea.
      The strategy below will plot variable1 in the indicator on panel1, also the indicator is able to access the value of variable2 (defined in the strategy) and perform some logic like change the color of a line if variable2 exceeds a certain threshold-value.

      Indeed this doesn't work on backtesting, but it does work on replay or live. Since I use this technique to plot a red/green filled realized profit/loss graph, I doesn't matter if it only works in live trading.

      Marco


      indicator:

      Code:
       
      namespace NinjaTrader.Indicator
      {
      ///<summary>
      /// Empty placeholder indicator for creating plots in NinjaScript Strategies
      ///</summary>
      [Description("Empty placeholder indicator for creating plots in NinjaScript Strategies")]
      publicclass StrategyPlot : Indicator
      {
      #region Variables
       
      privatedouble variable1 = 0; // Default setting for variable1
      privatedouble variable2 = 0; // Default setting for variable2
      privateint id = 0; // Default setting for Id
       
      #endregion
      ///<summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      ///</summary>
      protectedoverridevoid Initialize()
      {
      Add(new Plot(Color.Orange, PlotStyle.Line, "Strategy_Plot"));
      Add(new Line(Color.White,0, "dd")); // Stored in Lines[0] 
       
      CalculateOnBarClose = false;
      DrawOnPricePanel = false;
      }
      ///<summary>
      /// Called on each bar update event (incoming tick)
      ///</summary>
      protectedoverridevoid OnBarUpdate()
      {
      variable2 = Lines[0].Value; // readout variable2 as defined in the strategy.
      //Print(variable2); 
       
       
      }
      #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 Strategy_Plot
      {
      get { return Values[0]; }
      }
       
       
      [Description("")]
      [Category("Parameters")]
      publicint Id
      {
      get { return id; }
      set { id = Math.Max(0, value); }
      }
       
      #endregion
      }
      }
      and strategy:

      Code:
       
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Reference sample demonstrating how to plot from within a strategy
      /// </summary>
      [Description("Reference sample demonstrating how to plot from within a strategy")]
      public class SampleStrategyPlot : Strategy
      {
      #region Variables
       
      private double variable1 = 0; // Default setting for variable1
      private double variable2 = 0; // Default setting for variable2
       
      #endregion
      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      CalculateOnBarClose = false;
       
      Add(StrategyPlot(0) );
      StrategyPlot(0).Panel = 1;
      }
      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      variable1++; // increment with 1
      variable2++;
       
      StrategyPlot(0).Value.Set(variable1); // pass variable1 to the indicator Plot ( plot values are not accessible to indicator code )
      StrategyPlot(0).Lines[0].Value = variable2; // pass variable2 to the indicator Lines[0] Y-coordinates (this value IS accessible to indicator code for indicator logic 
      // like change color of line if variable exceeds 50)
       
      }
      #region Properties
      #endregion
      }
      }

      Comment


        #18
        Thank You .... understand much better now.... the question remaining is why it should not plot in backtesting as the value would be accessible within the indicator as for the other standard indicatorsss..

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cre8able, Today, 03:20 PM
        1 response
        9 views
        0 likes
        Last Post cre8able  
        Started by fiddich, Today, 05:25 PM
        0 responses
        3 views
        0 likes
        Last Post fiddich
        by fiddich
         
        Started by gemify, 11-11-2022, 11:52 AM
        6 responses
        804 views
        2 likes
        Last Post ultls
        by ultls
         
        Started by ScottWalsh, Today, 04:52 PM
        0 responses
        4 views
        0 likes
        Last Post ScottWalsh  
        Started by ScottWalsh, Today, 04:29 PM
        0 responses
        7 views
        0 likes
        Last Post ScottWalsh  
        Working...
        X