Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtesting w/ PnL Strategy Halt

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

    Backtesting w/ PnL Strategy Halt

    Hello,

    I have to start by saying that I have no C# skills but have a decent ability to peice code together by looking at samples and getting a feel for how certain codes work. So with that being said, I have designed a strategy that works well for me but I would like to back test it and cannot effectively do it because I have a built-in strategy halt that kicks in when I have hit a certain profit or loss for the day.

    Here is a portion of my current code:
    // Checks to see if the day of the week is Monday or Friday. Only allows trading if the day of the week is not Saturday or Sunday.
    if (Time[0].DayOfWeek != DayOfWeek.Saturday && Time[0].DayOfWeek != DayOfWeek.Sunday)
    {
    // After strategy has a PnL greater than ($profit) or less than -($loss) we will stop our strategy
    if (Performance.AllTrades.TradesPerformance.Currency. CumProfit > profit
    || Performance.AllTrades.TradesPerformance.Currency.C umProfit < -loss)
    {return;}
    /* Checks to see if the time is during normal Market Trading hours. Time used is local time*/
    if (ToTime(Time[0]) >= st && ToTime(Time[0]) < et)
    {
    }

    Ok so if I want to backtest over several days it will not work because once it hits the PnL strategy halt, it obviously stops. I want it to backtest using the PnL, but have it stop just for the day it is testing and then move on to the next day until it hits the Profit or Loss, then on and on. So how do I code this to reset the PnL or CumProfit upon Midnight of each backtesting day or right before the beginning of a new session?

    Thanks,
    insomniac

    p.s. The strategy is also set to start at 7:30am(mst) and end at 2:10pm(mst) or once PnL is hit, whichever comes first.

    #2
    Maintain your own intraday variable instead of the aggregate Performance.AllTrades.TradesPerformance.Currency.C umProfit property. Reset it at the start of each day so it doesn't accumulate. That should allow you to test multiple days.

    You can determine the PnL through math calculations based on Performance.AllTrades.TradesPerformance.Currency.C umProfit. At the start of the day, save the value of Performance.AllTrades.TradesPerformance.Currency.C umProfit then as the day goes on take the new Performance.AllTrades.TradesPerformance.Currency.C umProfit and subtract it from the value you saved. That will give you the current day's CumProfit.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh! That is kind of what I thought but I do not know how to go about resetting the variable at the beginning of each day. Is there a pre-programmed function/code that will signal to the strategy that we are now at the first bar of a new day, or will I need to code a Time based function to reset to zero at the end of the day or at the beginning of a new day?

      Keep in mind that this is only to be used for backtesting purposes, but that it currently works fine with Sim or Live trading because the PnL automatically gets reset by the broker feed.

      Sorry for being so ignorant. Like I said, I am not a C# coder. I work off of piecing samples together and then modifying them to work for me.

      insomniac

      Comment


        #4
        ignorance is bliss! . . . try this:

        if (ToDay(Time[0] != ToDay(Time[1]))
        {
        // do something for the new day
        }

        This is one idea but there are others like if(FirstBarOfSession) that you can examine in the help.

        Regards,

        Whitmark
        whitmark
        NinjaTrader Ecosystem Vendor - Whitmark Development

        Comment


          #5
          I'm in the same boat... hope it works

          Comment

          Latest Posts

          Collapse

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