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 love2code2trade, Yesterday, 01:45 PM
    4 responses
    28 views
    0 likes
    Last Post love2code2trade  
    Started by funk10101, Today, 09:43 PM
    0 responses
    7 views
    0 likes
    Last Post funk10101  
    Started by pkefal, 04-11-2024, 07:39 AM
    11 responses
    37 views
    0 likes
    Last Post jeronymite  
    Started by bill2023, Yesterday, 08:51 AM
    8 responses
    45 views
    0 likes
    Last Post bill2023  
    Started by yertle, Today, 08:38 AM
    6 responses
    26 views
    0 likes
    Last Post ryjoga
    by ryjoga
     
    Working...
    X