Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How would I get this to work?

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

    How would I get this to work?

    I'm just trying to add a MACD Histogram to my strategy. I know I'm not doing it right, but this is just from the best of my memory of how it should be done.

    This is on a lower timeframe chart with 30 minute MACD added

    Strategy
    {
    private Series <double> signal;
    private Series <double> macdhistogram;


    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 30);
    }

    protected override void OnBarUpdate()
    {

    if (CurrentBar < BarsRequiredToTrade)
    return;

    if (BarsInProgress == 1)
    {

    signal[0] = EMA(MACD(BarsArray[1], 12, 26, 9), 9)[0];
    macdhistogram[0] = (MACD(BarsArray[1], 12, 26, 9)[0] - signal[0]);

    Print(string.Format("{0};{1};{2};{3};{4};{5};{6}", Time[0], Open[0], High[0], Low[0], Close[0], Position.MarketPosition, macdhistogram[0]));

    //Longafter
    if (((macdhistogram[0] - macdhistogram[1]) > 0) && ((ToTime(Time[0]) > 010000 && ToTime(Time[0]) < 080000)) || ((ToTime(Time[0]) > 170000 && ToTime(Time[0]) < 230000)) && Position.MarketPosition != MarketPosition.Long)
    {
    EnterLong("Longafter");
    }

    --------------

    But I keep getting an error of Error on calling 'OnBarUpdate' method on bar 314: Object reference not set to an instance of an object.


    #2
    Hello unpronounceable1700,

    In the code you provided the series macdhistogram and signal are never created so those would be null when trying to access them. If you are trying to visualize that data you need to remove the Series<double> variables hat you created and use AddPlot instead. https://ninjatrader.com/support/help...ghtsub=addplot

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello unpronounceable1700,

      In the code you provided the series macdhistogram and signal are never created so those would be null when trying to access them. If you are trying to visualize that data you need to remove the Series<double> variables hat you created and use AddPlot instead. https://ninjatrader.com/support/help...ghtsub=addplot
      Yeah I was actually just coming back to this post to say I solved it. Went back to an earlier post where someone commented that you need to add

      signal = new Series<double>(this);
      macdhistogram = new Series<double>(this);​

      to the OnStateChange

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      62 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X