Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time stop active if profit is < 0?

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

    Time stop active if profit is < 0?

    How do I tell my strategy to close a position if the unrealized profit of the position has been below 0 for a specific amount of time?

    For example: I take a long position which immediately starts going the wrong way. After, lets say 15 minutes, (or three 5-minute bars) I am stopped out since the position has been in the red for 15 minutes in a row. However, IF the position had been profitable at some point during those 15 minutes, I would not have been stopped out, until the 15 minutes in-a-row loss criteria is met.


    Any help is appreciated. I have not seen any threads about time stops but it would seem completely unimaginable that NT would NOT support such a basic and priceless function.

    Best regards
    Last edited by Fierze; 12-05-2009, 05:09 PM.

    #2
    Hello,

    You will need to track the price of the entry and do a check against the current price. You may want to use IOrder to do this:
    DenNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Ben View Post
      Hello,

      You will need to track the price of the entry and do a check against the current price. You may want to use IOrder to do this:
      http://www.ninjatrader-support.com/H...V6/IOrder.html

      Thank you Ben. I am glad that this seems to be possible since it will make backtesting alot more realistic.

      Since I am a NT beginner and have very little programming experience, I would appreciate if someone can give a more detailed example on how to use IOrder to acheive this kind of time stop. It is not really obvious to me yet.

      Comment


        #4
        Hello,

        We don't code for people, but if you give it using the following and post questions and code we can answer them the best we can:

        - DateTime.Now
        - IOrder and this link:

        - Avg.Price and this link:

        - This link:

        - This link:


        I find it helpful to write the logic on paper then code it. Or ask for someone to post an example for you.
        DenNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ben View Post
          Hello,

          We don't code for people, but if you give it using the following and post questions and code we can answer them the best we can:
          Ok, bear with me as I start at the steep end of my learning curve

          When searching throught the help guide, I came across the GetAtmStrategyUnrealizedProfitLoss() syntax:


          protected override void OnBarUpdate()
          {
          Print("Unrealized PnL is " + GetAtmStrategyUnrealizedProfitLoss("id").ToString( ));
          }


          Is it possible to use this for only a single position instead of an ATMstrategy, to track the unrealized profit of the position?
          I was thinking that this value could then be tracked to see how many minutes or bars the profit has been below 0.

          Comment


            #6
            Fierze, this can be used only when calling ATM templates in a NinjaScript strategy for exit management - for the open PNL of your strategy position you can use GetProfitLoss() - http://www.ninjatrader-support.com/H...rofitLoss.html

            You could for example monitor when this turns negative from a certain 'green' threshhold and then count the bars it stays below the 0 level.

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              Fierze, this can be used only when calling ATM templates in a NinjaScript strategy for exit management - for the open PNL of your strategy position you can use GetProfitLoss() - http://www.ninjatrader-support.com/H...rofitLoss.html

              You could for example monitor when this turns negative from a certain 'green' threshhold and then count the bars it stays below the 0 level.
              Of course, GetProfitLoss() is exactly what I was looking for. Thank you.

              Now I just have to find a way for the strategy to count for how long time GetProfitLoss() has had a negative value, and then close the position when a value of X minutes or X bars has been reached.

              Any suggestions on how to track the duration of a negative GetProfitLoss() value?

              Comment


                #8
                Rudimentary way could just be to check bar timestamps. If it persists to the next bar still negative, aggregate the time difference from that bar to the next.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  Rudimentary way could just be to check bar timestamps. If it persists to the next bar still negative, aggregate the time difference from that bar to the next.
                  I presume this can not be done in the strategy wizard?

                  Are there any code examples of this (or similar strategys) on the forum? Seeing how others have coded similar things would by far be the fastest way for me to learn this stuff...
                  Last edited by Fierze; 12-07-2009, 02:26 PM.

                  Comment


                    #10
                    To do this you would probably want to do custom programming. Unfortunately we do not have any examples that you could look at outlining this.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Josh View Post
                      To do this you would probably want to do custom programming. Unfortunately we do not have any examples that you could look at outlining this.


                      I have seen in some threads, like the above, that the expression "BarsSince" has been used. This sounds like it would be perfect here, as long as I can specify which condition BarsSince would relate to. In this case it is when the value of GetProfitLoss < 0.


                      Would the following be on the right track?

                      int BarsSince = 0;
                      If ((GetProfitLoss() < 0) = true && BarsSince >=3);
                      ExitLong();
                      ExitShort();



                      I know there is probably a lot of code missing but this is just a rough example. Please point me in the right direction.

                      Comment


                        #12
                        Fierze, yes you could also work with BarsSinceEntry for this, returning a count for how many bars the current position is open - http://www.ninjatrader-support.com/H...inceEntry.html

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          Fierze, yes you could also work with BarsSinceEntry for this, returning a count for how many bars the current position is open - http://www.ninjatrader-support.com/H...inceEntry.html
                          Maybe so, but I don't really see the point of knowing how many bars I have had an open position. All that is important for this strategy is to know how long the position has been a loser, with unrealized profit below 0.

                          I have not learned enough about C# and NinjaScript to be able to formulate it properly in code yet, but basically this is what I want:

                          IF (GetProfitLoss() < 0) = true AND BarsSince (GetProfitLoss() > 0) EQUALS 3 bars or more,THEN ExitLong(); AND ExitShort();

                          I think you know what I am trying to do by now. As long as I know this will work, it is just a matter of sitting down and learning how to write the code. The main question is this line:

                          If ((GetProfitLoss() > 0) = true && BarsSince > 3);

                          Is this the same as telling Ninja Trader that "IF more than 3 bars have passed since unrealized profit was above zero" or do I have to further specify what I mean by "BarsSince"?
                          Last edited by Fierze; 12-08-2009, 07:21 AM.

                          Comment


                            #14
                            Sorry I did not follow the last time, then BarsSinceEntry / Exit would not be your method of choice. You would need to code it out and save the barnumber for reference to a variable as your open Pnl turns negative, then you can compare this to the CurrentBar (http://www.ninjatrader-support.com/H...urrentBar.html) to trigger your exit logic once you've been in the 'red' for this position for x bars.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              Sorry I did not follow the last time, then BarsSinceEntry / Exit would not be your method of choice. You would need to code it out and save the barnumber for reference to a variable as your open Pnl turns negative, then you can compare this to the CurrentBar (http://www.ninjatrader-support.com/H...urrentBar.html) to trigger your exit logic once you've been in the 'red' for this position for x bars.
                              Tank you, i appreciate your help. Are there any examples (on the forum or in the help guide) on how to save the barnumber for reference to a variable like you suggest?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              88 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              48 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              30 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              34 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              68 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X