Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnStateChange' method

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

    Error on calling 'OnStateChange' method

    Hi, all,

    I'm trying to get my head around things and build my first simple strategy with Strategy builder.
    Strategies from tutorials work, mine doesn't.
    I want to wait for a pullback with very simple rules before taking a trade.

    The rules:
    - If the previous candle opened and closed above it's own median and the current candle's low falls below that median, go long and set profit and stop loss.​​
    - If the previous candle opened and closed below it's own median and the current candle's high rises above that median go short and set profit and stop loss.​​

    It complies fine but when I try to use it, I get this error:
    Code:
    1/24/2025 9:59:57 AM,Default,Strategy 'SimplePullBack': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    Strategy builder's generated code:
    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 SimplePullBack : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Wait for a pullback";
                    Name                                        = "SimplePullBack";
                    Calculate                                    = Calculate.OnPriceChange;
                    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.Configure)
                {
                    SetProfitTarget(@"Long", CalculationMode.Price, (Median[1] + 4) );
                    SetStopLoss(@"Long", CalculationMode.Price, (Median[1] - 2) , false);
                    SetProfitTarget(@"Short", CalculationMode.Price, (Median[1] - 4) );
                    SetStopLoss(@"Short", CalculationMode.Price, (High[1] - 2) , false);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                 // Set 1
                if ((Open[1] > Median[1])
                     && (Close[1] > Median[1])
                     && (Low[0] < Median[1]))
                {
                    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
                }
                
                 // Set 2
                if ((Open[1] < Median[1])
                     && (Close[1] < Median[1])
                     && (High[0] > Median[1]))
                {
                    EnterLong(Convert.ToInt32(DefaultQuantity), @"Short");
                }
                
            }
        }
    }
    I'd also like to ask if I am using the right candle numbers to accomplish this.
    Thank you!

    #2
    Hello jiffy1111,

    Series cannot be used for Set stop loss or Set profit target in the strategy builder. You will need to use Exit methods in the conditions and actions.

    Below are links to examples.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I will go through those, thank you very much, Chelsea.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CaptainJack, 04-24-2026, 11:07 PM
      0 responses
      36 views
      0 likes
      Last Post CaptainJack  
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      130 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      185 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      95 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      139 views
      0 likes
      Last Post cmoran13  
      Working...
      X