Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto trailing stop for a stragegy

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

    Auto trailing stop for a stragegy

    Hi, I am attempting to set up a trailing stop for a strategy, it appears that I need to enter some code to do this, I am not a programmer, all I want to do have a 35 pip trailing stop following the current price of the pair that is being traded, does anyone out there have a coded solution?

    #2
    Strazzman, to add a simple trailing stop to your strategy takes just one line of code in the Initialize() section:
    Code:
    protected override void Initialize()
    {
        SetTrailStop(CalculationMode.Ticks, 35);
    }
    To add that code, you can go to Tools -> Edit NinjaScript -> Strategy and then select your strategy.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Trailing stop question

      Thanks for the reply Austin, however now I am wondering where the initialize section you are referring to is found?. Have done tools > edit ninjascript > strategy > selected strategy > ok.
      Now do I unlock code on the opening page or next onto the stops and targets page?

      Comment


        #4
        You don't have to unlock it and enter it manually, you could also add the trailing stop on the Stops and Targets page in the wizard.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi,

          Extending a little bit the topic:

          1) How can I make the stop loss to jump to breakeven + 1 tick when the price reaches a certain trigger value (so many ticks above/below the entry price) in the direction of my trade?

          2) Then, when the price reaches a 2nd trriger, the stop should become a trailing stop (by a certain no of ticks/amount/%).

          3) Then, when the price reaches a 3rd trigger, the trailing stop should tighten.

          I would like to do all this in the Strategy Wizard, without un-locking the code. This is important.

          Thx.

          Comment


            #6
            BBzDan,

            Unfortunately you can only do what you want through unlocked code with the programming techniques demonstrated in this reference sample: http://www.ninjatrader-support2.com/...ead.php?t=3222
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Breakeven and 2-tier trailing stops

              Hello,
              I've been trying to develop a breakeven and 2-tier trailing stop strategy, as mentioned in a previous post, using various suggestions for developing the code found in the forum and in the manual (as suggested), but there is something wrong with my code. I've done 2 versions, one using if and one using switch. Both behave in the same way:
              • they place correctly the stop and target. However,
              • the stop doesn't move to breakeven + 2 ticks (as programmed), but jumps above this, to something else
              • the stop remains there, it doesn't move/trail anymore, even if the price moves in the direction of the trade well beyond the targets set to trigger the movement of 1st trailing stop and the 2nd, tightened trailing stop.
              Can someone with more experience in C# than me (which I have almost none) take a look at the attached files and tell me what's wrong, and correct them?

              The codes have some extra parameters/variables not used in the program, just ignore them.

              As mentioned, I did look in the manual, and at the SamplePriceModification.cs, and other examples found in the forum, trying to follow what's there.

              Thank you,
              Dan
              Attached Files

              Comment


                #8
                BBzDan, please print out your Stop values used to doublecheck the calcs are working out as expected. Next please ensure TraceOrders is turned on so you can debug the strategy's order behavior with the output window opend.



                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Bertrand,
                  Thank you for prompt reply and suggestion, I'll try this.

                  Dan

                  Comment


                    #10
                    OK, made some progress: managed to bring the stop loss to breakeven + n ticks, and make it trail a little. Now, I have a problem with the trailing condition.

                    1st question:



                    Is it possible to get the current price, or the price of last trade, instead of Bid or Ask, and use it in an if statement? I've been trying something like this:
                    1. To bring the stop loss to BE + n ticks
                    if (GetCurrentBid() > Position.AvgPrice + breakEvenTrigger * TickSize
                    && initialBreakEven == 0.000)
                    {
                    initialBreakEven = Position.AvgPrice + plusBreakEven * TickSize;
                    bid = GetCurrentBid();
                    SetStopLoss("Long", CalculationMode.Price,initialBreakEven, false);
                    }

                    which is OK.

                    2. To start trailing, once at BE+n and after the trailing trigger is reached (trailing==1, which is a flag showing the trailing trigger was reached):

                    if (GetCurrentBid() > bid
                    && trailing == 1)
                    {
                    trail = trail + trailStep * TickSize;
                    trail = trail == 0 ? initialBreakEven + trailStep * TickSize : trail + trailStep * TickSize;
                    SetStopLoss(
                    "Long", CalculationMode.Price, trail, false);
                    }

                    This doesn't work well, because it uses bid price instead of actual last price. Also, I do not want to compare it with the high, or close of the bar, (as I use range bars), but with the price of the previous tick. Thus, how do I get the current price, and the price of the last tick (the previous price)?

                    2nd question:

                    Is it possible to convert a stop loss to a trailing stop (after it reached BE+n ticks)? If YES, how?

                    Thank you.

                    Comment


                      #11
                      Dan, great progress - for the last price just use for example Close[0] in combination with CalculateOnBarClose = false.

                      It's not possible to convert it - you continue to adjust the stop loss level as the trade moves into more and more profit, thus you code in at what steps you like to move it by calling SetStopLoss again to adjust the placed value.

                      You may want to check into our sharing where some good scripts have been published on implmenting a trail stop logic.

                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Bertrand,
                        Thx for comments and for quick reply.
                        Regarding Close(0) in combination with CalculateOnBarClose = false, the problem is that the entry conditions are based on calculating the indicator I use at the close of the bar, not tick-by-tick. Is it possible to have two regions, one with CalculateOnBarClose=true (for entry conditions) and one with ...=false, for stop trailing? If yes, how?

                        I'll check the file sharing section, see what's there, thx.

                        Comment


                          #13
                          You can have two regions. Use CalculateOnBarClose = false and then filter the regions where you want it to behave like true with this:

                          Code:
                          if (FirstTickOfBar)
                          Then all of your [] indexing should be 1 back. So [0] would be [1]. The opening of a new bar (a.k.a. first tick) is the same event as the closing of the previous bar so this is the exact same time the code would have processed with CalculateOnBarClose = true.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            OK, thank you, I'll try it.

                            Dan

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by adolfosanz44, Today, 01:53 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post adolfosanz44  
                            Started by alphatango, 09-16-2024, 02:09 PM
                            2 responses
                            30 views
                            0 likes
                            Last Post alphatango  
                            Started by zehua, Today, 11:03 AM
                            1 response
                            11 views
                            0 likes
                            Last Post TradeSaber  
                            Started by Estherwanger, Today, 01:04 PM
                            1 response
                            9 views
                            0 likes
                            Last Post marcelfiedler010  
                            Started by martinsedward28, Today, 11:43 AM
                            0 responses
                            7 views
                            0 likes
                            Last Post martinsedward28  
                            Working...
                            X