Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Control reference not behaving as expected

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

    Control reference not behaving as expected

    I am trying to create a control that will detect crossovers of two moving averages based on the name and type that the user has on their chart. So if a user has an HMA and an EMA already on the chart it will get a reference to those indicators and flag the crossover. However, the reference does not update with each OnBarUpdate and it outputs the same MA value. I have included the code that just attempts to reference one MA. Any ideas?

    public class MyCrossOverIndicator : Indicator {
    private IndicatorBase _ib;
    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Name = "MyCrossOverIndicator";
    Calculate = Calculate.OnBarClose;

    AddPlot(Brushes.Orange, "plot");
    }
    else if (State == State.Configure) {
    ClearOutputWindow();
    }
    else if (State == State.DataLoaded) {
    // Attempt to find an EMA indicator already applied on the chart
    _ib = FindIndicator(IndicatorName, typeof(EMA));
    }
    }

    protected override void OnBarUpdate() {
    if (_ib != null) {
    EMA ema = (EMA)_ib;
    Values[0][0] = ema[0];
    Print($"name: {ema.Name}/{ema.Count}, value: {ema[0]}");
    }
    }
    private IndicatorBase FindIndicator(string indicatorName, Type indicatorType) {
    foreach (IndicatorBase indicator in ChartControl.Indicators) {
    if (indicator.GetType() == indicatorType && indicator.Name == indicatorName)
    return indicator;
    }
    return null;
    }

    [NinjaScriptProperty]
    [Display(Name = "Indicator Name", GroupName = "Setup")]
    public string IndicatorName { get; set; }
    }​

    #2
    Hello love2code2trade,

    This code would be undocumented and not supported by the NinjaTrader Support team.
    There can be limitations using unsupported code and advanced C# concepts in a NinjaScript, so your mileage may vary.

    This thread will remain open for any community members that would like to assist with the unsupported code.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      i would appreciate those contacts. also, do know of any other threads on the forum that discuss this topic that i could review? thanks.

      Comment


        #4
        Hello love2code2trade,

        I'm not actually quite sure, but I don't think it's properly accessing the series.
        A hosted symbiote indicator is linked to the price series for the host. In this case your custom indicator is not it's host so it can't properly access the price series as a data series.
        This might be totally incorrect, and I would be just guessing.

        But I notice if you use:
        Values[0][0] = ema.Values[0].GetValueAt(CurrentBar);

        This seems to produce the correct value.

        You can search our extensive library of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more!
        Programming Services - https://ninjatraderecosystem.com/sea...mming-services
        Educators - https://ninjatraderecosystem.com/sea...ures=education

        You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third party services for NinjaTrader all pricing and support information will need to be obtained through the consultant.

        This NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

        Please let me know if you have any questions, or if I can provide any further assistance.​
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        670 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        379 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        582 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X