How to check last high or low value on stochastic using while loop

Created at 03 Nov 2022, 05:07
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MongolTrader's avatar

MongolTrader

Joined 12.02.2015

How to check last high or low value on stochastic using while loop
03 Nov 2022, 05:07


I need check whether last stochastic value was higher than 95 or lower than 5 using while loop. It cant use for loop. Because i don't know how many period before this values match with my condition. 


@MongolTrader
Replies

firemyst
03 Nov 2022, 11:01

Why do you need to check it in a while loop? What's your logic or what are you trying to accomplish?


@firemyst

pick
03 Nov 2022, 11:49

Like the user above, I'm unsure as to why you require it to be a while loop. 

There are many options for what it sounds like you're looking to achieve.

The second statement in a for loop is a boolean check, for example you could do something like this:

bool found = false;

for (int i = startIndex; !found; i--)
{
	if (stoch[i] > 95 || stoch[i] < 5)
		found = true;
}

 

Ideally, you should identify the bounds of the loop, but you can combine that check:

bool found = false;

for (int i = startIndex; i >= 0 && !found; i--)
{
	if (stoch[i] > 95 || stoch[i] < 5)
		found = true;
}

 

However, if you really do require it to be a while loop, here's a simple example:

bool found = false;
int i = startIndex;

while (!found)
{
	if (stoch[i] > 95 || stoch[i] < 5)
		found = true;
		
	i--;
}

Just be cautious to not get stuck in an infinite loop, or access invalid indices from collections.


@pick

MongolTrader
04 Nov 2022, 06:02

RE:

pick said:

Like the user above, I'm unsure as to why you require it to be a while loop. 

There are many options for what it sounds like you're looking to achieve.

The second statement in a for loop is a boolean check, for example you could do something like this:

bool found = false;

for (int i = startIndex; !found; i--)
{
	if (stoch[i] > 95 || stoch[i] < 5)
		found = true;
}

 

Ideally, you should identify the bounds of the loop, but you can combine that check:

bool found = false;

for (int i = startIndex; i >= 0 && !found; i--)
{
	if (stoch[i] > 95 || stoch[i] < 5)
		found = true;
}

 

However, if you really do require it to be a while loop, here's a simple example:

bool found = false;
int i = startIndex;

while (!found)
{
	if (stoch[i] > 95 || stoch[i] < 5)
		found = true;
		
	i--;
}

Just be cautious to not get stuck in an infinite loop, or access invalid indices from collections.

May i know your telegram please. When condition goes true it only take value one time. And it resets value to 0 and not continiuosly gives last data value. 


@MongolTrader

MongolTrader
04 Nov 2022, 06:10

RE:

firemyst said:

Why do you need to check it in a while loop? What's your logic or what are you trying to accomplish?

I need to know continuously every tick or bar what was last overbought or oversold point.  


@MongolTrader

firemyst
04 Nov 2022, 11:02

RE: RE:

MongolTrader said:

firemyst said:

Why do you need to check it in a while loop? What's your logic or what are you trying to accomplish?

I need to know continuously every tick or bar what was last overbought or oversold point.  

What I would consider doing then is:

1) in your OnStart do the while loop and find the last overbought/sold

2) create a class variable and store the index of the last overbought bar index in it

3) for every onbar event, check the stoch. If it's overbough/sold, update the global variation with the new bar number.

 

When you want bots running fast to respond to the market,  so no need to execute a loop every single time a new bar or tick occurs.


@firemyst