Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator Saving Issue in NinjaTrader

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

    Indicator Saving Issue in NinjaTrader


    I created an indicator with the following code, but when I clickSave as Default, it says there is an error generating XLM document and I can’t save the settings. Also, when I restart NinjaTrader, this indicator is the only one that doesn’t save automatically. Which part of the code could be causing this issue?


    using System;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript.Strategies;
    using System.Windows.Media;
    using NinjaTrader.NinjaScript.DrawingTools;

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class _EMA : Indicator
    {
    private EMA fastEMA;
    private EMA slowEMA;

    [NinjaScriptProperty]
    public int FastPeriod { get; set; } = 5;

    [NinjaScriptProperty]
    public int SlowPeriod { get; set; } = 10;

    [NinjaScriptProperty]
    public Brush BullishColor { get; set; } = Brushes.Gold;

    [NinjaScriptProperty]
    public Brush BearishColor { get; set; } = Brushes.White;

    // New property for arrow size
    [NinjaScriptProperty]
    public double ArrowSize { get; set; } = 100; // Default size value

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Indicator to mark EMA crossovers with arrows";
    AddPlot(Brushes.Silver, "_EMA");
    }
    else if (State == State.DataLoaded)
    {
    fastEMA = EMA(FastPeriod);
    slowEMA = EMA(SlowPeriod);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < Math.Max(FastPeriod, SlowPeriod)) return;

    // Use the ArrowSize property for adjustable arrow size
    double arrowOffset = TickSize * ArrowSize;

    if (CrossAbove(fastEMA, slowEMA, 1))
    {
    // Draw an up arrow for Bullish Cross
    Draw.ArrowUp(this, "BullishCross" + CurrentBar, false, 0, Low[0] - arrowOffset, BullishColor);
    }
    else if (CrossBelow(fastEMA, slowEMA, 1))
    {
    // Draw a down arrow for Bearish Cross
    Draw.ArrowDown(this, "BearishCross" + CurrentBar, false, 0, High[0] + arrowOffset, BearishColor);
    }
    }
    }
    }

    region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
          public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
          {
                private _EMA[] cache_EMA;
                public _EMA _EMA(int fastPeriod, int slowPeriod, Brush bullishColor, Brush bearishColor, double arrowSize)
                {
                      return _EMA(Input, fastPeriod, slowPeriod, bullishColor, bearishColor, arrowSize);
                }

                public _EMA _EMA(ISeries<double> input, int fastPeriod, int slowPeriod, Brush bullishColor, Brush bearishColor, double arrowSize)
                {
                      if (cache_EMA != null)
                            for (int idx = 0; idx < cache_EMA.Length; idx++)
                                  if (cache_EMA[idx] != null && cache_EMA[idx].FastPeriod == fastPeriod && cache_EMA[idx].SlowPeriod == slowPeriod && cache_EMA[idx].BullishColor == bullishColor && cache_EMA[idx].BearishColor == bearishColor && cache_EMA[idx].ArrowSize == arrowSize && cache_EMA[idx].EqualsInput(input))
                                        return cache_EMA[idx];
                      return CacheIndicator<_EMA>(new _EMA(){ FastPeriod = fastPeriod, SlowPeriod = slowPeriod, BullishColor = bullishColor, BearishColor = bearishColor, ArrowSize = arrowSize }, input, ref cache_EMA);
                }
          }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
                public Indicators._EMA _EMA(int fastPeriod, int slowPeriod, Brush bullishColor, Brush bearishColor, double arrowSize)
                {
                      return indicator._EMA(Input, fastPeriod, slowPeriod, bullishColor, bearishColor, arrowSize);
                }

                public Indicators._EMA _EMA(ISeries<double> input , int fastPeriod, int slowPeriod, Brush bullishColor, Brush bearishColor, double arrowSize)
                {
                      return indicator._EMA(input, fastPeriod, slowPeriod, bullishColor, bearishColor, arrowSize);
                }
          }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
                public Indicators._EMA _EMA(int fastPeriod, int slowPeriod, Brush bullishColor, Brush bearishColor, double arrowSize)
                {
                      return indicator._EMA(Input, fastPeriod, slowPeriod, bullishColor, bearishColor, arrowSize);
                }

                public Indicators._EMA _EMA(ISeries<double> input , int fastPeriod, int slowPeriod, Brush bullishColor, Brush bearishColor, double arrowSize)
                {
                      return indicator._EMA(input, fastPeriod, slowPeriod, bullishColor, bearishColor, arrowSize);
                }
          }
    }

    #endregion​

    #2
    Hello imagologie89,

    Welcome to the NinjaTrader forums!

    Public Brush object types need to be serialized.

    See the support article linked below for sample code.



    As a tip, you can export a script and attach the export instead of posting large text of code on the forum or in email.

    To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript Add-on...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter the script name in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    http://ninjatrader.com/support/helpG...nt8/export.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3

      Hi Chelsea, thanks for your reply! I followed your advice and serialized the public Brush object type, but I’m still having issues when trying to save as default. It says there’s a problem generating the XML document, and the settings aren’t being saved automatically. Could you take another look? I’d really appreciate it!

      Attached Files

      Comment


        #4
        Hello imagologie89,

        On line 26 the brush is not using the XmlIgnore attribute.
        On line 29 the brush is not using the XmlIgnore attribute.

        The default value for variables should be assigned in State.SetDefaults of OnStateChange().

        You do not need to declare a second BrushToString method. You do not need to declare a second StringToBrush method
        Use the existing Serialize.BrushToString() method and existing Serialize.StringToBrush() method.

        Your code should appear like the sample code 'Serialize a ‘Brush’ code sample' in the support article.
        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
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X