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

get the list of trades and their net profit and commissions for current trading day

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

    get the list of trades and their net profit and commissions for current trading day

    Hi,

    I am tracking my strategy PnL and its commissions for the current trading day. The idea is that once it hit above a certain threshold after a position is closed, then the strategy pause to take new trades for the current day. I saw that we have SystemPerformance.RealTimeTrades can help me calculate current trading PnL and commission. However, will SystemPerformance.RealTimeTrades work in Strategy Analyzer as strategy analyzer use historical data. Can you confirm that SystemPerformance.RealTimeTrades works the same way in Strategy Analyzer as when the strategy runs in realtime?

    Code:
    //calculate total netprofit and commissions
             if (SystemPerformance.RealTimeTrades.Count > 1)
               {
                    Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];
                    DailyRealizedPL = DailyRealizedPL + lastTrade.ProfitCurrency;
                    DailyCommission = DailyCommission + lastTrade.Commission;
                }
    
    
    if (DailyRealizedPL <= -1*Risk_per_trade*3 || DailyRealizedPL >= (Risk_per_trade*2*2 + DailyCommission) )
                    return;​ // stop looking for new entry.
    Will this check work in Strategy Analyzer as well? I don't think I can use SystemPerformance.AllTrades instead of RealTimeTrades because that includes historical trades run in historical state.

    One other question: is Trade object a Position? Say if I open a long position with 5 contracts, then sell 4 contracts and keep 1. Later I close the position after selling the last contract. The position is closed. Does Trade object give the net PnL of the closed position?
    Last edited by affilife; 02-26-2024, 02:21 PM.

    #2
    Hello affilife,

    To support backtests you would need to use the AllTrades collection so historical trades can be included. There is a sample in the help guide link below that goes over how to use all trades and also split the PnL to per session PnL so the strategy can start working again the following day.

    JesseNinjaTrader Customer Service

    Comment


      #3
      But if I use AllTrades, it will stop taking new positions if historical trades PnL during the current trading day meet the threshold condition at the time I enable the strategy

      Comment


        #4
        and Bars.IsFirstBarOfSession is for the new trading day

        Comment


          #5
          Hello affilife,

          If you start the strategy after the trading session then yes historical trades for that day would be considered in the current days PnL. If you start the strategy before the start of the session then it will place no trades and would only include the realtime trades for the session.
          JesseNinjaTrader Customer Service

          Comment


            #6
            In the sample strategy, I think it's done incorrectly. Should the following code run in State.Transition as well because that's where it transitions into realtime?
            Code:
            // Store the strategy's prior cumulated realized profit and number of trades
                            priorTradesCount = SystemPerformance.AllTrades.Count;
                            priorTradesCumProfit = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;​
            And I certainly do not want historical trades for that day to be considered in the current day PnL as I start the strategy in the middle of the trading session. I don't run my strategy 24/7
            Last edited by affilife; 02-26-2024, 02:54 PM.

            Comment


              #7
              Hello affilife,

              The way that strategy works is that it uses math inside OnBarUpdate to separate the total PnL from the sessions PnL. That logic is how you would allow a strategy to be backtestable based on performance information. That code needs to be executed from OnBarUpdate so it happens on the first bar of each new session.

              Later in the condition you subtract that value which was set on the first bar of the session to get the sessions pnl:

              SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit - priorTradesCumProfit

              Or read as

              Total - TotalFromYesterday = Difference which is the PnL for Today
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ChartTourist, Today, 08:22 AM
              0 responses
              4 views
              0 likes
              Last Post ChartTourist  
              Started by LiamTwine, Today, 08:10 AM
              0 responses
              2 views
              0 likes
              Last Post LiamTwine  
              Started by Balage0922, Today, 07:38 AM
              0 responses
              5 views
              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  
              Working...
              X