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

Best way to code indicator output when using custom class as output

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

    Best way to code indicator output when using custom class as output

    I have a requirement for an indicator which identifies swing points, i.e. higher highs or lower lows on a bar chart. Coding it visually is done, and I now need to expose the list of swing points so I can access them in another indicator or strategy.

    The output is a dictionary of objects of my custom class, keyed by bar number.

    If I set up my strategy to work like this:

    Code:
    public class PermaCodeSwingState : Indicator
    {
        PermaCodeSwing swingIndy;
        
        protected override void Initialize() 
        {
            Overlay = true;
            CalculateOnBarClose = true;
            Add(PermaCodeSwing(2, 2, 20, 0.00001));
            swingIndy = PermaCodeSwing(2, 2, 20, 0.00001);    
        }
        
        protected override void OnBarUpdate()
        {
            swingIndy.Update();
            if (CurrentBars[0] < BarsRequired)
            {                
                return;
            }
            
        }
    I have to include that Update() call because otherwise the indicator doesn't update. This is the relevant part of the indie code, showing just instantiation of the SwingPoint for a swing high, and putting it in the dictionary:

    Code:
    private Dictionary<int, SwingPoint> swingHighs 
        = new Dictionary<int, SwingPoint>(), swingLows = new Dictionary<int, SwingPoint>();
    public class SwingPoint
    {
        public int before, after;
        public IBar bar;
        public int barNum;
        public IBar creationBar;
        public int creationBarNum;
        public DateTime creationTime;
        public SwingPoint(int newBefore, int newAfter, int swingBarNum, IBar swingBar, 
            int cb, IBar currentIBar)
        {
            this.before = newBefore;
            this.after = newAfter;
            this.bar = swingBar;
            this.barNum = swingBarNum;
            this.creationBar = currentIBar;
            this.creationBarNum = cb;
        }
    }
    protected override void OnBarUpdate()
    {
        if (isSwingHigh) 
        {
            SwingPoint swingPoint = new SwingPoint(before, after, 
                CurrentBar - swingBar, Bars.Get(CurrentBar - swingBar), 
                CurrentBar, Bars.Get(CurrentBar));
            swingHighs.Add(CurrentBar - swingBar, swingPoint);
        }
    }
    
    public Dictionary<int, SwingPoint> SwingHighs 
    {
        get { return swingHighs; }
    }
    My question is whether this way of doing it is going to be a performance killer compared to another way, maybe subclassing IDataSeries. I have an old recollection of discussing IDataSeries but I can't find any reference to subclassing it now online.

    Additionally I'm worried at this point that writing the swing points algorithm into a seperate indicator will also incur unnecessary overhead compared to writing it as a simple function in an NinjaTrader.Indicator partial class. Please advise.

    Thanks.

    #2
    An Dataseries interface would be more resource intensive as it is synced with each bar on the chart.

    If you only have a handful of swing points, a dictionary or list would be more efficient and easier to access these values.

    I have no advice on writing a new indicator or using a function. This would ultimately depend on how you're using it. They should both process the same amount of resources.
    MatthewNinjaTrader Product Management

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by selu72, Today, 02:01 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Zachary  
    Started by WHICKED, Today, 02:02 PM
    2 responses
    10 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by f.saeidi, Today, 12:14 PM
    8 responses
    21 views
    0 likes
    Last Post f.saeidi  
    Started by Mikey_, 03-23-2024, 05:59 PM
    3 responses
    51 views
    0 likes
    Last Post Sam2515
    by Sam2515
     
    Started by Russ Moreland, Today, 12:54 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Erick  
    Working...
    X