Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to code for if last trade was a win/ loss w count

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

    How to code for if last trade was a win/ loss w count

    Hi nt8 support,

    How do I code for if the last trade was a win or loss and can I get a count of the no of trades in a row that are wins/losses in a similar way?

    #2
    Hello Austiner87,

    Thanks for opening the thread.

    You can get the last trade from SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]

    The trade before that would be SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 2]

    From here you can reference Trade.ProfitCurrency to see if that trade was a win or loss. If the last three trade's ProfitCurrency are positive then the last three trades are winners.

    SystemPerformance - https://ninjatrader.com/support/help...erformance.htm

    TradeCollections - https://ninjatrader.com/support/help...collection.htm

    Trade objects - https://ninjatrader.com/support/help.../nt8/trade.htm

    One of our examples demonstrates halting a strategy after consecutive losses which may be useful for what you are trying to accomplish. I have linked it below.



    We look forward to assisting.

    Comment


      #3
      Hi, is it possible to get the PnL for Historical Trades or this is just for Real Time trades?

      Comment


        #4
        Hello omermirza,

        Live trades that are made from the strategy are recorded in the NinjaTrader database and can be reviewed in a Trade Performance window.

        Using Trade Performance - https://ninjatrader.com/support/help...erformance.htm

        Historical trades that are calculated when the strategy processes historical data can be reviewed from a historical strategy performance report. Programmatically, these historically calculated trades can be read from the SystemPerformance.AllTrades collection.

        Please let us know if you have any additional questions.

        Comment


          #5
          https://forum.ninjatrader.com/forum/...agement?t=4084
          /* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
          If you want to only count profits from real-time trades please use .RealtimeTrades. */​



          Code:
          Print(" ");
          Print("CurrentBars[0] " + CurrentBars[0]);
          Print("SystemPerformance.AllTrades.Count " + SystemPerformance.AllTrades.Count);
          
          if (SystemPerformance.AllTrades.Count > 0)
          {
          Print("SystemPerformance.AllTrades.Count2 " + SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]);
          }​

          CurrentBars[0] 26857
          SystemPerformance.AllTrades.Count 3486
          SystemPerformance.AllTrades.Count2 instrument='NQ 09-24' entryPrice=19241.5 exitPrice=19308.25 quantity=1 marketPosition=Short entryTime='11/09/2024 18:00:10' exitTime='12/09/2024 00:00:00' profitCurrency=-1339.18


          Code:
          Print(" ");
          Print(Times[0][0] + " CurrentBars[0] " + CurrentBars[0]);
          Print(Times[0][0] + " SystemPerformance.AllTrades.Count " + SystemPerformance.AllTrades.Count);
          
          if (SystemPerformance.AllTrades.Count > 0)
          {
          Print(Times[0][0] + " SystemPerformance.AllTrades.Count2 " + SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]);
          
          Trade oneTradeAgoPnL = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
          
          Print(Times[0][0] + " oneTradeAgoPnL " + oneTradeAgoPnL);
          
          }​
          12/09/2024 08:17:20 CurrentBars[0] 26914
          12/09/2024 08:17:20 SystemPerformance.AllTrades.Count 3433
          12/09/2024 08:17:20 SystemPerformance.AllTrades.Count2 instrument='NQ 09-24' entryPrice=19241.5 exitPrice=19308.25 quantity=1 marketPosition=Short entryTime='11/09/2024 18:00:10' exitTime='12/09/2024 00:00:00' profitCurrency=-1339.18
          12/09/2024 08:17:20 oneTradeAgoPnL instrument='NQ 09-24' entryPrice=19241.5 exitPrice=19308.25 quantity=1 marketPosition=Short entryTime='11/09/2024 18:00:10' exitTime='12/09/2024 00:00:00' profitCurrency=-1339.18


          Code:
          Print(" ");
          Print(Times[0][0] + " CurrentBars[0] " + CurrentBars[0]);
          Print(Times[0][0] + " SystemPerformance.AllTrades.Count / All Data " + SystemPerformance.AllTrades.Count);
          
          if (SystemPerformance.AllTrades.Count > 0)
          {
          Print(Times[0][0] + " SystemPerformance.AllTrades.Count2 " + SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]);
          
          Trade oneTradeAgoData = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
          
          Print(Times[0][0] + " oneTradeAgoData " + oneTradeAgoData);
          
          // Calculate the PnL for the last completed real-time/virtual trade
          double oneTradeAgoPnL = oneTradeAgoData.ProfitCurrency;
          
          Print(Times[0][0] + " oneTradeAgoPnL " + oneTradeAgoPnL);
          
          }​
          12/09/2024 08:21:20 CurrentBars[0] 26938
          12/09/2024 08:21:20 SystemPerformance.AllTrades.Count / All Data 3408
          12/09/2024 08:21:20 SystemPerformance.AllTrades.Count2 instrument='NQ 09-24' entryPrice=19241.5 exitPrice=19308.25 quantity=1 marketPosition=Short entryTime='11/09/2024 18:00:10' exitTime='12/09/2024 00:00:00' profitCurrency=-1339.18
          12/09/2024 08:21:20 oneTradeAgoData instrument='NQ 09-24' entryPrice=19241.5 exitPrice=19308.25 quantity=1 marketPosition=Short entryTime='11/09/2024 18:00:10' exitTime='12/09/2024 00:00:00' profitCurrency=-1339.18
          12/09/2024 08:21:20 oneTradeAgoPnL -1339.18​
          Last edited by PaulMohn; 09-12-2024, 06:23 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          23 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          120 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          63 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          41 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          45 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X