Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to draw a line using the PlotStyle from a Plot?

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

    #61
    Based on RenkoTrader v1.6 Ninjatrader
    Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.


    lines 1204:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)​

    1220:

    DrawRows(chartControl, chartScale, backBrush, outBrush, textBrush, upBrush, dnBrush);​

    and 1257:

    private void DrawRows(ChartControl chartControl, ChartScale chartScale, SharpDX.Direct2D1.Brush backBrush, SharpDX.Direct2D1.Brush outlineBrush, SharpDX.Direct2D1.Brush textBrush, SharpDX.Direct2D1.Brush upBrush, SharpDX.Direct2D1.Brush dnBrush)


    SharpDX PlotOneBrush(chartControl, chartScale); Callable method in/from OnRender():

    Code:
            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                
                base.OnRender(chartControl, chartScale);
                
                SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
                RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive;
                
                PlotOneBrush(chartControl, chartScale);
                
                RenderTarget.AntialiasMode = oldAntialiasMode;
            }​

    Code:
            private void PlotOneBrush(ChartControl chartControl, ChartScale chartScale)
            {
                SharpDX.Direct2D1.Brush plot1BrushDx;
                plot1BrushDx = plot1Brush.ToDxBrush(RenderTarget);
                
                Vector2            point0            = new Vector2();
                Vector2            point1            = new Vector2();
                
                Vector2            point2            = new Vector2();
                Vector2            point3            = new Vector2();
    
                for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                {
                    
                    double    plotValue0 = 0, plotValue1 = 0;
                    float    valY0, xHeight0, x0, barWidth0, xWidth0,
                            valY1, xHeight1, x1, barWidth1, xWidth1,
                            outlineWidth;
                    
                    plotValue0        = MyPlot1.GetValueAt(barIndex);
                    valY0             = chartScale.GetYByValue(plotValue0);
                    
                    x0                = chartControl.GetXByBarIndex(ChartBars, barIndex);
                      barWidth0         = (float)chartControl.BarWidth;
                    
                    outlineWidth    = ChartBars.Properties.ChartStyle.Stroke2.Width; // 1.0f;
                    
                    point0.X         = x0 - barWidth0 - outlineWidth;
                    point0.Y         = valY0;
                    point1.X         = x0 + barWidth0 + outlineWidth;
                    point1.Y         = valY0;
                    
                    xHeight0        = 10.0f;
                    xWidth0            = (point1.X - point0.X);
                    
                    bool Cond0 =  ( plotValue1 != double.NaN && plotValue0 != double.NaN
                                    && ( plotValue1 != plotValue0 ) || (plotValue1 == plotValue0 ));
                    
                    if ( Cond0 )
                    {
                        RenderTarget.DrawLine(point0, point1, plot1BrushDx, 4);
                    }
                    
                    
                    plotValue1        = MyPlot1.GetValueAt(barIndex-1);
                    valY1             = chartScale.GetYByValue(plotValue1);
                    
                    x1                = chartControl.GetXByBarIndex(ChartBars, barIndex-1);
                    
                    point2.X         = x1 + barWidth0 - outlineWidth;
                    point2.Y         = valY1;
                    point3.X         = x0 - barWidth0 + outlineWidth;
                    point3.Y         = valY1;
                    
                    xHeight1        = 10.0f;
                    xWidth1            = (point3.X - point2.X);
                    
                    bool Cond1 =  ( plotValue1 != double.NaN && plotValue0 != double.NaN
                                    && plotValue1 == plotValue0 && !( plotValue1 != plotValue0 ));
                    
                    if ( Cond1 )
                    {
                        RenderTarget.DrawLine(point2, point3, plot1BrushDx, 4);
                    }
                }
            }​
    Last edited by PaulMohn; 08-20-2024, 03:05 AM.

    Comment


      #62

      Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.


      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      
      
      using SharpDX;
      using SharpDX.Direct2D1;
      #endregion
      
      // RenkoTrader v1.6 Ninjatrader: https://web.archive.org/web/20240820083931/https://pastebin.com/PDxtHiec
      // _OnRenderPlot v.1 : https://forum.ninjatrader.com/forum/ninjatrader-8/indicator-development/1075200-how-to-draw-a-line-using-the-plotstyle-from-a-plot?p=1314890#post1314890
      // _OnRenderPlot v.0 : https://forum.ninjatrader.com/forum/ninjatrader-8/indicator-development/1075200-how-to-draw-a-line-using-the-plotstyle-from-a-plot?p=1314597#post1314597
      // _OnRenderPlot v.1 : https://web.archive.org/web/20240820085505/https://pastebin.com/3My8NcAM
      
      //This namespace holds Indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class _OnRenderPlot : Indicator
          {
              private int cBar;
              private double cHigh;
              
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                 = @"Enter the description for your new custom Indicator here.";
                      Name                                        = "_OnRenderPlot";
                      Calculate                                   = Calculate.OnEachTick;
                      IsOverlay                                   = true;
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = true;
                      DrawHorizontalGridLines                     = true;
                      DrawVerticalGridLines                       = true;
                      PaintPriceMarkers                           = true;
                      ScaleJustification                          = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive                    = true;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
              
                      
                      ShowTransparentPlotsInDataBox = true;
                      
                      
                      plot1Brush = Brushes.ForestGreen;
                      
                       // Adds a Cyan Dash-Line style plot with 5pixel width and 3% opacity to hide the plot
                      AddPlot(new Stroke(Brushes.Transparent, DashStyleHelper.Dash, 5, 3), PlotStyle.Hash, "MyPlot1");
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
              
              #region REMOVE PARAMETERS LABELS FROM CHART
      
                  public override string DisplayName
                  {
                      get { return this.Name;}
                  }
      
              #endregion
      
              protected override void OnBarUpdate()
              {    
                  if (CurrentBar <21) return;
                  
                  if (Open[0] < Close[0])
                  {
                      cBar = CurrentBar;
                      cHigh = High[0];
                      MyPlot1[0] = cHigh;
                  }
                      
                  if ((CurrentBar - cBar) < 4)
                  {
                      MyPlot1[0] = cHigh;
                  }
                  
              }
              
              protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
              {
                  
                  base.OnRender(chartControl, chartScale);
                  
                  SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
                  RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive;
                  
                  PlotOneBrush(chartControl, chartScale);
                  
                  RenderTarget.AntialiasMode = oldAntialiasMode;
              }
              
              
              private void PlotOneBrush(ChartControl chartControl, ChartScale chartScale)
              {
                  SharpDX.Direct2D1.Brush plot1BrushDx;
                  plot1BrushDx = plot1Brush.ToDxBrush(RenderTarget);
                  
                  Vector2            point0            = new Vector2();
                  Vector2            point1            = new Vector2();
                  
                  Vector2            point2            = new Vector2();
                  Vector2            point3            = new Vector2();
      
                  for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                  {
                      
                      double    plotValue0 = 0, plotValue1 = 0;
                      float    valY0, xHeight0, x0, barWidth0, xWidth0,
                              valY1, xHeight1, x1, barWidth1, xWidth1,
                              outlineWidth;
                      
                      plotValue0        = MyPlot1.GetValueAt(barIndex);
                      valY0             = chartScale.GetYByValue(plotValue0);
                      
                      x0                = chartControl.GetXByBarIndex(ChartBars, barIndex);
                        barWidth0         = (float)chartControl.BarWidth;
                      
                      outlineWidth    = ChartBars.Properties.ChartStyle.Stroke2.Width; // 1.0f;
                      
                      point0.X         = x0 - barWidth0 - outlineWidth;
                      point0.Y         = valY0;
                      point1.X         = x0 + barWidth0 + outlineWidth;
                      point1.Y         = valY0;
                      
                      xHeight0        = 10.0f;
                      xWidth0            = (point1.X - point0.X);
                      
                      bool Cond0 =  ( plotValue1 != double.NaN && plotValue0 != double.NaN
                                      && ( plotValue1 != plotValue0 ) || (plotValue1 == plotValue0 ));
                      
                      if ( Cond0 )
                      {
                          RenderTarget.DrawLine(point0, point1, plot1BrushDx, 4);
                      }
                      
                      
                      plotValue1        = MyPlot1.GetValueAt(barIndex-1);
                      valY1             = chartScale.GetYByValue(plotValue1);
                      
                      x1                = chartControl.GetXByBarIndex(ChartBars, barIndex-1);
                      
                      point2.X         = x1 + barWidth0 - outlineWidth;
                      point2.Y         = valY1;
                      point3.X         = x0 - barWidth0 + outlineWidth;
                      point3.Y         = valY1;
                      
                      xHeight1        = 10.0f;
                      xWidth1            = (point3.X - point2.X);
                      
                      bool Cond1 =  ( plotValue1 != double.NaN && plotValue0 != double.NaN
                                      && plotValue1 == plotValue0 && !( plotValue1 != plotValue0 ));
                      
                      if ( Cond1 )
                      {
                          RenderTarget.DrawLine(point2, point3, plot1BrushDx, 4);
                      }
                  }
              }
              
              
              #region Properties
              
                  [Browsable(false)]
                  [XmlIgnore]
                  public Series<double> MyPlot1
                  {
                    get { return Values[0]; }
                  }
                  
                  
                  [NinjaScriptProperty]
                  [XmlIgnore]
                  [Display(Name="Plot Brush", Order=1, GroupName="Parameters")]
                  public System.Windows.Media.Brush plot1Brush
                  { get; set; }
      
                  [Browsable(false)]
                  public string plot1BrushSerializable
                  {
                      get { return Serialize.BrushToString(plot1Brush); }
                      set { plot1Brush = Serialize.StringToBrush(value); }
                  }
                  
              #endregion
          }
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
          {
              private _OnRenderPlot[] cache_OnRenderPlot;
              public _OnRenderPlot _OnRenderPlot(System.Windows.Media.Brush plot1Brush)
              {
                  return _OnRenderPlot(Input, plot1Brush);
              }
      
              public _OnRenderPlot _OnRenderPlot(ISeries<double> input, System.Windows.Media.Brush plot1Brush)
              {
                  if (cache_OnRenderPlot != null)
                      for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++)
                          if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].plot1Brush == plot1Brush && cache_OnRenderPlot[idx].EqualsInput(input))
                              return cache_OnRenderPlot[idx];
                  return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ plot1Brush = plot1Brush }, input, ref cache_OnRenderPlot);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
              public Indicators._OnRenderPlot _OnRenderPlot(System.Windows.Media.Brush plot1Brush)
              {
                  return indicator._OnRenderPlot(Input, plot1Brush);
              }
      
              public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , System.Windows.Media.Brush plot1Brush)
              {
                  return indicator._OnRenderPlot(input, plot1Brush);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
              public Indicators._OnRenderPlot _OnRenderPlot(System.Windows.Media.Brush plot1Brush)
              {
                  return indicator._OnRenderPlot(Input, plot1Brush);
              }
      
              public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , System.Windows.Media.Brush plot1Brush)
              {
                  return indicator._OnRenderPlot(input, plot1Brush);
              }
          }
      }
      
      #endregion
      
      ​

      ​​

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      601 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      347 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      103 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      559 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      558 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X