Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error in strategy analyzer with custom BarsType

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

    Error in strategy analyzer with custom BarsType

    Hello Ninja!

    I have coded a custom BarsType named 'RangeBarsTypeVola' which is the standard RangeBarsType with a modification that does calculate the bars.BarsPeriod.Value ( = bar range) dynamically as 10% of the prior session daily range. When applied to a chart it looks fine. But when I run a strategy in the Strategy Analyzer I get the following error message:

    'myStrategy' tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load ES 06-18 Globex: RangeBarsTypeVola
    How can I solve this?

    I have attached my script. My modifications compared to the standard RangeBarsType are just a few lines and are found in the OnDataPoint method and named #region Added code 1 and #region Added code 2.

    Code:
    if (isNewSession)
                {
                    #region Added code 1
    
                    // Recalc. bars.BarsPeriod.Value at the beginning of a new session
                    if (bars.Count > 0 )
                    {
                        double priorSessionRange_10percent     = (sessionHigh - sessionLow) * 0.1;
    
                        // new range in ticks
                        bars.BarsPeriod.Value                = (int) Math.Round(priorSessionRange_10percent/bars.Instrument.MasterInstrument.TickSize);  
                    }
    
                    // Set initial values for new session
                    sessionHigh     = close;
                    sessionLow         = close;
    
                    #endregion
    
                    SessionIterator.GetNextSession(time, isBar);
                }

    Code:
                #region Added code 2
    
                // Update. sessionHigh, sessionLow
                sessionHigh = close > sessionHigh ? close : sessionHigh;
                sessionLow = close < sessionLow ? close : sessionLow;
    
                #endregion
            }
    / poseidon_sthlm
    Attached Files

    #2
    Hello poseidon_sthlm,

    Thank you for the post.

    I believe what you are seeing here is expected based on the syntax i can see being used. The following line creates the variable value as you note:

    Code:
    bars.BarsPeriod.Value                = (int) Math.Round(priorSessionRange_10percent/bars.Instrument.MasterInstrument.TickSize);
    Not using this syntax allows for a successful run of the test. The reason I believe this would be expected is that this situation is mentioned in the help guide for a different area which is accomplishing essentially the same goal. AddDataSeries notes that you should not use variables which are created in runtime, you get a similar error when trying to backtest a strategy which uses AddDataSeries to control the bars type values although it may work in a chart. In the same sense you are controlling the bars types values in runtime which the analyzer throws an error about.

    For this question, I will need to go a step further and put in a question to development to see if there would be any solution here to use the bars type in this way or if this is just going to be expected based on the dynamic nature of what is being done. Once I have more details I will reply back here.


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

    Comment


      #3
      Thanks for the prompt reply and for checking with development.

      I have tested to replace the 'bars.BarsPeriod.Value' with a custom variable (int rangeInTicks) and it did work as expected in the Strategy Analyzer. The only disadvantage is that I can't use the bars.BarsPeriod.Value in another script. Is there a way to expose the variable 'rangeInTicks' in the BarsType so it could be used in an indicator or straetgy script?
      Last edited by poseidon_sthlm; 05-29-2019, 02:12 PM.

      Comment


        #4
        Hello poseidon_sthlm,

        I am not certain what you mean by the following:

        The only disadvantage is that I can't use the bars.BarsPeriod.Value in another script.
        The Value property is inherited by all bars types, this should be able to be observed from other areas such as an indicator. Generally this is found using the Bars object and its properties: Bars.BarsPeriod.Value

        If you are reading that value, that would be fine however if you are trying to reset the value dynamically you will very likely return to the same problem.

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

        Comment


          #5
          Sorry for being unclear. What I meant was that if I asign the dynamically calculated bar range to a custom variable instead of the 'bars.BarsPeriod.Value', then there is no way to get the calculated range in another indicator or strategy script.

          In my updated version, RangeBarsTypeVola2, I replaced the occurences of bars.BarsPeriod.Value with a new variable 'rangeInTicks'. This barsType worked in the Strategy Analyzer.

          Code:
          // declare new variable      
          private int rangeInTicks;
          
          // Asign the dynamically calculated bar range to the new var. (instead of bars.BarsPeriod.Value as in my first attempt)
          rangeInTicks    = (int) Math.Round(priorSessionRange_10percent/bars.Instrument.MasterInstrument.TickSize);  
          
          // Replace bars.BarsPeriod.Value (as in the standard RangeBarsType) with rangeInTicks
          double  rangeValue    = Math.Floor(10000000.0 * rangeInTicks * tickSize) / 10000000.0;
          Attached Files
          Last edited by poseidon_sthlm; 05-30-2019, 02:54 AM.

          Comment


            #6
            Hello poseidon_sthlm,

            Yes the dynamic change would be the source of the problem and there is not a specific suggestion for sharing data in different ways from BarsTypes at this time. You could potentially do something similar to the volumetric bars type sample in how the bars type is cast before using its methods: https://ninjatrader.com/support/help...sub=volumetric

            You could try and experiment with creating a public property and then where it is being used you can cast the BarsType as your type to use its properties or methods. I am unaware of any side effects of sharing a variable in this way from a BarsType or if this works/is reliable as this is undocumented territory. This would be something that you could test in the areas of the platform which you use the strategy. BarsTypes are not specifically intended for dynamic use like shown in this sample so there are no specific guidelines I can provide on how you should share this value to other scripts.


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

            Comment


              #7
              Hello!

              As described above, I have coded a custom BarsType named 'RangeBarsTypeVola2' which is the standard RangeBarsType with a modification that does calculate the bar range dynamically as 10% of the prior session daily range. The BarsType script is attached in post #5 and it's a simple as possible example script.

              I have encountered an issue with my custom BarsType that occurs when a chart or a backtest uses a cached bars object (OHLCV) for a backadjusted futures contract . When I do load a chart the first time, then the bars are calculated correctly. But when i reload the chart and change the number of "Days to load" to a lesser number of days, then there are some days where the range is incorrectly set to 0 ticks. To reproduce this issue you can load a chart with BarsType 'RangeBarsTypeVola2' for the ES contract and set 'Days to load' = 70 days and then reload the chart with 'Days to load' = 69 days. You will then see that besides the last day on the chart also two or three days now have a range of 0 ticks, which is incorrect.

              I have found that the reason for this issue likely is that the cahed bars are recalculated on the days where the range is 0 ticks. (I can see that OnBarUpdate and OnStateChange is called.) Since my dynamic range calculation is based on a variable 'rangeInTicks' that depends on values from the prior session, the range isn't correctly recalculated. The days when the cached bars are recalculated are the days before the contract roll-over date.

              So how can I either prevent the recalculation of the bars or make NT recalculate my varaible 'rangeInTicks' correctly?
              Is there some kind of flag that one can use to check if a cashed bar already exists for the current date?
              If I would want to delete the cahched barsObject for my custom bars type, how can I do this?

              / poseidon_sthlm
              Last edited by poseidon_sthlm; 06-16-2019, 05:57 AM.

              Comment


                #8
                Hello poseidon_sthlm,

                The script in post 5 is using unsupported concepts for a BarsType so I really wouldn't have any additional suggestion here to get this working. BarsTypes are not intended to be dynamic or request other data than what OnDataPoint provides. There is no property to disable caching, the only supported BarsType properties are listed in the help guide.

                Your BarsType's logic would need to be in a similar format to the other existing bars types which are not dynamic in the way they calculate. Using a BarsRequest from this context is also not supported or suggested. You can certainly experiment with the items you have shown in post 5, however there is no expectation that this would work correctly in contrast to how BarsTypes operate.

                I have put in a feature request to allow for dynamically changing values in a BarsType and loading additional data, however these are not current features.


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


                JesseNinjaTrader Customer Service

                Comment


                  #9
                  If I want to delete the cahched barsObject for my custom bars type, how can I do this?

                  Comment


                    #10
                    Hello poseidon_sthlm,

                    You can manually delete the cache by doing the following:
                    1. Exit NinjaTrader
                    2. Open the folder: Documents\NinjaTrader 8\db
                    3. Remove the "cache" folder
                    4. Restart the platform and reload historical data.
                    There is no means to do this from NinjaScript. This is also not suggested to do unless advised by support or you are specifically having a problem surrounding cache, it is not expected to have to empty this folder regularly.

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

                    Comment


                      #11
                      Ok, thanks for the advice on how to delete the cache.

                      The reason for asking this is that it seems as I can use my custom BarsType as intended as long as I apply the same start and end date as when the bars are created (and cached) the first time. No recalculation of bars occur as long as one apply the same start and end date for a chart or a backtest as when the bars were originally created.
                      Last edited by poseidon_sthlm; 06-17-2019, 01:37 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by condorhero, Today, 09:28 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post condorhero  
                      Started by MaraAnkh, Today, 08:44 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post MaraAnkh  
                      Started by wooofad, Today, 05:38 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post wooofad
                      by wooofad
                       
                      Started by caacapital, Today, 10:10 AM
                      2 responses
                      18 views
                      0 likes
                      Last Post caacapital  
                      Started by DiverG20, Today, 10:23 AM
                      0 responses
                      23 views
                      0 likes
                      Last Post DiverG20  
                      Working...
                      X