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

CS0103: A local or parameter named 'ATR_Stop' cannot be declared..

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

    CS0103: A local or parameter named 'ATR_Stop' cannot be declared..

    Hello,

    I am trying to make a stop loss and take profit targets dynamic based on values of a specified average true range. I'd like to include max and min values for the ATR, thus keeping my stop sizes and take profits from being too large or too small. I keep getting this CS0103 error though. I'm not sure why as it seems like the for block I have to set ATR_Stop defines the value, so I am confused as to why I cannot call the value later in the code as a reference for my stop loss value. Any help here would be greatly appreciated.

    The variable that is tripping me up is 'ATR_Stop'

    // Set ATR max and min
    bool ATR_middle = ATR(20)[0] >= 1.5 && ATR(20)[0] <= 5;
    bool ATR_min = ATR(20)[0] < 1.5;
    bool ATR_max = ATR(20)[0] > 5;

    if (ATR_middle)
    {
    double ATR_Stop = ATR(20)[0];
    }
    else if (ATR_min)
    {
    double ATR_Stop = 1.5;
    }
    else
    {
    double ATR_Stop = 5;
    }

    // Enter Long Positions

    if (trade_times && upper_threshold)

    {

    if (cross_above)
    {
    EnterLong("LongPosition");

    // Sets Stop-Loss
    SetStopLoss("LongPosition", CalculationMode.Price, Closes[0][0] - (atrMultiplierStop * ATR_Stop), false);

    // Sets Take-Profit
    SetProfitTarget("LongPosition", CalculationMode.Price, Closes[0][0] + (atrMultiplierProfit * ATR_Stop), false);
    }


    }

    // Exit Positions
    if (!market_hours)
    {
    ExitLong();
    ExitShort();
    }

    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="FastMA", Order=1, GroupName="Parameters")]
    public int FastMA
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="MidMA", Order=2, GroupName="Parameters")]
    public int MidMA
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="SlowMA", Order=3, GroupName="Parameters")]
    public int SlowMA
    { get; set; }
    #endregion

    }
    }
    Last edited by jrbertram; 11-15-2023, 06:14 PM.

    #2
    With some trial and error I was able to solve this.

    By maintaining the use of the defined variable inside the same if statement it was defined in this seemed to do the trick.

    Instead of
    "if (ATR_middle)
    {
    double ATR_Stop = ATR(20)[0];
    }
    else if (ATR_min)
    {
    double ATR_Stop = 1.5;
    }
    else
    {
    double ATR_Stop = 5;
    }

    // Enter Long Positions

    if (trade_times && upper_threshold)

    {

    if (cross_above)
    {
    EnterLong("LongPosition");

    // Sets Stop-Loss
    SetStopLoss("LongPosition", CalculationMode.Price, Closes[0][0] - (atrMultiplierStop * ATR_Stop), false);

    // Sets Take-Profit
    SetProfitTarget("LongPosition", CalculationMode.Price, Closes[0][0] + (atrMultiplierProfit * ATR_Stop), false);
    }
    ​"

    I meshed the two to this:

    if (ATR_middle)
    {
    double ATR_Stop = ATR(20)[0];
    SetStopLoss("LongPosition", CalculationMode.Price, Closes[0][0] - (atrMultiplierStop * ATR_Stop), false);
    SetProfitTarget("LongPosition", CalculationMode.Price, Closes[0][0] + (atrMultiplierProfit * ATR_Stop), false);
    }
    else if (ATR_min)
    {
    double ATR_Stop = 1.5;
    SetStopLoss("LongPosition", CalculationMode.Price, Closes[0][0] - (atrMultiplierStop * ATR_Stop), false);
    SetProfitTarget("LongPosition", CalculationMode.Price, Closes[0][0] + (atrMultiplierProfit * ATR_Stop), false);
    }
    else
    {
    double ATR_Stop = 5;
    SetStopLoss("LongPosition", CalculationMode.Price, Closes[0][0] - (atrMultiplierStop * ATR_Stop), false);
    SetProfitTarget("LongPosition", CalculationMode.Price, Closes[0][0] + (atrMultiplierProfit * ATR_Stop), false);
    }

    And this seemed to do the trick.

    Comment


      #3
      Hello jrbertram,

      The scope refers to the hierarchy in which a variable is declared and can be accessed. A Class Level variable is declared within the block (opening and closing curly brace) of the class and can be accessed within any method and code block. A Method Level variable is declared within the block of a method and be accessed within the method below the variable definition. A variable declared in a code block such as a if statement, can only be accessed within the if statement code block and cannot be accessed outside of the code block even in the same method.
      Code blocks (logic such as if statements) are within the scope of a method. A method will be declared within the scope of a class. A class is declared within the scope of a namespace.

      Below is a publicly available link to a 3rd party educational site on C# that discusses scope.

      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Awesome, thank you very much!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        92 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        6 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        158 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        7 views
        0 likes
        Last Post Belfortbucks  
        Started by zstheorist, Yesterday, 07:52 PM
        0 responses
        7 views
        0 likes
        Last Post zstheorist  
        Working...
        X