Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't get backtesting to Print to output window

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

    Can't get backtesting to Print to output window

    I developed a small strategy for Scakping, and while backtesting on ES, I see it only enter some of the orders. I added Print commands.

    When compiling, I see the initials Print, but none of the OnBarUpdate ones (make sense), but when running the backtesting the output window stays clean I run the test over the last month only).

    Here is the code. Any help is blessed.

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// BDTtheTradeScalper
    /// </summary>
    [Description("BDTtheTradeScalper")]
    public class BDTtheTradeScalper : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int activeMin = 12; // Default setting for ActiveMin
    private string aTRprofitTicks = @"000002030303040404"; // Default setting for ATRprofitTicks
    private string aTRstopTicks = @"000005060607080809"; // Default setting for ATRstopTicks
    private string newsHours = @"0830100014001430"; // Default setting for NewsHours
    private int newsBrake = 2; // Default setting for NewsBrake
    private double minATR = 2.000; // Default setting for MinATR
    private double profitTicks = 3; // Default setting for ProfitTicks
    private double stopTicks = 6; // Default setting for StopTicks
    // User defined variables (add any user defined variables below)
    int LongCount = 0;
    int ShortCount = 0;
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    SetProfitTarget("", CalculationMode.Ticks, ProfitTicks);
    SetStopLoss("", CalculationMode.Ticks, StopTicks, false);

    CalculateOnBarClose = true;


    //ClearOutputWindow();
    TraceOrders = true;
    Print("Run " + DateTime.Now.ToString("dd/MM/yyyy") + "," + DateTime.Now.ToString("HH:mm:ss") + " | " +aTRprofitTicks);

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    Print(Time[0].ToString("dd/MM/yyyy") + "," + Time[0].ToString("HH:mm:ss") + "," + Open[0] + "," + High[0] + "," + Low[0] + "," + Close[0]);

    // Condition set 1
    if (BDTttsCurBarNum()[0] == 15)
    {
    LongCount = 0;
    ShortCount = 0;
    ExitLong("", "ScpLnog");
    ExitShort("", "ScpShort");
    }

    Print("---CurBarNum: " + BDTttsCurBarNum()[0]);
    Print(" LongCount: " + LongCount);
    Print(" ShortCount: " + ShortCount);

    // Condition to Scalp
    if (BDTttsCurBarNum()[0] <= ActiveMin
    && ATR(4)[0] >= MinATR * TickSize)
    {
    Print(" Condition Right");
    // First Condition Long per period
    if (LongCount == 0)
    {
    // R#1 - Crose above previous high
    if (CrossAbove(Close, BDTttsPerivousHigh(), 1))
    {
    Print(" R#1 Enter Long: " + BDTttsPerivousHigh());
    EnterLongLimit(0, true, DefaultQuantity, BDTttsPerivousHigh()[0], "ScpLong");
    LongCount++;
    }

    // R#2 - 2 Consecutive wicks higher over previous high
    if (Close[0] == Open[0]
    && High[0] > BDTttsPerivousHigh()[0]
    && Close[1] == Open[1]
    && High[1] > BDTttsPerivousHigh()[1])
    {
    Print(" R#2 Enter Long: " + Close[0]);
    EnterLongLimit(0, true, DefaultQuantity, Close[0], "ScpLong");
    LongCount++;
    }
    }
    // First Condition Short per period
    if (ShortCount == 0)
    {
    // R#1 - Crose below previous low
    if (CrossBelow(Close, BDTttsPerivousLow(), 1))
    {
    Print(" R#1 Enter Short: " + BDTttsPerivousLow()[0]);
    EnterShortLimit(0, true, DefaultQuantity, BDTttsPerivousLow()[0], "ScpShort");
    ShortCount++;
    }

    // R#2 - 2 Consecutive wicks higher over previous high
    if (Close[0] == Open[0]
    && Low[0] < BDTttsPerivousLow()[0]
    && Close[1] == Open[1]
    && Low[1] < BDTttsPerivousLow()[1])
    {
    Print(" R#2 Enter Short: " + Close[0]);
    EnterShortLimit(0, true, DefaultQuantity, Close[0], "ScpShort");
    ShortCount++;
    }
    }
    }
    }

    #region Properties
    [Description("String with Limit order Enter rules")]
    [GridCategory("Parameters")]
    public int ActiveMin
    {
    get { return activeMin; }
    set { activeMin = Math.Max(1, value); }
    }

    [Description("String with 2 digits Exit Profit rules")]
    [GridCategory("Parameters")]
    public string ATRprofitTicks
    {
    get { return aTRprofitTicks; }
    set { aTRprofitTicks = value; }
    }

    [Description("String with 2 digits Exit Limit rules")]
    [GridCategory("Parameters")]
    public string ATRstopTicks
    {
    get { return aTRstopTicks; }
    set { aTRstopTicks = value; }
    }

    [Description("String with 4 digits hours")]
    [GridCategory("Parameters")]
    public string NewsHours
    {
    get { return newsHours; }
    set { newsHours = value; }
    }

    [Description("Length of News Brake")]
    [GridCategory("Parameters")]
    public int NewsBrake
    {
    get { return newsBrake; }
    set { newsBrake = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double MinATR
    {
    get { return minATR; }
    set { minATR = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double ProfitTicks
    {
    get { return profitTicks; }
    ; }
    set { profitTicks = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double StopTicks
    {
    get { return stopTicks; }
    set { stopTicks = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Are you receiving any errors on the log tab of the Control Center related to the OnBarUpdate method?

    If I remove your custom code, and only use the following

    Code:
    protected override void OnBarUpdate()
    {
    Print(Time[0].ToString("dd/MM/yyyy") + "," + Time[0].ToString("HH:mm:ss") + "," + Open[0] + "," + High[0] + "," + Low[0] + "," + Close[0]);
    }
    I get print statements as we expect. Do you see the same behavior if you comment out everything else below?
    MatthewNinjaTrader Product Management

    Comment


      #3
      Originally posted by Shai Samuel View Post
      I developed a small strategy for Scakping, and while backtesting on ES, I see it only enter some of the orders. I added Print commands.

      When compiling, I see the initials Print, but none of the OnBarUpdate ones (make sense), but when running the backtesting the output window stays clean I run the test over the last month only).

      Here is the code. Any help is blessed.
      You cannot access High[1] or Close[1] until there are at least 2 bars on the chart. You want to use an escape condition:

      Code:
      if (CurrentBar < 1) return;
      before your code accessing the bars.

      Comment


        #4
        Thank you so much!

        Well, it turns out that I was checking another strategy (the wizard original), which was very close with the name. Yes, Print() does work with strategy backtesting.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        663 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        376 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        110 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X