Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I write a condition that says IF Unrealized PNL is negative $100 do?

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

    How do I write a condition that says IF Unrealized PNL is negative $100 do?

    Trying to create a blanket exit strategy if the position is open and the loss is larger than $100.
    Here is what I came up with:

    if ((Position.GetUnrealizedProfitLoss(PerformanceUnit .Currency, Close[0]) > -100)
    {
    Order_BuyExit_SellStopLoss[0] = location_above_bar;
    }

    I'm getting this error:

    An object reference is required for a non-static field, method or property.


    What am I missing?



    #2
    Hello darkgreen,

    Thank you for your post.

    First, if you want the loss to be more than 100, you should be checking whether or not Position.GetUnrealizedProfitLoss(PerformanceUnit .Currency, Close[0] is less than -100, not greater. -200 would be a greater loss than -100.

    It's unclear from your code sample what this line is doing:

    Order_BuyExit_SellStopLoss[0] = location_above_bar;

    I would likely look at that line as the line before looks fine.

    Is this the full text of the error if you hover your mouse over it? If not, what is the full error text?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post
      Hello darkgreen,

      Thank you for your post.

      First, if you want the loss to be more than 100, you should be checking whether or not Position.GetUnrealizedProfitLoss(PerformanceUnit .Currency, Close[0] is less than -100, not greater. -200 would be a greater loss than -100.

      It's unclear from your code sample what this line is doing:

      Order_BuyExit_SellStopLoss[0] = location_above_bar;

      I would likely look at that line as the line before looks fine.

      Is this the full text of the error if you hover your mouse over it? If not, what is the full error text?

      Thanks in advance; I look forward to assisting you further.

      Here is what I'm checking:
      var hundo=Position.GetUnrealizedProfitLoss(Performance Unit .Currency, Close[0]);

      Here is the sell order:

      if (hundo < -100)
      {
      Order_BuyExit1[0] = location_above_bar;
      }

      Here is the error:
      An object reference is required for the non-static field, method, or property 'NinjaTrader.Cbi.Position.GetUnrealizedProfitLoss( NinjaTrader.Cbi.PerformanceUnit, double)' CS0120




      Comment


        #4
        Hello darkgreen,

        Thank you for your note.

        I notice the code you've provided has spaces in "Performance Unit .Currency" that shouldn't be there in the actual code. Can you confirm that the line reads like this, without the spaces?:

        Code:
        var hundo=Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);
        Edit: looks like the forum might be adding those spaces, but just to be sure.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Kate View Post
          Hello darkgreen,

          Thank you for your note.

          I notice the code you've provided has spaces in "Performance Unit .Currency" that shouldn't be there in the actual code. Can you confirm that the line reads like this, without the spaces?:

          Code:
          var hundo=Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);
          Edit: looks like the forum might be adding those spaces, but just to be sure.

          Thanks in advance; I look forward to assisting you further.
          confirmed, there are no spaces. The error is exactly as I wrote above.

          Comment


            #6
            Hello darkgreen,

            That particular line is working fine for me in the attached example script. Please test to see that it is working - it will print the current value of hundo to the NinjaScript Output Window.

            Where within your script is this code found? Is it within OnBarUpdate?

            Thanks in advance; I look forward to assisting you further.
            Attached Files
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Kate View Post
              Hello darkgreen,

              That particular line is working fine for me in the attached example script. Please test to see that it is working - it will print the current value of hundo to the NinjaScript Output Window.

              Where within your script is this code found? Is it within OnBarUpdate?

              Thanks in advance; I look forward to assisting you further.
              You're only printing data, try to compile it using this:

              if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0] < -100))
              {
              ExitLong("", "");
              }

              Does it give you an error?

              Comment


                #8
                @NinjaTrader_Kate any luck?

                Comment


                  #9
                  Hello darkgreen,

                  Thank you for your reply.

                  You have a typo there. What you have does not compile because you moved the closing parenthesis for GetUnrealizedProfitLoss() to the far right of the -100. Change it to this and it will compile:

                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) < -100)
                  {
                  ExitLong("", "");
                  }
                  Please let us know if we may be of further assistance to you.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Kate View Post
                    Hello darkgreen,

                    Thank you for your reply.

                    You have a typo there. What you have does not compile because you moved the closing parenthesis for GetUnrealizedProfitLoss() to the far right of the -100. Change it to this and it will compile:



                    Please let us know if we may be of further assistance to you.
                    Thanks it's compiling now.
                    Will it factor in when using the strategy analyzer back-testing output or just live trading?

                    Comment


                      #11
                      Hello darkgreen,

                      Thank you for your reply.

                      You likely need to set IncludeTradeHistoryInBacktest to true in State == State.SetDefaults:

                      https://ninjatrader.com/support/help...inbacktest.htm

                      Please let us know if we may be of further assistance to you.
                      Kate W.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Kate View Post
                        Hello darkgreen,

                        Thank you for your reply.

                        You likely need to set IncludeTradeHistoryInBacktest to true in State == State.SetDefaults:

                        https://ninjatrader.com/support/help...inbacktest.htm

                        Please let us know if we may be of further assistance to you.
                        Thanks, I don't think it's working even with the includetradehistory.. enabled in the strategy.
                        The reason is the largest losing trade is ($1,916) well over the criteria for the unrealized loss

                        if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) < -300)
                        {
                        ExitLong("sell8", "");
                        }



                        Click image for larger version

Name:	MRnI6eW.png
Views:	23
Size:	6.0 KB
ID:	1115699


                        Thoughts?

                        Comment


                          #13
                          Originally posted by NinjaTrader_Kate View Post
                          Hello darkgreen,

                          Thank you for your reply.

                          You likely need to set IncludeTradeHistoryInBacktest to true in State == State.SetDefaults:

                          https://ninjatrader.com/support/help...inbacktest.htm

                          Please let us know if we may be of further assistance to you.
                          hey there, any thoughts?

                          Thanks

                          Comment


                            #14
                            Hello darkgreen,

                            Thank you for your patience.

                            We do have a high number of inquiries to answer each day, and we do try to respond as soon as possible.

                            In a backtest, fills are determined based on 4 data points, OHLC of a bar since that is the only information that is known during a backtest and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

                            Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

                            What does this mean for your testing? It's likely you will see greater losses than $300, as the position will not be exited until the next bar after the Unrealized PnL reaches over -300.

                            For example, let's imagine we're in a position and on a bar our losses are currently 280. On the next bar, the market keeps going down, and the bar closes with our position having an Unrealized PnL of 380. Since we can't exit the position intrabar, as it's calculating On Bar Close, the position will be exited when the next bar after that opens.

                            You can test with the attached example to see this in action - if you use a ruler and measure to the close of the bar 2 previous to the one the order in question was exited on, I suspect you'll find that on the bar previous to the bar that triggered the exit order, you'll get a result in currency that is less than -300. If this is not what you are seeing with your own code, there may be an issue in your logic preventing the exit from occurring when you'd expect.

                            Please let us know if we may be of further assistance to you.
                            Attached Files
                            Kate W.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by RobotSyndicate, Today, 04:27 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post RobotSyndicate  
                            Started by Ray12345, Today, 04:10 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post Ray12345  
                            Started by Behungr101, Today, 02:16 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jason  
                            Started by omermirza, Yesterday, 08:37 PM
                            2 responses
                            12 views
                            0 likes
                            Last Post NT-Roland  
                            Started by aelagha, Today, 01:28 AM
                            1 response
                            12 views
                            0 likes
                            Last Post NinjaTrader_Jason  
                            Working...
                            X