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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      55 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      132 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      73 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
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X