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 cre8able, Yesterday, 01:16 PM
3 responses
11 views
0 likes
Last Post cre8able  
Started by Harry, 05-02-2018, 01:54 PM
10 responses
3,203 views
0 likes
Last Post tharton3  
Started by ChartTourist, Today, 08:22 AM
0 responses
6 views
0 likes
Last Post ChartTourist  
Started by LiamTwine, Today, 08:10 AM
0 responses
2 views
0 likes
Last Post LiamTwine  
Started by Balage0922, Today, 07:38 AM
0 responses
5 views
0 likes
Last Post Balage0922  
Working...
X