Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Managed Approach: Enter any time in next bar

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

    Managed Approach: Enter any time in next bar

    Hi there.

    I'm beginning the process of converting an indicator into a strategy.

    Using the managed approach, I can successfully enter and exit via:
    -EnterLong
    -SetStopLoss or SetProfitTarget

    In backtest mode I checked the time of the trades and noticed that I only enter at the start of the next bar. That leads me to believe after the beginning of the bar the order is inactive. I want it active until the end of that bar.

    i.e. IF() conditions are met and Close[0] is complete. From the start of the next bar until it completes have the order remain open.

    I read info on the managed approach but I don't grasp it how to do it.

    How do I accomplish that?

    #2
    Hello ij001,

    Thank you for your post.

    The historical backtest will use the Close value of the bar when the condition is true.

    During backtest, strategies can ONLY be processed at the close of each bar.

    This means that your entry and exit conditions will only be triggered in backtesting when the bar has closed.

    You have the option of using the strategy to calculate tick by tick by setting CalulateOnBarClose to False in real-time, however there is no way to change how these orders are processed during the backtesting.

    Please let me know if you have additional questions.
    MatthewNinjaTrader Product Management

    Comment


      #3
      My Indicator is based on calculating values when CalculateOnBarClose = true.

      I am now automating it into a Strategy, so CalculateOnBarClose still must be true however how do I manage the order entry to enter anytime above High[0]?

      That is, I'm restricted to CalculateOnBarClose = true for the order entry. Anyway to make it = false just for the order entry then make it true for the next occurrence of the strategy?

      IJ

      Comment


        #4
        IJ, please check into this sample here, it would show you how to work both parts (COBC 'false' and 'true') in one script - http://www.ninjatrader.com/support/f...ad.php?t=19387

        Comment


          #5
          Hi Bertrand

          I went over the sample. I am only confused at one part.

          From the Sample:

          /* Since we're technically running calculations at the open of the bar (with open and close being the same event), we need to shift index values back one value, because the open = close = high = low at the first tick. Shifting the values ensures we are using data from the recently closed bar. */
          if (LinReg(Median, 10)[1] > LinReg(Median, 10)[2] && LinReg(Median, 10)[1] > SMA(Median, 10)[1])
          EnterLong("long entry");

          Since the first tick has open = close = high = low I understand that we have to shift values. In theory it makes sense. However, I don't understand why indicator values were chosen to be shifted. Does it even apply to someone like me who is only comparing high, lows and closes values?

          i.e. Most of my code is Close[0] >= Close[1] && High[0] > High[1]

          Thx
          IJ

          Comment


            #6
            IJ, it would apply to indicator or price series data, it would not matter - to work with the data of previous bar at the open of the next bar (where you're at then technically) you would need to reference one bar more back to makeup for calling a bar later.

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              IJ, please check into this sample here, it would show you how to work both parts (COBC 'false' and 'true') in one script - http://www.ninjatrader.com/support/f...ad.php?t=19387
              It appears that I can get my buy (enter) to work when CBOC = false after using FirstTickOfBar and Position.MarketPosition == MarketPosition.Flat with the provided sample.

              However, I am running into problems with the exit (sell) since it is compared at every tick.

              I have a predefined stoploss and target that is set from calculation logic.

              i.e. SetStopLoss("Buy1",CalculationMode.Price, Low[3], true);
              SetProfitTarget("Buy1",CalculationMode.Price, High[1]-High[3]);

              If I put that code in: if (Position.MarketPosition == MarketPosition.Long)

              calculations are way off because of every tick where the bar is not closed.

              The sample only uses indicators as comparison not price data. What can I do to properly set my stops and target?

              Thx

              Comment


                #8
                Another question:

                My indicator works nicely. I have been using it for a while. I am simply trying to automate it and see where that backtest and real-time takes me next.

                Can I call / reference the indicator in the strategy and keep the strategy simple with only entering and exiting?

                That is, the indicator contains all of the calculation logic from price data and numerous IF statements. Also COBC = true.

                In theory if the indicator returns true I could use that in the strategy and focus just on entering and exiting. I am worried my strategy code is just duplicating my previous efforts with just adding entering and exiting.

                However, I have only seen

                Add(PeriodType.Minute, 3)

                not any indicators that are true or boolean


                Been a long day.

                Thx

                Comment


                  #9
                  ij001, you could set the stop loss / target as well when you're in FirstTickOfBar.

                  Yes, you could call the indicator method from the strategy directly and could focus on signal logic here exclusively then, the Add() you saw is for adding series or indicators for visual display.

                  If you like to call the indicator from the strategy then method and it's exposed values (per default the plots) are readily available to you.

                  Comment


                    #10
                    My question is about the SampleCancelOrder strategy. I am not an experience programmer.

                    If you use the CancelOrder code after 3 bars like in the strategy and it is all in the OnBarUpdate() block will that cancel the buy order or all orders?

                    I want to cancel my buy order after 1 bar and also the Set() orders for profit and stop.
                    I was hoping they get cancelled automatically with CancelOrder like with my broker IB.

                    Comment


                      #11
                      ij001, the CancelOrder() command works on the specifc IOrder object it was called for, if you would like to have your Buy orders cancel after one bar, then could also just let them expire with the default NT order handling. If you submit a non market order it would only last for the bar it was submitted on and expire on the next bar update seen.

                      Comment


                        #12
                        Originally posted by NinjaTrader_Bertrand View Post
                        IJ, it would apply to indicator or price series data, it would not matter - to work with the data of previous bar at the open of the next bar (where you're at then technically) you would need to reference one bar more back to makeup for calling a bar later.
                        Hi Bertrand

                        It been works perfectly in backtesting but I've removed historical now and I think it has been all wrong in real time.

                        So, I would just like to clarify your comments with some examples I have below so I understand how FirstTickOfBar works. I'm confused at the moment.

                        1. When you say indicator or price series data that means calling an indicator like the below

                        MyMethod().MyIndicator[0] == 1

                        needs to changed to

                        MyMethod().MyIndicator[1] == 1

                        2. Previous bar at the open


                        High[0] can remain High[0]


                        3. Any other bar before the previous bar you need to reference one bar earlier


                        SetStopLoss(
                        "Buy1",CalculationMode.Price, Low[2], true);


                        needs to be


                        SetStopLoss(
                        "Buy1",CalculationMode.Price, Low[3], true);

                        Comment


                          #13
                          2. Previous bar at the open


                          High[0] can remain High[0]

                          Sorry, I think this is wrong needs to be High[1] not High[0] which would be tick by tick.

                          I want previous bar.
                          Last edited by ij001; 10-21-2011, 01:34 PM.

                          Comment


                            #14
                            ij001

                            Yes, that's accurate. You check using FirstTickOfBar for the opening tick of that bar, but if you want to refer to the "just closed" bar, then would index 1 bar back.
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              In reference to the below and trading live do I need to also shift values by 1 for

                              Time[0] and say CurrentHigh[0]?

                              I would need to shift High[0] to High[1] to reference the closed bar and I think CurrentHigh[0] to CurrentHigh[1] as well but wanted to confirm...

                              However I am confused about Time.

                              /** Extract value from 10 AM **/
                              if (
                              ToTime(Time[
                              0]) == 100000 &&
                              CurrentDayOHL().CurrentHigh[
                              0] == High[0]
                              )

                              Thx for any help. Irvin


                              Originally posted by ij001 View Post
                              Hi Bertrand

                              I went over the sample. I am only confused at one part.

                              From the Sample:

                              /* Since we're technically running calculations at the open of the bar (with open and close being the same event), we need to shift index values back one value, because the open = close = high = low at the first tick. Shifting the values ensures we are using data from the recently closed bar. */
                              if (LinReg(Median, 10)[1] > LinReg(Median, 10)[2] && LinReg(Median, 10)[1] > SMA(Median, 10)[1])
                              EnterLong("long entry");

                              Since the first tick has open = close = high = low I understand that we have to shift values. In theory it makes sense. However, I don't understand why indicator values were chosen to be shifted. Does it even apply to someone like me who is only comparing high, lows and closes values?

                              i.e. Most of my code is Close[0] >= Close[1] && High[0] > High[1]

                              Thx
                              IJ

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X