NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-08-2010, 10:16 AM   #1
Roumeau
Junior Member
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default Help, I need somebody...

Hello,

I want to make an indicator but there is a big problem;

the idea is that when a situation occur, like a line cross above another, a variable, well let's call it flag, equals 1.
When an another situation occurs, the flag = -1.

The problem is that i've got to declare this variable, typically int flag = 0;
and when a situation on a bar a situation occurs, flag = 1 or -1, but the next bar, it equals 0 (due to the declaration).
I want that the value of flag remains until a new situation or event.

Is that possible? I knew that is possible in Strategy with user variable but in an indicator?

Thank U a lot!!!

(Sorry for my english!! )
Roumeau is offline  
Reply With Quote
Old 03-08-2010, 10:27 AM   #2
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hello Romeau,

Welcome to our forums.

A variable will use its last state. You don't have to declare a default value for the variable.
When you are declaring the variable use:

Code:
int myInt;
instead of

Code:
int myInt = 1;
NinjaTrader_RyanM is offline  
Reply With Quote
Old 03-08-2010, 11:07 AM   #3
Roumeau
Junior Member
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanx but it doesn't work.
Here is my code:


if (Close[0]>KotHigh)
{
flag = 1;
}
if (Close[0]<KotLow)
{
flag = 0;
}

and the error:

Indicator\Kot.cs Use of unassigned local variable 'flag' CS0165 - click for info 63 8

I think that the two events don't appear often so there is no value for flag...

Arrrrrr
Roumeau is offline  
Reply With Quote
Old 03-08-2010, 11:30 AM   #4
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Can you post the whole code segment you're using? It may be the variable is not available within the scope you're trying to access it from.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 03-08-2010, 11:32 AM   #5
Roumeau
Junior Member
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

protected override void OnBarUpdate()
{
double KotHigh = SMA(High,periodSma)[0] + (Aa * ATR(Bb)[0]);
double KotLow = SMA(Close,periodSma)[0] - (Aa * ATR(Bb)[0]);

int flag;
double signal;






if (Close[0]>KotHigh)
{
flag = 1;
}
if (Close[0]<KotLow)
{
flag = 0;
}

while (flag != 1 || flag != 0)
{
flag = 0;
}




if (flag==1)
{
signal=KotLow;
}
else
{
signal=KotHigh;
}


// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
Plot0.Set(signal);
Roumeau is offline  
Reply With Quote
Old 03-08-2010, 11:33 AM   #6
Roumeau
Junior Member
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

OOpppss, forget the last post :



protected override void OnBarUpdate()
{
double KotHigh = SMA(High,periodSma)[0] + (Aa * ATR(Bb)[0]);
double KotLow = SMA(Close,periodSma)[0] - (Aa * ATR(Bb)[0]);

int flag;
double signal;






if (Close[0]>KotHigh)
{
flag = 1;
}
if (Close[0]<KotLow)
{
flag = 0;
}




if (flag==1)
{
signal=KotLow;
}
else
{
signal=KotHigh;
}


// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
Plot0.Set(signal);
Roumeau is offline  
Reply With Quote
Old 03-08-2010, 11:51 AM   #7
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Move your variables declaration from OnBarUpdate() to the variables region.

Variables region is colored in grey and you have to click the arrow to expand this section. Click the arrow and then add those declarations.


Code:
 
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below.
int flag;
double signal;
#endregion
NinjaTrader_RyanM is offline  
Reply With Quote
Old 03-08-2010, 12:09 PM   #8
Roumeau
Junior Member
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Smile

It works fine!!!!!

Thank you Ryan!!!
Roumeau 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


All times are GMT -6. The time now is 12:23 AM.