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

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.

      Brandon H.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by nuobo, Today, 07:43 PM
      0 responses
      1 view
      0 likes
      Last Post nuobo
      by nuobo
       
      Started by ETFVoyageur, Today, 02:04 PM
      3 responses
      21 views
      0 likes
      Last Post ETFVoyageur  
      Started by cre8able, Today, 06:18 PM
      0 responses
      6 views
      0 likes
      Last Post cre8able  
      Started by ETFVoyageur, Today, 06:05 PM
      0 responses
      7 views
      0 likes
      Last Post ETFVoyageur  
      Started by TAJTrades, 04-28-2024, 09:46 AM
      2 responses
      18 views
      0 likes
      Last Post TAJTrades  
      Working...
      X