Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

stoploss modification x 2

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

    stoploss modification x 2

    Hello,
    In the stoploss area of my strategy i have used a SetStopLoss template example with allow modification to breakeven. Plus i have made some of my own adjustments to suit.

    I.e. Once the price is greater than +30 ticks, set stop loss to breakeven + 5 ticks. This is fine, it works OK.

    HOWEVER,,, i would like some advice on the code logic needed to do a second modification on the stoploss.

    I.e. Once the price is greater than +60ticks, set stop loss to breakeven + 20 ticks.

    Here is my code, displaying, If going long:

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

    // If a long position is open, allow for stop loss modification to breakeven
    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+30 ticks, set stop loss to breakeven + 5 ticks
    if (Close[0] > Position.AvgPrice + 30 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice +
    5 * TickSize);
    }
    }

    (this section below does not work in addition with the above stoploss statemets....?)

    // If a long position is open, allow for stop loss modification to breakeven
    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+60 ticks, set stop loss to breakeven + 20 ticks
    if (Close[0] > Position.AvgPrice + 60 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice +
    20 * TickSize);
    }
    }

    FYI. i am feeling my way through this code language for the first time ever this week, so please excuse if i dont use correct code terminology!

    Steven

    #2
    steven4,

    You should tier it in the reverse direction.

    Pseudo-code
    Code:
    if (price > 60 ticks)
        move stoploss BE+20ticks
    else if price > 30 ticks
        move stop loss BE+5 ticks
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thank you,

      I am still stuck with this, please review my code.
      You will see, as the statements are set up it will not allow both stoploss criteria to work for the single Long trade.

      (p.s. is there a better way to cut/paste my code into this support forum?)

      // RESET the stop loss to the original value when all positions are closed
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(
      "", CalculationMode.Percent, 0.002, false);
      }

      // If a LONG position is open, allow for stop loss modification to breakeven
      elseif (Position.MarketPosition == MarketPosition.Long)
      {
      if (Close[0] > Position.AvgPrice + 60 * TickSize);
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice +
      20 * TickSize);
      }
      if(Close[0] > Position.AvgPrice + 30 * TickSize);
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice +
      5 * TickSize);
      }
      }

      // If a SHORT position is open, allow for stop loss modification to breakeven
      elseif (Position.MarketPosition == MarketPosition.Short)
      {
      if (Close[0] < Position.AvgPrice - 60 * TickSize)
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice -
      20 * TickSize);
      }
      if(Close[0] < Position.AvgPrice - 30 * TickSize);
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice -
      5 * TickSize);
      }
      }

      Comment


        #4
        steven4, you could wrap code tags around it to improve it's readability -

        Code:
         
        myCode looks nicer with this code tags
        I don't a particular issue with your code, for which symbol and connection are you testing this?

        You may need to include Print statements to check your code reaches the stop loss modification sections as intended -



        Also TraceOrders are very helpful to debug the order behavior of your strategy - http://www.ninjatrader-support2.com/...ead.php?t=3627

        Comment


          #5
          hey

          I am trading FDAX 09-09, with zenfire connection.
          I can try debug but i dont understand the language...yet.

          Comment


            #6
            Yes, it will take some time to get accustomed with the 'lingo' used - I would suggest you move ahead piecewise, trying to break the strategy in simple parts and then make sure those work as you would expect before adding another layer of complexity. If you like, I can let this run on my end as well, just attach it here as zip or send to us support at ninjatrader dot com Attn Bertrand

            Comment


              #7
              Originally posted by steven4 View Post
              Thank you,

              if (Close[0] > Position.AvgPrice + 60 * TickSize);
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice +
              20 * TickSize);
              }
              if(Close[0] > Position.AvgPrice + 30 * TickSize);
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice +
              5 * TickSize);
              }
              Steven,

              In the above code BOTH tests will be true, and so both SetStopLosses are being executed, the second overwriting the first. Try changing the second of these "if"s to an "else if", as in Josh's original post.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              637 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              366 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              571 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X