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

Simple script but keep on getting this error!!!

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

    Simple script but keep on getting this error!!!

    I am trying to write an indicator to display the high/low of the current bar. Here is my code:


    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class RMHighLow : Indicator
    {
    #region Variables
    // Wizard generated variables
    private double high = 0; // Default setting for High
    private double low = 0; // Default setting for Low
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Khaki), PlotStyle.Line, "High1"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Khaki), PlotStyle.Line, "Low1"));
    Overlay = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    High1.Set(High[0]);
    Low1.Set(Low[0]);

    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries High1
    {
    get { return Values[0]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Low1
    {
    get { return Values[1]; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double High
    {
    get { return high; }
    set { high = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double Low
    {
    get { return low; }
    set { low = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private RMHighLow[] cacheRMHighLow = null;

    private static RMHighLow checkRMHighLow = new RMHighLow();

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public RMHighLow RMHighLow(double high, double low)
    {
    return RMHighLow(Input, high, low);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public RMHighLow RMHighLow(Data.IDataSeries input, double high, double low)
    {
    if (cacheRMHighLow != null)
    for (int idx = 0; idx < cacheRMHighLow.Length; idx++)
    if (Math.Abs(cacheRMHighLow[idx].High - high) <= double.Epsilon && Math.Abs(cacheRMHighLow[idx].Low - low) <= double.Epsilon && cacheRMHighLow[idx].EqualsInput(input))
    return cacheRMHighLow[idx];

    lock (checkRMHighLow)
    {
    checkRMHighLow.High = high;
    high = checkRMHighLow.High;
    checkRMHighLow.Low = low;
    low = checkRMHighLow.Low;

    if (cacheRMHighLow != null)
    for (int idx = 0; idx < cacheRMHighLow.Length; idx++)
    if (Math.Abs(cacheRMHighLow[idx].High - high) <= double.Epsilon && Math.Abs(cacheRMHighLow[idx].Low - low) <= double.Epsilon && cacheRMHighLow[idx].EqualsInput(input))
    return cacheRMHighLow[idx];

    RMHighLow indicator = new RMHighLow();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    indicator.High = high;
    indicator.Low = low;
    Indicators.Add(indicator);
    indicator.SetUp();

    RMHighLow[] tmp = new RMHighLow[cacheRMHighLow == null ? 1 : cacheRMHighLow.Length + 1];
    if (cacheRMHighLow != null)
    cacheRMHighLow.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheRMHighLow = tmp;
    return indicator;
    }
    }
    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.RMHighLow RMHighLow(double high, double low)
    {
    return _indicator.RMHighLow(Input, high, low);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator.RMHighLow RMHighLow(Data.IDataSeries input, double high, double low)
    {
    return _indicator.RMHighLow(input, high, low);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.RMHighLow RMHighLow(double high, double low)
    {
    return _indicator.RMHighLow(Input, high, low);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator.RMHighLow RMHighLow(Data.IDataSeries input, double high, double low)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.RMHighLow(input, high, low);
    }
    }
    }
    #endregion

    When I try to compile this, it keeps on complaining Cannot apply indexing with [] to an expression of type double. CS 0021. (for the highlighted lines)

    I cant understand whats wrong. Its exactly as other indicators like PriceOHLC are doing. Any pointers as to how to fix this?

    Appreciate all the help!!!

    #2
    Hi magnatron,

    When I try to compile this, it keeps on complaining Cannot apply indexing with [] to an expression of type double. CS 0021. (for the highlighted lines)

    I cant understand whats wrong. Its exactly as other indicators like PriceOHLC are doing. Any pointers as to how to fix this?
    You're right, syntax is exactly fine there. My biggest suspicion is using the names High and Low for your public inputs. I would delete this script and create a different one. When creating the inputs, use something like MyHigh or MyLow, or avoid specifying at all. It doesn't look like the script uses them.

    What is happening, is the script thinks you're trying to set the plot based on your double input High and not the built in series High.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks that solved the problem. Its working fine now. Truly appreciate the help!!!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Tim-c, Today, 03:54 AM
      0 responses
      3 views
      0 likes
      Last Post Tim-c
      by Tim-c
       
      Started by FrancisMorro, Today, 03:24 AM
      0 responses
      2 views
      0 likes
      Last Post FrancisMorro  
      Started by Segwin, 05-07-2018, 02:15 PM
      10 responses
      1,771 views
      0 likes
      Last Post Leafcutter  
      Started by Rapine Heihei, 04-23-2024, 07:51 PM
      2 responses
      31 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by Shansen, 08-30-2019, 10:18 PM
      24 responses
      945 views
      0 likes
      Last Post spwizard  
      Working...
      X