Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plot a condition below the chart with a strategy

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

    Plot a condition below the chart with a strategy

    Hello,

    I would like to add one or more horizontal lines to the chart of a strategy which is showing a condition based on colors like rising (green), neutral (yellow) and falling (red). The color should change bar by bar depending on the condition with a horizontal line on the bottom of the chart. The parameters of the condition are calculated by the strategy - not by an indicator.

    I already looked here in the forum, but I did not find anything that fits to this problem.

    Sincerely
    Gerik

    #2
    Hello Gerik,

    Thanks for your post.

    You could consider plotting from the strategy with AddPlot() and using IsOverlay=false.

    Or, you could consider adding an indicator to the strategy and updating the indicator plot from the strategy.

    See the reference sample in the help guide documentation below for sample code and more information regarding plotting from within a NinjaScript strategy.

    Plotting from within a NinjaScript Strategy: https://ninjatrader.com/support/help..._a_ninjasc.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Brandon,

      thank you for your answer. I tried to use AddPlot(), but this seems not to work. Maybe something is wrong with my syntax:

      Code:
      public class MyCustomStrategy1 : Strategy
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "MyCustomStrategy1";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      IsOverlay=false;
      AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.HLine, "Plot1");
      AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.HLine, "Plot2");
      AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.HLine, "Plot3");
      }
      else if (State == State.Configure)
      {
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (Close[1] > Close[0]) Plot1.Brushes.Red;
      if (Close[1] < Close[0]) Plot1.Brushes.Green;
      Plot1[0] = 1;
      Plot2[0] = 2;
      Plot3[0] = 3;
      }
      
      #region Properties
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> LongTermTrend
      {
      get { return Values[0]; }
      }
      #endregion
      What is wrong with the code? I used the NinjaScript Wizard and added some code out of the help are you sent me.

      Sincerely
      Gerik

      Comment


        #4
        Hello Gerik,

        Thanks for your note.

        Your public Series<double> name does not match the name of the plot. Instead of LongTermTrend, you would need to use the name Plot1.

        You would also need to add a public Series<double> for Plot2 and Plot3. The code might look something like this.

        Code:
        //In OnStateChange
        if (State == State.SetDefaults)
        {
        ...
        AddPlot(Brushes.Gold, "Plot1");
        AddPlot(Brushes.Fuchsia, "Plot2");
        AddPlot(Brushes.Aqua, "Plot3");
        }
        
        //Properties
        #region Properties
        
        [Browsable(false)]
        [XmlIgnore]
        public Series<double> Plot1
        {
        get { return Values[0]; }
        }
        
        [Browsable(false)]
        [XmlIgnore]
        public Series<double> Plot2
        {
        get { return Values[1]; }
        }
        
        [Browsable(false)]
        [XmlIgnore]
        public Series<double> Plot3
        {
        get { return Values[2]; }
        }
        #endregion
        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hello Brandon,

          thank you for your code correction. How can I change the color of a plot? I would like to change the color each bar depending on a condition.

          Code:
          public class MyCustomStrategy1 : Strategy
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "MyCustomStrategy1";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = false;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          IsOverlay=false;
          //AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.HLine, "Plot1");
          //AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.HLine, "Plot2");
          //AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.HLine, "Plot3");
          
          AddPlot(Brushes.Gold, "Plot1");
          AddPlot(Brushes.Fuchsia, "Plot2");
          AddPlot(Brushes.Aqua, "Plot3");
          }
          else if (State == State.Configure)
          {
          }
          }
          
          protected override void OnBarUpdate()
          {
          //if (Close[1] > Close[0]) Plot1.Brushes.Red;
          //if (Close[1] < Close[0]) Plot1.Brushes.Green;
          Plot1[0] = 1;
          Plot2[0] = 2;
          Plot3[0] = 3;
          }
          
          //Properties
          #region Properties
          
          [Browsable(false)]
          [XmlIgnore]
          public Series<double> Plot1
          {
          get { return Values[0]; }
          }
          
          [Browsable(false)]
          [XmlIgnore]
          public Series<double> Plot2
          {
          get { return Values[1]; }
          }
          
          [Browsable(false)]
          [XmlIgnore]
          public Series<double> Plot3
          {
          get { return Values[2]; }
          }
          #endregion

          Sincerely
          Gerik

          Comment


            #6
            Hello Gerik,

            Thanks for your note.

            PlotBrushes would be used to change the color of a plot when a certain condition becomes true.

            PlotBrushes[int PlotIndex][int barsAgo]

            See the help guide documentation below for more information and sample code.

            PlotBrushes: https://ninjatrader.com/support/help...lotbrushes.htm

            Let us know if we may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hello Brandon,

              thank you again for your answer. It works now, but unfortunately other graphical elements are drawn into the section of the plot and not in the chart. This happens with Draw.TextFixed as well as with rectangles. I want to have the other elements drawn on the chart as if there would be no plot.

              Code:
              public class MyCustomStrategy1 : Strategy
              {
              private Brush axisColor;
              private Gui.Tools.SimpleFont chartFont;
              
              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Strategy here.";
              Name = "MyCustomStrategy1";
              Calculate = Calculate.OnBarClose;
              EntriesPerDirection = 1;
              EntryHandling = EntryHandling.AllEntries;
              IsExitOnSessionCloseStrategy = true;
              ExitOnSessionCloseSeconds = 30;
              IsFillLimitOnTouch = false;
              MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
              OrderFillResolution = OrderFillResolution.Standard;
              Slippage = 0;
              StartBehavior = StartBehavior.WaitUntilFlat;
              TimeInForce = TimeInForce.Gtc;
              TraceOrders = false;
              RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
              StopTargetHandling = StopTargetHandling.PerEntryExecution;
              BarsRequiredToTrade = 20;
              // Disable this property for performance gains in Strategy Analyzer optimizations
              // See the Help Guide for additional information
              IsInstantiatedOnEachOptimizationIteration = true;
              IsOverlay=false;
              //AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.HLine, "Plot1");
              //AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.HLine, "Plot2");
              //AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.HLine, "Plot3");
              
              AddPlot(new Stroke(Brushes.Blue, DashStyleHelper.Solid, 25, 150), PlotStyle.Line, "Plot1");
              AddPlot(new Stroke(Brushes.Blue, DashStyleHelper.Solid, 25, 150), PlotStyle.Line, "Plot2");
              AddPlot(new Stroke(Brushes.Blue, DashStyleHelper.Solid, 25, 150), PlotStyle.Line, "Plot3");
              
              
              
              }
              else if (State == State.Configure)
              {
              }
              }
              
              protected override void OnBarUpdate()
              {
              //if (Close[1] > Close[0]) Plot1.Brushes.Red;
              //if (Close[1] < Close[0]) Plot1.Brushes.Green;
              Plot1[0] = 1;
              Plot2[0] = 2;
              Plot3[0] = 3;
              PlotBrushes[0][0] = Brushes.Blue;
              PlotBrushes[1][0] = Brushes.Red;
              PlotBrushes[2][0] = Brushes.Green;
              Draw.TextFixed (this, "testf", "\n\n\n\n\n\n\nTest:\t\t\t", TextPosition.TopRight, axisColor, chartFont, Brushes.Transparent, Brushes.Transparent, 0);
              Draw.TextFixed (this, "testf1","\n\n\n\n\n\n\n"+ "XXXX" , TextPosition.TopRight, Brushes.Green, chartFont , Brushes.Transparent, Brushes.Transparent,0 );
              
              
              }
              
              //Properties
              #region Properties
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Plot1
              {
              get { return Values[0]; }
              }
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Plot2
              {
              get { return Values[1]; }
              }
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Plot3
              {
              get { return Values[2]; }
              }
              #endregion
              How can I manage that only the plots are shown in the separate section below the chart, but all other drawings are shown in the chart above the plot?

              Sincerely
              Gerik
              Last edited by Gerik; 04-21-2022, 03:38 PM.

              Comment


                #8
                Hello Gerik,

                Thanks for your note.

                If you are drawing from a strategy and adding a panel from the strategy, you could change DrawOnPricePanel to draw in the indicator panel or price panel.

                See the attached example script demonstrating adding a plot to a strategy, having that plot placed in Panel 2 by setting IsOverlay to false, and drawing objects on the price panel by setting DrawOnPricePanel to true.

                See the help guide documentation below for more information.
                DrawOnPricePanel: https://ninjatrader.com/support/help...pricepanel.htm

                Let us know if we may assist further.
                Attached Files
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Hello Brandon,

                  thank you for your message and the sample. This works fine with the rectangles and with the text messages. Unfortunantely my buttons have now disappeared.

                  Do I need to do something special with the buttons to keep them?

                  Sincerely
                  Gerik

                  Comment


                    #10
                    Hello Gerik,

                    Thanks for your note.

                    I am unsure about what buttons you are referring to based on the previous code you shared.

                    That said, debugging steps would need to be taken to understand how a script is processing data and behaving based on the logic you wrote.

                    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                    In the script, add prints (outside of any conditions) that print the values of every variable used in every condition along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                    https://ninjatrader.com/support/foru...121#post791121

                    Please let me know if I may further assist
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Hello Brandon,

                      when I scroll with the arrow keys up and down in the chart, the plot below in the second window is also moving.

                      How can I avoid that?

                      Kind regards
                      Gerik

                      Comment


                        #12
                        Hello Gerik,

                        To confirm, this is a completely separate chart window and is not a second panel below the chart bars within the same chart window correct?

                        Do you have the global crosshairs enabled?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello Chelsea,

                          thank you for your message.

                          No, it is not a separate chart window. There is a second panel below the chart and within the same window. Global crosshairs is disabled. However, there is a horizontal line which separates both vertical axis. Both axis have a different scale. The panel below the chart is an oscillator. Therefore, while moving around the chart, the oscillator with the panel below should not move as the scale is between -1 and +1. The panel should be fixed.

                          Kind regards
                          Gerik

                          Comment


                            #14
                            Hello Gerik,

                            All panels on the same chart use the same time scale and all scroll together.

                            Use a separate chart if you want different time scales that scroll separately.

                            Panels cannot be fixed.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Chelsea,

                              of course they use the same time scale and scroll together to the left and to the right.

                              But once I scroll up and down, I do not want the second panel to scroll up/down, because the second panel is an oscillator. The oscillator will then disappear because it scrolls up/down in sync with the chart, which I want to avoid. The second panel shall be fixed on the y-axis.

                              Kind regards
                              Gerik

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by gyilaoliver, Today, 08:28 AM
                              7 responses
                              15 views
                              0 likes
                              Last Post gyilaoliver  
                              Started by Darkslide_Tom, 03-23-2025, 11:08 PM
                              3 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by rtwave, 03-13-2025, 04:09 PM
                              4 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by MiCe1999, 12-01-2024, 09:01 PM
                              4 responses
                              45 views
                              0 likes
                              Last Post Leeroy_Jenkins  
                              Started by coopgrafik, Today, 07:57 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X