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

How can I obtain the highest unrealised profit in a trade

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

    How can I obtain the highest unrealised profit in a trade

    There is a "GetProfitLoss()" that I can use, and if I were to include BarsSinceEntry(), it wouldn't include the bar in which the entry was made.

    Could someone please help?

    Regards

    Kay Wai

    #2
    Kay, for this you would need to keep track of the highest open profit seen since the trade was open, easiest done by a custom variable tracking this in your code.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      I am having trouble accessing the highest open profit since entry.

      If I use barssinceentry, it can track from the bar after the entry bar.

      If I wanted to track this on a real-time basis, I would have to use cobc = false if I were to use a custom variable, is that correct? That may mean re-writing my entire code!

      I want to track the highest profit specifically on the entry bar so that if a desired profit is hit, I can exit the trade on that bar. Any other suggestions?

      Regards

      Kay Wai
      Last edited by kaywai; 09-28-2011, 07:38 AM. Reason: Missing word

      Comment


        #4
        Originally posted by kaywai View Post
        If I wanted to track this on a real-time basis, I would have to use cobc = false if I were to use a custom variable, is that correct? That may mean re-writing my entire code!
        you can put the relevant codes in OnMarketData. this will ensure the calculations is done in rt even when cobc is set to true.

        Comment


          #5
          Thanks bukkan. OnMarketData is great for getting real time values.

          You could use something like this for historical long positions.

          Variables Region:
          private double HighestHighinTrade;

          Code:
          if (Position.MarketPosition == MarketPosition.Long)
          HighestHighinTrade = Position.GetProfitLoss(MAX(High, BarsSinceEntry() + 1)[0], PerformanceUnit.Currency);
          Would use MIN and Low for shorts.
          Last edited by NinjaTrader_RyanM1; 09-28-2011, 01:35 PM.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Hi Ryan,

            Thanks for that. Just one question pertaining to barssinceentry. It omits the entry bar but includes all future bars for the trade, right?

            If that were the case, then I would only be getting unrealized profit or loss from the next bar after the entry bar if I were to use your sample code, right?

            Regards

            Kay Wai

            Comment


              #7
              BarsSinceEntry returns a value. The entry bar is when BarsSinceEntry == 0.

              In the example there, it functions as a dynamic lookback value for MAX. Lookbacks should always be >= 1, hence the + 1 in the (corrected) example.

              That expression will include the entry bar in its lookback.
              Last edited by NinjaTrader_RyanM1; 09-28-2011, 01:41 PM.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Hi Ryan,

                Thank you for that piece of code. Appreciate it.

                If I could just trouble with one more query.....

                1) What if I only wanted to track the unrealised pnl of the bar of entry? How would I do that with your code?

                Regards

                Kay Wai
                Last edited by kaywai; 09-28-2011, 07:54 PM. Reason: missing word

                Comment


                  #9
                  For long positions, would be:

                  double myEntryBarHigh = High[BarsSinceEntry()];
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    No +1 needed for the lookback?

                    And assuming all this is done in OnMarketData, right?

                    Comment


                      #11
                      No, in this case BarsSinceEntry is not used in a lookback, but in the bar indexing. Bar indexes start at 0 so no need for +1. This works in bars, most commonly accessed in OnBarUpdate(), although you can access bar objects from OnMarketData.

                      If you wanted to work in OnMarketData, best would be to create your own custom technique for storing values. OnMarketData is a stream of bid/ask/last updates. There's no built in indexing system for accessing prior values in OnMarketData.
                      Last edited by NinjaTrader_RyanM1; 09-29-2011, 10:40 AM.
                      Ryan M.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by GussJ, 03-04-2020, 03:11 PM
                      11 responses
                      3,221 views
                      0 likes
                      Last Post xiinteractive  
                      Started by andrewtrades, Today, 04:57 PM
                      1 response
                      10 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by chbruno, Today, 04:10 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post chbruno
                      by chbruno
                       
                      Started by josh18955, 03-25-2023, 11:16 AM
                      6 responses
                      436 views
                      0 likes
                      Last Post Delerium  
                      Started by FAQtrader, Today, 03:35 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post FAQtrader  
                      Working...
                      X