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

Set Stop Loss at Lowest Bar from Entry

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

    Set Stop Loss at Lowest Bar from Entry

    I would li**** set my stop loss at the lowest price of the 4th bar from entry. My code doesn't seem to work according to the attached photo.

    {
    if (Position.MarketPosition == MarketPosition.Flat)

    signalLong = string.Format("LONG");
    SetProfitTarget(signalLong, CalculationMode.Ticks, Strategy_TakeProfitTicks);
    SetStopLoss(Convert.ToString(LowestBar(Close, 4)), CalculationMode.Price, 0, false);
    EnterLong(signalLong);
    }

    #2
    Hello Tacto,

    Thank you for the post.

    One item that I see here is that you are passing a String of the price as the entry signal and 0 as the price:

    Code:
    SetStopLoss(Convert.ToString(LowestBar(Close, 4)), CalculationMode.Price, 0, false);
    Based on the entry it should instead look like this:

    Code:
    SetStopLoss(signalLong, CalculationMode.Price, LowestBar(Close, 4), false);
    Could you check if that resolves the issue? If not please reply back and we can continue from there. Please also note in your reply what the problem is that you are seeing with the change, such as the incorrect price used or incorrect bar was used.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I used the new code you provided, but still not working. Please see attached photo.

      {
      if (Position.MarketPosition == MarketPosition.Flat)

      signalLong = string.Format("LONG");
      SetProfitTarget(signalLong, CalculationMode.Ticks, Strategy_TakeProfitTicks);
      SetStopLoss(signalLong, CalculationMode.Price, LowestBar(Close, 4), false);
      EnterLong(signalLong);
      }

      I've also used other alternative stop loss codes and still did not work out:
      // SetStopLoss(signalLong, CalculationMode.Price, Low[0] - 2 * TickSize, false);
      // SetStopLoss(signalLong, CalculationMode.Price, Swing(3).SwingLowBar(0, 1, 10), false);
      // SetStopLoss(signalLong, CalculationMode.Price, MIN(Low, 10)[0] - 3 * TickSize, false);

      Comment


        #4
        Hello Tacto,

        From the given image I can only see that the stop was not hit, did you take a look to make sure the stop was actually submitted and at what prices the stop was being set at? That would help to confirm if that was correct price or if something else happened.

        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          I am trying to do this same thing and it wont enter any trades when I use this line of code

          SetStopLoss("", CalculationMode.Ticks, MIN1[1], false);

          Comment


            #6
            Hello jmass13,

            You may need to use a signal name in place of "". From the single line of code you really wouldn't be able to know what the problem is.

            I would suggest checking the signal name and adding that if you had used one with the entry.
            SetStopLoss("MyEntryName", CalculationMode.Ticks, MIN1[1], false);

            You may also want to try TraceOrders to see if the order was ignored for some reason. https://ninjatrader.com/support/help...ub=traceorders

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks for your response. I added a signal name but nothing changed. here is the rest of my code

              IsInstantiatedOnEachOptimizationIteration = true;

              }
              else if (State == State.Configure)
              {
              // SetStopLoss("", CalculationMode.Ticks, LowestBar(Low, 14),false);
              }
              else if (State == State.DataLoaded)
              {
              MACD1 = MACD(Close, 12, 26, 9);
              EMA1 = EMA(Close, 200);
              MIN1 = MIN(Low, 14);
              MACD1.Plots[0].Brush = Brushes.DarkCyan;
              MACD1.Plots[1].Brush = Brushes.Crimson;
              MACD1.Plots[2].Brush = Brushes.DodgerBlue;
              MIN1.Plots[0].Brush = Brushes.DarkCyan;
              AddChartIndicator(MACD1);
              AddChartIndicator(MIN1);
              SetStopLoss("LONG", CalculationMode.Ticks, MIN1[1], false);
              }
              }

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

              if (CurrentBars[0] < 1)
              return;



              // Set 1
              if ((CrossAbove(MACD1.Diff, 0, 1))
              && (MACD1.Avg[0] < 0)
              && (Close[0] > EMA1[0]))
              {
              EnterLong(Convert.ToInt32(DefaultQuantity), "LONG");
              }

              // Set 2
              if (Low[0] < MIN1[1])
              {
              ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
              }

              }
              }
              }

              Comment


                #8
                Hello jmass13,

                You can't use price or indicator values in OnStateChange, that would need to move to OnBarUpdate before your entry.

                SetStopLoss("LONG", CalculationMode.Ticks, MIN1[1], false);
                EnterLong(Convert.ToInt32(DefaultQuantity), "LONG");

                If you are using the builder still you cannot do this using this builder and would instead need to stop using the stops and targets page and follow the sample here: https://ninjatrader.com/support/foru...596#post806596

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  I made the change and it runs now but it never executes a stop loss it only closes a position on session close

                  Comment


                    #10
                    Hello jmass13,

                    Are you able to see the stop submitted and if so what price is the stop shown as? Was that price ever met before the session close?

                    If you cannot see the stop submitted you may want to add a Print in with the entry to see what the Min[1] was at that time:

                    Print(MIN[1]);

                    You are using Ticks so if you are not inputting some series which will return a MIN value in ticks that would be the problem. You can also use Price for the CalculationMode if you wanted a specific MIN price from the price series.


                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by RookieTrader, Today, 09:37 AM
                    3 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by kulwinder73, Today, 10:31 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post kulwinder73  
                    Started by terofs, Yesterday, 04:18 PM
                    1 response
                    23 views
                    0 likes
                    Last Post terofs
                    by terofs
                     
                    Started by CommonWhale, Today, 09:55 AM
                    1 response
                    3 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Started by Gerik, Today, 09:40 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post Gerik
                    by Gerik
                     
                    Working...
                    X