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

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 rtwave, 04-12-2024, 09:30 AM
                2 responses
                20 views
                0 likes
                Last Post rtwave
                by rtwave
                 
                Started by tsantospinto, 04-12-2024, 07:04 PM
                5 responses
                67 views
                0 likes
                Last Post tsantospinto  
                Started by cre8able, Today, 03:20 PM
                0 responses
                6 views
                0 likes
                Last Post cre8able  
                Started by Fran888, 02-16-2024, 10:48 AM
                3 responses
                49 views
                0 likes
                Last Post Sam2515
                by Sam2515
                 
                Started by martin70, 03-24-2023, 04:58 AM
                15 responses
                115 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X