Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with trailing stops

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

    Help with trailing stops

    Hello,
    I'm a newbie in programming. I'm currently working on automating a strategy. I'm stuck on the trailing stop part of it.
    I do not have a hard profit target, but what I do have is a trailing stop being placed, when a certain condition occurs.

    Here is my code snippet:

    if ((Position.MarketPosition == MarketPosition.Long)

    &&(CrossBelow(Stochastics(BarsArray[1],3, 5, 2).K, 65, 1))
    &&(Falling(Stochastics(BarsArray[2],3, 5, 2).K) == true))

    {
    BarColor = Color.Gold;
    SetStopLoss(CalculationMode.Price, Low[0] - 5 * TickSize);
    }

    It basically paints a candle a certain color(simply for visual aid, while i'm working on the strategy) and places a stop loss 5 ticks under the low. However, I want the stop loss to move up with each candle after it has been placed. What currently happens, the stop gets placed and just sits there until the next similar condition occurs in the trend. Only then it moves. Does anyone have any ideas on how i can make it move up with each candle?

    #2
    Hi Mikethelen,

    Rather than using SetStopLoss(), you will want to use SetTrailStop(). SetTrailStop() includes the same overloads as SetStopLoss(), so you can pass in the same values that you are currently using. Keep in mind that if you are calling SetStopLoss() anywhere else on the same trade, it could conflict with this particular call to SetTrailStop().

    For more information on this method, please see the help guide article below:



    Please let me know if I can assist further.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      I tried that and it only enters one trade and rides it for a week, while ignoring all the conditions.I do not have a stop loss anywhere else. I have a trail stop in the initialize method in order to reset it after flat.

      Comment


        #4
        Hi Mikethelen,

        The Initialize() method is only called once when a strategy is initiated, so you will want to move your call to SetTrailStop() into OnBarUpdate(). To reset the stop loss when your account is flat, you will want to place the logic within the following condition:

        if ((Position.MarketPosition == MarketPosition.Flat)

        To give you some insight into why the strategy is only placing one trade, I would need to see the logic used to enter trades. It appears that the code snippet you provided is only setting the stop loss when the condition is true.

        For further reference, please see the forum post below:



        Please let me know if I can assist further.
        Dave I.NinjaTrader Product Management

        Comment


          #5
          I moved my trailing stop to OnBarUpdate()
          if (Position.MarketPosition == MarketPosition.Flat)

          {
          SetTrailStop(CalculationMode.Ticks, stoplossticks);
          }

          Now the trades execute, but its not trailing under the low of the signal candle. I realize that if i wish to trail below the low, i need to use "price" instead of ticks in the above code, but the trades only execute when i put "ticks" in that place.

          Comment


            #6
            Hi Mikethelen,

            Just to confirm, when you use CalculationMode.Price, are you still passing in stoplossticks as the second parameter? If so, the strategy will attempt to place the stop at a price equal to the stoplossticks value. That will work fine if the value of stoplossticks happens to be the low price of the previous candle, but it could cause unexpected behavior if you have it set to a tick value intended for us with CalculationMode.Ticks. Can you explain how you currently set the value of stoplossticks?

            To find the price value of the previous bar, you can use Low[1].

            Please let me know if I can assist further.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              I really appreciate that you are taking your time helping me with this situation.
              My trailing stop looks like this: SetTrailStop(CalculationMode.Ticks, Low[0] - 10 * TickSize);
              when I'm in the trade. My stoplossticks variable is set to 20. Can i program a private int variable to something like: Low[0] - 5 ? Because with that variable stored, i guess i can place "price" instead of "ticks" in the calculation mode.

              Comment


                #8
                It is my pleasure to help. Yes, it would be possible to do something like that, then pass that variable into SetTrailStop() using CalculationMode.Price. Just make sure that you are updating the variable in OnBarUpdate(), so that it recalculates using each new completed bar. You can see a great example of resetting the Set() order at the link below:



                Please let me know if I can assist further.
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  I've tried everything last night and I could not make my trailing stops move under the low of the signal candle. It only moves if I have "ticks" as a calculation mode,but then it only trails by ticks and not the low . I don't need a trailstop to breakeven. I just need a trail stop that activates after a certain signal and moves up with the price.

                  Comment


                    #10
                    Hi Mikethelen,

                    I will be happy to take a look and see what may be causing the issue, but at this point I believe it would best if I could see your entire strategy. Please write in to platformsupport [at] ninjatrader [dot] com, and write "Attn: Dave, #1214729" either in the subject line or the body of the email. Please attach your strategy to the email, and I will take a look further.

                    To export your strategy, click File > Utilities > Export NinjaScript. Locate your strategy in the left pane of the window that appears, then click ">" to add it to the export list. Enter a name for the exported file in the "File Name" field, then click "Export," and attach the exported file to your email.

                    I look forward to your reply.
                    Dave I.NinjaTrader Product Management

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Stanfillirenfro, Today, 07:23 AM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by FitSpressoHonest, Today, 09:14 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post FitSpressoHonest  
                    Started by Davide999, 05-18-2023, 03:55 AM
                    4 responses
                    557 views
                    1 like
                    Last Post kcwasher  
                    Started by rexsole, Today, 08:39 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Started by cmtjoancolmenero, Yesterday, 03:58 PM
                    6 responses
                    29 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Working...
                    X