Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help - Multi-market trading hours indicator

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

    Help - Multi-market trading hours indicator

    NT Forum,

    Please find attached a NT indicator "TestTradingHours". This indicator identifies whether the current bar occurs within cash market hours for a US or Australian instrument.

    This script works as expected as an indicator. However, as part of a strategy, it returns the error message:
    Indicator 'TestTradingHours': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    There is a design flaw in the indicator:
    1. "AddDataSeries()" must occur in State.Configure
    2. "Instrument.Exchange" is not available in State.Configure (but is available in State.DataLoaded)
    3. Instrument.Exchange must be known to add the correct DataSeries

    Any suggestions on how to refactor this code while remaining effecient would be greatly appreciated.

    Regards
    Shannon
    Attached Files

    #2
    Hello Shansen,

    Thank you for writing in. You can create a non-browsable property and pass the exchange to that in your strategy.
    For example (Only including changes to your current code below):
    Code:
    #region Using declarations
    //...
    using System.ComponentModel;
    //...
    #endregion
    //...
    private bool IsTradedOnAsx()
    {
    	if(StratExchange == null || StratExchange == "")
    	{
    		return Instrument.Exchange == Exchange.Asx || Instrument.Exchange == Exchange.Snfe;
    	}
    	else
    	{
    		return StratExchange == "Asx" || StratExchange == "Snfe";
    	}
    }
    //...
    #regionProperties
    //...
    [NinjaScriptProperty]
    [Browsable(false)]
    public string StratExchange
    { get; set;}
    //...
    #endregion
    And then in your strategy just use:
    Code:
    AddChartIndicator(TestTradingHours(Instrument.Exchange.ToString()));
    This would allow your indicator's functionality to remain the same when directly applied to a chart, while accounting for the issue with the strategy calling it.

    Please let me know if this does not resolve the issue or if I may be of further assistance.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      MichaelM,

      Thank you for your solution. It works as expected where:
      - The Indicator TestTradingHours is loaded as an Indicator in a chart, and
      - The Indicator TestTradingHours is instantiated in the Strategy TestStrategy.

      However, there is an example I, in error, omitted from my original query. Where the Indicator TestTradingHours is instantiated in the Indicator TestIntermediateIndicator which in turn is instantiated in the Strategy TestStrategy.

      In this scenario, it is possible to "pass through" your suggested non-browsable property StratExchange from the TestIntermediateIndicator to TestTradingHours. However, I am aware passing through an argument in this fashion is not the best coding practice (attached).

      Please confirm this is the approach to take, or suggest a preferred approach.

      Again, thanks
      Shannon
      Attached Files

      Comment


        #4
        Hello Shansen,

        To confirm, you are saying that when you have a strategy calling an indicator which calls TestTradingHours, and to solve the original issue in this scenario, you are passing the exchange from the strategy through the middle indicator to TestTradingHours?

        So then in your middle indicator you have the StratExchange property so that you can assign it from the strategy and then you call TestTradingHours using the passed StratExchange property?

        I would agree this is not an ideal way to go about this. I will need to check with our product management team to confirm whether the original behavior is expected or not and I will update this post when I have further information.

        Thank you for your patience in the meantime.
        Michael M.NinjaTrader Quality Assurance

        Comment


          #5
          MichaelM,

          I confirm your understanding of the situation is correct. The StratExchange property is passed from the Strategy to middle indicator which, in turn, is passed from the middle indicator to the indicator TestTradingHours.

          Any insight you can offer would be greatly appreciated.

          Comment


            #6
            Hello Shansen,

            We could expect the same base behavior in NT7's Initialize(). While NT8 has improved where we now have that State.DataLoaded which does let you conditionally set something in State.DataLoaded, you cannot call AddDataSeres() in that state, therefore you cannot conditionally Add a data series based off a Bars/Instrument related properly.
            So to confirm, what you are doing in the first place is not recommended or technically supported as there are no guarantees as to the Instrument data being available in State.Configure.

            If you require this functionality you may handle it on a case by case basis. Unfortunately I cannot think of any other workarounds besides what I posted and what you posted to accomplish this.

            Please let me know if I may be of further assistance.
            Michael M.NinjaTrader Quality Assurance

            Comment


              #7
              MichaelM,

              Thanks for the confirmation. Thanks again for your help.

              Shannon

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by andrewtrades, Today, 04:57 PM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by chbruno, Today, 04:10 PM
              0 responses
              3 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              7 views
              0 likes
              Last Post FAQtrader  
              Started by rocketman7, Today, 09:41 AM
              5 responses
              19 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X