Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Help Identifying Issue in Strategy Code

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

    Need Help Identifying Issue in Strategy Code

    What's the issue with the code in this strategy? I've compiled it without encountering any errors, but when I run the backtest, it doesn't produce any results. Can you help me figure out what's going wrong? Here's the strategy code...



    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    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

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class NSBreakoutLogicVer2 : Strategy
    {
    private double highestHighValue;
    private double lowestLowValue;

    private int highestHighPeriod = 2;
    private int lowestLowPeriod = 2;
    private int yTicks = 1; // Default value for Y ticks

    public int HighestHighPeriod
    {
    get { return highestHighPeriod; }
    set { highestHighPeriod = value; }
    }

    public int LowestLowPeriod
    {
    get { return lowestLowPeriod; }
    set { lowestLowPeriod = value; }
    }

    public int YTicks
    {
    get { return yTicks; }
    set { yTicks = value; }
    }

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Custom breakout strategy based on price action.";
    Name = "NSBreakoutLogicVer2";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = Cbi.TimeInForce.Gtc;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < HighestHighPeriod || CurrentBar < LowestLowPeriod)
    return;

    CalculateHighestHigh();
    CalculateLowestLow();

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Entry conditions for long positions
    if (Close[0] > highestHighValue + TickSize * YTicks)
    EnterLong();

    // Entry conditions for short positions
    if (Close[0] < lowestLowValue - TickSize * YTicks)
    EnterShort();
    }
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Exit conditions for long positions
    if (Close[0] < lowestLowValue - TickSize * YTicks)
    ExitLong();
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Exit conditions for short positions
    if (Close[0] > highestHighValue + TickSize * YTicks)
    ExitShort();
    }
    }

    private void CalculateHighestHigh()
    {
    highestHighValue = High[HighestBar(High, HighestHighPeriod)];
    }

    private void CalculateLowestLow()
    {
    lowestLowValue = Low[LowestBar(Low, LowestLowPeriod)];
    }
    }
    }


    #2
    What kinds of debugging have you tried?

    Have you ...
    added some Print statements, opened the
    output window, then rerun the backtest?

    The best way to learn where you went
    wrong is to figure it out yourself using
    some judiciously placed Print statements.

    Comment


      #3
      Hello sukhob,

      Thanks for your post.

      Do you receive an error on screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?

      Have you added debugging prints to understand how the logic in your script is behaving?

      Here is a NinjaTrader Support article detailing Debugging using Print() and TraceOrders: https://support.ninjatrader.com/s/ar...nd-TraceOrders

      See this NinjaTrader Support article also which details diagnosing a strategy with no results: https://support.ninjatrader.com/s/ar...ults-checklist

      Please let us know if we may assist further.

      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      66 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      141 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      76 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      47 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      51 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X