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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    569 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    548 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X