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

Moving StopLoss and forced orders problem

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

    Moving StopLoss and forced orders problem

    Hello,

    I have 2 issues with a strategy of mine.

    1) I have a stop loss set to break even and then follow price. How can I prevent my stop loss from moving back to the previous level if price begins to pull back. Example: If price moves 10 ticks in my favor and my stop loss is set to move to AveragePrice + 4 ticks, if price moves back to 9 ticks profit or less, my stop loss will move back to the previous level, which would be my breakeven.

    2) If I have an order in progress (lets say long position) and the conditions for a short order are met, my strategy will try to force close my long position and immediately open a short position. How can I stop this from happening and let my open position close on its own, then have my strategy wait until the conditions for another order is met? Currently, the strategy will force close and try to immediately open a short order, causing the strategy to crash and close.

    Thank you very much,
    Chris

    #2
    Hello chrisca,

    Thanks for your inquiries.

    1) I have a stop loss set to break even and then follow price. How can I prevent my stop loss from moving back to the previous level if price begins to pull back. Example: If price moves 10 ticks in my favor and my stop loss is set to move to AveragePrice + 4 ticks, if price moves back to 9 ticks profit or less, my stop loss will move back to the previous level, which would be my breakeven.
    The Stop Loss would only move if you have specifically told it to move. If you have a condition that moves the stop loss to BE + 4 after 10 ticks of profit, you can modify the logic so it sets a bool "MoveStopLossOk" to true. When that bool is true, update your stop loss to BE + 4. When you are back to flat, be sure to reset your stop loss so your next entry does not use the previous stop loss value used.

    Please see the SetStopLoss() documentation for more information on using the method properly.

    SetStopLoss() - https://ninjatrader.com/support/help...etstoploss.htm

    2) If I have an order in progress (lets say long position) and the conditions for a short order are met, my strategy will try to force close my long position and immediately open a short position. How can I stop this from happening and let my open position close on its own, then have my strategy wait until the conditions for another order is met? Currently, the strategy will force close and try to immediately open a short order, causing the strategy to crash and close.
    When you are in a position and you call an Entry method for the opposite direction, that entry will close you out of your current position and re-enter you in the opposite position. I may suggest keeping strategy logic separate for long and short entries and running both "long" and "short" strategies on the same instrument to go long and short at the same time, or I may advise writing your strategy with the Internal Order Handling rules in mind. Any other approach would be outside of the Internal Order Handling rules of the Managed Approach and the Unmanged Approach would then need to be used.

    Internal Order Handling rules can be found here - https://ninjatrader.com/support/help...antedPositions

    Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

    Please let me know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim,

      Here is my stop loss logic. All I need is something to prevent the stop loss from moving to a previous level once moved to lock in profit.

      // Resets the stop loss to the original value when all positions are closed
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(CalculationMode.Ticks, Stop);
      }

      // If a long position is open, allow for stop loss modification to breakeven
      else if (Position.MarketPosition == MarketPosition.Long)
      {
      // Once the price is greater than entry price + 4 ticks, set stop loss to breakeven
      if (Close[0] > Position.AveragePrice + 4 * TickSize)
      {
      Print(Time[0].ToString()+ @"This should be changing SL");
      SetStopLoss(CalculationMode.Price, Position.AveragePrice);
      }

      // Once the price is greater than entry price + 10 ticks, move stop loss to 4 ticks profit
      if (Close[0] > Position.AveragePrice + 10 * TickSize)
      {
      Print(Time[0].ToString()+ @"This should be changing SL");
      SetStopLoss(CalculationMode.Price, Position.AveragePrice + 4 * TickSize);
      }

      // Once the price is greater than entry price + 16 ticks, move stop loss to 10 ticks profit
      if (Close[0] > Position.AveragePrice + 16 * TickSize)
      {
      Print(Time[0].ToString()+ @"This should be changing SL");
      SetStopLoss(CalculationMode.Price, Position.AveragePrice + 10 * TickSize);
      }

      // Once the price is greater than entry price + 24 ticks, move stop loss to 18 ticks profit
      if (Close[0] > Position.AveragePrice + 24 * TickSize)
      {
      Print(Time[0].ToString()+ @"This should be changing SL");
      SetStopLoss(CalculationMode.Price, Position.AveragePrice + 18 * TickSize);
      }

      // Once the price is greater than entry price + 28 ticks, move stop loss to 26 ticks profit
      if (Close[0] > Position.AveragePrice + 28 * TickSize)
      {
      Print(Time[0].ToString()+ @"This should be changing SL");
      SetStopLoss(CalculationMode.Price, Position.AveragePrice + 26 * TickSize);
      }
      }
      As for the order handling, If I have the long condition in //Set 1 and short conditions in //Set 2, wouldn't those be considered separated? I am a tad confused with your advice.

      Chris

      Comment


        #4
        Hello chrisca,

        With the code provided you are going to call SetStopLoss() on every bar if you are above BE + 4. You can observe how many times your print is made when the current market price reaches a new price level to move your stop loss. You will need to restrict the logic so you only call SetStopLoss() only when you allow a stop loss to move. My suggestion was to use boolean variables to control the logic for moving the stop loss, so you only move the stop loss once.

        As for order handling, yes, you could separate the logic for long and short entries so you only enter long or short when you are flat, but if you were to mix reversing your position by calling EnterLong() when you are short, for example, you will have to mind the Internal Order Handling rules that are outlined in the help guide.

        If you need to ignore the internal order handling rules, the Unmanaged Approach is available.

        Please let me know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by hurleydood, 09-12-2019, 10:45 AM
        14 responses
        1,096 views
        0 likes
        Last Post Board game geek  
        Started by cre8able, Yesterday, 04:16 PM
        1 response
        16 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cre8able, Yesterday, 04:22 PM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by stafe, 04-15-2024, 08:34 PM
        5 responses
        28 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by StrongLikeBull, Yesterday, 04:05 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X