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

Trailing Stop with Secondary Time Frame

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

    Trailing Stop with Secondary Time Frame

    Trying to create an exit based on a secondary time frame (1 tick) but am unable to get it to function properly.

    It simply doesn't exit based on the secondary time frame.

    I created a simple test in the strategy builder... see attached.

    I set the entry for a close above the previous close and the exit 2 ticks below the previous close.

    I thought it might have something to do with BarsInProgress but I also thought the strategy builder would create the correct code.
    Attached Files

    #2
    Hello sdauteuil,

    I did not try to run your strategy or check to see that this answer solves the problem.. However..

    I would do something different with this opening statement in OBU().

    Set 2 (your exit) will never execute because your are returning anytime an update from BarsArray[1] sends an event update to OBU().

    Code:
    OBU()
    
    if (BarsInProgress != 0)
    return;

    Comment


      #3
      I thought you can access a secondary series from another BarsInProgress object. (not sure if object is the right term referring to BarsInProgress)

      I am trying to add this to a current strategy that is in use which seems be able to call the second time series when BarsInProgress !=0.


      This is the beginning of OBU() for the current strategy. In the logic I am checking that the 3 minute bar is up.

      State == State.Configure

      AddDataSeries(Data.BarsPeriodType.Tick, 1);
      AddDataSeries(Data.BarsPeriodType.Minute, 3);


      OnBarUpdate()

      // sync secondary series with primary series for cumulative delta
      if (BarsInProgress == 1)
      cumulativeDelta.Update(cumulativeDelta.BarsArray[1].Count - 1, 1);

      // check if we are on primary series
      if (BarsInProgress != 0)
      return;

      // check there are enough bars loaded for calculations
      if ((CurrentBars[0] < 20)
      || (CurrentBars[1] < 20))
      //|| (CurrentBars[2] < 20))
      return;

      //Trade Logic
      (Close[0] > Closes[2][0])
      Last edited by sdauteuil; 11-29-2020, 04:35 PM.

      Comment


        #4
        I tried changing the test code but this does not work either.

        protected override void OnBarUpdate()
        {

        if (BarsInProgress == 0)


        if (CurrentBars[0] < 20
        || CurrentBars[1] < 20)
        return;

        // Enter Trade
        if (Close[0] > High[1])
        {
        EnterLong(Convert.ToInt32(DefaultQuantity), @"Test Entry");
        }

        // Exit
        if (BarsInProgress == 1)

        if (CurrentBars[0] < 20
        || CurrentBars[1] < 20)
        return;

        if (Closes[1][0] < (Close[1] + (-10 * TickSize)) )
        {
        ExitLong(Convert.ToInt32(DefaultQuantity), @"Exit", @"Test Entry");
        }


        Comment


          #5
          Hi sdauteuil,
          First of all, I can't find a Trailing Stop in your StrategyBuilder code. It should show up in State.Configure right after the AditionalDataSeries, e.g. as:
          SetTrailStop("", CalculationMode.Ticks, 20, false);
          Second, you seem to have activated Calculate.OnBarClose.
          Third, I would have expected you use the smaller time frame (Ticks) as primary and the higher time (1 min) as secondary series.
          I quickly built such (flipped around) strategy (as always with a few prints) and it seems to work as expected.

          ...
          else if (State == State.Configure)
          {
          AddDataSeries(Data.BarsPeriodType.Minute, 1);
          SetTrailStop("", CalculationMode.Ticks, 20, false);
          }
          }

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

          if (CurrentBars[0] < 1
          || CurrentBars[1] < 1)
          return;

          // Set 1
          if (Closes[1][0] > Closes[1][1])
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), @"Test Entry");
          }

          // Set 2
          if (Close[0] < (Closes[1][1] + (-2 * TickSize)) )
          {
          Print(@"Date/Time " + Times[0][0].ToString());
          Print(@"Close Ticks " + Close[0].ToString());
          Print(@"1 Min Close [1] - 2* TickSize " + (Closes[1][1] + (-2 * TickSize)) .ToString());
          Print(@"");
          ExitLong(Convert.ToInt32(DefaultQuantity), @"Tick Exit", "");
          }

          NT-Roland

          Comment


            #6
            Sorry I wasn't more clear.

            I am not using the trailing stop from NT. It is my understanding you cannot engage the trailing stop after X amount of ticks, it is automatically engaged when a position is opened.

            I have been using a trailing stop that engages after X amount of ticks. I have been using this for a long time and it works well.

            TriggerPrice = (Position.AveragePrice + (TstopTrigger * TickSize));

            Once the trigger price is hit the Atr Trailing stop becomes the exit.


            My problem is if I am using a large bar/brick (Like a 50 tick renko bar) with OnBarClose() the exit doesn't occur until there is a close below the Atr Trailing stop and the stop is just too far away.

            I want to create a trailing stop that will engage after the TriggerPrice is hit and trail the stop will be X amount of ticks below price.

            I want to have the stop based on 1 tick bar to eliminate giving back so much waiting for the bar to close.

            Basically take what I have now and have the close based on 1 tick time series instead of the primary series.

            Comment


              #7
              Hello sdauteuil,

              If you are wanting to trigger events on a 1 tick added series, this would be BarsInProgress 1 not BarsInProgress 0.

              Below is a link to a forum post on intra-bar granularity .


              Also, below is a link to some examples on custom exit order movement.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                That is what I was looking for.


                Is it not possible to access a secondary time series if it is not under the corresponding BarsInProgress ?

                I have a strategy which I want to check a secondary 3 minute time series to see if the close on the primary series is higher than the previous close on the 3 minute.

                AddDataSeries(Data.BarsPeriodType.Tick, 1);
                AddDataSeries(Data.BarsPeriodType.Minute, 3);

                Under OnBarUpdate() I have

                // sync secondary series with primary series for cumulative delta
                if (BarsInProgress == 1)
                cumulativeDelta.Update(cumulativeDelta.BarsArray[1].Count - 1, 1);

                // check if we are on primary series
                if (BarsInProgress != 0)
                return;

                My trade logic is under BarsInProgress != 0

                // check 3 minute candle
                including (Close[0] > Closes[2][0])

                Is the above statement functioning in this context?

                Thanks for the help



                Comment


                  #9
                  Hello sdauteui,

                  Yes, you can access the data from other series by using the BarsInProgress index for that series, no matter what series is currently updating OnBarUpdate().

                  Closes[0][0] is the primary series, Closes[1][0] is the first added series.

                  Below is a link to the help guide on Closes.


                  Your understanding is correct. Are you finding that the values are not what you expect when you test the code?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    I was confused by this part of you response

                    If you are wanting to trigger events on a 1 tick added series, this would be BarsInProgress 1 not BarsInProgress 0.

                    I believe the statement (Close[0] > Closes[2][0]) is working.


                    However I was trying to execute my trailing stop in the same strategy based on 1 tick and I could not get it to work. Which is what started this thread.

                    The code for the exit below works fine on the primary time series but when I changed from Close to Closes it was still only exiting the position based on the primary series.

                    if ((Close[1] < AtrTrailingOnClose1.Lower[1])
                    && Close[0] >= AtrTrailingOnClose1.Lower[1]
                    && Position.MarketPosition == MarketPosition.Short
                    && (TriggerState == 2))
                    {
                    ExitShort(Convert.ToInt32(Contracts), @"Trailing Stop Lx", Convert.ToString(""));

                    Changed to exit on secondary series

                    if ((Closes[1][1] < AtrTrailingOnClose1.Lower[1])
                    && Closes[1][0] >= AtrTrailingOnClose1.Lower[1]
                    && Position.MarketPosition == MarketPosition.Short
                    && (TriggerState == 2))
                    {
                    ExitShort(Convert.ToInt32(Contracts), @"Trailing Stop Lx", Convert.ToString(""));


                    Do I have to put the code for the exit under BarsInProgress == 1
                    Last edited by sdauteuil; 11-30-2020, 06:43 PM.

                    Comment


                      #11
                      I got it working!

                      If I am correct you can reference the secondary series anywhere in the code but if you want to trigger an exit or other type of event in must be
                      under BarsInProgress == (corresponding number)


                      Sorry I do not yet understand all the terminology in C... series objects etc...
                      Otherwise I could be more precise in my descriptions.

                      Comment


                        #12
                        Hello sdauteuil,

                        OnBarUpdate() will run for every data series, including the primary series and all added series.

                        The series currently updating OnBarUpdate will pointed to in the BarsInProgress index.

                        if (BarsInProgress == 0)
                        {
                        // the primary series is processing
                        }

                        if (BarsInProgress == 1)
                        {
                        // the first added series is processing
                        }

                        Below is a link to a forum post with helpful information about getting started with NinjaScript. I recommend starting with the training videos.
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by f.saeidi, Today, 11:02 AM
                        1 response
                        2 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by geotrades1, Today, 10:02 AM
                        4 responses
                        12 views
                        0 likes
                        Last Post geotrades1  
                        Started by rajendrasubedi2023, Today, 09:50 AM
                        3 responses
                        16 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by lorem, Today, 09:18 AM
                        2 responses
                        11 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by geddyisodin, Today, 05:20 AM
                        4 responses
                        30 views
                        0 likes
                        Last Post geddyisodin  
                        Working...
                        X