Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Displaying Profit and Loss When Having Multiple Exit Types?

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

    Displaying Profit and Loss When Having Multiple Exit Types?

    I have a strategy that has the same entry for both trades but has different exits coded for each exit. I have my system set up to account for my wins, losses, and PnL. Is there an extra function where I could have Win, Loss, PnL for trade 1 and trade 2 exits?

    This could be very beneficial so I can run through market replay quicker while testing my strategies. If anyone is familiar with any code for this, it would be greatly appreciated. Thanks!

    #2
    Hello durdcash,

    Thank you for your post.

    It is possible to track the PnL for a specific exit by using a collection in the SystemPerfomance object. You could loop through the trades in the collection to find trades with a specific exit name, and then track the PnL in a variable based on the exit name.

    I created an example by modifying the SampleOnOrderUpdate reference sample. It adds the following variables:
    private int tradeCount;
    private double stopPnL;
    private double targetPnL;​

    In lines 105-121 of OnBarUpdate() I added the following:
    Code:
    // keeping track of tradeCount so that the following block of code will only be processed when a new trade is added to the collection
    if (SystemPerformance.AllTrades.Count > tradeCount)
    {
    tradeCount = SystemPerformance.AllTrades.Count;
    foreach (Trade myTrade in SystemPerformance.AllTrades)
    {
    if (myTrade.Exit.Name == "MyStop")
    {
    stopPnL += myTrade.ProfitCurrency;
    }
    if (myTrade.Exit.Name == "MyTarget")
    {
    targetPnL += myTrade.ProfitCurrency;
    }
    }
    Print(String.Format("The total number of trades is: {0} | stopPnL: {1} | targetPnL: {2}", tradeCount, stopPnL, targetPnL));
    }
    What this does is checks if the total count of trades in the SystemPerformance.AllTrades collection has increased, and if so it updates the tradeCount. It then loops through all trades and checks for the exit name to be either "MyStop" or "MyTarget." Based on the exit name, it adds the ProfitCurrency to either stopPnL or targetPnL and finally it prints the total tradeCount as well as the values for stopPnL and targetPnL.

    Here is the example that you may download:
    ModifiedOnOrderUpdatePnL_NT8.zip

    Please let me know if I may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    41 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    29 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    46 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    37 views
    0 likes
    Last Post CarlTrading  
    Working...
    X