Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Volume Average

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

    Volume Average

    (Maybe someone could help changing the TS code below into Ninja, I am absolutely new to Ninja indicator coding)

    I'd like the "classical"
    Volume Average indicator ie. Volume histogram with simple moving average
    and
    the histogram bars in different colour depending on

    if High > High[1] and Low >= Low[1] then
    SetPlotColor( 1, UpColor )

    else if Low < Low[1] and High <= High[1] then
    SetPlotColor( 1, DownColor ) ;



    This is the complete Tradestation code:

    inputs:
    AvgLength( 10),
    AlertPct( 50),
    UpColor( darkmagenta),
    DownColor( darkcyan) ;

    variables:
    VVol( 0 ),
    AvgVVol( 0 ),
    TVol( 0 ),
    AvgTVol( 0 ),
    AlertFactor( 1 + AlertPct * .01 ),
    AlertStr( NumToStr( AlertPct, 2 ) ) ;

    if BarType >= 2 then { ie, not tick/minute data }
    begin
    VVol = Volume ;
    AvgVVol = AverageFC( Volume, AvgLength ) ;
    Plot1( VVol, "Vol" ) ;
    Plot2( AvgVVol, "VolAvg" ) ;
    { Alert criteria }
    if VVol crosses over AvgVVol * AlertFactor then
    Alert( "Volume breaking through " + AlertStr + "% above its avg" ) ;
    end
    else { if tick/minute data; in the case of minute data, also set the "For volume,
    use:" field in the Format Symbol dialog to Trade Vol or Tick Count, as desired }
    begin
    TVol = Ticks ;
    AvgTVol = AverageFC( Ticks, AvgLength ) ;
    Plot1( TVol, "Vol" ) ;
    Plot2( AvgTVol, "VolAvg" ) ;
    { Alert criteria }
    if TVol crosses over AvgTVol * AlertFactor then
    Alert( "Volume breaking through " + AlertStr + "% above its avg" ) ;
    end ;

    { Color criteria }
    if H > H[1] and L >= L[1] then
    SetPlotColor( 1, UpColor )
    else if L < L[1] and H <= H[1] then
    SetPlotColor( 1, DownColor ) ;




    #2
    Hello TJRemington,

    Thanks for your post.

    We can leave this thread open for anyone that would like to create the indicator for you.

    If you would like to create it yourself we can provide references to Ninjascript to help.

    If you are unable to create it or no one provides that service to you, we can provide a link to third party programmers in the NinjaTrader Ecosystem that can provide that service.

    To create on your own, I would recommend using the Ninjascript wizard as this will help create the basic indicator structure. What you will want to do is to know what the user input variables are and what you want for an output as the wizard will create the input variables and plot structures. Here is a link to the help guide on the Ninjascript wizard: https://ninjatrader.com/support/help...?ns_wizard.htm

    For working with price data (like you show here: if High > High[1] and Low >= Low[1] , Ninjascript is based on C# programming language so the syntax is governed by the programming language and you can find plenty of free online tutorials. That like would be written as if (High[0] > High[1] && Low[0] >= Low[1]) Here is a reference to working with price data in Ninjascript: https://ninjatrader.com/support/help...ice_series.htm

    For the line: SetPlotColor( 1, UpColor ) In NinjaTrader this is done with PlotBrushes, please see this reference example for coloring the plot with conditions: https://ninjatrader.com/support/help...lotbrushes.htm

    Creating variables and assigning default values as well as dynamic values is something you would want to review C# programming for.

    You may want to review previously created and free indicators in the Ninjatrader Ecosystem as what you may want could have already been created or perhaps something is close and can be modified to suit your needs. Here is a link to the NinjaTrader8 Indicators section of the NT User Apps section of the NinjaTrader Ecosystem: https://ninjatraderecosystem.com/use...r-8-indicators
    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; 04-12-2021, 06:20 AM. Reason: Added note about NinjaTrader Ecosystem

    Comment


      #3
      Hello, thanks for your help
      I am trying it with trial and error

      could you explain why the below code works with changing the color of Values[1](0)(depended on if (Close[0]>Open[0]) as a test)
      but when I reference back eg if (Close[0]>Open[1]) it makes the indicator not work anymore, ie nothing shown in chart at all
      Can't I back-reference in "protected override void OnBarUpdate()"?
      Thanks.






      protected override void OnBarUpdate()
      {
      Values[0][0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ?
      Core.Globals.ToCryptocurrencyVolume((long)sma[0]) : sma[0];

      Values[1][0] =Volume[0];
      if (Close[0]>Open[0])
      PlotBrushes[1][0]=Brushes.Blue;

      Comment


        #4
        Hello TJRemington,

        Thanks for your reply.

        Yes, you can access prior bars data.

        Whenever a script is not working as expected, a good first step would be to check the "Log" tab of the NinjaTrader control center for any related error messages. While a script may compile fine the compiler cannot test dynamic changes that occur while running so any run-time errors will cause the indicator/script to stop and any errors would be sent to the log tab.

        Based on what I see, I suspect the error will be of trying to access a bar that does not exist.

        Background: When a script is applied, it will run its code on every single bar of the data entire data series starting with the first bar. When you change the Open[0] to an Open[1], when the script starts on the first bar there is no prior bar[1] to access. You would need to prevent your script from access a previous bar until the script has processed as many bar as needed to allow that access.

        Using a simple example, if you wanted to access the price of 10 bars ago using Open[10] this means you would have to wait until there are 10 bars that have been processed. You can prevent the processing by checking what bar the system is processing. The system uses the variable CurrentBar as the bar number of the bar currently being processed. Using the [10] example you can prevent errors with:

        if (CurrentBar < 10) return; // return means that it will not execute the code below this line until the 10th bar.

        Reference: https://ninjatrader.com/support/help...currentbar.htm

        Comment


          #5
          Thank you. That was the problem!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          548 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          549 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X