Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Optimization issue

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

    Optimization issue

    I have a strategy that I run successfully in playback mode, but when I run it through the strategy analyzer I get this message: "Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.". Any thoughts? thanks

    Here's my OnStateChange section:


    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MyNT8FractalLimit";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    EntriesPerDirection = 99999;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    TraceOrders = true;
    BarsRequiredToTrade = 10;
    IsInstantiatedOnEachOptimizationIteration = false; // Disable this property for performance gains in Strategy Analyzer optimizations
    IsUnmanaged = true;
    IsAutoScale = false;
    OrderFillResolution = OrderFillResolution.Standard;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;

    // // Disable this property for performance gains in Strategy Analyzer optimizations
    // IsInstantiatedOnEachOptimizationIteration = false;

    smallFont = new Gui.Tools.SimpleFont("Courier New", smallFontSize);
    mediumFont = new Gui.Tools.SimpleFont("Courier New", mediumFontSize);
    largeFont = new Gui.Tools.SimpleFont("Courier New", largeFontSize);
    AddPlot(new Stroke(Brushes.Blue, 2), PlotStyle.Line, "AlligatorJaw");
    AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Line, "AlligatorTeeth");
    AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.Line, "AlligatorLip");
    Fast = 5;
    Slow = 35;
    Smooth = 1;
    } //State == State.SetDefaults

    else if (State == State.Configure)
    {
    AL1_baseLine = new Series<double>(this, MaximumBarsLookBack.Infinite);
    if (outputTab == 1) PrintTo = PrintTo.OutputTab1; else PrintTo = PrintTo.OutputTab2;
    ClearOutputWindow();

    } //State == State.Configure

    else if (State==State.DataLoaded)
    {
    instName= Instrument.MasterInstrument.Name;
    sessionTxtwithSpacers = sessionTxt;
    DirectoryInfo dir = new DirectoryInfo(NinjaTrader.Core.Globals.InstallDir + "sounds");
    soundFileDir = dir.ToString()+"\";
    AL1_firstPasses = true;
    MyNT8ZeroLagMACD1 = MyNT8ZeroLagMACD(Close, 12, 26, 9);
    VOL1 = VOL(Close);
    ATR1 = ATR(Close, ATRperiod);


    shortTrades = shortButtonTrades;
    longTrades = longButtonTrades;
    smoothSma = new Series<double>(this);
    smaFast = SMA(Fast);
    smaSlow = SMA(Slow);
    smaSmooth = SMA(smoothSma, Smooth);
    entryStopPrice = new Series<double>(this);

    // parameter edits go here:

    }
    // ************************************************** ****
    } //State==State.DataLoaded

    else if(State==State.Historical)
    {
    AddButtonToToolbar();
    } //State==State.Historical

    else if(State==State.Terminated)
    {
    if(IsToolBarButtonAdded) DisposeCleanUp();
    } //State==State.Terminated

    else if (State == State.Realtime)
    {
    // one time only, as we transition from historical convert any old historical order object references to the new live order submitted to the real-time account
    if (longLimitOrder1 != null) longLimitOrder1 = GetRealtimeOrder(longLimitOrder1);
    if (longLimitOrder2 != null) longLimitOrder2 = GetRealtimeOrder(longLimitOrder2);
    if (longLimitOrder3 != null) longLimitOrder3 = GetRealtimeOrder(longLimitOrder3);
    if (shortLimitOrder1 != null) shortLimitOrder1 = GetRealtimeOrder(shortLimitOrder1);
    if (shortLimitOrder2 != null) shortLimitOrder2 = GetRealtimeOrder(shortLimitOrder2);
    if (shortLimitOrder3 != null) shortLimitOrder3 = GetRealtimeOrder(shortLimitOrder3);
    if (stopOrder1 != null) stopOrder1 = GetRealtimeOrder(stopOrder1);
    if (stopOrder2 != null) stopOrder2 = GetRealtimeOrder(stopOrder2);
    if (stopOrder3 != null) stopOrder3 = GetRealtimeOrder(stopOrder3);
    if (targetOrder1 != null) targetOrder1 = GetRealtimeOrder(targetOrder1);
    if (targetOrder2 != null) targetOrder2 = GetRealtimeOrder(targetOrder2);
    if (targetOrder3 != null) targetOrder3 = GetRealtimeOrder(targetOrder3);
    }//State==State.Realtime
    }


    #2
    Hello pman777,

    Thanks for your post.

    I don't see an obvious issue with the code presented. Testing the code with debugging prints added or using try-catches would need to be done to see where exactly the strategy is hitting the error.

    You do know that the issue is happening in OnStateChange(), so you could add try catches to each state to see which section the error is getting thrown. You can then add prints to each line to see where exactly the error occurs. It may also be worth checking if your Toolbar adding code references objects like ChartControl without a null check as ChartControl will be null in the Strategy Analyzer.

    Also as a tip, you can use the [CODE] blocks when posting your code so proper formatting is maintained and the posted code will be easier to read.

    Publicly available resources on debugging from our help guide are linked below.

    Debugging Tips - https://ninjatrader.com/support/helpGuides/nt8/en-us/debugging_your_ninjascript_cod.htm

    Using try-catches - https://ninjatrader.com/support/helpGuides/nt8/en-us/using_try-catch_blocks.htm

    Checking for null references (Object reference not set to an instance of an object) - https://ninjatrader.com/support/helpGuides/nt8/en-us/checking_for_null_references.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    57 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    19 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    9 views
    0 likes
    Last Post cre8able  
    Working...
    X