Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

config Stoploss and target profit

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

    config Stoploss and target profit

    Hi Everyone

    Can someone help me set this up?

    How do I set a stoploss to an order in high or low to one depending on the case in the previous bar? without oco id problem for ninjatrader 8

    And also how do I set a targetprofit for an order with a 2 to 1 percentage ratio of the stoploss?

    thanks...

    #2
    Hello voltlon,

    Thanks for your post.

    Are you working with the Strategy Builder or are you coding directly in Ninjascript?

    Comment


      #3
      Hi Paul

      Thanks for answering, I made the strategy with builder and then unlock code for edit, Im not expert for coding.
      I try to search the forum to find the codes I need but it gives me errors

      for now i have this

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if ((Close[1] <= Bollinger1.Lower[1])
      && (Close[0] >= Bollinger1.Lower[0])
      && (Low[1] <= Bollinger2.Lower[1])
      && (Low[0] <= Bollinger1.Lower[0]))
      {
      EnterLong(Convert.ToInt32(DefaultQuantity), @"BUY");
      Draw.VerticalLine(this, Convert.ToString(CurrentBars[0]) + @" buy1", 0, Brushes.DodgerBlue, DashStyleHelper.DashDotDot, 2);
      }
      else if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("BUY", CalculationMode.Price, Low[0], false);
      }
      // Set 2
      if ((Close[1] >= Bollinger1.Upper[1])
      && (Close[0] <= Bollinger1.Upper[0])
      && (High[1] >= Bollinger2.Upper[1])
      && (High[0] >= Bollinger1.Upper[0]))
      {
      EnterShort(Convert.ToInt32(DefaultQuantity), @"SELL");
      Draw.VerticalLine(this, Convert.ToString(CurrentBars[0]) + @" sell1", 0, Brushes.OrangeRed, DashStyleHelper.DashDotDot, 2);
      }
      else if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("SELL", CalculationMode.Price, High[0], false);
      }

      }


      }

      Comment


        #4
        Hello voltlon,

        Thanks for your reply.

        As you have unlocked the code then we will work from a Ninjascript perspective.

        What specific errors do you get?

        Are you running the strategy with Calculate.OnBarClose, Calculate.OnEachtick, Calculate.OnPriceChange?



        Comment


          #5
          Hi Paul,

          Originally posted by NinjaTrader_PaulH View Post
          Hello voltlon,

          Thanks for your reply.

          As you have unlocked the code then we will work from a Ninjascript perspective.

          What specific errors do you get?
          Click image for larger version

Name:	ninja.png
Views:	194
Size:	17.9 KB
ID:	1118735





























          errors are not all the time, just sometimes

          Are you running the strategy with Calculate.OnBarClose, Calculate.OnEachtick, Calculate.OnPriceChange?

          Calculate.OnBarClose
          THANKS FOR YOU REPLY

          Comment


            #6
            Hello voltlon,

            Thanks for your reply.

            I suggest including Position.MarketPosition == MarketPosition.Flat as part of the entry conditions and repeat your tests.

            Comment


              #7
              Hi Paul

              I tried what you suggest but I get the same errors

              I don't know if I edited the code correctly

              protected override void OnBarUpdate()
              {
              if (BarsInProgress != 0)
              return;

              if (CurrentBars[0] < 1)
              return;

              // Set 1
              if ((Close[1] <= Bollinger1.Lower[1])
              && (Close[0] >= Bollinger1.Lower[0])
              && (Low[1] <= Bollinger2.Lower[1])
              && (Low[0] <= Bollinger1.Lower[0]))
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              EnterLong(Convert.ToInt32(DefaultQuantity), @"BUY");
              Draw.VerticalLine(this, Convert.ToString(CurrentBars[0]) + @" buy1", 0, Brushes.DodgerBlue, DashStyleHelper.DashDotDot, 2);
              SetStopLoss("BUY", CalculationMode.Price, Low[0], false);
              }
              // Set 2
              if ((Close[1] >= Bollinger1.Upper[1])
              && (Close[0] <= Bollinger1.Upper[0])
              && (High[1] >= Bollinger2.Upper[1])
              && (High[0] >= Bollinger1.Upper[0]))
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              EnterShort(Convert.ToInt32(DefaultQuantity), @"SELL");
              Draw.VerticalLine(this, Convert.ToString(CurrentBars[0]) + @" sell1", 0, Brushes.OrangeRed, DashStyleHelper.DashDotDot, 2);
              SetStopLoss("SELL", CalculationMode.Price, High[0], false);
              }

              }
              Click image for larger version

Name:	ninja.png
Views:	221
Size:	222.6 KB
ID:	1118919

              thanks....

              Comment


                #8
                Hello voltlon,

                Thanks for your reply.

                When you use the "set" methods (SetStopLoss, SetProfitTarget, etc.) it is critical that these be set to a value that is correct for the intended entry and before the actual entry. What is happening is that your entry is getting filled and the stop values are applied after the entry. However, even if you move the code line above the entry line (so that it executes first) the issue will still occur because the order is being filled on a new bar when the new bar price is already below (or above) the stop being used and that causes the error of the inappropriate price exit order.

                You would need to set your exit orders farther away to ensure they are farther away from the current price. You might want to consider using the SetStopLoss with calculationMode.Ticks and setting a value such as 10 ticks (or your preferred number of ticks): SetStopLoss(CalculationMode.Ticks, 10); In this way, every entry will have the stop automatically set 10 ticks away from the actual entry price. You can, if you wish then adjust the stop closer to price as long as you do not move it to an inappropriate price level.

                Comment


                  #9
                  Hi Paul

                  so, how do I make the stop loss calculated in ticks with a value of 1 tick in a previous bar? thanks

                  Comment


                    #10
                    Hello voltlon,

                    Thanks for your reply.

                    My suggestion to you was to use CalculationMode.Ticks and set a specific value (not tied to a bars value) for your "initial" stop-loss that would not be rejected. After the entry order has filled and you are in a position, then resubmit the stoploss, using CalculationMode.Price at a price value that works for you.

                    For example:

                    if (your entry conditions)
                    {
                    SetStopLoss(calcualtionmode.Ticks, 10); // set initial stop to 10 ticks
                    EnterLong(Convert.ToInt32(DefaultQuantity), @"BUY");
                    }

                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                    if (Low[0] > (Low[1] - 1 *TickSize)) // only adjust if low of the current bar is higher
                    {
                    SetStopLoss(CalculationMode.Price, Low[1] - 1 * tickSize);
                    }
                    }

                    Comment


                      #11
                      Hi Paul

                      I already understood your suggestion, and it works fine, thanks

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      81 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      150 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      79 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      52 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      59 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X