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

Winning Trades Count differs in live trading vs backtesting

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

    Winning Trades Count differs in live trading vs backtesting

    My NT8 strategy has a parameter named "MaxWinners" to stop after X number of winning trades per trading session/day.

    Also, the strategy uses "NumContracts" to set the default number of contracts to trade.

    Here is an example:

    Code:
    if (Bars.IsFirstBarOfSession)
    {
    PriorWinningTrades = SystemPerformance.AllTrades.WinningTrades.Count;
    }
    
    CurrentWins = SystemPerformance.AllTrades.WinningTrades.Count - PriorWinningTrades;
    
    // Stop trading if MaxWinners reached
                if (CurrentWins >= MaxWinners)
                {
                    Print(Time[0] + " Trading Stopped... Max Winners Reached: " + CurrentWins);
                    return;
                }​
    
    // Entry logic
                if (Close[0] < Close[1])
                        {
                            EnterShortLimit(NumContracts, Close[0], @"Short");
                        }​



    In backtesting and optimization, everything works as intended and can be verified in the NinjaScript Output window. For example, when NumContracts is set to "4" and MaxWinners is set to "2" and the strategy executes two separate winning trades of 4 contracts each, the strategy will not place any more trades, and outputs:

    "4/12/2023 2:29:10 PM Trading Stopped... Max Winners Reached: 2"

    However, in LIVE trading using the same parameters, one winning trade made with 4 contracts sets "CurrentWins" to 4, instead of 1 like in backtesting. Live excution interprets one 4 contract winning trade as 4 separate winning trades instead of a single winning trade, which is my intention. This messes up my strategy in live trading since it will stop trading after one winning trade since CurrentWins=4 is greater than MaxWinners=2. Here is the output in LIVE trading after one single winning trade of 4 contracts:

    "7/17/2023 11:35:30 AM Trading Stopped... Max Winners Reached: 4"

    What can I do to have "CurrentWins" behave the same in live trading and backtesting?


    EDIT: I have run this through Market Replay and discovered that sometimes a single trade of 4 contracts will result in a different number of trades, such as 2 contracts + 2 contracts, 3 contacts + 1 contract, etc.
    Last edited by fredb987; 04-17-2023, 01:41 PM.

    #2
    Hi fredb987, thanks for posting. Have you tried implementing intrabar granularity for better fill estimation in the backtest? We have a guide on what intrabar granularity is and how to use it in your script and an example here:

    https://ninjatrader.com/support/foru...-backtest-live


    Kind regards,

    -ChrisL​
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply, Chris. I can try what you're suggesting, but I'm not sure it will help. I've decided to go a different route that will achieve the same results using CumProfit instead.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by AaronKoRn, Yesterday, 09:49 PM
      0 responses
      11 views
      0 likes
      Last Post AaronKoRn  
      Started by carnitron, Yesterday, 08:42 PM
      0 responses
      10 views
      0 likes
      Last Post carnitron  
      Started by strategist007, Yesterday, 07:51 PM
      0 responses
      12 views
      0 likes
      Last Post strategist007  
      Started by StockTrader88, 03-06-2021, 08:58 AM
      44 responses
      3,982 views
      3 likes
      Last Post jhudas88  
      Started by rbeckmann05, Yesterday, 06:48 PM
      0 responses
      10 views
      0 likes
      Last Post rbeckmann05  
      Working...
      X