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

Understanding AddBar()

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

    Understanding AddBar()

    Hi

    can you help me with the syntax of AddBar()

    Suppose I want to develop an Indicator which is composed of 4 values, something like this.
    --

    x[0] = SMA(Close, 5)
    y[0] = SMA(Close, 4)

    Values[0][0] = (y[0] * 4 + Open[0])/5;
    Values[1][0] = (y[0] * 4 + High[0])/5;
    Values[2][0] = (y[0] * 4 + Low[0])/5;
    Values[3][0] = x[0];

    ----

    Now I want to plot a new Bar plot in a new panel, with Values[0] as the Open of the new plot, Values[1] as the High, Values[2] as the Low, Values[3] as the Close of the new bar plot.

    The syntax of AddBar from NT 8's language reference is:

    AddBar(Bars bars, double open, double high, double low, double close, DateTime time, long volume)

    1. What do I write in the State == State.Defaults section (within OnStateChange()) ?

    (a) do I define a blank panel in State == State.Defaults, and later add each new Bar in my OnBarUpdate() logic ?

    (b) How would I call AddBar() in the OnBarUpdate() logic ?

    I can write AddBar(Bar bars, Value[0], Value[1], Value[2], Value[3], DateTime time, 0) - but what should I replace for bars and time ?

    Thanks

    #2
    Hello uday12,
    Thanks for your post.

    Indicators can not make bars, so please clarify. Are you trying to make a BarsType which generates bars or use an indicator to calculate data from bars that were created?

    If you want to physically make a new bar, you should research bars types. If you want to make plots, and put those in multiple panels you would be looking at indicators and possibly a strategy.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh,

      I am not trying to make a bar, but want to draw the 4 Values lines as if it were a bar chart on a separate panel (not price overlay) below the regular price panel.

      Comment


        #4
        Are you simply trying to plot four lines that would be the OHLC values for a regular candle?

        I am not trying to make a bar,
        If that is the case why are you using AddBar()? AddBar() will only be used for creating a custom bar type.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Yes, Josh, that's correct. I simply have four lines which I would like to plot as if they were a OHLC values of a candle

          Comment


            #6
            Something similar to this snippet would plot 4 lines as the OHLC values of a candle. Does it achieve what you are looking for?
            Code:
            protected override void OnStateChange()
            {
            	if (State == State.SetDefaults)
            	{
            		AddPlot(Brushes.Blue, "OpenPlot");  		// Defines the plot for Values[0]
            		AddPlot(Brushes.Red, "HighPlot");   		// Defines the plot for Values[1]
            		AddPlot(Brushes.Green, "LowPlot");  		// Defines the plot for Values[2]
            		AddPlot(Brushes.Yellow, "ClosePlot");	        // Defines the plot for Values[3]
            	}
            
            }
            protected override void OnBarUpdate()
            {
            	Values[0][0] = Open[0];   
            	Values[1][0] = High[0];   
            	Values[2][0] = Low[0];    
            	Values[3][0] = Close[0];  
            }
            Josh G.NinjaTrader Customer Service

            Comment


              #7
              Hello, I do have same question. from a long time ago I'm using RSI indicator to show me not close line, but OHLC bars ( see pic ). I suppose uday12 want to do the same. My issue is when I try to load more bars ( to show more bars ) my indicator slowdown the platform cos it uses Objects, so I'm drawing only 200-500 bars. My question is can I use AddBar() to make the same indicator and avoid limitation and speed up the platform. Never had an issue with the speed, but it would be nice to improve it Thanks . here is part of my code :

              BarsLookBack = 250 // this is the limit of bars to show

              // 4 series for OHLC
              rOpen = new Series<double>(this);
              rHigh = new Series<double>(this);
              rLow = new Series<double>(this);
              rClose = new Series<double>(this);

              ropen = RSI(Open,14,1);
              rhigh = RSI(High,14,1);
              rlow = RSI(Low,14,1);
              rclose = RSI(Close,14,1);rOpen = new Series<double>(this);


              // drawing lines like Candles / Bars

              if (CurrentBar > Count-BarsLookBack)
              {

              if (rclose[0] > ropen[0] )
              Draw.Line(this, "Body"+CurrentBar, false, 0, ropen[0], 0, rclose[0], rColorUp, DashStyleHelper.Solid, BodyWidht);

              if (rclose[0] < ropen[0] )
              Draw.Line(this, "Body"+CurrentBar, false, 0, ropen[0], 0, rclose[0], rColorDn, DashStyleHelper.Solid, BodyWidht);

              Draw.Line(this, "Wick"+CurrentBar, false, 0, rhigh[0], 0, rlow[0],rWickColor , DashStyleHelper.Solid, WickWidht);

              }

              Click image for larger version

Name:	MYM 12-21 (15 Minute) 2021_09_10 (16_04_17)_Rsi_Bars.png
Views:	221
Size:	74.5 KB
ID:	1170697
              Attached Files

              Comment


                #8
                Hello Lyubomir Kratinov,

                Thanks for your post.

                AddBar is only used inside of a script that creates a bar type.

                You are drawing an excessive number of draw objects and that is or will lead to performance issues. If you want to continue on this route then I would suggest limiting the tag names to some value such as 100 (or however many bars you expect the chart to show at any one time). You can create a counter that adds its value to the tag name (instead of CurrentBar) and once the 100th has been drawn the counter is reset, on each object drawn thereafter the prior one that matches the name will be automatically removed. This will keep your performance load more of a constant regardless of the number of days loaded (by this I mean once all data has been loaded, the resource use would not increase).

                However, the best/recommended approach here would be to use OnRender() to draw the bars you wish. You can find an example of this in the NT uset Apps section of the Ninjatrader Ecosystem with the indicator Heiken-Ashi8. This indicator will first set transparent the standard bar candles and replace them with candles based on the Heiken-Ashi calculations. You may in fact be able to adapt this to your needs by replacing lines 71-74 with your RSI inputs. (and commenting out setting the standard bars transparent) You will also need to comment out lines 78-81 unless you want to apply Heiken-Ashi formula to them.


                This indicator is publicly available on our NinjaTrader Ecosystem website:
                Here is a basic guideline of how to import NinjaScript add-ons in NinjaTrader 8:

                Note — To import NinjaScripts you will need the original .zip file.

                To Import:
                1. Download the NinjaScripts to your desktop, keep them in the compressed .zip file.
                2. From the Control Center window select the menu Tools > Import > NinjaScript Add-on...
                3. Select the downloaded .zip file
                4. NinjaTrader will then confirm if the import has been successful.

                Critical - Specifically for some NinjaScripts, it will prompt that you are running newer versions of @SMA, @EMA, etc. and ask if you want to replace, press 'No'

                Once installed, you may add the indicator to a chart by:
                • Right-click your chart > Indicators... > Select the Indicator from the 'Available' list on the left > Add > OK

                Here is a short video demonstration of the import process:
                The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
                Last edited by NinjaTrader_PaulH; 09-10-2021, 09:37 AM. Reason: Added note about line 78-81
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you for this light fast reply! and thanks for recommendation to use HA indicator.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post algospoke  
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by quantismo, Today, 05:13 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post quantismo  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  8 responses
                  168 views
                  0 likes
                  Last Post jeronymite  
                  Started by cre8able, Today, 04:22 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X