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

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 TheWhiteDragon, 01-21-2019, 12:44 PM
              4 responses
              541 views
              0 likes
              Last Post PaulMohn  
              Started by GLFX005, Today, 03:23 AM
              0 responses
              2 views
              0 likes
              Last Post GLFX005
              by GLFX005
               
              Started by XXtrader, Yesterday, 11:30 PM
              2 responses
              11 views
              0 likes
              Last Post XXtrader  
              Started by Waxavi, Today, 02:10 AM
              0 responses
              7 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Started by TradeForge, Today, 02:09 AM
              0 responses
              14 views
              0 likes
              Last Post TradeForge  
              Working...
              X