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

'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 ScottWalsh, Today, 06:52 PM
    3 responses
    19 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    18 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    1 view
    0 likes
    Last Post cre8able  
    Started by Haiasi, Today, 06:53 PM
    1 response
    4 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by ScottW, Today, 06:09 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X