How to improve my fractals function

Created at 03 Jul 2023, 17:06
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!
Best.Algo.Trader's avatar

Best.Algo.Trader

Joined 24.05.2023

How to improve my fractals function
03 Jul 2023, 17:06


Dear all,

I am using the following implementation for finding a recent fractal high:

protected double? FractalHigh(DataSeries source, int leftBars, int rightBars)
{
    double barValue = source.Last(rightBars + 1); // bar value in the middle of the range
            
    if (barValue == source.Maximum(leftBars + rightBars + 2)) // maximum includes the new open bar
    {
        return barValue;
    }
        
    return null;
}
        

But this does not support an offset parameter which would allow finding a fractal x bars back. The only other implementation would be to iterate over each bar and check manually whether it is higher than the price of the bar in the middle of my range. 

Any suggestions for improving my implementation to support an offset parameter?


@Best.Algo.Trader
Replies

firemyst
04 Jul 2023, 02:29

What you should try doing is looking at how others have done it, and adapt your code as appropriate.

For example:

 

 


@firemyst

Best.Algo.Trader
04 Jul 2023, 08:21

RE:

firemyst said:

What you should try doing is looking at how others have done it, and adapt your code as appropriate.

For example:

 

 

Hi, thanks for your message. Of course I checked for other implementations and also had a look at your example. That is what I meant by "The only other implementation would be to iterate over each bar and check manually whether it is higher than the price of the bar in the middle of my range". I was curious if someone here found another way...


@Best.Algo.Trader

firemyst
04 Jul 2023, 11:00

Hypothetically what you could do is:

//To find a bearish fractal in a range:
int startIndex = 4;
int endIndex = 7;
array.Skip(startIndex).Take(endIndex - startIndex).Max();

And see if the middle value of the array is equal the max value returned. If so, it's a bearish fractal.

note that according to the definition of a bearish fractal is as follows from Investopedia:

  • bearish turning point occurs when there is a pattern with the highest high in the middle and two lower highs on each side.

     

It doesn't say each high has to be lower/higher than the previous/next. Only that the one in the middle has to be the highest/lowest. So given that interepretation, you could use the sample code I put in at the top here.

If you do want it with each candle getting consecutively higher/lower from the middle one, I don't know how you do that without looping over each value.


@firemyst

Best.Algo.Trader
04 Jul 2023, 17:55

RE:

firemyst said:

Hypothetically what you could do is:

//To find a bearish fractal in a range:
int startIndex = 4;
int endIndex = 7;
array.Skip(startIndex).Take(endIndex - startIndex).Max();

And see if the middle value of the array is equal the max value returned. If so, it's a bearish fractal.

note that according to the definition of a bearish fractal is as follows from Investopedia:

  • bearish turning point occurs when there is a pattern with the highest high in the middle and two lower highs on each side.

     

It doesn't say each high has to be lower/higher than the previous/next. Only that the one in the middle has to be the highest/lowest. So given that interepretation, you could use the sample code I put in at the top here.

If you do want it with each candle getting consecutively higher/lower from the middle one, I don't know how you do that without looping over each value.

Thanks a lot! I will give it a try. Actually my current implementation only checks whether the bar in the middle is the same, as the highest value of the whole range. Which leaves the option that there are other bars with the same value. 


@Best.Algo.Trader