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

SetTrailingStop

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

    SetTrailingStop

    I am trying to set a trailing stop after the market moves 'x' amount of points in my favor.
    Below is the code that I am using but it is not working.
    I have three IF statements:

    1.) Check to see if a position is open
    2.) Check to see if I have already set the trailing stop
    3.) Check to see if the trade is three points in my favor

    I have verified that the innermost IF statement is being triggered but it seems it is being triggered multiple times resetting the Trailing Stop each time.

    Code:
                if (Position.MarketPosition != MarketPosition.Flat)
                {
                    if (bolTrailStop == false)
                    {
                        if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > 3)
                        {
                            SetTrailStop(CalculationMode.Ticks, 12);
                            bolTrailStop = true;
                        }    
                    }
                else
                        bolTrailStop = false;
                }
    Is there a better way to check to see if a position is open?
    Is there a better way to check if a Trailing Stop is already in place rather then setting a boolean value?

    #2
    Hello,

    Thanks for your forum post.

    Based on your stement below:

    have verified that the innermost IF statement is being triggered but it seems it is being triggered multiple times resetting the Trailing Stop each time.


    You mention that the trailing stop is being triggered multiple times. Isnt this what you want with a trailing stop. What exactly does it do that you do not expect?

    Also, are you running this with CalculateOnBarClose = True or False?

    Also, do you want to backtest this is this why your not using an ATM Strategy instead?

    I look forward to assisting you further.

    Comment


      #3
      Hey Brett,

      My goal with the trailing stop is to set it one time and let it run for the duration of the trade. But I only want to set the trailing stop if I am 3 points in the black.

      Example:
      Long at 1000
      Market goes to 1010 (At 1003 the trailing stop should have been activated)
      Market goes to 995

      With the trailing stop I should have gotten out at 1007 but it is not doing that. I can post screen shots if you need to see something more visual.

      As for your last question I am back testing this. Though I do not know the difference between what I am doing and an ATM Strategy.
      Last edited by Force; 12-27-2010, 10:49 AM.

      Comment


        #4
        Hello,

        Ok, you have 2 ways to do this.

        One is you use an ATM Strategy that you could setup. To do this would cause you not to be able to backtest this as ATM Strategies only work live.



        Or you can code this modification yourself using the following sample. You would need to create code that would calculate the value you would want to start the trail then once price/ Close[0] is above this calculated price then start the auto trail.



        Let me know if I can be of further assistance.

        Comment


          #5
          This IF statement is what I was using to determine if the stop should be set. Is this not correct?

          Code:
          if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > 3)
          {
              SetTrailStop(CalculationMode.Ticks, 12);
          }

          Comment


            #6
            Hello,

            This will not work as SetTrailStop() or SetStopLoss() must be set before you enter the position with EnterLong(). Therefor SetTrailStop will Autotrail but it must auto trail from the entry forward. You cannot specify it to start after 3 points later.

            Let me know if I can be of further assistance.

            Comment


              #7
              Is there anyway to convert an existing stop to a trailing stop? I would imagine there would have to be some way to setup a trailing stop once an order is already in place.

              If I create a new stop with an existing stop already in place does it automatically delete the previous? If it comes down to it I guess I could create incrementing stops per tick.
              Last edited by Force; 12-27-2010, 12:24 PM.

              Comment


                #8
                Hello,

                Unfortunately not. You must use one or the other. Or use one of the two options I presented in a previous post.

                Let me know if I can be of further assistance.

                Comment


                  #9
                  Thanks for all your help Brett. I setup my code to auto increment the stops.

                  Comment


                    #10
                    Hello,

                    Great!

                    Let me know if I can be of further assistance.

                    Comment


                      #11
                      Hey Brett,

                      I came across another question trying to get these stops to work.

                      I have noticed that variables reset every bar or tick. Is there a way to have a variable that is not cleared or reset each time the OnBarUpdate is called? Or is there anyway to see what the current StopLoss price point is?

                      Comment


                        #12
                        Hello Force,

                        Yes, it's possible for variables not to update using custom programming. A common approach is using bool flags. You check a bool variable in an if statement and then set the other direction in that block. You later need a statement that resets it back to an initial state.

                        Searching this forum for bool flag should give you some examples to work from.

                        You can get the price of stop loss orders as well. The technique for this is outlined here:
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          If I wanted to prevent a variable of type double from updating how would I go about that?
                          It seems like the class is being reinitialized every time the OnBarUpdate is being called.

                          Comment


                            #14
                            Hi Force,

                            Please see my previous reply. The bool flag technique can be used for all variable types.

                            Example.

                            if (myBool)
                            {
                            double myDouble = Close[0];
                            myBool = false;
                            }
                            Last edited by NinjaTrader_RyanM1; 12-28-2010, 12:29 PM.
                            Ryan M.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by chbruno, 04-24-2024, 04:10 PM
                            4 responses
                            50 views
                            0 likes
                            Last Post chbruno
                            by chbruno
                             
                            Started by TraderG23, 12-08-2023, 07:56 AM
                            10 responses
                            398 views
                            1 like
                            Last Post beobast
                            by beobast
                             
                            Started by lorem, Yesterday, 09:18 AM
                            5 responses
                            20 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by WHICKED, Today, 12:56 PM
                            2 responses
                            15 views
                            0 likes
                            Last Post WHICKED
                            by WHICKED
                             
                            Started by Felix Reichert, Today, 02:12 PM
                            0 responses
                            4 views
                            0 likes
                            Last Post Felix Reichert  
                            Working...
                            X