Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Declaring Variables based on BarArray()

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

    Declaring Variables based on BarArray()

    What do I need to do when I run into errors in the NinjaScript generated code? I usually see:

    Invalid token 'namespace' in class, struct, or interface member declaration
    Invalid token '{' in class,struct, or interface declaration
    } expected
    Class member declaration expected.

    Here is my indicator. Any other suggestions you see in the code would be helpful. Specifically drawing the indicators in the panel.

    [
    Code:
    public class MyCustomIndicator : Indicator
    {
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MyCustomIndicator";
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    IsSuspendedWhileInactive = true;
    
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 3);
    }
    
    else if (State == State.DataLoaded)
    {
    
    
    primaryseriesd = new Series<double>(Stochastic(BarsArray[0],3,5,3).D);
    primaryseriesd = new Series<double>(Stochastic(BarsArray[0],3,5,3)).D;
    
    secondaryseriesk = new Series<double>(Stochastic(BarsArray[1],3.5,3).K);
    secondaryseriesk = new Series<double>(Stochastic(BarsArray[1],3.5,3).K);
    }
    }
    
    
    protected override void OnBarUpdate()
    
    {
    
    
    }
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartscale)
    
    {
    // 1 - SetUp Your Resource
    SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.DodgerBlue);
    
    // 2 - Use your resource
    
    RenderTarget.DrawLine(startPoint, endPoint, customDXBrush);
    
    // 3 - dispose of your resource
    
    customDXBrush.Dispose();

    #2
    Hello JT2020,

    The errors you mentioned are general C# errors when you have incorrect structure. That would usually be caused by a missing curly brace or semi colon or could be caused by other errors in syntax.

    One item I can see is that the parenthesis on the following line are not consistent:

    primaryseriesd = new Series<double>(Stochastic(BarsArray[0],3,5,3).D);
    primaryseriesd = new Series<double>(Stochastic(BarsArray[0],3,5,3)).D;

    This code also seems to be duplicated.

    if the OnRender in your script is missing the closing brace } that would cause some of the errors as well.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I am receiving the following list of errors.

      The name 'Smooth' does not exist in the current context
      The name 'K' does not exist in the current context
      The name 'PeriodD' does not exist in the current context
      The name 'fastKp' does not exist in the current context
      The name 'fastKp' does not exist in the current context
      The name 'fastKp' does not exist in the current context
      The name 'K' does not exist in the current context
      The name 'D' does not exist in the current context

      I receive the same error messages for the secondary series based on the logic in State == State.DataLoaded.

      I think its the same problem. What should I do? Have I defined my valuables improperly?


      Code:
      {
      public class MyCustomIndicator : Indicator
      {
      
      private Series<double> denp;
      private Series<double> FastKp;
      private MAX maxp;
      private MIN minp;
      private Series<double> nomp;
      private SMA smaKp;
      private SMA smaFastKp;
      
      
      private Series<double> dens;
      private Series<double> FastKs;
      private MAX maxs;
      private MIN mins;
      private Series<double> noms;
      private SMA smaKs;
      private SMA smaFastKs;
      
      
      
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Indicator here.";
      Name = "MyCustomIndicator";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      DisplayInDataBox = true;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
      //See Help Guide for additional information.
      IsSuspendedWhileInactive = true;
      PeriodD = 3;
      PeriodK = 5;
      Smooth = 3;
      AddPlot(Brushes.DodgerBlue, NinjaTrader.Custom.Resource.StochasticsD);
      AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.StochasticsK);
      
      AddLine(Brushes.DarkCyan, 20, NinjaTrader.Custom.Resource.NinjaScriptIndicatorLo wer);
      AddLine(Brushes.DarkCyan, 80, NinjaTrader.Custom.Resource.NinjaScriptIndicatorUp per);
      
      
      }
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Minute, 3);
      
      
      }
      
      else if (State == State.DataLoaded)
      {
      denp = new Series<double>(this);
      nomp = new Series<double>(this);
      FastKp = new Series<double>(this);
      minp = MIN(Low, PeriodK);
      maxp = MAX(High, PeriodK);
      smaFastKp = SMA(fastKp,Smooth);
      smaKp = SMA(K, PeriodD);
      
      dens = new Series<double>(this);
      noms = new Series<double>(this);
      FastKs = new Series<double>(this);
      mins = MIN(Low, PeriodK);
      maxs = MAX(High, PeriodK);
      smaFastKs = SMA(fastKp, Smooth);
      smaKs = SMA(K, PeriodD);
      
      
      }
      
      }
      protected override void OnBarUpdate()
      {
      
      
      if(BarsInProgress == 0)
      
      
      {
      
      double minp0 = minp[0];
      nomp[0] = Close[0] - minp0;
      denp[0] = maxp[0] - minp0;
      if (denp[0].ApproxCompare(0) == 0)
      fastKp[0] = CurrentBar == 0 ? 50 :fastkp[1];
      else
      fastKp[1] = Math.Min(100,Math.Max(0, 100 * nomp[0] / denp[0]));
      
      K[0] = smaFastKp[0];
      D[0] = smaKp[0];
      
      
      }
      
      else if(BarsInProgress == 1)
      
      {
      
      double mins0 = mins[0];
      noms[0] = Close[0] - mins0;
      dens[0] = maxs[0] - mins0;
      if (dens[0].ApproxCompare(0) == 0)
      fastKs[0] = CurrentBar == 0 ? 50 :fastks[1];
      else
      fastKs[1] = Math.Min(100,Math.Max(0, 100 * noms[0] / dens[0]));
      
      K[0] = smaFastKs[0];
      D[0] = smaKs[0];
      
      
      
      
      
      }
      
      
      
      }
      
      }
      
      
      
      
      }

      Comment


        #4
        HI JT2020,

        The error "The name ... does not exist in the current context" means you have skipped ahead of the required foundational step of defining the variables before trying to use them.

        Easy to fix! Just define them.

        For each internal data series (maybe fastKp) define them above OnStateChange() just like "private Series<double> denp;" is defined. For fastKp maybe just change the exising current FastKp to the lower case fastKp?


        For the rest I am guessing many are lengths and periods you want visible as Properties and then data series for Plots you want to be on the chart.

        Just extend (copy, paste and edit) your current Properties region to Define remaining elements.

        Something like ....
        Code:
        #region Properties
        
        [Range(1, int.MaxValue), NinjaScriptProperty]
        [Display(ResourceType = typeof(Custom.Resource), Name = "PeriodD", GroupName = "NinjaScriptParameters", Order = 100)]
        public int PeriodD
        { get; set; }
        
        
        [Range(1, int.MaxValue), NinjaScriptProperty]
        [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 110)]
        public int Smooth
        { get; set; }
        
        
        
        [Browsable(false)]
        [XmlIgnore()]
        public Series<double> K
        {
        get { return Values[0]; }
        }
        
        
        [Browsable(false)]
        [XmlIgnore()]
        public Series<double> D
        {
        get { return Values[1]; }
        }
        
        
        #endregion
        
        }
        }
        Note that you need to add the Properties above the second to the last curly brace so something like.

        Comment


          #5
          I have tried both of those before, Ninjascript fixes the Properties suggestions once the code above it is right. I am not sure how the Ninjascript stochastic formula works in onBarUpdate(). The user input in State.SetDefaults for PeriodD, PeriodK and Smooth is not flowing through. My errors for fastK probably have to do with the common indicators. Any suggestions from Customer Support?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sandman, Today, 02:45 PM
          0 responses
          2 views
          0 likes
          Last Post sandman
          by sandman
           
          Started by grande846, Today, 01:04 PM
          4 responses
          14 views
          0 likes
          Last Post NinjaTrader_ChrisL  
          Started by Gianni78bari, Today, 01:04 PM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by nikorrriko, Today, 12:24 PM
          1 response
          17 views
          0 likes
          Last Post NinjaTrader_ChristopherS  
          Started by tonynt, Today, 12:36 PM
          1 response
          243 views
          0 likes
          Last Post NinjaTrader_Kate  
          Working...
          X