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?

    Paul H.NinjaTrader Customer Service

    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?



        Paul H.NinjaTrader Customer Service

        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:	12
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.

            Paul H.NinjaTrader Customer Service

            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:	17
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.

                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by traderharley, Today, 08:46 AM
                0 responses
                4 views
                0 likes
                Last Post traderharley  
                Started by NinjaCustomer, Today, 08:22 AM
                0 responses
                5 views
                0 likes
                Last Post NinjaCustomer  
                Started by n8dogg59, Today, 07:58 AM
                2 responses
                9 views
                0 likes
                Last Post n8dogg59  
                Started by CoffeeOutlaw, Yesterday, 07:33 PM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Kate  
                Started by localappleseed, Yesterday, 08:16 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X