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

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:	162
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:	187
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


                  #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);
                    }
                    }
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Paul

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

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by usazencort, Today, 01:16 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post usazencort  
                      Started by kaywai, 09-01-2023, 08:44 PM
                      5 responses
                      603 views
                      0 likes
                      Last Post NinjaTrader_Jason  
                      Started by xiinteractive, 04-09-2024, 08:08 AM
                      6 responses
                      22 views
                      0 likes
                      Last Post xiinteractive  
                      Started by Pattontje, Yesterday, 02:10 PM
                      2 responses
                      21 views
                      0 likes
                      Last Post Pattontje  
                      Started by flybuzz, 04-21-2024, 04:07 PM
                      17 responses
                      230 views
                      0 likes
                      Last Post TradingLoss  
                      Working...
                      X