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

Trailing Stop Not Trailing

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

    Trailing Stop Not Trailing

    Hello

    I am working on a little strategy and the trailing stop does not appear to be trailing.


    Here are my variables
    Code:
            private string direction = "Long"; // Long = Only take Buy trades; opposite is true for Short
            private bool trade = false; // True = Take a trade
            private double support; // Support price level
            private double resistance; // Resistance price level
    Initial stop loss levels and trailing stops are found in Initialize()
    Code:
                SetStopLoss("Buy Long", CalculationMode.Price, Support, false);
                SetStopLoss("Sell Short", CalculationMode.Price, Resistance, false);
                SetTrailStop("Buy Long", CalculationMode.Price, Support, false);
                SetTrailStop("Sell Short", CalculationMode.Price, Resistance, false);
    
                CalculateOnBarClose = true; // Will be change to false when code is finished
    Now, in OnBarUpdate() I start out defining what "support" is (only testing the short side right now):
    Code:
    			if ((Low[5] > Low[3] 
    				&& Low[4] > Low[3] 
    				&& Low[2] > Low[3] 
    				&& Low[1] > Low[3]) 
    				|| (Low[5] > Low[3] 
    				&& Low[4] > Low[3] 
    				&& Low[2] == Low[3] 
    				&& Low[1] > Low[3]))
    			{
    				Support = Low[3];
    				Print(Time[0] + " Support = " + Support);
    Also in OnBarUpdate() I define how a short trade should be entered
    Code:
                if (Trade == true
                    && Low[0] < Support
    				&& Direction == "Short")
                {
                    EnterShort(DefaultQuantity, "Sell Short");
    				Print(Time[0] + " " + "Short Entry. Stop = ");
    In the above code, my expectation was that every time a new "resistance" was created, and I am in a Short trade, then the stop would be moved to that price level.

    At this point, though, the stop just remains in place (it never moves from the initial stop placement). I have checked the Output Window and it shows that it is storing new "resistance" levels - but just not moving the stop to those levels.

    Could you please help me with what is going on in order to get the stop to trail correctly?

    Thank you very much for your help!

    #2
    Hello jg123,

    SetStopLoss and SetTrailStop cannot be used together.

    From the help guide:
    "The SetTrailStop() method can NOT be used concurrently with the SetStopLoss() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names."

    http://ninjatrader.com/support/helpG...ttrailstop.htm


    You have to change your code to use one or the other. You cannot use both.
    You can use logic to move your SetStopLoss tick for tick and code in trailing logic yourself.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much, Chelsea. I think that I knew that at one point but had forgotten.

      This was something that was generated by the Wizard. Of course I put in the parameters, but it generated it. Maybe a feature for a future release (9.0 or something??) would be that the Wizard can "know" these types of rules and let you know, with a reference to the article, that it is not possible to do this type of action.

      Also, I am reading the notes on the trailing stop that you sent to me and noticed this sentence at the end:

      A trail stop order is automatically cancelled if the managing position is closed by another strategy generated exit order
      Will the trailing stop also be cancelled if the trade is closed manually?

      I do not actually need my stop loss to trail tick by tick. In fact, I only need it to check conditions at the end of every bar in order to determine if it should move closer to the price. It could go several bars before needing to move and then wait for several bars again before needing to move.
      Last edited by jg123; 08-06-2015, 04:25 AM.

      Comment


        #4
        Hello jg123,

        No, this will not work for a manually exited trade. This is because the trade from the strategy is not actually exited. The account position and strategy position are separate. Placing a manual order to change the account position to flat does not change the strategy position to flat.

        So when you place a manual order to exit a trade by a strategy, it doesn't actually exit the trade in the strategy itself.

        No manual orders will affect the position or behavior of the strategy.

        Below are links to the help guide on account position vs strategy position and a link to a video showing the same thing.

        Help guide - http://ninjatrader.com/support/helpG..._account_p.htm

        Video - https://www.youtube.com/watch?v=US9c...A140D7&index=7


        Also, while the wizard is useful for providing the building blocks, the wizard is not meant to write the logic for you. The issue with the stop loss and trail stop is a logic issue. There are other logic issues that can result through the condition builder.
        For example, try exiting short and entering long in the same condition set. This will either result in an overfill or will result in the position becoming double in the opposite direction. Either way it results in unwanted behavior. This means that it is your responsibility to code a strategy that works. Think of the Strategy Wizard as a starting point similar to the Indicator Wizard. NinjaTrader will not be able to prevent every situation that can result because of logic issues and still provide the flexibility for advanced programmers.

        Also, I am including an example of something similar to what you have suggested. One made with the strategy wizard one made custom coded without the wizard.
        Attached Files
        Last edited by NinjaTrader_ChelseaB; 08-06-2015, 08:17 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by falcondroneus, Today, 01:15 AM
        0 responses
        2 views
        0 likes
        Last Post falcondroneus  
        Started by Vikuno1, 11-28-2023, 11:42 PM
        15 responses
        357 views
        1 like
        Last Post PaulMohn  
        Started by PaulMohn, Yesterday, 02:06 AM
        3 responses
        12 views
        0 likes
        Last Post PaulMohn  
        Started by gertrudeabrams, Today, 12:33 AM
        0 responses
        3 views
        0 likes
        Last Post gertrudeabrams  
        Started by NJA_MC, 01-03-2021, 10:34 PM
        11 responses
        150 views
        0 likes
        Last Post wzgy0920  
        Working...
        X