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

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.
    JesseNinjaTrader Customer Service

    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.
        JesseNinjaTrader Customer Service

        Comment


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

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by TheWhiteDragon, 01-21-2019, 12:44 PM
          5 responses
          551 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by rtwave, 04-12-2024, 09:30 AM
          5 responses
          37 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by funk10101, Today, 12:02 AM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Started by GLFX005, Today, 03:23 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by nandhumca, Yesterday, 03:41 PM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X