PDA

View Full Version : Bars since a condition


BurtOD
03-17-2009, 06:10 AM
I need to plot the number of bars since a particular condition. What key words are available for this? I found some key words: CurrentBar, BarCount, GotBars. I would like a list of any other key words related to counting bars.

Would a statement like this work?
If(RSI(close,14,3)> StochasticsFast(3,14), BarCount[10])
Any other suggestions?

NinjaTrader_Josh
03-17-2009, 07:12 AM
BurtOD,

Not sure where you got some of those terms. To count bars you have CurrentBar. To find a particular bar index you have GetBar().

BurtOD
03-17-2009, 07:15 AM
I am confused. How do I get bars since a particular condition?

NinjaTrader_Josh
03-17-2009, 07:48 AM
You can save out CurrentBar at the time it was true and then compare it to the most recent CurrentBar. Run the math and you have how many bars ago it occurred.

BurtOD
08-26-2009, 01:50 PM
Josh, ref your suggestion: "You can save out CurrentBar at the time it was true and then compare it to the most recent CurrentBar. Run the math and you have how many bars ago it occurred. " Great suggestion just what I need.

I am using the following code:
protectedoverridevoid OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
int BarsSince = 0;
int TriggerBar = 0;
if (CCI(14)[0] > 100 && BarsSince == 0)
{
TriggerBar = CurrentBar - 1;
BarsSince = CurrentBar - TriggerBar;
}
elseif (CCI(14)[0] > 100 && BarsSince > 0)
{BarsSince = CurrentBar - TriggerBar;}
elseif (CCI(14)[0] > 100 != true)
{
BarsSince = 0;
TriggerBar = 0;
}
PlotTB.Set(TriggerBar);
PlotCB.Set(CurrentBar);
PlotBS.Set(BarsSince);
My problem is Triggerbar updates at every bar as opposed to saving that initial value that I can later compare to CurrentBar. How do I "save out" as you say?
Thanks,
Burt

NinjaTrader_Josh
08-26-2009, 01:54 PM
You need to limit your if-condition to only be true once. You can do this with a bool variable

if (CCI... > 100 && conditionSet == false)
{
TriggerBar =...;
conditionSet = true;
}Then you simply reset the bool when you want to start using the trade condition again.

Paul79
02-06-2010, 05:11 AM
I dont get it (truth is that a few hours ago I saw for the first time this ninja trader language), I just need a simple bars since a event function.

For Tradestation I managed to soleve it in a few minutes like this:
if CCI(14) > 100 and CCI(14) 1 bar ago < 100 then
TriggerBar = BarNumber;
if BarNumber >= TriggerBar then
BarsSince = CurrentBar-TriggerBar;

With Amibroker is much more easy:
Barssince(CCI(14)[0] > 100 and ref(CCI(14),-1) < 100)

But with Ninja I struggle for 4 hours and cant save that darn Trigger bar, God why do you people make this softwares for expert programmers and not for simple traders

Can someone please, create this code from start to end coz, I do not know what I miss, should be simple.

Also how to replicate Valuewhen function from amibroker?

NinjaTrader_Austin
02-06-2010, 11:22 AM
For Tradestation I managed to soleve it in a few minutes like this:
if CCI(14) > 100 and CCI(14) 1 bar ago < 100 then
TriggerBar = BarNumber;
if BarNumber >= TriggerBar then
BarsSince = CurrentBar-TriggerBar;
Hi there, in NinjaScript the code is very similar:

if (CCI(14)[0] > 100 && CCI(14)[1] < 100)
TriggerBar = CurrentBar;

if (CurrentBar >= TriggerBar)
BarsSince = CurrentBar - TriggerBar;

Paul79
02-06-2010, 11:40 AM
Austin thanks for the answer but it`s not working, I tried that and skewed it in all possible ways but cant get the right answer.Here is the code

protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.

int TriggerBar=0;
int BarsSince=0;
if (CCI(14)[0] > 100 && CCI(14)[1] < 100)
TriggerBar = CurrentBar;
if (CurrentBar >= TriggerBar)
BarsSince = CurrentBar - TriggerBar;

Plot0.Set(BarsSince);
}

The above code does nothing more than counting bars and when the condition is true (when CCI crosses 100) the output is zero since Currentbar=Triggerbar, as i said the rest of plot is just Currentbar plot. Perhaps I am missing something...?

Paul79
02-07-2010, 07:24 AM
Is really nobody going to help on this forum or everyone is just as "pro" as I am? By the time i will finish learning this programming language i could start developing software within Visual Studio c++ and stop trading, really nobody is seeing that this is not for traders but for expert programmers and one has really nothing to do with the other. You people at Ninja company should really peek a little at tradestation easylanguage or amibroker AFL or metastock MSFL or other softwares made for traders and not for programmers. Unbelieveble in 2 full days I did not manage to solve this simple thing that took me 5 minutes on other platforms.

Ralph
02-07-2010, 12:11 PM
Paul,

notice that the following declarations are reset to zero every time OnBarUpdate() is invoked:

int TriggerBar=0;
int BarsSince=0;

I guess you intended to put these statements into the variables section of your indicator?

Regards
Ralph

Ralph
02-07-2010, 12:27 PM
... I personally would prefer to code something like this:

int BarsSince;

protected override void OnBarUpdate()
{
BarsSince = CrossAbove(CCI(14), 100, 1) ? 0 : BarsSince++;
Plot0.Set(BarsSince);
}

Regards
Ralph

Paul79
02-07-2010, 12:29 PM
Yeah Ralph, I`ve figure that 1 hour ago..where have you been for 2 days :D. I managed to solve barssince function and now I try to solve valuewhen function and highestsince (lowestsince). It should be a little easier now. It is good that at least functions can be created to be use anywhere trough "UserdefinedMethods". Thank you for the answer.

Ralph
02-07-2010, 12:34 PM
..where have you been for 2 days :D...

was just ignorant, happens sometime.:D

Ralph
02-07-2010, 12:39 PM
...I try to solve valuewhen function and highestsince (lowestsince)...

Consider to use something like this: HigestBar(<High,Low,Open,Close>, <lookback period>)

Paul79
02-07-2010, 01:11 PM
Thanks Ralph but what i mean by highestsince is different (this is from amibroker AFL and this is what i want):

AFL Function Reference - HIGHESTSINCE HIGHESTSINCE
- highest value since condition met




SYNTAX highestsince( EXPRESSION, ARRAY, Nth = 1 ) RETURNS ARRAY FUNCTION Returns the highest ARRAY value since EXPRESSION was true on theNth most recent occurrence. EXAMPLE highestsince( Cross( macd(), 0 ), Close, 1 ) returns the highest close price since macd() has crossed above zero.
Also there is valuewhen

AFL Function Reference - VALUEWHEN VALUEWHEN
- get value of the array when condition met




SYNTAX valuewhen(EXPRESSION, ARRAY, n = 1) RETURNS ARRAY FUNCTION Returns the value of the ARRAY when the EXPRESSION was true on the n -th most recent occurrence. Note: this function allows also 0 and negative values for n - this enables referencing future EXAMPLE valuewhen( cross( close, ma(close,5) ) ,macd(), 1)
so basically the above ready made function I search. In the end i will probably manage to solve them and if i define them within "UserdefinedMethods" I should be able to use them in any indicator. what I do not understand is why they are not already defined, should be extremely easy for a programmer but for me its pretty difficult to learn c++ and I dont need C++ to trade. That is why I said that Ninja programmers should make this easier for traders by defining such functions because I personally am really not interested in learning computer languages.

PS:
Ralph if you can solve them fast and put them here I wont mind ;)

Ralph
02-07-2010, 01:55 PM
Something like this?
Note that BarsSince starts with 1 for this application.

int BarsSince;
int hBar;

protected override void OnBarUpdate()
{
BarsSince = CrossAbove(CCI(14), 100, 1) ? 1 : BarsSince++;
hBar = HigestBar(<High,Low,Open,Close>, BarsSince);
}

Regards
Ralph

Paul79
02-07-2010, 02:11 PM
Not quite, Highestbar method (function returns number of bars ago highest price value occured for lookback period and I need the highest high for the data series since the event took place. Besides that highestsince should be able to return more than last highest high. Its hard to explain but this says all:

highestsince( EXPRESSION, ARRAY, Nth = 1 )

Returns the highest ARRAY value since EXPRESSION was true on theNth most recent occurrence. Like for example finding the highest high of the close price since second last time when CCI(14) crosses 100 would be

highestsince(CrossAbove(CCI(14)[0],100,1),Close,2)

...just as a example if it would be defined, so basically thats what I want to create and then define with "UserDefinedMethods" so I can replace CCI and Close with any other indicators or prices.

Thanks anyway Ralph.

Ralph
02-07-2010, 04:35 PM
The List hBars should contain the bar numbers of the highest bar after each crossover event (not tested).

Regards
Ralph


List<int> hBars = new List<int>();
int lastIndex = -1;
double lastHigh;

protected override void OnBarUpdate()
{
if (CurrentBar < 2) return;
if (FirstTickOfBar)
{
if (CCI(14)[1] > 100 && CCI(14)[2] < 100)
{
hBars.Add(CurrentBar - 1);
lastHigh = High[1];
lastIndex++;
}
else if (lastIndex >= 0)
{
if (High[1] > lastHigh)
{
lastHigh = High[1];
hBars[lastIndex] = CurrentBar - 1;
}
}
}

Paul79
02-08-2010, 04:45 AM
Thanks Ralph but its not what I want. Thanks anyway. However here`s a new problem: i have defined barssince function (method) within "UserDefinedMethods" so I can use it with any custom indicator but the problem is that it only works once. I mean if i try to use it 2 times in the same custom indicator then it messes up everything and does not give the right result.

here is the code used in "UserDefinedMethods"

partial class Indicator
{
int trigger;
public int barssince(bool array)
{
if (array==true)
{
trigger=CurrentBar;
}
if(CurrentBar>trigger)
{
return(CurrentBar-trigger);
}
else
{
return(0);
}
}
}

the above code defines a function barssince(event) that will count the number of bars since event was true.

It works like this barssince(CrossAbove(CCI(14),100,1)) this will count the number of bars after each cross of CCI14 above 100 then will reset to zero and count again at the next cross. It works fine alone but if I try to add another barssince like this barssince(CrossBelow(CCI(14),-100,1)) in the same custom indicators then it does not give the right result. see the code bellow;

double a;
double b;

protected override void OnBarUpdate()
{
if(CurrentBar>0)
{
a=barssince(CrossAbove(CCI(14),100,1));
}
if(CurrentBar>0)
{
b=barssince(CrossBelow(CCI(14),-100,1));
}
Plot0.Set(a);
Plot1.Set(b);
}

Maybe someone knows why and how to solve it? Maybe there is something I did not do right in "UserDefinedMethods" and because of that it messes up everything when used twice in the same custom indicator?

...help???

Paul79
02-08-2010, 04:56 AM
It seems, I post i answer, however sometimes the simpliest solutions are the hardest to see. I just added another barssince named barssince1 in to the userdefined methods like this

partial class Indicator
{
int trigger;
public int barssince(bool array)
{
if (array==true)
{
trigger=CurrentBar;
}
if(CurrentBar>trigger)
{
return(CurrentBar-trigger);
}
else
{
return(0);
}
}
int trigger1;
public int barssince1(bool array1)
{
if (array1==true)
{
trigger1=CurrentBar;
}
if(CurrentBar>trigger1)
{
return(CurrentBar-trigger1);
}
else
{
return(0);
}
}
}

Ralph
02-08-2010, 05:01 AM
Exactly Paul,

you need one for CrossAbove and one for CrossBelow. A better approach would be to implement barssince() as a class. Then you could instantiate class instances as often as you desire and use them independently without the need to duplicate code.

Regards
Ralph

Paul79
02-08-2010, 05:09 AM
what is that Ralph? I mean..what is a class? and how to implement it?. If you mean to have a single barssince(event) method that works anywhere like a new function then that will be great. If you know how to do it then maybe we could add a few new functions to Ninja to make it a little easier. i have no ideea what a class means but I supose that is what you meant.

Ralph
02-08-2010, 06:28 AM
An indicator is implemented as a class for instance.
First you design a class.
Second you instantiate an instance of your class in your application and then you can access the class' public methods and properties.

However, since this concept is unknown to you lets consider this task from another side: Why even implement it as a function? I mean the functionality required is as simple as this:

int BarsSinceCrossAbove;

protected override void OnBarUpdate()
{
BarsSinceCrossAbove= CrossAbove(CCI(14), 100, 1) ? 1 : BarsSinceCrossAbove++;
Plot0.Set(BarsSinceCrossAbove);
}

Regards
Ralph

Paul79
02-08-2010, 06:42 AM
Ralph, the below code does not work, however I solved barssince and it would be truly nice to implement it as a class from what I understand. I am not used with all the "nuts and bolts" C++ requires and honestly dont want to know them but I know very well to code in languages like Metstock`s or easylanguage or amibroker`s. Implementing functions like barssince(event) as a class so they could be used as easy as a SMA for example would be great and I think would not be such a big deal for someone that is a programmer. I dont understand why ninja people want to make programmers out of traders. Now I have to solve that highestsince function (method) and again I am stuck...by the time I will finish I could start to write a new software myself :p and I trade not develop software...LOL. Thanks for your help.

NinjaTrader_Bertrand
02-08-2010, 06:56 AM
Paul79, jumping in here...did you check into the MRO / LRO methods supplied per default for those tasks?

Paul79
02-08-2010, 07:09 AM
Bertrand I have no clue what MRO / LRO or whatever means, and about jumping in here, I expected to solve some simple problems in less than 1 hour as i did with whatever other software for traders BUT Ninja that took me 2 days to solve barssince(event)... "nuts and bolts" as I was saying. Now if you are kind enough to explain MRO /LRO and enlighten me that would be great else thanks for mention I will search them myself.

Paul79
02-08-2010, 07:11 AM
Oh..Bertrand you sure you know how to talk with customers? 995$ paid for the software its not a small amount and I hate when people are rude after I paid money.

Paul79
02-08-2010, 07:19 AM
For that matter your MRO /LRO Bertrand has nothing to do with solving methods like valuewhen, highestsince or others and pretty much makes no difference, barssince I solve it already...after 2 days.

Ralph
02-08-2010, 10:18 AM
Hi Paul, here is an implementation of the trigger bar counter as a separate class (HelperClass). In HelperClassTest you can see how this class is applied 2 times (up- and down-counter). Just use File->Utilities->NinjaScriptImport to install.

The little picture depicts the HelperClassTest in action. The green line counts the bars after the down trigger events, the orange line accordingly vice versa.

However, with this example you can see which way to go if using this approach. You should try to understand this concept before starting own developments, otherwise you won't get happy with that.

Regards
Ralph

Paul79
02-08-2010, 10:39 AM
Hi Ralph, from your picture it seems that it correctly reflects barssince(event). I will try and see what is in those helpers, thanks for trying to help.