Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SystemPerformance.AllTrades Issue with Live Trading

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

    SystemPerformance.AllTrades Issue with Live Trading

    Created a simple strategy to troubleshoot code which is supposed to print the realized P&L from the last trade a strategy has made, regardless of historical or live bars. Please see attached file for the strategy.

    When backtesting, this is what I see, and is what I expect:

    .....
    Position Opened
    Position Closed
    Last Trade PL = ($35.00)
    .....
    However, when the strategy is run live, on historical bars, I do not see the "Last Trade PL" line at all.

    This indicates that SystemPerformance.AllTrades is not being populated when a live strategy is run on historical bars. Not sure if this is a bug or I am misusing something, but since I'm not using SystemPerformance.RealTimeTrades, I would have expected it to include historical trades as well.

    I have gotten here by trying to debug. Initially, I tried the following without the nested if statement as I assumed that the second Print() would only be called when a trade is exited (and thus AllTrades.Count having at least 1 item) but this caused NT8 to not respond/force close on me.

    Code:
    				if (execution.Order.Name.Substring(0,4) == "Exit")
    				{
    					Print("Position Closed");
    					if (SystemPerformance.AllTrades.Count > 0)
    						Print("Last Trade PL = " + SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency.ToString("C"));
    				}
    Attached Files

    #2
    Hello,

    Thank you for sharing that with us. I'm going to take some time to test this out on my end, and see if I can find a solution or pinpoint what is happening. I will get back to you as soon as I have some info to share.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      I modified the entry conditions to test this in realtime, so it would trade immediately when run, and also so I would see timestamps:

      Code:
      		protected override void OnBarUpdate()
      		{
      			Print (Time[0]);
      			
      			if (State == State.Realtime)
      				EnterLong("Entry");
      			//Add your custom strategy logic here.
      			
      			if (BarsSinceEntryExecution("Entry") >= 5)
      				ExitLong("Exit", "Entry");	
      		}
      However, it seems that even in realtime, I am not getting the "Last Trade PL" printed. Output contents as follows:

      5/15/2015 3:17:00 PM
      NinjaScript strategy 'MyCustomStrategy/42297425' submitting order
      Position Opened
      5/15/2015 3:18:00 PM
      5/15/2015 3:19:00 PM
      5/15/2015 3:20:00 PM
      5/15/2015 3:21:00 PM
      5/15/2015 3:22:00 PM
      5/15/2015 3:23:00 PM
      NinjaScript strategy 'MyCustomStrategy/42297425' submitting order
      Position Closed

      Comment


        #4
        Hello,

        Thank you for your patience. After testing on my end, everything seems to be printing as expected. The "Last Trade" prints were buried within hundreds of lines of timestamps, but they were there (see below)

        http://screencast.com/t/FIKopOKp7
        http://screencast.com/t/8VFdv59C
        http://screencast.com/t/f7fPWvaY

        Is it possible that they were just so buried in the timestamps on your end that you missed them? Or do you still not see them at all?
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Hi,

          Thanks for investigating. I can confirm that they do not come up at all, and removing the nested if statement causes NT8 to crash.

          However, this behaviour only occurs when I run the strategy from the strategies grid. When backtesting or running the strategy from a chart, it works as expected.

          Comment


            #6
            Hello,

            Thank you for clarifying that. I'm seeing the same thing on my end when I apply the strategy directly via the Strategies tab. After a bit of debugging, it appears the issue stems from SystemPerformance.AllTrades.Count always returning 0 when the strategy is applied within the Strategies tab.

            I have submitted a QA report on this issue to our development team. Thank you once more for sharing this with us.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              Originally posted by NinjaTrader_Dave View Post
              Hello,

              Thank you for clarifying that. I'm seeing the same thing on my end when I apply the strategy directly via the Strategies tab. After a bit of debugging, it appears the issue stems from SystemPerformance.AllTrades.Count always returning 0 when the strategy is applied within the Strategies tab.

              I have submitted a QA report on this issue to our development team. Thank you once more for sharing this with us.
              This is still an issue in NT8Beta3 with Playback-market replay.

              After my stoploss was hit, and the trade closed, it is always reporting 0. In NT7 this counted.


              Code:
              Print ( "NumberOfTrades="+ NumberOfTrades + " Performance.AllTrades.Count=" + SystemPerformance.AllTrades.Count );

              NumberOfTrades=1 Performance.AllTrades.Count=0
              NumberOfTrades=1 Performance.AllTrades.Count=0
              NumberOfTrades=1 Performance.AllTrades.Count=0
              NumberOfTrades=1 Performance.AllTrades.Count=0
              NumberOfTrades=1 Performance.AllTrades.Count=0
              NumberOfTrades=1 Performance.AllTrades.Count=0

              Comment


                #8
                And it looks like nothing is there. I counted my trades in TotalTrades and subtracted 1 to access and errored out.

                NumberOfTrades=1 TotalTrades=1 Performance.AllTrades.Count=0
                Strategy 'strategy1': Error on calling 'OnBarUpdate' method on bar 11101: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                Disabling NinjaScript strategy 'strategy1/51210416'

                Comment


                  #9
                  Hello,

                  Try setting "IncludeTradeHistoryInBacktest = true" within State.SetDefaults, and see if that gives you a result. This property is set to false by default in certain cases, such as when a strategy is enabled directly in a grid (the Strategies tab or Strategy Analyzer, etc.).
                  Dave I.NinjaTrader Product Management

                  Comment


                    #10
                    I'll give that a try tonight.

                    Comment


                      #11
                      Originally posted by NinjaTrader_Dave View Post
                      Hello,

                      Try setting "IncludeTradeHistoryInBacktest = true" within State.SetDefaults, and see if that gives you a result. This property is set to false by default in certain cases, such as when a strategy is enabled directly in a grid (the Strategies tab or Strategy Analyzer, etc.).
                      Nope! DOES NOT work in Playback - Market Replay for me.

                      NinjaScript strategy 'strategy1/51210416' submitting order
                      1a marketPosition=Long
                      1b position.MarketPosition=Long
                      2 OnPositionUpdate.MarketPosition=LONG
                      NinjaScript strategy 'strategy1/51210416' submitting order
                      sjsV1issuL- 20140121,93500,ES,Entered Long @1842.75
                      NinjaScript strategy 'strategy1/51210416' submitting order
                      1a marketPosition=Flat
                      1b position.MarketPosition=Long
                      2 OnPositionUpdate.MarketPosition=FLAT
                      20140121,110900,ES,Flat
                      NumberOfTrades=1 TotalTrades=1 Performance.AllTrades.Count=0

                      Comment


                        #12
                        It seems to be working as expected on my end in Market Replay. I receive Prints that update Alltrades.Count each time a trade was taken on historical bars before State.Realtime. Can you share a snippet of code that shows your order entry and exit logic and the placement of your Prints so that I can test further?
                        Dave I.NinjaTrader Product Management

                        Comment


                          #13
                          Originally posted by NinjaTrader_Dave View Post
                          It seems to be working as expected on my end in Market Replay. I receive Prints that update Alltrades.Count each time a trade was taken on historical bars before State.Realtime. Can you share a snippet of code that shows your order entry and exit logic and the placement of your Prints so that I can test further?
                          We might be still a step apart on understanding each other here. Yes I'll put something together tonight.

                          In the meantime, This is in playback-market replay. There are no historical trades.

                          The first trade is with the playback button going forward, max speed. There is no trade performance on the loss.

                          Comment


                            #14
                            Originally posted by NinjaTrader_Dave View Post
                            It seems to be working as expected on my end in Market Replay. I receive Prints that update Alltrades.Count each time a trade was taken on historical bars before State.Realtime. Can you share a snippet of code that shows your order entry and exit logic and the placement of your Prints so that I can test further?
                            This codes contains both the marketPosition/Alltrades issues.




                            This code runs 1 time. I connect to PlayBack-Market Replay. I add and enabled the strategy. I turn playback up to max. hit play.

                            The code will enterLong at 9:34am or so. Hopefully you have a day where the stopLoss is hit.

                            In the case below - exitOnClose happened.

                            There is nothing in AllTrades, and the position.MarketPosition shows Long, inside of OnPositionUpdate?






                            Code:
                            NinjaScript strategy 'MyNT8test/51210427' submitting order
                            1a marketPosition=Long
                            1b position.MarketPosition=Long
                            2 OnPositionUpdate.MarketPosition=LONG
                            NinjaScript strategy 'MyNT8test/51210427' submitting order
                            test1- 20131230,93500,ES,Entered Long @1837.25
                            NinjaScript strategy 'MyNT8test/51210427' submitting order
                            NinjaScript strategy 'MyNT8test/51210427' submitting order
                            1a marketPosition=Flat
                            1b position.MarketPosition=Long
                            2 OnPositionUpdate.MarketPosition=FLAT
                            20131230,144300,ES,Flat
                             Performance.AllTrades.Count=0
                             Performance.AllTrades.Count=0
                            Attached Files
                            Last edited by sledge; 08-20-2015, 07:37 PM. Reason: added log/trace

                            Comment


                              #15
                              Thank you -- I'll take a look at the code and see if I can gain any further insight.

                              It is expected that position.MarketPosition would show long in the OnPositionUpdate() called by exiting a long position. When OnPositionUpdate() is called, the Position object passed into it contains info about the position as it stood before OnPositionUpdate() was called. The MarketPosition object passed into OnPositionUpdate() should contain the current position. We do this to allow the previous state of the position to be compared with the new state, for any kind of logic you may need that requires both.
                              Dave I.NinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by junkone, 04-28-2024, 02:19 PM
                              8 responses
                              89 views
                              1 like
                              Last Post brucerobinson  
                              Started by mkouitra, 10-23-2021, 04:40 PM
                              17 responses
                              1,961 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by Vietanhnguyen2hotmailcom, 05-03-2024, 10:29 AM
                              4 responses
                              29 views
                              0 likes
                              Last Post Vietanhnguyen2hotmailcom  
                              Started by PhillT, 04-19-2024, 02:16 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post PhillT
                              by PhillT
                               
                              Started by ageeholdings, 05-01-2024, 05:22 AM
                              5 responses
                              39 views
                              0 likes
                              Last Post ageeholdings  
                              Working...
                              X