NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 04-06-2010, 07:44 PM   #1
KLG08
Junior Member
 
Join Date: Dec 2008
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default '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 at 09:34 PM.
KLG08 is offline  
Reply With Quote
Old 04-06-2010, 10:27 PM   #2
roonius
Certified NinjaScript Consultant
 
Join Date: Oct 2008
Location: Chicago, IL
Posts: 523
Thanks: 0
Thanked 3 times in 3 posts
Send a message via Skype™ to roonius
Default

Quote:
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;
}
roonius is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ATR -Trailing Stops PrTester NinjaScript File Sharing Discussion 36 12-03-2012 05:31 PM
Error on implementing ATR Trailing Stop cowcool Strategy Development 5 06-01-2009 06:59 AM
ATR Trailing Stop cowcool ATM Strategies (Discretionary Trading) 4 05-30-2009 10:24 AM
ATR Trailing Stop BigDog008 Strategy Development 12 03-12-2009 10:20 AM
Using atr for variable Keltner Bands latkinso Strategy Development 2 02-05-2008 06:46 AM


All times are GMT -6. The time now is 06:40 PM.