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

Working with plots

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

    Working with plots

    I have written a simple indicator works same as OBV, but with multicolor view and some addons
    I want to connect high/low pivots on OBV indicator together, if new high pivot is higher than previous high pivot it should connect by green line if lower connect by red line
    here is indicator code
    Code:
            private int prevSWH = -1;
            private int prevSWL = -1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"TheMavisV3.0";
                    Name                                        = "MavisNT";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = false;
                    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;
                    AddPlot(Brushes.Transparent, "OBV");
                    AddPlot(Brushes.Black, "OBV_UP");
                    AddPlot(Brushes.Magenta, "OBV_DN");
                    AddPlot(new Stroke(Brushes.Green, DashStyleHelper.Solid, 2), PlotStyle.TriangleDown, "SWH_UP");
                    AddPlot(Brushes.Red, "SWH_DN");
                    AddPlot(Brushes.Green, "SWL_UP");
                    AddPlot(Brushes.Red, "SWL_DN");
                    Plots[1].Width=2;
                    Plots[2].Width=2;
                }
                else if (State == State.Configure)
                {
                }
            }
    ​
            protected override void OnBarUpdate()
            {
                //Add your custom indicator logic here.
                double volume0 = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                if (CurrentBar == 0)
                {
                    OBV[0]=volume0;
                    OBV_UP[0]=volume0;
                    OBV_DN[0]=volume0;
                    SWH_UP[0]=0;
                    SWH_DN[0]=0;
                    SWL_UP[0]=0;
                    SWL_DN[0]=0;
                    prevSWH=-1;
                    prevSWL=-1;
                }
                else
                {
                    if ((High[0]-Open[0])>(Open[0]-Low[0])) OBV[0]=OBV[1]+volume0;
                    else if ((High[0]-Open[0])<(Open[0]-Low[0])) OBV[0]=OBV[1]-volume0;
                    else OBV[0]=OBV[1];
                    OBV_UP[0]=OBV[0];
                    OBV_DN[0]=OBV[0];
                    if (IsRising(OBV))
                    {
                        PlotBrushes[1][0]=Brushes.Black;
                        PlotBrushes[2][0]=Brushes.Transparent;
                    }
                    else if (IsFalling(OBV))
                    {
                        PlotBrushes[1][0]=Brushes.Transparent;
                        PlotBrushes[2][0]=Brushes.Magenta;
                    }
                    else
                    {
                        PlotBrushes[1][0]=PlotBrushes[1][1];
                        PlotBrushes[2][0]=PlotBrushes[2][1];
                    }
                    if (CurrentBar>3)
                    {
                        if (OBV[2]>OBV[3] && OBV[2]>OBV[1] && prevSWH!=CurrentBar-2)
                        {
                            if (prevSWH!=-1 && OBV[2]>=OBV[CurrentBar-prevSWH])
                            {
                                SWH_UP[2]=OBV[2];
                                SWH_UP[CurrentBar-prevSWH]=OBV[CurrentBar-prevSWH];
                            }
                            else if (prevSWH!=-1 && OBV[2]<=OBV[CurrentBar-prevSWH])
                            {
                                SWH_DN[2]=OBV[2];
                                SWH_DN[CurrentBar-prevSWH]=OBV[CurrentBar-prevSWH];
                            }
                            prevSWH = CurrentBar-2;
                        }
                    }
                }
            }
    ​
    normally I expect I can do it with line plot style, when I choose plot style as Dot, i can see green dots on pivots like image below, but when I choose plot style as line, no line is drawing, I noticed lines only draw when indicator values are on consecutive candles, but lines are not drawing at all when we have buffers filled with some candles between two pivot points

    Click image for larger version  Name:	image.png Views:	0 Size:	66.5 KB ID:	1297027

    image below show how indicator looks like when I choose Line style for that plot (there is no line drawn)
    Click image for larger version  Name:	image.png Views:	0 Size:	66.2 KB ID:	1297028
    this is part of coed that handles detecting pivots and filling related data series
    Code:
                    if (CurrentBar>3)
                    {
                        if (OBV[2]>OBV[3] && OBV[2]>OBV[1] && prevSWH!=CurrentBar-2)
                        {
                            if (prevSWH!=-1 && OBV[2]>=OBV[CurrentBar-prevSWH])
                            {
                                SWH_UP[2]=OBV[2];
                                SWH_UP[CurrentBar-prevSWH]=OBV[CurrentBar-prevSWH];
                            }
                            else if (prevSWH!=-1 && OBV[2]<=OBV[CurrentBar-prevSWH])
                            {
                                SWH_DN[2]=OBV[2];
                                SWH_DN[CurrentBar-prevSWH]=OBV[CurrentBar-prevSWH];
                            }
                            prevSWH = CurrentBar-2;
                        }
                    }
    ​
    Last edited by tidicofx; 03-25-2024, 04:30 AM.

    #2
    Hello tidicofx,

    This is correct, with plot style line a line will only be drawn between two segments if there are two segments that are consecutive.

    If not all bars are being set a value, to see the line you could choose set the value to the previous bars value to ensure every bar has a value.
    Below is a link to an example that does something similar.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I try to make it more simple
      suppose I have written an Indicator that marks pivots on indicator like image below
      Click image for larger version

Name:	image.png
Views:	29
Size:	45.1 KB
ID:	1297116
      now I need to add new plot that connect these dots together with line
      how this new plot can be? I have tried with line style but it does not work
      here is my code
      Code:
              private int prevSWH = -1;
              private int prevSWL = -1;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"TheMavisV3.0";
                      Name                                        = "MavisNT";
                      Calculate                                    = Calculate.OnEachTick;
                      IsOverlay                                    = false;
                      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;
                      AddPlot(Brushes.Transparent, "OBV");
                      AddPlot(Brushes.Black, "OBV_UP");
                      AddPlot(Brushes.Magenta, "OBV_DN");
                      AddPlot(new Stroke(Brushes.Green, DashStyleHelper.Solid, 4), PlotStyle.Dot, "SWH");
                      AddPlot(new Stroke(Brushes.Green, DashStyleHelper.Solid, 1), PlotStyle.Line, "SWH_Line");
                      AddPlot(Brushes.Green, "SWL_UP");
                      AddPlot(Brushes.Red, "SWL_DN");
                      Plots[1].Width=2;
                      Plots[2].Width=2;
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  //Add your custom indicator logic here.
                  double volume0 = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                  if (CurrentBar == 0)
                  {
                      OBV[0]=volume0;
                      OBV_UP[0]=volume0;
                      OBV_DN[0]=volume0;
                      SWH[0]=0;
                      SWH_Line[0]=0;
                      SWL_UP[0]=0;
                      SWL_DN[0]=0;
                      prevSWH=-1;
                      prevSWL=-1;
                  }
                  else
                  {
                      if ((High[0]-Open[0])>(Open[0]-Low[0])) OBV[0]=OBV[1]+volume0;
                      else if ((High[0]-Open[0])<(Open[0]-Low[0])) OBV[0]=OBV[1]-volume0;
                      else OBV[0]=OBV[1];
                      OBV_UP[0]=OBV[0];
                      OBV_DN[0]=OBV[0];
                      if (IsRising(OBV))
                      {
                          PlotBrushes[1][0]=Brushes.Black;
                          PlotBrushes[2][0]=Brushes.Transparent;
                      }
                      else if (IsFalling(OBV))
                      {
                          PlotBrushes[1][0]=Brushes.Transparent;
                          PlotBrushes[2][0]=Brushes.Magenta;
                      }
                      else
                      {
                          PlotBrushes[1][0]=PlotBrushes[1][1];
                          PlotBrushes[2][0]=PlotBrushes[2][1];
                      }
                      if (CurrentBar>3)
                      {
                          if (OBV[2]>OBV[3] && OBV[2]>OBV[1] && prevSWH!=CurrentBar-2)
                          {
                              SWH[2]=OBV[2];
                              SWH_Line[2]=OBV[2];
                              prevSWH = CurrentBar-2;
                          }
                      }
                  }
              }
      ​

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello tidicofx,

        This is correct, with plot style line a line will only be drawn between two segments if there are two segments that are consecutive.

        If not all bars are being set a value, to see the line you could choose set the value to the previous bars value to ensure every bar has a value.
        Below is a link to an example that does something similar.
        https://ninjatrader.com/support/foru...196#post820196
        sorry I missed your message
        let me study your link
        thanks

        Comment


          #5
          ok, I look at your link, it does not seem to be what I am going to do
          I believe this is what happening in zigzag indicator, as you see zigzag indicator has lines connecting different candles together with some candles between with no value
          I have studied zigzag indicator code, but I dont see any difference between that code and what I did
          what you suggested "to see the line you could choose set the value to the previous bars value to ensure every bar has a value" makes chart look like this that is different from what I need
          Click image for larger version

Name:	sample.png
Views:	23
Size:	51.1 KB
ID:	1297125
          look at green lines, i dont want it to look that way, I have drawn a straight red line myself, that is how I want to connect dots, same way zigzag is working

          Comment


            #6
            Hello tidicofx,

            Some value needs to be set on the bars where no value is being set.

            Setting to the previous bars value does cause a stair step.
            If you want a diagonal line you would need to calculate a value such as start price plus (start price - end price) / number of bars.

            Or you could custom render lines or a geometry path in OnRender().

            https://ninjatrader.com/support/helpGuides/nt8/sharpdx_direct2d1_rendertarget_drawgeometry.htm
            https://ninjatrader.com/support/helpGuides/nt8/sharpdx_direct2d1_rendertarget_drawline.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello tidicofx,

              Some value needs to be set on the bars where no value is being set.

              Setting to the previous bars value does cause a stair step.
              If you want a diagonal line you would need to calculate a value such as start price plus (start price - end price) / number of bars.

              Or you could custom render lines or a geometry path in OnRender().

              https://ninjatrader.com/support/helpGuides/nt8/sharpdx_direct2d1_rendertarget_drawgeometry.htm
              https://ninjatrader.com/support/helpGuides/nt8/sharpdx_direct2d1_rendertarget_drawline.htm
              thanks you for quick reply
              Ok, I got the restriction, anyway I think this is not good that this option is not in NT as default option (possibility to connect lines from different candles (not consecutive)), in other platforms it is a basic option
              as you suggested I think best solution is to calculate intermediate values from start and end candle values and fill values accordingly

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, Yesterday, 10:57 PM
              0 responses
              6 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by MacDad, 02-25-2024, 11:48 PM
              7 responses
              158 views
              0 likes
              Last Post loganjarosz123  
              Started by Belfortbucks, Yesterday, 09:29 PM
              0 responses
              7 views
              0 likes
              Last Post Belfortbucks  
              Started by zstheorist, Yesterday, 07:52 PM
              0 responses
              7 views
              0 likes
              Last Post zstheorist  
              Started by pmachiraju, 11-01-2023, 04:46 AM
              8 responses
              151 views
              0 likes
              Last Post rehmans
              by rehmans
               
              Working...
              X