For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

# Question regarding Fractal Indicator

• Hi,

I have some experience w python but Im fairly new to bt.
Im currently codeing up a simply dummy strategy with the fractal indicator. The logic is simple:
Buy if the current bullish_fractal is higher than the last bullish_fractal.

class Rotation_Strategy(bt.Strategy):

``````   # ## added in loggin fuction as it helpful in bug fixing
# def log(self, txt, dt=None):
#     dt = self.datas.datetime.date(0)
#     print('%s, %s' % (dt.isoformat(), txt))

def __init__(self):
self.dclose = self.datas.close
self.high = self.datas.high
self.open = self.datas.open
self.low = self.datas.low
self.fractal = Fractal(self.data, period=5, bardist=0.01)

def notify_order(self, order):

if order.status in [order.Completed]:
self.bar_executed = len(self)

### record entry price for logging purposes
self.bar_entry = order.executed.price

def next(self):

if not self.position:
if self.fractal.fractal_bullish > self.fractal.fractal_bullish[-1]:

else:
if len(self) >= (self.bar_executed + 3):
self.close()
``````

However, when I run this logic, it takes the LAST BAR (regardless of whether it has a bullish_fractal or not) - But
I want it to take the value of the last bullish fractal. Hope that made sense.

Thx!

• However, when I run this logic, it takes the LAST BAR (regardless of whether it has a bullish_fractal or not) - But
I want it to take the value of the last bullish fractal. Hope that made sense.

Sorry, but at least here it doesn't make any sense.

Additionally, if you want to post code (or output) please see the top of the forum

``````For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
``````

• Thx for reply and sorry about the code thing. Let me rephrase that question:
I want to buy if the current bullish fractal is higher than the last bullish fractal.
With the code that I posted, it simply looks at the last bar and checks if I there
has been a bullish fractal at the last bar - and since thats never the case, because
the fractal indicator has period 5, it doesnt give me any signals.

Hope that makes sense now.
Thank you

• I believe you are mistaken into what the `Fractal` study does. It shows you things that have happened in the past ... and it actually logs them in the past.

You are never going to get a real-time signal to buy/sell with `Fractal`, and that's why it is called a `Study` and categorized as one in the sources.

From the reference quoted in the documentation for `Fractal`

``````The obvious drawback here is that fractals are lagging indicators. A fractal can't be drawn until we are two days into the reversal
``````

You need to look the appropriate (period-wise) number of bars in the past to understand in the study identified a turning point in the past. If you then believe the trend is going to continue, you can enter.

• Quote from Investopedia: "The obvious drawback here is that fractals are lagging indicators. A fractal can't be drawn until we are two days into the reversal."

I get that part about the past.

Here is the simple idea:
If the market made a bullish fractal - call it "low 2"- on day 0, which I know earliest 2 bars (days) later (given I use a 5 bar fractal) and this bullish fractal is higher than the last bullish fractal BEFORE - call that one "low 1"- so a higher low so to speak. Now I want to after enter long on day 3, so 2 days after low 2 (because it doesnt reveal itself any earlier).

The problem (at least for me) again:
How do I access the last bullish fractal (low 1) in my buying logic? This:

``````self.fractal.fractal_bearish[-1]
``````

gives me somehow just the last bar and not low 1

I initally thought that

``````            if self.fractal.fractal_bearish > self.fractal.fractal_bearish[-1]:
``````

would do the job, but it doesnt.

I hope that this made sense.

• How do I access the last bullish fractal (low 1) in my buying logic? This:
self.fractal.fractal_bearish[-1]

gives me somehow just the last bar and not low 1

Yes, index `-1` will always give you the last value (aka last bar), which is right before the current one, which is at index `0`. This is so for all indicators and lines.

I initally thought that

``````            if self.fractal.fractal_bearish > self.fractal.fractal_bearish[-1]:
``````

would do the job, but it doesnt.

You haven't then got this.

Quote from Investopedia: "The obvious drawback here is that fractals are lagging indicators. A fractal can't be drawn until we are two days into the reversal."
I get that part about the past.

Depending on the period you use, the last `bullish`, `bearish` indication, will be at least `-x` periods in the past. For the default period of `5` this will will `[-2]`.

Comparing if the value is greater than or less than makes no sense, because you are looking for an indication at a given point in time (when the study marks that a swing happened) and not for a value which is greater than or less than. Any value which is not an indication will be a `NaN` because there is nothing there.

okay, so is there a way to store all the bearish/bullish fractals in a container (dict/array) and have them printed out, so that I can gather some statistics on them ?

I have done the following:
I created an empty container in the constructor of the strategy.

``````self.__special_array_for_later = []
``````

Then I wanted to fill it up with the bearish fractals (highs) when iterating over the bars:

``````        if self.fractal.fractal_bearish:
self.__special_array_for_later.append(self.high[-2])
``````

But when I print it out later it gives me all the high prices instead of only those highs that were marked with
a bearish fractal.

• But when I print it out later it gives me all the high prices instead of only those highs that were marked with
a bearish fractal.

``````    if self.fractal.fractal_bearish:
self.__special_array_for_later.append(self.high[-2])
``````

Any value which is not an indication will be a NaN because there is nothing there.

It was clearly stated that values which are not marked as fractal are `NaN`, so you should be specifically looking to skip `NaN` values. In any case the logic is flawed, because the mark for a fractal doesn't happen now, it happens two periods in the past, exactly as the `high` you want to store.

okay, so is there a way to store all the bearish/bullish fractals in a container (dict/array) and have them printed out, so that I can gather some statistics on them ?

They are already stored in the indicator itself, which is a line. You probably want to read the documentation here: Docs - Platform Concepts and look for Slicing

});