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

Object lifecycle issue (I think)

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

    Object lifecycle issue (I think)

    i have created an indicator (MyPositionActionIndicator) that iterates through all the indicators on a chart and only executes operations on indicators that implement an interface that i have defined, IPositionAction. i am getting weird behavior that i think is related to the indicator lifecycle but i am not sure. i have written indicators that generate trading signals and each implements the IPositionAction interface which defines a GetPosition() function that returns an enum of PositionDirection { Long, Short, Flat}. the MyPositionIndicator "watches" for when all trading signals are equal i.e. each indicator that implements the interface returns an identical enum value for short or long and acts accordingly. the indicators that implement the interface have logic in the OnBarUpdate method which assigns class level variables that get referenced in the GetPosition() function (it is at this point where the breakdown occurs, i think). below is my code for the MyPositionActionIndicator. Any ideas, thanks?

    public class MyPositionActionIndicator : Indicator {
    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Description = @"Tracks all indicators that implement the IPositionAction interface to execute common operations";
    Name = "MyPositionActionIndicator";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    }
    else if (State == State.Configure) {
    ClearOutputWindow();
    }
    }
    protected override void OnBarUpdate() {
    var positions = new LimitedList<PositionDirection>(DefaultLimitedListS ize);
    foreach (var i in ChartControl.Indicators) {
    if (i != null && i is IPositionAction ipa)
    positions.Add(ipa.GetPosition()); // breakdown occurs here.
    }
    if (positions.Count > 0 && positions.AreItemsEqual()) {
    var pos = positions.Last();
    if (pos == PositionDirection.Long)
    Draw.TriangleUp(this, "EnterLong" + CurrentBar, true, 0, 1, Brushes.LimeGreen, false);
    else if (pos == PositionDirection.Short)
    Draw.TriangleDown(this, "EnterShort" + CurrentBar, true, 0, 1, Brushes.Red, false);
    else
    Draw.Dot(this, "Range" + CurrentBar, true, 0, 1, Brushes.Black, false);

    }
    }
    }​

Latest Posts

Collapse

Topics Statistics Last Post
Started by Segwin, 05-07-2018, 02:15 PM
14 responses
1,788 views
0 likes
Last Post aligator  
Started by Jimmyk, 01-26-2018, 05:19 AM
6 responses
837 views
0 likes
Last Post emuns
by emuns
 
Started by jxs_xrj, 01-12-2020, 09:49 AM
6 responses
3,293 views
1 like
Last Post jgualdronc  
Started by Touch-Ups, Today, 10:36 AM
0 responses
12 views
0 likes
Last Post Touch-Ups  
Started by geddyisodin, 04-25-2024, 05:20 AM
11 responses
62 views
0 likes
Last Post halgo_boulder  
Working...
X