Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is there a built-in method to get the net % change of an instrument?

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

    Is there a built-in method to get the net % change of an instrument?

    Hello,

    I'm looking for a way to get the current net % change of an instrument using Ninjascript. Similar to how you would see it in the Market Analyzer window, see attached pic.

    I suppose I can get the previous days close, then get the current price, then do the calculation. But is there a quicker way to do this?

    Thanks!

    Click image for larger version

Name:	image.png
Views:	78
Size:	16.3 KB
ID:	1311057

    #2
    Hello devatechnologies,

    Have a look at the 'Net change display' indicator included with NinjaTrader and add this to a chart.

    This may give you some ideas for creating your own custom script.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Thanks for the recommendation. I was able to find the Net Change Display indicator and integrate it into my strategy. When I enable it I do see it on the chart. However, when I try and access or print the values it just shows -.01% on each update.

      Any suggestions?

      Thank you!

      Output window:
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%
      Net Change Percentage: -0.01%​

      Here is the code I'm testing with.
      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.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class Test : Strategy
          {    
              private NetChangeDisplay netChangeDisplay;
              
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Strategy here.";
                      Name                                        = "Test";
                      Calculate                                    = Calculate.OnEachTick;
                      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;
                  }
                  else if (State == State.DataLoaded)
                  {
                  // Initialize your indicator here
                  netChangeDisplay = NetChangeDisplay(PerformanceUnit.Percent, NetChangePosition.BottomLeft);
                  AddChartIndicator(netChangeDisplay);
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBars[0] < 1)
                  return;
      
                  // Calculate net change percentage using the indicator
                  double netChangePercentage = netChangeDisplay.NetChange;
          
                  // Print or use the net change percentage in your strategy logic
                  Print("Net Change Percentage: " + netChangePercentage.ToString("F2") + "%");
          
                  // Example strategy logic based on net change percentage
                  if (netChangePercentage > 0)
                  {
                      // Implement your strategy's action when net change is positive
                  }
                  else if (netChangePercentage < 0)
                  {
                      // Implement your strategy's action when net change is negative
                  }
                  else
                  {
                      // Implement your strategy's action when net change is zero
                  }
              }
          }
      }
      
      ​

      Comment


        #4
        Hello devatechnologies,

        Since you are calling this from a host script and the data is not in a series, you will need to call <Indicator>.Update() before returning any non-series variables.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          I read the reference page for Net Change Display and it will only display correctly on live data. I've had to figure out a simple solution that will work on historical data as well. The code is below, in case it helps anyone in the future.

          Note: I'm using five data series in my strategy. This checks data series [1], the S&P500 5min chart. Then gets the previous close from the S&P500 daily chart, index [5] for my strategy. Then it calculates the percentage difference and prints the results.


          [CODE]protected override void OnBarUpdate()
          {
          // Ensure we are working with the 5-minute S&P 500 data series (BarsInProgress 1)
          if (BarsInProgress == 1)
          {
          // Ensure there are enough bars in the daily data series
          if (CurrentBars[5] < 1) return;

          // Capture the current price from the 5-minute S&P 500 data series
          double currentPrice = Closes[1][0]; // Current close price in the 5-minute data series

          // Capture the previous day's close from the daily S&P 500 data series
          double previousDailyClose = Closes[5][0]; // Close of the previous daily bar

          // Print the daily close for verification
          Print("Previous daily close: " + previousDailyClose);
          Print("Current time: " + Time[0].ToString("HH:mm:ss dd-MM-yyyy"));
          Print("Current price: " + currentPrice);

          // Calculate the percentage change
          double percentageChange = ((currentPrice - previousDailyClose) / previousDailyClose) * 100;

          // Print the result for debugging
          Print("Percentage Change: " + percentageChange.ToString("F2") + "%");

          // Placeholder for your logic based on percentage change
          if (percentageChange > 0.90)
          {
          Print("The instrument is up by more than 0.90%");
          // Add your logic here
          }
          else if (percentageChange < -0.90)
          {
          Print("The instrument is down by more than 0.90%");
          // Add your logic here
          }
          }
          }
          ​/CODE]

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          20 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          119 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          63 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          41 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          45 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X