• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetUnrealizedProfitLoss() hangs Market Replay

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

    GetUnrealizedProfitLoss() hangs Market Replay

    So maybe Market Replay isn't such a piece of junk after all. Maybe me calling GetUnrealizedProfitLoss() is the reason Market Replay seems like a piece of junk.

    I finally got around to debugging my base class to see exactly what the problem was and why I couldn't run the thing in Market Replay. It took about four hours but I finally figured it out.

    GetUnrealizedProfitLoss() does not like the Market Replay at all. It doesn't care if you call it in OnBarUpdate() or OnExecutionUpdate(). It will hang the MR regardless and you will never know that it isn't the Market Replay because GetUnrealizedProfitLoss() works just fine in a live environment and there is nothing appearing in the Log output.

    To reproduce, bring up any instrument in any time frame on a chart and drop the attached strategy - which is an editable version of the stock SampleMACrossOver - on that chart. I have GetUnrealizedProfitLoss in both OnBarUpdate() and OnExecutionUpdate(). You can comment one and uncomment the other and run it in Market Replay. It will hang the Market Replay just as sure as the sun rises.
    Attached Files

    #2
    In one of my strategies I added in OnBarUpdate

    Before any trade:

    currentPrice=1837.25
    CurrentProfit=0
    currentPrice=1837.25
    CurrentProfit=0
    currentPrice=1837.25
    CurrentProfit=0
    While in a trade:

    Code:
    currentPrice=1836.75
    CurrentProfit=100
    currentPrice=1836.75
    CurrentProfit=100
    currentPrice=1836.75
    CurrentProfit=100

    Code:
    		protected override void OnBarUpdate()
    		{
    			
    			if (BarsInProgress != 0)
    				return;
    			
    			if (CurrentBars[0] < 0 || CurrentBars[1] < 0)
    				return;
    			//Add your custom strategy logic here.
    			// only process on real-time data 
       			if (State == State.Historical) 
    				return; 
    
    			double currentPrice = (Position.MarketPosition == MarketPosition.Long) ? GetCurrentBid() : GetCurrentAsk();	
    			Print ("currentPrice="+currentPrice );
    			double currentProfit = Position.GetUnrealizedProfitLoss( PerformanceUnit.Currency, currentPrice);
    			Print ("CurrentProfit="+currentProfit );
    in OnExecutionUpdate:


    Code:
    OEU- currentPrice=1839
    OEU- CurrentProfit=-12.5
    - 20140122,94100,ES,Entered Short @1838.75
    OEU- currentPrice=1840.5
    OEU- CurrentProfit=0
    20140122,155000,ES,Flat: 1840.25

    I do NOT have any AddChartIndicator calls in my strategy. I also attached my strategy to a chart and not the control center.
    Last edited by sledge; 02-11-2017, 10:04 PM.

    Comment


      #3
      I added it to a chart and to the control center (1minute).

      Nothing has happened.


      https://www.screencast.com/t/nDAhBczKw6M

      Comment


        #4
        Yeah, that figures. After I saw your post I tried it in the sample script and it worked. So it would be just my luck to go back to my base class and try that and have it work there too. But it didn't - the method that calls GetUnrealizedProfitLoss() is commented and runs like a champ. When I uncomment that method, it doesn't work. Except now it behaves differently this afternoon. It doesn't hang like it did 10 hours ago with zero code changes, it disables my strategy with no notifications in the control center. But it keeps running, at least.

        And the bad behavior was definitely traced back to GetUnrealizedProfitLoss() earlier this afternoon. I did that by putting return statements after each line of code and seeing where it would choke, and it didn't choke until I got to the call to GetUnrealizedProfitLoss(). Then I created that sample app and after it reproduced using that simple script, I left it at that. One of these days I'll smarten up and learn how to post videos if I can find a free site. I already had my 12 year old show me how to record videos and one of these days I'll find a free site to post them so ya'll will know I'm not crazy.

        So here's the $65,000 question: Why the hell did it hang this afternoon, and just now it runs fine, with the same code and the very same market replay? And even the same chart!! And why did my base class strategy exhibit the same behavior as the sample this afternoon but different behavior this evening, with the same code and even the same chart??!! (Different builds since I was moving the return statement down line-by-line but still, essentially the same code.)

        Market Replay...
        Last edited by traderpards; 02-11-2017, 11:39 PM.

        Comment


          #5
          No idea.

          I even handicapped your strategy by NOT restarting NT8 and resetting the slider after a 3 year replay and compiling in changes and then add your strategy and resetting the slider.

          If you can keep it under 5 minutes, https://www.techsmith.com/jing.html

          Where you deep into something without restarting NT8? I do overnight replays of 3 years of ES... and always restart after that. But if you can't reproduce after restart and maybe 3 replays after it blows up - then the focus probably needs to be on the reset replay...

          Of course if you are using VS xxxx, that's another sticking point to cause havoc overall. (I'm not using VS).

          Comment


            #6
            Thanks for the link Sledge... I was able to make a recording proving I'm not crazy after all...

            I wish I was in your shoes - never having issues like this. I can't explain why it ran last night but it sure didn't run yesterday afternoon and it doesn't run this afternoon.

            Here's proof!
            Market Replay hangs...

            When it hangs like that, all I can do is restart the strategy, after which I'm completely reset and can debug, which is what I did for hours and hours in order to track the problem down to GetUnrealizedProfitLoss().

            BTW: I use the NT editor to code with and only use VS to debug, which I did last night because I needed to be reminded why something didn't enter when I thought it should, but it turned out I had forgotten a criterion. And that didn't cause the Replay to hang like it has before, or at least I was willing to blame it on VS at the time. And as you can see in the video, I didn't even touch the slider or even reset it at all - it just hung up on its own free will simply because I loaded probably the simplest strategy you can (almost) possibly load.

            Comment


              #7
              Hello traderpards,

              Thank you for your patience and for providing a video on the matter.

              Can you replicate this in a new workspace with only one chart and no other workspaces open? If so, can you attach that workspace to your response? You will find the workspace under Documents\NinjaTrader 8\workspaces after you save it.

              I look forward to your response.
              Patrick H.NinjaTrader Customer Service

              Comment


                #8
                Yes I can, easy peasy. I just replicated it twice. The first time was on a 1-minute chart with the call to GetUnrealizedProfitLoss() uncommented in OnExceutionUpdate() and the second time with Range bars with the call to GetUnrealizedProfitLoss() uncommented in OnBarUpdate().

                I ran it a third time with both calls to GetUnrealizedProfitLoss() commented and the market replay runs like it's supposed to.

                (Once it gets hung up, you have to restart Ninja so I did that each time.)

                Here's the workspace. Hopefully that's all you need...
                Attached Files

                Comment


                  #9
                  Thank you, traderpards,

                  I am able to reproduce and will report to development.
                  Patrick H.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_PatrickH View Post
                    Thank you, traderpards,

                    I am able to reproduce and will report to development.
                    Cool... Thanks Patrick! I am currently trying to find a workaround to calling GetUnrealizedProfitLoss().

                    I don't suppose I could talk you out of the source code for GetUnrealizedProfitLoss(), assuming there is nothing proprietary in there, which I'm assuming there isn't, could I?

                    If you can't do that, can you suggest an alternate algorithm that I can use to get how much the current position is up/down at any given moment?

                    Comment


                      #11
                      Hello traderpards,

                      Thank you for your response.

                      A basic Unrealized PnL calculation would be the following:
                      Code:
                      			double diff = Close[0] - Position.AveragePrice;
                      			double upnl = (diff * Instrument.MasterInstrument.PointValue) * Position.Quantity;
                      Please let me know if you have any questions.
                      Patrick H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by guvenators, Today, 05:18 PM
                      1 response
                      13 views
                      0 likes
                      Last Post NinjaTrader_ShawnB  
                      Started by LANwarr1or, Today, 03:00 PM
                      2 responses
                      8 views
                      0 likes
                      Last Post LANwarr1or  
                      Started by Newtrader101, Today, 02:53 PM
                      2 responses
                      10 views
                      0 likes
                      Last Post Newtrader101  
                      Started by JuanPabloQuevedo, Today, 02:53 PM
                      2 responses
                      9 views
                      0 likes
                      Last Post JuanPabloQuevedo  
                      Started by samir, Today, 03:16 PM
                      2 responses
                      14 views
                      0 likes
                      Last Post samir
                      by samir
                       
                      Working...
                      X