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

Profit Target plus Trailing Stop?

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

    Profit Target plus Trailing Stop?

    I understand that using the SetTrailStop would be an alternative method to SetStopLoss, and that you cannot use both of them together. However, I would like to have a general StopLoss for all trades and also employ a trailing stop after a ProfitTarget trigger has been hit. How can I do this?

    Thank you.

    #2
    Hello rcollen,

    Thanks for your post.

    You could use the fixed SetStopLoss and through code compare the current price to your entry price (Position.AvgPrice) and adjust the level of the fixed stop as needed to fullfill your trailing stop needs. You would need your code to execute with CalculateOnBarClose = false to ensure timely adjustments.

    After the order is over (Position.MarketPosition == MarketPosition.Flat) you would then reset the setStopLoss to your original distance so that the next entry would have the proper stop loss.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello Paul. Thanks for your help.

      In Variables, I have declared:
      private double stopLoss
      private double trailPercent

      In Initialize() I have declared:
      CalculateOnBarClose = false,
      SetStopLoss (StopLoss) to create a global stop loss for all trades.

      In OnBarUpdate() in order to put a Trailing Stop on trades that run >$1000, I have coded:
      if (Position.Close() >= Position.AvgPrice + (1000))
      SetTrailStop(CalculationMode.Percent(trailPercent) );

      This is not working for the purpose of creating a Trailing Stop that hits the Profit Target.
      Please advise.

      Comment


        #4
        Hello rcollen,

        Thanks for your reply.

        You are not able to use settrailstop and setstoploss together, the setstoploss will supercede in all cases when they are used together.

        You would use setstopLoss for both the initial stop and then adjust it as needed to act as the trailing stop.

        Here is an example (only long side shown) which i will comment between sections. I prefer to work with ticks so my example will show that. It starts with a 20 tick initial stop. After 10 ticks profit will move stop to the current Close price - 10 ticks. As price moves up will increment the stop to retain no less than 10 ticks behind close[0].

        In variables section:
        private bool firsttime = true; // flag for first move of stop
        private double stopprice = 0.0; // variable to hold the new stop price

        In the initialize section:
        CalculateOnBarClose = false;
        SetStopLoss (CalculationMode.Ticks, 20); // for the very first order only...

        In OnBarUpdate section:

        // You have your own order entry section
        // so below is after order entry section
        // First check to see if we are long
        if (Position.MarketPosition == MarketPosition.Long)
        {
        if (Close[0] > Position.AvgPrice + 10 *TickSize) // check to see if price is > 10 tick of the entry price
        {
        if (firsttime) // for the first time, to establish stopprice
        {
        SetStopLoss (CalculationMode.Price, Close[0] - 10 *TickSize); // move stop
        stopprice = Close[0]-10*TickSize; // save stop price
        DrawDot ("test"+CurrentBar,true,0, stopprice,Color.Red); // visualize on chart with dot
        firsttime = false; // set flag so this is not repeated (until new order)
        }
        if (!firsttime && stopprice < Close[0]-10*TickSize) // on subsequent checks test against stop price
        {
        SetStopLoss (CalculationMode.Price, Close[0] - 10 *TickSize); // adjust stop
        stopprice = Close[0]-10*TickSize; // set new stop price
        DrawDot ("test"+CurrentBar,true,0, stopprice,Color.Red); // continue visualization
        }
        }
        }

        if (Position.MarketPosition == MarketPosition.Flat) // Once we are flat. reset variables
        {
        firsttime = true;
        SetStopLoss (CalculationMode.Ticks, 20); // rest to initial stoploss for next entry.
        }

        The example above is for the long side, you would need a similar process for the short side.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Missing a cast?

          Thanks very much, Paul. I would prefer to use CalculationMode.Percent to calculate the trailing stop after reaching a profit target. I declare the variable "double trailPercent" but when I set up the parameter box in the Properties section, I get the error:

          Cannot implicitly convery type "double" to "int." An explicit conversion exists (are you missing a cast?)

          Please clarify. NT Customer Service ROCKS !!!!!!!!

          Comment


            #6
            Hello rcollen,

            Thanks for your reply and comments!

            I suspect that your input property has been set to an integer type instead of a double.
            In the properties section of the code look for your variable "trailPercent" and observe is it says public int or public double. For the percent you are correct in that it needs to be a double type.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Perfect !!!!

              Yes, of course.
              My learning curve has suddenly accelerated.
              It's like coming out of a dark tunnel.

              Thanks again.

              Comment


                #8
                Paul,
                I changed the variable "trailPercent" to "double" in both the Variables and the Properties section.
                However, when I use the Strategy Analyzer, the parameter box does not show the default double value for trailPercent (in this case, 0.5). It shows the digit 1. I am using the following getter setter:

                [Description("")]
                [GridCategory ("Parameters")]
                public double TrailPercent
                {
                get {return trailPercent;}
                set {trailPercent = Math.Max(1, value);}
                }

                Please advise.

                Comment


                  #9
                  Hello rcollen,

                  Thanks for your reply.

                  It looks like the issue is Math.Max(1, value). Value in this case would be the number that you entered externally in the parameters section. Math.Max is saying to use the maximum of 1 or the value (if value was greater than 1). You want to use the Min function , so Math.Min(1, value) which says to use the smaller of 1 or the value entered if less than 1.

                  Please let me know if I can be of further assistance.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Great!

                    Paul,

                    OK. It's good to go.
                    Thank you very much.

                    Comment


                      #11
                      Entry Level Strategy of one trade per day.

                      Paul (and previous help from Jesse) -

                      I have tried to incorporate your ideas within this thread into the attached "FirstTrade111" Strategy which PatrickH and Jesse helped me with in recent parallel threads. This Strategy will compile, but it does not show any exits( except for the end of session). It does not show either the intended Stop Loss exits or the intended Trailing Stop exits after hitting a Profit Target trigger. Also, it takes more than one trade per day.

                      This Strategy is supposed to take just one trade (2 or 3 contracts) a day after a delay of (x) Bars from the Session Open, either Long when the Price rises (x) Ticks above the Session Open, or else Short when Price falls (x) Ticks below the Session Open. Either the trade should Stop Out at a hard -$200 (User Defined variable), or else exit after it reaches an UnRealized Profit >= $1000 which then becomes subject to a 50% trailing stop.(all User Defined) Ultimately, I would like to incorporate a 2nd Profit Target with Trailing Stop of >=$2000 subject to 25% trailing stop.

                      Please advise. Thank you. You guys are great. I know that this takes time to review, but hopefully this can be a valuable example for other beginners to follow as well.
                      Attached Files

                      Comment


                        #12
                        Hello rcollen,

                        Thanks for your reply.

                        So that I might replicate your observations, what instrument(s) are you using the strategy on? What time frame are the bars being used?

                        I look forward to your reply.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Paul - I am using the TF 09-15 144 Tick chart

                          Comment


                            #14
                            Hello rcollen,

                            I need to understand what your thoughts are when using CalculationMode.Percent. If you use 50% or 75% then that means you will have stop of 50% of the current value of the TF which at the moment would put your stop at a price level of about 640.0 I'm pretty sure you really don't want that. You show an initial stop of 200, is that $200.00? Appreciate any clarity you can provide here. If you would rather discuss offline, feel free to send in an email to PlatFormSupport[At]Ninjatrader[dot]Com with attention; Paul and a link to this thread in the subject line.
                            Paul H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Max238, Today, 01:28 AM
                            4 responses
                            33 views
                            0 likes
                            Last Post Max238
                            by Max238
                             
                            Started by r68cervera, Today, 05:29 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by geddyisodin, Today, 05:20 AM
                            1 response
                            11 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by timko, Today, 06:45 AM
                            2 responses
                            13 views
                            0 likes
                            Last Post NinjaTrader_ChristopherJ  
                            Started by habeebft, Today, 07:27 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post habeebft  
                            Working...
                            X