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

Accessing P&L of last two trades

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

    Accessing P&L of last two trades

    Hello,
    As a part of my strategy I need to access P&L of the last two trades. The code is very simple and I am using it on $EURUSD.

    As you can see below, I am trying store P&L of my "Long1" and "Short1" entry signals to two different TradeCollection objects - myTrades and myTrades1. The challenge is when I see the Output window, I can see the strategy printing P&L for my "Long1" entrysignal but nothing for "Short1" entrysignal.

    I am backtesting the concept in Market Replay mode. Why can I see the P&L for Long1 signal but not for Short1. Any idea what could be wrong?

    if (Historical)
    return;

    if (CrossAbove(SMA(Fast), SMA(Slow), 1))
    {
    EnterLong(1000,"Long1");
    }

    if (CrossBelow(SMA(Fast), SMA(Slow), 1))
    {
    EnterShort(1000,"Short1");
    }

    TradeCollection myTrades = Performance.AllTrades.GetTrades("$EURUSD", "Long1", 1);
    TradeCollection myTrades1 = Performance.AllTrades.GetTrades("$EURUSD", "Short1", 1);

    Print("Profit for the last Long1 trade is: $"+myTrades.TradesPerformance.Currency.AvgProfi t);
    Print("Profit for the last Short1 trade is: $"+myTrades1.TradesPerformance.Currency.AvgProfit) ;

    #2
    Hello Pandyav,

    Thank you for writing in.

    This is a bug, thank you for reporting this. I have passed this on to development.

    What you could do to work around this would be to use the code below, and instead of using the print statement you’d have the trade added an array or trade collection. You could then reference the last value in the array/trade collection.


    Code:
    	
    TradeCollection myTrades1 = Performance.ShortTrades;
    
    foreach(Trade trade in myTrades1)
    {
    
    		if(trade.EntryExecution.Name == "Short1")
    		{
    			Print(trade.ToString());
    		}
    }
    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hello pandyav,

      To follow up on the issue, this bug has been resolved in NT8 and there are no immediate plans to resolve this in NT7,

      Thank you for your report.
      Alan P.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CortexZenUSA, Today, 12:46 AM
      0 responses
      0 views
      0 likes
      Last Post CortexZenUSA  
      Started by usazencortex, Today, 12:43 AM
      0 responses
      2 views
      0 likes
      Last Post usazencortex  
      Started by sidlercom80, 10-28-2023, 08:49 AM
      168 responses
      2,262 views
      0 likes
      Last Post sidlercom80  
      Started by Barry Milan, Yesterday, 10:35 PM
      3 responses
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by WeyldFalcon, 12-10-2020, 06:48 PM
      14 responses
      1,429 views
      0 likes
      Last Post Handclap0241  
      Working...
      X