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

Acessing another data series from a Bar Type

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

    Acessing another data series from a Bar Type

    how could I access for example a 1 minute data series ?

    also is it possible to access the data series of another chart (from within a custom bartype)?
    Last edited by NinjaCustomer; 05-12-2018, 05:29 AM.

    #2
    Hello NinjaCustomer,

    Bar type scripts are not able to use synchronized series.

    You can instead do a BarsRequest if you need to get data.
    Below is a public link to the help guide.


    I'm not aware of any documented or undocumented way to get the primary series of an open chart programmatically.

    I am happy to submit a feature request on your behalf for this if you would like.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello!

      I would like to modify the RangeBarsType to a custom BarsType so that the size of the range is dynamically adjusted at the beginning of each session to the volatility of the prior session.

      My idea is to create a method UpdateRange() that is called at the beginning of each new session. This method would:
      1. ...calculate the volatility where volatility = average range of all 5 minute bars in the prior session.
      2. ... update the variable rangeValue (that is used in the standard RangeBarsType) to volatility
      Is this possible in NT8 and will it work without issues in real time, in Tick Replay as well as in backtests in the Strategy Analyzer? Can you help me with this or guide me in the right direction? Would a BarsRequest for a minute bars series at session begin be a way to go?

      The parameter bars.BarsPeriod.Value for this custom RangeBarsType would have no meaning since the range is dynamic. Can I somehow hide this parameter? It would also be nice to have a parameter for the period value of the minute bars used to calculate the volatility. How can I add this parameter for this custom RangeBarsType? Or would it be possible to use the parameter bars.BarsPeriod.Value for the period of the minute bars?

      / poseidon_sthlm
      Last edited by poseidon_sthlm; 01-23-2019, 03:35 AM.

      Comment


        #4
        Hello poseidon_sthlm,

        If you are wanting to call historical 5 minute bars specifically, a BarsRequest may be suitable for this, but is advised to avoid in a BarsType script.

        BarsRequest work pretty much anywhere and would work in TickReplay, and when backtesting in the Strategy Analyzer.
        https://ninjatrader.com/support/help...arsrequest.htm

        (edit) While BarsRequests can be made anywhere, this will have a severe impact on the performance of the BarsType script. Further BarsRequests take place in a different thread which introduces multi-threading considerations.
        https://ninjatrader.com/support/help...-threading.htm


        You can also use a session iterator to get the session start and end times to supply these to the BarsRequest.
        https://ninjatrader.com/support/help...oniterator.htm

        I have one example that does a bars request in an Addon that you may find helpful.
        https://ninjatrader.com/support/foru...208#post786208

        It is possible to remove a property with Properties.Remove(). Or you could reassign this with a new name with SetPropertyName().
        The help guide page on SetPropertyName() has examples of these.
        https://ninjatrader.com/support/help...pertyname2.htm

        The UniRenko script also renames some properties. Below is a public link to the NinjaTrader Ecosystem site.
        https://ninjatraderecosystem.com/use...nko-bartype-8/


        The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
        Last edited by NinjaTrader_ChelseaB; 06-04-2019, 07:12 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello!

          I have tried to create the custom range BarType as described in post #3 with a BarRequest, but the BarRequest doesn't work as expected. I do call the BarRequest in a method at the beginning of each new session, but from my print statements on a Historical chart it looks like all BarRequests are returned at the last bar of the chart. I have attached my "AddOn" with print statments for trouble shooting. Can you advice me how to make the BarReqest trigger at the beginning of each new session?

          / poseidon_sthlm
          Attached Files

          Comment


            #6
            Hello poseidon_sthlm,

            bars.Bars is null when the BarsRequest begins processing historical data. This looks to be because barsRequest.Dispose() is called immediately after the BarsRequest. I would recommend to dispose of this object in State.Terminated.

            I've attached a modification of the code that will print the rangeSum value. The code remains to have the BarsRequest triggered when isNewSession is true.

            Please let us know if we can be of further assistance.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello NinjaTrader_Jim!

              Thanks a lot for your modified version and your time. Much appreciated. (I see that you have made some corrections and improvements besides disposing the BarsRequest object in state terminated. Thanks!)

              When I add your modified version of my RangeBars2Type I still don't get the desired result. The range of the bars is 0 (instead of the average range of the 5 min bars from the prior session). I see that the print statments do print the average range and the rangeSum for all sessions at the end of the historical chart and not at the beginning of a every new session as intended. I can't figure out the reason for this. Do I miss something obvious or do you also get this result? What can I do to make the BarsRequest return the new 5 min barsObject that is requierd to calculate the volatility adjusted range when isNewSession == True?

              /poseidon_sthlm

              Comment


                #8
                Hello poseidon_sthlm,

                The method is returning 0 because it is returning before the BarsRequest completes.

                There are some synchronous/asynchronous challenges here and I am currently researching this topic further.

                I'll bump this thread after I have been able come up with a viable solution.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the info and your effort to find a solution.

                  /poseidon_sthlm

                  Comment


                    #10
                    Thanks for your patience, poseidon_sthlm.

                    EDIT: I have removed this attachement as the version attached is not a working example. This concept also delves into what we would consider unsupported. Please see post #12 for details.

                    I've attached further modifications to get the ASync BarsRequest to update synchronously with the rest of the script.

                    The general gist is:
                    1. Create an AutoResetEvent to be used block the running thread until the BarsRequest finishes
                    2. Deliberately fire the BarsRequest on a separate thread. (Make your own thread or use a Dispatcher)
                    3. using try-finally, Set your AutoResetEvent object
                    4. Before you continue calculating with your code use AutoResetEvent.WaitOne() to wait for the BarsRequest to finish
                    We do not generally make modifications to client code in the support department, but for this particular case, I wanted to create some sort of direction for others that would want to perform a BarsRequest and use it synchronously with their code. I must also be clear that BarsTypes were not designed to have BarsRequests performed from within them. While BarsRequests can technically be made anywhere, taking such an approach could introduce threading complications which we would often suggest to avoid.

                    Please see our Multi Threading Considerations page for more insights - https://ninjatrader.com/support/help...-threading.htm

                    Please let us know if you have any questions.
                    Last edited by NinjaTrader_Jim; 02-07-2019, 01:15 PM.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Thank you for the modified BarsType script! . Since coding with threading and Dispatchers is way above my level , I do appreciate the help.

                      When I attached the new RangeBars2 to a chart I get this error message:

                      Failed to call 'Add' method: System.ObjectDisposedException: The Safe handle has been closed
                      at System.Threading.WaitHandle.WaitOneNative(SafeHand le waitableSafeHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
                      at System.Threading.WaitHandle.InternalWaitOne(SafeHa ndle waitableSafeHandle, Int64 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
                      at System.Threading.WaitHandle.WaitOne(Int32 millisecondsTimeout, Boolean exitContext)
                      at NinjaTrader.NinjaScript.BarsTypes.RangeBars2Type.O nDataPoint(Bars bars, Double open, Double high, Double low, Double close, DateTime time, Int64 volume, Boolean isBar, Double bid, Double ask)
                      at NinjaTrader.Data.BarsSeries.Add(Bars bars, Double open, Double high, Double low, Double close, DateTime time, Int64 volume, Double tickSize, Boolean isBar, Double bid, Double ask)
                      '

                      Do I do somerhing wrong or do you also get this error?

                      /poseidon_sthlm


                      Comment


                        #12
                        Hello poseidon_sthlm,

                        I had uploaded an older export in post #10 which does depict the issue. I have removed this from my post. I also found additional complications with the approach I was taking.

                        While I had investigated this matter out of genuine interest, I will not be able to assist further with accomplishing this goal.

                        The big issue is that BarsRequests are asynchronous and we would have to make NinjaTrader wait until the BarsRequests finishes so the BarsRequest can be used in a synchronous fashion. This means we would have to do 2 very unsupported things that NinjaTrader was never designed to do.

                        1. Come up with a hack solution to make the asynchronous BarsRequest synchronous
                        2. Apply unsupported multi threading concepts to the BarsType to get it to work with the BarsRequest.

                        There are other complications here that would involve recursion where the BarsType is loaded and multiple additional BarsRequests are made each time a new session is seen. As this happens all at once and happens in a multi threaded way as historical data is fetched, #2 becomes very difficult.

                        You may have limited success having the BarsType start with 0 bars to load and have the BarsType develop with realtime data which may give the BarsRequest enough time to load and have the BarsType calculate another Range after the BarsRequest finishes. This would not be exactly on the new session but sometime after.

                        We have strict policies not to create modify or debug client code. I had gone beyond this policy with this ticket and went much deeper than we are allowed to offer for our clients.

                        You may use this insight to consider if you still want to approach this concept, but we would not be able to provide further direction and the concepts we have looked into here would be considered unsupported.

                        If you have another NinjaScript related question, please don't hesitate to open a new thread.
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Ok, thanks for taking the time to look into this. Opposed to some other posts in this forum you have made it clear that the BarsRequest() is not suitable to use with custom BarsTypes.
                          I try to avoid unsupported solutions and will look for an alternative approach to accomplish a volatility adjusted RangeBarsType.

                          /poseidon_sthlm

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by DJ888, 04-16-2024, 06:09 PM
                          3 responses
                          10 views
                          0 likes
                          Last Post NinjaTrader_Erick  
                          Started by RookieTrader, Today, 07:41 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post RookieTrader  
                          Started by maybeimnotrader, Yesterday, 05:46 PM
                          1 response
                          18 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by Perr0Grande, Yesterday, 08:16 PM
                          1 response
                          7 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by f.saeidi, Yesterday, 08:12 AM
                          3 responses
                          27 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X