![]() |
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
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!! )
|
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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; Code:
int myInt = 1;
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
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
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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.
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
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); |
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
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); |
|
|
|
|
|
#7 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Junior Member
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
It works fine!!!!!
Thank you Ryan!!! |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|