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

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
        BertrandNinjaTrader Customer Service

        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
            BertrandNinjaTrader Customer Service

            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 joselube001, 05-10-2024, 12:17 PM
              6 responses
              27 views
              0 likes
              Last Post joselube001  
              Started by bigc0220, 09-18-2018, 09:16 AM
              6 responses
              2,579 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by lorem, 04-25-2024, 09:18 AM
              18 responses
              78 views
              0 likes
              Last Post lorem
              by lorem
               
              Started by DawnTreader, 05-08-2024, 05:58 PM
              21 responses
              81 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by doihaveto13, Today, 12:46 PM
              2 responses
              4 views
              0 likes
              Last Post doihaveto13  
              Working...
              X