• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move stop loss from breakeven to lock in profit

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

    Move stop loss from breakeven to lock in profit

    Hello,

    I have a strategy that moves the stoploss to position.averageprice once price moves 4 ticks in my favor. I would like the stoploss to then move to lock in 3 ticks of profit once price moves 8 ticks in my favor, yet I can't seem to figure out how. Here is my break even logic so far. Can I simply duplicate this logic and use different number to accomplish what I want?

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

    // 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 + 3 ticks, set stop loss to breakeven
    if (Close[0] > Position.AveragePrice + 3 * TickSize)
    {
    Print(Time[0].ToString()+ @"This should be changing SL");
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }
    Thank you for any info you may have,
    Chris

    #2
    Hello Chris,

    Updating the stop loss again with SetStopLoss() would be valid. Keep in mind, when writing the logic, you should use if/else statements to prevent your strategy from moving the stop loss to the breakeven point after you have the strategy move the stop loss to +3.

    For example:
    Code:
    if (Close[0] > Position.AveragePrice + 8 * TickSize)
    {
        // Move to +3
    }
    else if (Close[0] > Position.AveragePrice + 3 * TickSize)
    {
        // Move to BE
    }
    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you very much!

      I will add this in and test it out.

      Chris

      Comment


        #4
        Hello Chris,

        Is the else if statement you gave an example of correct? When I use it, the strategy glitches and rejects orders, causing the strategy to shut off. When I don't use it, the stop loss moves but moves back to breakeven instead of staying at 4 ticks profit. Heres the code I have:

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

        else if (Close[0] > Position.AveragePrice + 4 * TickSize);
        {
        SetStopLoss(CalculationMode.Price, Position.AveragePrice + 4 * TickSize);
        }

        Comment


          #5
          Look carefully, you're using,

          Position.AveragePrice + 4 * TickSize

          in both calls to SetStopLoss.

          Comment


            #6
            So I changed it to this and I am still having the same problem:

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

            else if (Close[0] > Position.AveragePrice + 4 * TickSize);
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
            }

            Comment


              #7
              Hello Chris,

              You will also want to check that the Strategy is in a position before modifying a Stop Loss and the stop loss is always "reset" when in a flat position.

              For example:
              Code:
              if (Position.MarketPosition == MarketPosition.Flat)
              	SetStopLoss(CalculationMode.Ticks, 10);
              
              if (Position.MarketPosition == MarketPosition.Flat && Close[0] > Close[1])
              	EnterLong(1, "Signal");
              else if ((Position.MarketPosition == MarketPosition.Long))
              {
              	if((Close[0] > Position.AveragePrice + 8 * TickSize))
              	{
              		Print("Move to +3");
              		SetStopLoss(CalculationMode.Price, Position.AveragePrice + 3 * TickSize);
              	}
              	else if(Close[0] > Position.AveragePrice + 4 * TickSize)
              	{
              		Print("Move to BE");
              		SetStopLoss(CalculationMode.Price, Position.AveragePrice);
              	}
              }
              I may also suggest to enable TraceOrders to troubleshoot issues with order submissions/modifications. The SetStopLoss documentation also has some important notes to consider when using.

              TraceOrders - https://ninjatrader.com/support/foru...ead.php?t=3627

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

              Please let me know if I can be of further help.
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by sergey_z, Today, 02:34 AM
              3 responses
              12 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Started by frankduc, Today, 05:24 AM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Started by shmarola, Today, 01:01 AM
              0 responses
              4 views
              0 likes
              Last Post shmarola  
              Started by CriticalMind, 11-29-2015, 12:39 PM
              3 responses
              1,323 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by novel168, 06-14-2019, 09:20 PM
              2 responses
              10 views
              0 likes
              Last Post novel168  
              Working...
              X