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 Balage0922, Today, 07:38 AM
      0 responses
      1 view
      0 likes
      Last Post Balage0922  
      Started by JoMoon2024, Today, 06:56 AM
      0 responses
      6 views
      0 likes
      Last Post JoMoon2024  
      Started by Haiasi, 04-25-2024, 06:53 PM
      2 responses
      19 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      6 views
      0 likes
      Last Post Creamers  
      Started by Segwin, 05-07-2018, 02:15 PM
      12 responses
      1,786 views
      0 likes
      Last Post Leafcutter  
      Working...
      X