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

Buy on Close of a daily bar using minute data?

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

    Buy on Close of a daily bar using minute data?

    I am trying to get around the limitation of not being able to buy on the close of a daily bar by using a sub series of minute data.

    At the end of the day a need use the Close of the minute bar as an approximation of the Daily bar (not the close a day before).

    I have tried to add a new bar to the BarsAarry but not succeeded.

    1) Do you have any recommendations of how add data to BarsArray?

    2) Do you (or anyone else out there) have an alternative solution?

    Otherwise, thanks for a great application!

    Best Regards
    Jakob

    #2
    Hello,

    Thank you for the question.

    I wanted to further clarify what you are trying to accomplish. In your first statement you said you are trying to buy on the close, does this mean you are trying to buy after hours? If so are you using a session template that would allow for this?

    Also you asked how to add data to BarsArray, BarsArray would consist of bar data that comes from the provider and your historical data. You wouldn't really be able to create a false bar if that is what you are asking.

    Can you provide clarification on this?

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

    Comment


      #3
      Thanks for the fast answer regarding the BarsArray!

      I want to buy precisely before the close of a daily bar using 1-minute data.

      For example:

      - I am running on daily bars and have then added a series of 1-minutes bars. BarsArray then consists of two instruments.

      - Let say I have a strategy that buys a stock if it closes higher SMA(20). I need to buy it before the close of the daily bar and not on the opening of the next bar.

      - This by using the close of the minute bar ( for example at time 15.59) as an approximate value.

      In the code, how do I do that?

      I been thinking of creating a custom array, and adding the daily closes. Then after that adding the Closes[1][0] in the end of that array.

      Thanks!
      Jakob

      Comment


        #4
        Hello,

        Have you looked into the SampleMultiTimeframe strategy and how it manages logic based on which series is calling OnBarUpdate? What it sounds like you are needing is to use the BarsInProgress index to separate logic.

        BarsInProgress shows which series is calling OnBarUpdate, so if the 1 minute series calls OnBarUpdate you could execute code specific to the 1 minute series.

        If you will only be running this on one instrument and can have a static time check, you could check for the second to last bar of the session on the minute series.

        For example if the session end was 140000, 1 minute prior would be the bar before the Close.
        Code:
        if(BarsInProgress == 1) //assuming there is only 1 added series, the 1 minute series would be index 1
        {
             if(ToTime(Time[0]) == 135900)
             {
                  //do logic for the last remaining bar of the 1 minute series. 
             }
        }

        You can also submit orders to a specific series using the extra overload options, for example:

        EnterLong(int barsInProgressIndex, int quantity, string signalName)

        In this case you could submit an order to the Minute series instead of the Dailybars by using EnterLong(1, DefaultQuantity, "SignalName");

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

        Comment


          #5
          Thanks for a fast answer!

          I do exactly as you suggest (adding a time constrain for when to make the order using 1-min bars).

          But the problem arise when I use indicators on daily bars. At 13:59 in your example I want to use the current price as an approximation of the last Close-value.

          If I use an indicator in that moment the series will look like below:

          Closes[0][1] : Daily close of the day before yesterday

          Closes[0][0] : Daily close yesterday



          Instead I want the indicators to use a series that consist of the following values:


          Closes[0][2] : Daily close of the day before yesterday

          Closes[0][1] : Daily close yesterday

          Closes[0][0] : The price right now (i.e. the close of the 1-min bar/ Closes[1][0])

          It can´t be only me who want to buy right before the close of a daily bar, and using updated indicators for that decision?

          Best Regards

          Jakob

          Comment


            #6
            Hello,

            Thank you for the question.

            This would depend on the CalculateOnBarClose setting for how the barsAgo would work.

            Using CalculateOnBarClose false, the following would be true:

            Closes[0][2] : Daily close of the day before yesterday
            Closes[0][1] : Daily close yesterday
            Closes[0][0] : The price right now (would be the last price or last tick price)

            Using True, you would get the following because the bar has not yet closed:

            Closes[0][1] : Daily close of the day before yesterday
            Closes[0][0] : Daily close yesterday

            You would not be able to get the Last price or Current price using a Daily series and also using CalculateOnBarClose = true.

            If you are only trying to get the Close of prior days, I would recommend using the PriorDayOHLC indicator instead as you can access the Prior days close and also a BarsAgo to get prior closes. You could then use CalculateOnBarClose = false, or use a different series to access the Current price.



            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello,

              Thanks for your response!

              When running this strategy live it seems like a good idea to use the CalculateOnBarClose == False as you describe, but how do I simulate the same trades (results) during my backtesting in NT?

              I would like to include the current price (which would be an approximation of today´s close-value) together with previous close values in the indicators. Is there a way to do this?

              I know e.g TradeStation have a solution called “ByThisBarOnClose” to accomplish the same thing I am after. Is there anything similar in NT? If not, how do you recommend to solve this in NT?

              Hope you understand what I am looking for.

              Thank you!

              Comment


                #8
                Hello,

                Because the backtest works fundamentally different than realtime data, a tick series would need to be added to the script and then any intrabar logic could be accomplished. There is a sample of this called SampleMultiTimeFrame and also SampleMultiInstrument which utilize the BarsInProgress.

                Backtesting in general is not the same as realtime because of the way the fills are calculated. Backtesting uses historical data which can only use the OHLC of the bar. There is more information on this here: http://ninjatrader.com/support/helpG...htsub=backtest

                The current price is always Close[0], using CalculateOnBarClose false this would be the last tick, using true this would be the prior day bars close. Can you clarify what you mean here :"I would like to include the current price (which would be an approximation of today´s close-value) together with previous close values in the indicators.". Are you asking how to access the prior day bars close?


                I googled "ByThisBarOnClose" but there were 0 results, can you tell me what this method does and I could see if there is a similar NT method.


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

                Comment


                  #9
                  Hello,

                  I will try to clarify:

                  I am working on a strategy that buys near closure of the day. As I have not found any solution (when backtesting on daily data) that can buy at close, I am using a sub series of minute data.

                  I want to be able to include the value of a minute bar (Closes[1][0]) a couple of minutes before end of day in a data-series that it used by my indicators.

                  I know how to access prior day bars close, but my question is how to create a data-series that includes both the current value Closes[1][0] (which would be an approximation of Closes [0][0]) and the previous close values Closes[0][1] and so forth.

                  Here is a link to the TradeStadion function Buy this bar on close I mentioned in my previous post: http://help.tradestation.com/08_08/e...rved_word_.htm

                  Hope you will be able to help.

                  Thanks.

                  Comment


                    #10
                    Hello Jackson12,

                    Let me first tell you that what you are asking, the end result, CAN be achieved (just not in the way you're wanting to do it).. Its just very confusing and difficult. I've done the exact same thing you are doing and once you start down the path of trying to get in on the close with all your strategies or using indicators, it increases the level of difficulty of code.

                    Now that you know what you're getting into, here is my solution:

                    If the indicator that you are using must use data from the current day and you need that before the end of the day, so you can enter right before the close. You must first move the indicators code into your strategy.
                    Assuming your Primary is Day and Secondary is MIN, you need to put the indicator in both the Primary and Secondary. Having it calculate the same in the Primary as it did in the indicator. In the Secondary, you will want it to use all the last calculations of the Primary and the current Secondary Close[1][0], asking it say.. within the last 3min of the day to get a more accurate calculation.

                    Is there any specific Indicator you're try to use?

                    NOTE:
                    1. Here is something useful I've learned working with DAY and MIN time frames... when you're asking in the secondary Close[1][0], the primary Close[0][0] is still the previous day. Say your Day close is at 160000, this rule applies when the time is 155800, 155900.. But once you get 160000 primary ask and then 160000 secondary ask. So just to note when its 160000 secondary your Close[0][0] is now the current DAY close.. just something to remember..

                    2. I also created a "switch and case" for my different close times for all my markets. It also adjust depending on what time zone you're in. Here is a little example (fyi AdjustedCloseTime is my variable for 3min before the close):

                    TimeZoneInfo localZone = TimeZoneInfo.Local;

                    switch (Instrument.MasterInstrument.Name)
                    {
                    case "6A": case "6B": case "6C": case "6E": case "6J": case "6N": case "6S": case "DX":
                    CloseTime = 220000 + (localZone.BaseUtcOffset.Hours*10000) + (localZone.BaseUtcOffset.Minutes*100); // (CST 4:00PM, EST 5:00PM)
                    AdjustedCloseTime = 215700 + (localZone.BaseUtcOffset.Hours*10000) + (localZone.BaseUtcOffset.Minutes*100); // (CST 3:57PM, EST 4:57PM)
                    break;
                    }



                    Please let me know if I understood you correctly and if you have any more questions. Good luck!

                    Lucas

                    Comment


                      #11
                      Hello Lucas,

                      I really appreciate your response, thank you very much! Yes, you absolutely understood me correctly.

                      I use a lot of indicators : ADX, EMA, RSI and more…If I understood you correctly: I need to manually code my indicators in the strategy?

                      So there is no way to keep the indicators separate and instead create an array to use as input to the indicators?

                      Thanks again for taking the time to help out, and thanks for the extra tips that probably will be useful!

                      Comment


                        #12
                        Jackson, that is exactly right, you need to manually code the indicators into the strategy in both the primary and secondary, since you are wanting to use data from both time frames.

                        Yup, there is NO way to create an array with both primary and secondary to send to the indicators.. Wish this wasn't true I've tried many different ways to achieve this end result..

                        You're welcome! Please let me know if you need anymore help

                        Lucas

                        Comment


                          #13
                          I have also tried all types of arrays and solutions that I could think of

                          Right now I coding indicators, and will do that for a while I guess....

                          Thanks again, and have a nice day!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by CortexZenUSA, Today, 12:46 AM
                          0 responses
                          0 views
                          0 likes
                          Last Post CortexZenUSA  
                          Started by usazencortex, Today, 12:43 AM
                          0 responses
                          2 views
                          0 likes
                          Last Post usazencortex  
                          Started by sidlercom80, 10-28-2023, 08:49 AM
                          168 responses
                          2,262 views
                          0 likes
                          Last Post sidlercom80  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          3 responses
                          10 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by WeyldFalcon, 12-10-2020, 06:48 PM
                          14 responses
                          1,429 views
                          0 likes
                          Last Post Handclap0241  
                          Working...
                          X