Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The name "__" does not exist in the current context

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

    The name "__" does not exist in the current context

    I am trying to develop a strategy and am fairly new to coding, but have a little experience. However, I cannot figure out what I am missing. I am sure it is something so simple, but can anyone help me?

    Code as follows:

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class AutomatedHMAUnlocked : Strategy
    {
    private HMAFilterexample HMAFilterexample1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "AutomatedHMAUnlocked";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.High;
    OrderFillResolutionType = BarsPeriodType.Minute;
    OrderFillResolutionValue = 1;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    int SlopeBars = 3;
    int Quantity = 1;
    int HMAPeriod = 50;
    double HMAthreshold = 0.300;
    bool PrintSlope = false;

    IsInstantiatedOnEachOptimizationIteration = false;

    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    HMAFilterexample = HMAFilterexample(Close, HMAPeriod, SlopeBars, HMAthreshold, false);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < Math.Max(HMAPeriod, SlopeBars))
    {
    return;
    }

    double HMAslope = Slope(HMAFilterexample1, SlopeBars, 0); // Calculate slope of HMA line


    if (PrintSlope)
    {
    Print ("Time: "+Time[0].TimeOfDay+" Slope: "+HMAslope);
    }

    if (HMAslope > HMAthreshold)
    {
    PlotBrushes[0][0] = Brushes.Green;
    EnterLong(Convert.ToInt32(Quantity), "");
    }
    else if (HMAslope < -HMAthreshold)
    {
    PlotBrushes[0][0] = Brushes.Red;
    EnterShort(Convert.ToInt32(Quantity), "");
    }
    else
    {
    PlotBrushes[0][0] = Brushes.Gold;
    }


    }
    }
    }

    #2
    Hello bigc0220,

    Thank you for the question.

    I tried the code you provided but it looks like this is both not complete and uses non-standard indicators. Can you tell me, is this the entire syntax of the script or is there more you omitted from the bottom?

    The error The name "__" does not exist in the current context
    means that some variable you are using does not exist where you are trying to use it. If this is not the full script, I wouldnt be sure what specifically is the problem. If this is the full script, the problem is that you are missing the public properties you used such as HMAPeriod.

    When posting on the support forum, it is helpful to include the actual script instead of a copy paste to avoid any confusions. For a script that cannot compile, you can just include the .cs file from the folder NinjaTrader 8\bin\Custom\{type of item}\{items name}.cs

    For items that can compile, you can export them: https://ninjatrader.com/support/help...tAsSourceFiles

    I look forward to being of further assistance.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello bigc0220,

      Thank you for the question.

      I tried the code you provided but it looks like this is both not complete and uses non-standard indicators. Can you tell me, is this the entire syntax of the script or is there more you omitted from the bottom?

      The error The name "__" does not exist in the current context
      means that some variable you are using does not exist where you are trying to use it. If this is not the full script, I wouldnt be sure what specifically is the problem. If this is the full script, the problem is that you are missing the public properties you used such as HMAPeriod.

      When posting on the support forum, it is helpful to include the actual script instead of a copy paste to avoid any confusions. For a script that cannot compile, you can just include the .cs file from the folder NinjaTrader 8\bin\Custom\{type of item}\{items name}.cs

      For items that can compile, you can export them: https://ninjatrader.com/support/help...tAsSourceFiles

      I look forward to being of further assistance.

      My apologies. I did copy and paste the full script, but I also linked it below. I compared it to strategy done in the wizard and for the most part, everything was the same except for some little things. Are the public properties not lines 57-61? I tried using the CS0103 link associated with the error, but I don't think that is what I needed because I tried what it said and it is still giving me errors. I apologize for any inconvenience or headaches and thank you for your time!
      Attached Files

      Comment


        #4
        Hello bigc0220,

        Thank you for providing the script and actually, your comment points out the problem. I had overlooked where you placed the variables as this is not usually where you would see the public properties defined.

        Public properties are actually going to be outside of the methods you define, this is a C# concept so this is where your problem resides.

        What you have done in this script is declared a "local variable" instead of a "property".

        The error comes when you try to use the local variable out of the scope where it was declared. How you have it now, the variable HMAPeriod could only be used inside the { and }
        or :

        Code:
        if (State == State.SetDefaults)
        [COLOR="Red"]{
        //these variables can only be used inside of the { and } of this if statement because they have been declared here. [/COLOR]
        
        int SlopeBars	= 3;
        int Quantity	= 1;
        int HMAPeriod	= 50;
        double HMAthreshold	= 0.300;
        bool PrintSlope	= false;
        [COLOR="Red"]}[/COLOR]

        If you intend for these values to be settable in the user interface and also can be used througout your code, you need to delete these variables and instead make them like the following:

        Code:
        [Range(1, int.MaxValue), NinjaScriptProperty]
        [Display(Name = "HMAPeriod", GroupName = "NinjaScriptParameters", Order = 0)]
        public int HMAPeriod
        { get; set; }
        This is one property along with the correct attributes. You would additionally need to set a default value in SetDefaults:

        Code:
        if (State == State.SetDefaults)
        {
        
            HMAPeriod = 50;
        }
        You can find more on properties in the attributes section here: https://ninjatrader.com/support/help...attributes.htm

        Properties are defined near the bottom of the file inside of the main class but outside of any methods in the class. In the file you provided, this would start after line 106 or after OnBarUpdate but inside the class.

        You can view the SMA indicator for an example of a single property and how it is used in the remainder of the script. It has the Period property.

        Alternatively, you can generate a new strategy or indicator and use the "Input Parameters" section to generate this syntax for each type of input. This is suggested for Brushes specifically to generate the serialization syntax.

        I look forward to being of further assistance.
        Last edited by NinjaTrader_Jesse; 09-18-2018, 09:56 AM.

        Comment


          #5
          Thank you so much! You have been very helpful and it all makes more sense now.

          Comment


            #6
            ​I'm struggling with a similar issue. how do i fix it?

            Comment


              #7
              Hello bsteeze,

              Welcome to the NinjaTrader forums!

              In the screenshot you have provided, the error is on line 30 and 31.

              May I have you provide a screenshot of the top of this script or provide the .cs file?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                region Using declarations
                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.ComponentModel.DataAnnotations;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Windows;
                using System.Windows.Input;
                using System.Windows.Media;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Gui;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Gui.SuperDom;
                using NinjaTrader.Gui.Tools;
                using NinjaTrader.Data;
                using NinjaTrader.NinjaScript;
                using NinjaTrader.Core.FloatingPoint;
                using NinjaTrader.NinjaScript.Indicators;
                using NinjaTrader.NinjaScript.DrawingTools;
                #endregion


                namespace NinjaTrader.NinjaScript.Indicators
                {
                public class SupportResist : Indicator
                {
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = "Indicator for plotting support and resistance lines.";
                Name = "SupportResist";
                Calculate = Calculate.OnBarClose; // Or Calculate.OnEachTick, Calculate.OnPriceChange
                IsOverlay = true;
                }
                }

                protected override void OnBarUpdate()
                {
                if (CurrentBar < 1) return; // Ensures there is at least one bar to compare against

                double resistance = High[0]; // Placeholder for actual resistance calculation
                double support = Low[0]; // Placeholder for actual support calculation

                // Use 'this' to explicitly refer to instance members
                this.Draw.HorizontalLine("resistanceLine", resistance, Brushes.Red, DashStyles.Solid, 2);
                this.Draw.HorizontalLine("supportLine", support, Brushes.Green, DashStyles.Solid, 2);
                }
                }
                }

                region NinjaScript generated code. Neither change nor remove.

                namespace NinjaTrader.NinjaScript.Indicators
                {
                public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                {
                private SupportResist[] cacheSupportResist;
                public SupportResist SupportResist()
                {
                return SupportResist(Input);
                }

                public SupportResist SupportResist(ISeries<double> input)
                {
                if (cacheSupportResist != null)
                for (int idx = 0; idx < cacheSupportResist.Length; idx++)
                if (cacheSupportResist[idx] != null && cacheSupportResist[idx].EqualsInput(input))
                return cacheSupportResist[idx];
                return CacheIndicator<SupportResist>(new SupportResist(), input, ref cacheSupportResist);
                }
                }
                }

                namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                {
                public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                {
                public Indicators.SupportResist SupportResist()
                {
                return indicator.SupportResist(Input);
                }

                public Indicators.SupportResist SupportResist(ISeries<double> input )
                {
                return indicator.SupportResist(input);
                }
                }
                }

                namespace NinjaTrader.NinjaScript.Strategies
                {
                public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                {
                public Indicators.SupportResist SupportResist()
                {
                return indicator.SupportResist(Input);
                }

                public Indicators.SupportResist SupportResist(ISeries<double> input )
                {
                return indicator.SupportResist(input);
                }
                }
                }

                #endregion

                Comment


                  #9
                  im having issues with the 'draw' function. line 49 & 50

                  Comment


                    #10
                    Hello bsteeze,

                    Have you modified the using statements at the top of the file?

                    You will need to use the overload signature of Draw.HorizontalLine() as defined in the help guide.

                    Draw.HorizontalLine(NinjaScriptBase owner, string tag, bool isAutoScale, double y, Brush brush, DashStyleHelper dashStyle, int width)



                    For the owner parameter supply the word this to supply the indicator instance as the owner.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    56 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    132 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    73 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    45 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    49 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X