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, Today, 01:49 PM
                0 responses
                8 views
                0 likes
                Last Post rtwave
                by rtwave
                 
                Started by papaoutai, Today, 08:56 AM
                0 responses
                22 views
                0 likes
                Last Post papaoutai  
                Started by jdspwe, Today, 05:22 AM
                0 responses
                13 views
                0 likes
                Last Post jdspwe
                by jdspwe
                 
                Started by JeremuFemstu, Today, 04:49 AM
                0 responses
                16 views
                0 likes
                Last Post JeremuFemstu  
                Started by pjsmith, Today, 04:11 AM
                1 response
                22 views
                0 likes
                Last Post Green Lobster CBD  
                Working...
                X