Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy analyzer delivers wrong results on strategy level

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

    Strategy analyzer delivers wrong results on strategy level

    Hi,
    I encounter wrong results in strategy analyzer.
    The results look at first glance as the same value for buy and sell, but in cum net profit on trade level they are different, the sell should be -3596.32
    Please provide a solution idea for this behaviour.

    Click image for larger version

Name:	image.png
Views:	287
Size:	13.2 KB
ID:	1342216
    Result buy in trades
    Click image for larger version

Name:	image.png
Views:	94
Size:	202.1 KB
ID:	1342218
    Result sell:
    Click image for larger version

Name:	image.png
Views:	92
Size:	207.0 KB
ID:	1342219
    Here the 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 SZATRTimeWindowStrategy : Strategy
    {
    private NinjaTrader.NinjaScript.Indicators.SZVWAP SZVWAP1;
    private ATR atr;
    private bool tradedToday = false;

    region Parameters
    [NinjaScriptProperty]
    [Range(0, 23)]
    [Display(Name = "Start Hour", Order = 1)]
    public int StartHour { get; set; }

    [NinjaScriptProperty]
    [Range(0, 59)]
    [Display(Name = "Start Minute", Order = 2)]
    public int StartMinute { get; set; }

    [NinjaScriptProperty]
    [Range(0, 23)]
    [Display(Name = "End Hour", Order = 3)]
    public int EndHour { get; set; }

    [NinjaScriptProperty]
    [Range(0, 59)]
    [Display(Name = "End Minute", Order = 4)]
    public int EndMinute { get; set; }

    [NinjaScriptProperty]
    [Range(0, 600)]
    [Display(Name = "Trade Minute Index (ab Startzeit)", Order = 5)]
    public int TradeMinuteIndex { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "ATR Periode", Order = 6)]
    public int ATRPeriod { get; set; }

    [NinjaScriptProperty]
    [Display(Name = "Trade Richtung", Order = 7)]
    public TradeDirection Direction { get; set; }
    #endregion

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Handel in bestimmter Minute mit ATR-basiertem SL/TP.";
    Name = "SZATRTimeWindowStrategy";
    Calculate = Calculate.OnBarClose;
    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;
    IsInstantiatedOnEachOptimizationIteration = true;

    StartHour = 9;
    StartMinute = 30;
    EndHour = 16;
    EndMinute = 0;
    TradeMinuteIndex = 0;
    ATRPeriod = 14;
    Direction = TradeDirection.Buy;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    }
    else if (State == State.DataLoaded)
    {
    SZVWAP1 = SZVWAP(9, 30);
    atr = ATR(ATRPeriod);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0 || CurrentBar < ATRPeriod)
    return;

    DateTime today = Times[0][0].Date;
    DateTime startTime = today.AddHours(StartHour).AddMinutes(StartMinute);
    DateTime endTime = today.AddHours(EndHour).AddMinutes(EndMinute);
    DateTime targetTradeTime = startTime.AddMinutes(TradeMinuteIndex);

    if (Bars.IsFirstBarOfSession)
    tradedToday = false;

    if (Times[0][0] < startTime || Times[0][0] > endTime)
    return;

    if (!tradedToday && Times[0][0] == targetTradeTime)
    {
    double atrValue = atr[0];
    double entryPrice = Close[0];

    if (Direction == TradeDirection.Buy && Close[0] >= SZVWAP1.PlotVWAP[0])
    {
    EnterLong("MinBuy");
    SetStopLoss("MinBuy", CalculationMode.Price, entryPrice - atrValue, false);
    SetProfitTarget("MinBuy", CalculationMode.Price, entryPrice + atrValue);
    }
    else if (Direction == TradeDirection.Sell && Close[0] <= SZVWAP1.PlotVWAP[0])
    {
    EnterShort("MinSell");
    SetStopLoss("MinSell", CalculationMode.Price, entryPrice + atrValue, false);
    SetProfitTarget("MinSell", CalculationMode.Price, entryPrice - atrValue);
    }

    tradedToday = true;
    }
    }

    public enum TradeDirection
    {
    Buy,
    Sell
    }
    }
    }

    Attached Files

    #2
    Hello moneymaster,

    The exit orders are going to fill at different prices as these are placed on different sides of the market, and the market will be moving a particular direction.

    The profit of each trade will be different and the number of trades will also likely be different as the entries are affected by when a position is open.

    Further, 1-tick intra-bar granularity is necessary for accurate fill prices in backtest.


    Last, Set methods cannot be unset. You must call SetStopLoss() and SetProfitTarget() before calling a new entry, not after.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      thanks for your answer. It would address a different problem than I have, respectively can you clarify how the change would affect the wrong sum in the result list?

      My process is the following:
      1. Doing a backtest
      2. Exporting result data
      3. Analyzing result data
      4. Taking measures and back to 1.

      Now Ninjatrader seems to calculate correctly, but the results stated and the results that I export are wrong.
      Probably because of an internal technical error, the result of one test is set in the result list as the result of another test.
      In my case there are two times the same results stated (systematically for multiple tests), but inside the strategy result, on trade level, the results are different then stated.
      Please check my screenshots.

      Thanks and best regards
      Last edited by moneymaster; 05-09-2025, 07:28 AM.

      Comment


        #4
        Hello moneymaster,

        It would not be expected the performance would be the same for the same logic placing long and short positions.

        1-tick intra-bar granularity would improve the order fill accuracy to be at the price and time of the order instead of the end of bar information.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Dear Chelsea,

          could You please confirm that it is an error on Ninja side and that somebody will take care of it?

          I have performed an optimization, which delivers on the top level a wrong total net profit compared to the cum. net profit on trade level. I described it in my first original post.

          It doesn't matter how I test, it never shall occur, that the total net profit on the top level is not equal to the cum. net profit of the trade results. Am I right?

          Thanks and best regards
          Szymon

          Comment


            #6
            Hello Szymon,

            "could You please confirm that it is an error on Ninja side and that somebody will take care of it?"

            I'm not sure of the error you are referring to. Can you further clarify?

            "It doesn't matter how I test, it never shall occur, that the total net profit on the top level is not equal to the cum. net profit of the trade results. Am I right?"

            The cumulative profit changes as each trade is made, but I would expect the very last trade cumulative profit to be equal to the net profit.

            In the screenshot you provided in post # 1, the last trade showing in the screenshot, trade 182, has cumulative profit of 17'866.24. Above, the strategy net profit is also showing 17'866.24. These are the same and are matching.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            61 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            39 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            21 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            23 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            51 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X