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

Create an abstract indicator list class

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

    Create an abstract indicator list class

    Hi, so I have a few strategies that use a list of indicators. Since they are shared among strategies, and other times the list is unique to a particular strategy, I'd like to make a class which can hold a list of indicators to manage all the combinations.

    My thought is to use an abstract IndicatorList class, and then a sub-class of that for each actual list; some of the functionality will deal with exporting the data, etc, which can be shared through this top level abstract class. But when I try I don't have access to some of the Ninjascript functionality. What should the IndicatorList be a subclass of? I've tried various things and continue to run into snags. If it's a stand alone class, then I don't have access to the indicators and the Ninjascript functions (such as print to the output screen for debugging). Plus when trying to add an indicator I run into the following error:

    Non-invocable member 'ATR' cannot be used like a method
    I tried making the IndicatorList class a subclass of indicator, which will compile, and I have access to the Print from Ninjascript, etc. But now, when I go through the list to print it; the indicator is not updating, ie the indicators in the list are not getting called on OnBarUpdate. So I put a blank override method to call OnBarUpdate in the abstract class. However, this still doesn't update the indicators in the list.

    I've added a blank protected overide OnBarUpdate methods, which appear not called at all (checked using print statements). And when in the sub-class of the IndicatorList class, the CurrentBar = -1, even though on the strategy calling the CurrentBar > 5. So it looks like how I have it set up, the OnBarUpdate method is not being called for the indicator list itself, or for the indicators which are part of the class either.

    Could anyone offer a suggestion on a better architecture that could accomplish this purpose? Thanks. Here's some sample code:

    abstract class IndicatorList : Indicator
    {

    public List<Indicator> _indList = new List<Indicator>();
    public List<string> _indNameList = new List<string>();
    public List<int> _indSettingsList = new List<int>();

    public int _barIndex = 1;

    public ATR _atr;

    public void AddIndicators()
    {
    Print("adding base indicator");

    // baseline ATR
    _atr = ATR(10);
    _indList.Add(_atr);
    _indNameList.Add("ATR_10");
    _indSettingsList.Add(0); ....

    .......

    #2
    Originally posted by mohawkTrader View Post
    I have a few strategies that use a list of indicators.
    Could anyone offer a suggestion on a better architecture that could accomplish this purpose?
    Make the class an abstract class that inherits from Strategy.

    Then make your real strategy inherit from this abstract class.

    Your abstract class serves as a middle man, maintaining a list
    of indicators.

    Comment


      #3
      Hello bltdavid,

      A partial class could be used.
      Explanation: I wrote a base class Indicator class that I'm using to inherit all my other indicators from. So this baseclass is defined as: namespace NinjaTrader.NinjaScript.Indicators.AssistedTrades { public class ATBaseIndicator: Indicator { ... } } And any other indicator is defined as: namespace NinjaTrader.NinjaScr
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by mohawkTrader View Post

        Could anyone offer a suggestion on a better architecture that could accomplish this purpose? Thanks. Here's some sample code:

        I have seen approaches above from both bltdavid and Chelsea work.

        The value of the abstract class is that you will have reduced possibilities of thread conflicts.

        If you follow the partial class approach from Chelsea I recommend swapping your thread troublesome List for a shared static Concurrent Dictionary.

        HedgePlay
        Last edited by hedgeplay; 07-11-2021, 08:19 PM.

        Comment


          #5
          Thank you all. I think the abstract class best fits my needs. Thanks to bltdavid for the suggestion.

          Here's a more detailed summary in case it's helpful to someone. I have two abstract classes, one for the general functions, and then one which holds the actual list of indicators (each specific list). Then the actual strategy is a type of that.

          abstract public class IndicatorListStrategyGeneral : Strategy
          abstract public class IndicatorListStrategySpecific_1 : IndicatorListStrategyGeneral

          then the actual strategy which calls runs the market data:

          public class ActualStrategy_1 : IndicatorListStrategySpecific_1

          Hope that makes sense. Remember to make the abstract classes public to make it work (I guess it's private by default) and it took me some time to figure that out as well. This will make my code much more manageable. Appreciate the help.



          Comment


            #6
            Hint: Make sure 'abstract' is before 'public'.

            It's not important to C#, but it makes a difference (I think) to NinjaTrader.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,606 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            15 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X