Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

'Unassigned Local variable' Error in 'ATR Trailing Stops' indicator

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

    'Unassigned Local variable' Error in 'ATR Trailing Stops' indicator

    The "Indicator as described in Sylvain Vervoort's 'Average True Range Trailing Stops' June 2009 S&C article."
    works great!! However I'm trying to add a second condition to it with a CrossAbove an EMA(period) indicator & I can't resolve the 'Unassigned Local variable' Error for the variable 'trail'. The standard ATR script runs just fine. What am I doing wrong?
    [CODE]
    {
    #region Variables
    privateint period = 20;
    privatedouble multi = 3.5;
    privatedouble trail = 1;//I tride to assign the variable here.
    #endregion
    ///<summary>

    protectedoverridevoid OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;
    double trail;
    double loss = ATR(Input, Period)[0] * Multi;

    if (Close[0] > Value[1] && Close[1] > Value[1])
    trail = Math.Max(Value[
    1], Close[0] - loss);
    ***if (CrossAbove(trail, EMA(period), 1)); //This is where I get the ERROR saying "Unassigned Local variable 'trail'
    Last edited by KLG08; 04-06-2010, 09:34 PM.

    #2
    Originally posted by KLG08 View Post
    The "Indicator as described in Sylvain Vervoort's 'Average True Range Trailing Stops' June 2009 S&C article."
    works great!! However I'm trying to add a second condition to it with a CrossAbove an EMA(period) indicator & I can't resolve the 'Unassigned Local variable' Error for the variable 'trail'. The standard ATR script runs just fine. What am I doing wrong?
    [CODE]
    {
    #region Variables
    privateint period = 20;
    privatedouble multi = 3.5;
    privatedouble trail = 1;//I tride to assign the variable here.
    #endregion
    ///<summary>

    protectedoverridevoid OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;
    double trail;
    double loss = ATR(Input, Period)[0] * Multi;

    if (Close[0] > Value[1] && Close[1] > Value[1])
    trail = Math.Max(Value[
    1], Close[0] - loss);
    ***if (CrossAbove(trail, EMA(period), 1)); //This is where I get the ERROR saying "Unassigned Local variable 'trail'
    on a bar #2 you are trying to access Value[1] which is not defined. May be this will help:

    Set Value before returning

    if(CurrentBar < 1)
    {
    Value.Set(something);
    return;
    }

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    152 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    89 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    131 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    127 views
    1 like
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    107 views
    0 likes
    Last Post CarlTrading  
    Working...
    X