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

Stop Loss @ Low/High of Entry Bar minus one Bar

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

    Stop Loss @ Low/High of Entry Bar minus one Bar

    Greetings,

    I have been trying to develop a strategy (using NinjaTrader Script) and want to add a condition of stop loss order trigger at 10 ticks below the low of the bar before the bar where the long/short position was opened.

    "((Low(EntryBar()-1)-10) or ((High(EntryBar()-1+10)".

    Can someone in this forum help me to convert the above condition into NinjaTrader Script code?

    Thanks,

    Sami

    #2
    Code:
    Low[BarsSinceEntry()+1]-10*TickSize;
    High[BarsSinceEntry()+1]+10*TickSize;
    BarsSinceEntry() returns an int representing how many bars ago your strategy entered your position. (http://www.ninjatrader-support.com/H...inceEntry.html)
    You want to do BarsSinceEntry()+1 to reference the bar previous the Entry Bar. (an index of 0 references the current bar, 1 references the bar before the current bar, etc)
    TickSize returns a double that represents the minimum price fluctuation of your instrument as defined in the Instrument Manager (http://www.ninjatrader-support.com/H...helpguide.html)
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Wouldn't it be easier to add that when you enter a trade? So you have the entry and set the stop to Low[1] or High[1]?

      Comment


        #4
        The problem with doing it like that mazachan is that if you didn't hit your stop/target on the next bar your stop/target will keep getting modified. As each new bar is built, you end up moving your stop/target to the new respective Low[1] and High[1]. To keep the stop/target static (i.e. referencing the same Low/High bar value) regardless of how many bars have passed you need to add the BarsSinceEntry() into the parameter. Basically it acts as a kind of counter.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          But if you say something like:

          Code:
          if (SomeCondition){
                EnterLongPosition();
                double stop = Low[1] - (10 * TickSize);
                SetStopLoss(CalculationMode.Price, stop);
          }
          It should only be run when the trade is executed and should not be repeatedly updated, since that condition will only be hit on the entry bar.

          Comment


            #6
            That would work too.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Stop Loss w/ Close Beyond High/Low of Entry Bar

              Hello,

              (Bullish example) I wanted to set up a stop, if there's a close 1 tick or more below the low of the entry bar. Is there a way to do it?

              By the way this would be more of an exit than a stop, so if I had an existing stop, let's say of "15" ticks, I would want to close out the position, any remaining targets and also any existing stops.

              An incomplete example of what it would look like is below. I'm hoping you can fill in the blanks for me. I would put the statement in the "ManageOrders" routine section of the code.

              Thanks.

              private void GoLong()
              {
              SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
              SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);

              SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
              SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1 + Target2)*TickSize));

              EnterLong("target1");
              EnterLong("target2");
              }


              private void ManageOrders()
              {
              if (Position.MarketPosition == MarketPosition.Long)
              {
              if (Close[1] > t4tBlueWave(1.618, 3, 3).StopLine[1] && Close[0] < t4tBlueWave(1.618, 3, 3).StopLine[0] && t4tBlueWave(1.618, 3, 3).UpTrend[1])
              this.SetStopLoss("target2", CalculationMode.Price, Close[BarsSinceEntry() + 1]-1*TickSize);
              }
              Last edited by RookieTrader; 08-05-2013, 04:59 AM. Reason: For more clarity

              Comment


                #8
                RookieTrader, you mean you only want to activate the stop on your condition triggered?
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Bertrand,

                  Here's the specific code I'm working on (the within the quotes):

                  if (t4tBlueWave(1.618, 3, 3).UpTrend[1])
                  "this.SetStopLoss("target2", CalculationMode.Price, Close[BarsSinceEntry() + 0]-1*TickSize);"

                  I know it's incomplete, but I was hoping that when this condition exists on the current bar, it would act like a stop, closing the positions associated with "target2" (actual position, target and stop).

                  To answer your question: "RookieTrader, you mean you only want to activate the stop on your condition triggered?" I need a stop that specifically does the following:

                  1. Closes the remaining positions associated with "target2"
                  2. Will exit all positions associated with "target2" when (in a bullish situation) it closes 1 tick or more lower than the low of the entry bar.

                  I was wondering if this is possible.

                  Comment


                    #10
                    Bertrand,

                    For quick clarification, I guess I'm trying to do a quick adjustment and closing of the existing stop.

                    Comment


                      #11
                      Hi RookieTraders, yes you can always update stops by just resubmitting / calling SetStopLoss again for your positon - http://www.ninjatrader.com/support/f...ead.php?t=3222

                      As the sample shows it would just be important to reset to a default value when you're in a flat state.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Bertrand,

                        To try to clear things up, I can code to close out the stop 1 tick below the "Low" with the following:

                        "this.SetStopLoss("target2", CalculationMode.Price, Low[BarsSinceEntry() + 0]-1*TickSize);"

                        I can code to close out the stop 1 tick below the "Close" with the following:

                        "this.SetStopLoss("target2", CalculationMode.Price, Close[BarsSinceEntry() + 0]-1*TickSize);"

                        ... but I don't know how to set up code to close out the stop when the "Close" occurs 1 tick or more below the "Low". Is that possible?

                        Heading out to work. Will respond later.

                        Thanks

                        Comment


                          #13
                          '... but I don't know how to set up code to close out the stop when the "Close" occurs 1 tick or more below the "Low". Is that possible?'

                          Is that's alreay what you realize with the first snippet? You have the stop then 1 tick below the Low of the Entry bar, with a StopMarket order. If that level is breached, the order is triggered.
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            Bertrand,

                            See attached jpeg file for visual reference. The initial stop upon entry is "15" ticks. If the price is 1 tick or more above the high (in a bearish trade), but does not have a bar close above the high of the entry bar, the stop adjustment would not be triggered.
                            Only if there's a bar close above the high of the entry bar is the stop adjusted and the whole position exited. Is there a command(s) to do that?
                            Attached Files
                            Last edited by RookieTrader; 08-06-2013, 05:01 AM.

                            Comment


                              #15
                              Thanks RookieTrader, you can certainly adjust the stop only on a custom condition occurred, like the closing above the entry price for example. You set your initial 15 tick stop in Initialize() I guess, so this dynamic adjustment would then be done in OnBarUpdate() to tighten up this stop as your condition is seen.
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
                              55 responses
                              5,448 views
                              0 likes
                              Last Post BartMan
                              by BartMan
                               
                              Started by DawnTreader, 05-08-2024, 05:58 PM
                              16 responses
                              52 views
                              0 likes
                              Last Post DawnTreader  
                              Started by tradingnasdaqprueba, 05-07-2024, 03:42 AM
                              15 responses
                              60 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by kevinenergy, Yesterday, 12:01 PM
                              8 responses
                              28 views
                              0 likes
                              Last Post kevinenergy  
                              Started by nightstalker, Today, 01:32 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Working...
                              X