Announcement

Collapse
No announcement yet.

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 CaptainJack, 05-29-2026, 05:09 AM
0 responses
213 views
0 likes
Last Post CaptainJack  
Started by CaptainJack, 05-29-2026, 12:02 AM
0 responses
126 views
0 likes
Last Post CaptainJack  
Started by charlesugo_1, 05-26-2026, 05:03 PM
0 responses
145 views
0 likes
Last Post charlesugo_1  
Started by DannyP96, 05-18-2026, 02:38 PM
1 response
229 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 05-11-2026, 05:56 AM
0 responses
190 views
0 likes
Last Post CarlTrading  
Working...
X