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

How to iterate through total net profit, gross profit and gross loss on daily incr.

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

    How to iterate through total net profit, gross profit and gross loss on daily incr.

    Hi Ninja,

    I have a strategy running from 01-01-2017 till today.
    I would like to output the total net profit, gross profit and gross loss of the strategy on a daily basis (end of session) using the Print() function to the output window.
    This way I can follow the development of the strategy instead of only seeing the result as per today. Most notably I'm interested in the pressure on the margin of my account due to eod unrealized losing positions along the way.

    I have tried several methods in my print statement such as:
    - Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])
    - SystemPerformance.AllTrades.TradesPerformance.Gros sLoss

    ... but they do not show the correct eod data.
    This can be easily checked by doing something I wanted to avoid in the first place, namely iterating through the strategy analyzer manually and adding one day to the backtesting period, checking the outcome of the total net profit, gross profit and gross loss for the first day, for the first two days, for the first three days etc. until today and then entering all these data into Excel.
    Of course this would be too tedious and unrealistic for even longer back testing periods.

    Is there a way this can nonetheless be achieved?
    A simple sample line of code would be very much appreciated ;-)

    Regards,

    Vladja

    #2
    Hello Vladja,

    Thank you for the post.

    The Position object lets you access the unrealized PnL of an individual position. You could aggregate this value into a variable that holds the total, and print that out at the end of the session.

    You can use a SessionIterator object to keep track of the session begin/end time.

    More info on Position unrealized PnL:


    More info on session iterator:


    The simple way to do this, using SystemPerformance.AllTrades.TradesPerformance.Gros sLoss,
    would be to save this value at the beginning of the session (in IsFirstBarOfSession), and subtract it from the value at the end of the session.

    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for getting back ;-)

      I have spent a few hours with your suggested solutions, however cannot get it to work.
      The problem is, the SystemPerformance.AllTrades.TradesPerformance.Gros sLoss value you suggest always returns 0, beit at the beginning of the session or at the end.
      This may have to do with the fact that the strategy is a buy-and-hold strategy and never stops a position out, only takes profits.
      The strategy analyzer however is nonetheless "smart enough" to summarize open and therefore unrealized losing positions in its summary performance overview for the chosen period under the heading "Gross Loss".
      It somehow simulates the unrealized negative positions by faking a (losing) exit at the end of the session which indeed would show the unrealized drawdown on the account for that particular day.
      And this is exact;y what I'm looking for.

      I have also tried the other object Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) which does contain data,
      and substracted the begin session value from the end session value,
      but these numbers do not match with the numbers shown in the strategy analyzer summary performance page.

      By the way I'm currently using hourly candles so for the DAX there are 14 in a day, don't know if that is in anyway relevant.
      Obviously in the strategy I have the option "Exit on session close" switched off, otherwise I would still get stopped out of losing positions.

      What I simply want to do is print the three numbers Net Profit, Gross Profit and Gross Loss as shown in the strategy analyzer performance page for the particular backtesting period on a daily basis to the output window so I can then import them into Excel.

      I think it must be possible.
      Can you help further?

      Much obliged,
      Vladja

      Comment


        #4
        Hello Vladja,

        Thank you for the reply.

        To see this data, you will need to set TraceOrders and IncludeTradeHistoryInBacktest to 'True'

        TraceOrders will provide more information about each order being placed by your strategy and IncludeTradeHistoryInBacktest will save orders, trades, and execution history so they can be accessed from the output window during a backtest.



        Code:
        if(State == State.SetDefaults){
        ...
           TraceOrders = true;
            IncludeTradeHistoryInBacktest = true;
        }
        
        OnBarUpdate(){
        ...
        Print("Gross loss is: " + SystemPerformance.AllTrades.TradesPerformance.GrossLoss);
        Print("Net profit is: " + SystemPerformance.AllTrades.TradesPerformance.NetProfit);
        Print("Gross profit is: " + SystemPerformance.AllTrades.TradesPerformance.GrossProfit);
        ...
        Here are links to both TraceOrders and IncludeTradeHistoryInBacktest:



        Please let us know if we may be of any further assistance.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kaywai, 09-01-2023, 08:44 PM
        5 responses
        601 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by xiinteractive, 04-09-2024, 08:08 AM
        6 responses
        22 views
        0 likes
        Last Post xiinteractive  
        Started by Pattontje, Yesterday, 02:10 PM
        2 responses
        17 views
        0 likes
        Last Post Pattontje  
        Started by flybuzz, 04-21-2024, 04:07 PM
        17 responses
        230 views
        0 likes
        Last Post TradingLoss  
        Started by agclub, 04-21-2024, 08:57 PM
        3 responses
        17 views
        0 likes
        Last Post TradingLoss  
        Working...
        X