• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting a Trailing Stop?

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

    Setting a Trailing Stop?

    1)
    Hi there, how do I set an initial stop, and a trailing stop? Say I want to enter an order with a 5 tick stop loss. Then, for every two ticks in profit, I want to set a trailing stop 3 ticks behind my current position. I would think the code would go something like?

    SetStopLoss(CalculationMode.Ticks, 5);
    SetTrailStop("", CalculationMode.Ticks, 3, false);

    However, I can use both the SetStopLoss, and the SetTrailStop together. How to do? Thanks

    #2
    Hello timmbbo,

    Thanks for your post.

    Correct, in the managed approach you cannot use the SetTrailStop and SetStopLoss methods on the same order.

    What you could do in this case is to exclusively use SetStopLoss(), initially setting it to 5 ticks and then later by comparing the current price to entry price adjusting the value of the SetStopLoss through your code. Note that you will need to use CalculateOnBarClose = false so that you are working with the current price

    Once in a position, you can obtain the entry price from Position.AvgPrice.
    For example if you were long:

    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] >= Position.AvgPrice + 2 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Close[0] - 3 * TickSize);// set 3 ticks behind current price
    }
    }

    Note I have shown a very simple starting example, for it to work effectively you would need to use bools so that the trail stop does not go backwards when price reverses and so that the setstop is continually adjusted up as price continues up in the matter you desire.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul, thanks - is helpful. Setting CalculateOnBarClose to false, as you recommend, then has other consequences for my code. I do have a section of code that I want executed upon bar close. So, it then appears I need one section of code executed upon bar close, and another section of code executed upon tick.

      How do I accommodate executing code both upon Bar Close and Upon Tick? Thanks

      Comment


        #4
        Hello timmbbo,

        Thanks for your reply.

        To have a code section only run once per bar, you can use the system bool FirstTickOfBar

        if (FirstTickOfBar)
        {
        // code processed here only once per bar
        }
        //code here is executed on every tick


        Reference: http://ninjatrader.com/support/helpG...ttickofbar.htm

        For further study on the subject: http://ninjatrader.com/support/forum...ad.php?t=19387
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thanks Paul. I have my little script now executing properly.

          Comment


            #6
            Paul, I have a follow up on this. My code is:

            EnterLong(1);
            Print(Position.MarketPosition);
            Print(MarketPosition.Long);
            if (Position.MarketPosition == MarketPosition.Long) {
            ....

            The output is:
            Flat
            Long

            So, my If clause never hits, even though I'm successfully in a Long position. I can see the long position executed on the chart, so I know it's there. Suggestions? Thanks

            Comment


              #7
              Hello timmbbo,

              Thanks for your question.

              I believe the answer is related to the timing of the asynchronous events which I mention in your other thread here: http://ninjatrader.com/support/forum...ad.php?t=95788
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Yep, those two threads are related. Thanks, Paul.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Herrwolf1, Today, 05:11 AM
                1 response
                5 views
                0 likes
                Last Post NT-Roland  
                Started by Knortzsch, Today, 05:26 AM
                0 responses
                6 views
                0 likes
                Last Post Knortzsch  
                Started by aitropap, Today, 05:19 AM
                0 responses
                2 views
                0 likes
                Last Post aitropap  
                Started by authenticx, Yesterday, 03:51 PM
                1 response
                29 views
                0 likes
                Last Post NT-Roland  
                Started by hattieaguilar, Today, 02:58 AM
                0 responses
                5 views
                0 likes
                Last Post hattieaguilar  
                Working...
                X