Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SystermPerformance question

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

    SystermPerformance question

    Hi,
    In an enabled strategy, is it possible to gather statistics of the historical trades that are within "Days To Load"? I have a method using Trade lastTrade, which works when running back tests but not on the loaded historical trades of an enabled strat. What is possible?
    thanks,
    David

    #2
    Hello David,

    Thank you for your post.

    Yes, you could access all trades (both historical and real-time) with the SystemPerformance.AllTrades collection:


    TradeCollections are indexed in a sequence from oldest trade to newest trade. The last trade would just be the trade at the index of count - 1, and the first trade would be at index 0 of the collection. You could choose to loop through the collection if you'd like to gather information for each trade. The first example on this page demonstrates how to get the lastTrade and firstTrade:


    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi Emily,
      I get lastTrade prints when running the strategy in the Strategy Analyzer, but I don't get lastTrade prints when running the strategy live. What might i be missing?
      thanks,
      David

      Comment


        #4
        Hello David,

        Thank you for your reply.

        Please provide a code snippet of the value you are printing out so I may better understand and assist you.

        I look forward to your reply.

        Comment


          #5
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class LastTradeTest : Strategy
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "LastTradeTest";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = false;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 1;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {

          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          //entry
          if (Close[0] > Open[0])
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          }

          //exit
          if (BarsSinceEntryExecution() > 5)
          {
          ExitLong();
          }

          //collect stats
          if (SystemPerformance.AllTrades.Count > 1)
          {
          Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
          Trade firstTrade = SystemPerformance.AllTrades[0];

          Print("The last trade profit is " + lastTrade.ProfitCurrency);
          Print("The first trade profit is " + firstTrade.ProfitCurrency);
          }



          }
          }
          }​

          Comment


            #6
            Hello David,

            Thank you for your reply.

            I see the following prints should occur if the AllTrades.Count is greater than 1:
            Code:
            if (SystemPerformance.AllTrades.Count > 1)
            {
            Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
            Trade firstTrade = SystemPerformance.AllTrades[0];
            
            Print("The last trade profit is " + lastTrade.ProfitCurrency);
            Print("The first trade profit is " + firstTrade.ProfitCurrency);
            }​
            In order to understand why you are or are not seeing these prints when running the strategy live, I suggest printing the value for SystemPerformance.AllTrades.Count outside of the condition to understand if it is less than, equal to, or greater than 1:
            Code:
            Print(Time[0] + " SystemPerformance.AllTrades.Count is: " + SystemPerformance.AllTrades.Count);
            if (SystemPerformance.AllTrades.Count > 1)
            {
            Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
            Trade firstTrade = SystemPerformance.AllTrades[0];
            
            Print("The last trade profit is " + lastTrade.ProfitCurrency);
            Print("The first trade profit is " + firstTrade.ProfitCurrency);
            }​
            Please let us know if we may be of further assistance.

            Comment


              #7
              Hi Emily,
              I modified the strat to hold for 1 bar only and close. I then allowed a few trades to open and close. The trade count is always zero for historical and live bars. This is using the sim account.

              12/21/2023 2:19:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:20:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:21:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:22:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:23:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:24:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:25:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:26:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:27:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:28:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:29:00 PM SystemPerformance.AllTrades.Count is: 0
              Enabling NinjaScript strategy 'LastTradeTest/309086611' : On starting a real-time strategy - StartBehavior=ImmediatelySubmitSynchronizeAccount Position=MNQ MAR24 1L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
              Syncing account position on starting strategy 'LastTradeTest/309086611'. Account position=' MNQ MAR24' Strategy position='L MNQ MAR24'
              12/21/2023 2:30:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:31:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:32:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:33:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:34:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:35:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:36:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:37:00 PM SystemPerformance.AllTrades.Count is: 0
              12/21/2023 2:38:00 PM SystemPerformance.AllTrades.Count is: 0​

              Comment


                #8
                Hi Emily,
                I added this to State.SetDefaults (since i use the strategies tab):

                IncludeTradeHistoryInBacktest = true;

                and now i get prints
                Last edited by trader3000a; 12-21-2023, 02:00 PM.

                Comment


                  #9
                  Originally posted by trader3000a View Post
                  Hi Emily,
                  I added this to State.SetDefaults (since i use the strategies tab):

                  IncludeTradeHistoryInBacktest = true;

                  and now i get prints
                  Nice catch; I apologize for the inconvenience. IncludeTradeHistoryInBacktest defaults to true and it did not come to mind that this property being set to false would be the cause of the behavior you were reporting. For more information:


                  Please let us know if we may be of further assistance.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  53 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  130 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  70 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  44 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  49 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X