Warning! This section will be deprecated on February 1st 2025. Please move all your Indicators to the cTrader Store catalogue.
Description
fractals
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FractalBreakIndicator : Indicator
{
[Output("BullFractals", LineColor = "Turquoise", PlotType = PlotType.DiscontinuousLine, Thickness = 1)]
public IndicatorDataSeries BullFractalBreak { get; set; }
[Output("BearFractals", LineColor = "Orange", PlotType = PlotType.DiscontinuousLine, Thickness = 1)]
public IndicatorDataSeries BearFractalBreak { get; set; }
protected override void Initialize()
{
// Initialize and create nested indicators
}
public override void Calculate(int index)
{
ProcessBearFractalBreak();
ProcessBullFractalBreak();
}
private void ProcessBearFractalBreak()
{
bool shortPosition = false;
int confirmationIndex = -1;
// Find last bear and bull fractal
int index = this.Bars.Count - 2;
int? bearFractalIndex = FindLastBearFractal(index);
int? bullFractalIndex = FindLastBullFractal(index);
if (bearFractalIndex != null)
{
// DRAW BEAR FRACTAL LINE
for (int i = 0; i < Bars.Count; i++)
{
if (i < bearFractalIndex.Value)
BearFractalBreak[i] = double.NaN;
else
BearFractalBreak[i] = Bars[bearFractalIndex.Value].Low;
}
var txt1 = Chart.DrawText("BearFase1", "FRACTAL (WAITING FOR BREAK)", bearFractalIndex.Value, Bars[bearFractalIndex.Value].Low, Color.Orange);
var txt2 = Chart.DrawText("BearFase2", "", bearFractalIndex.Value, Bars[bearFractalIndex.Value].Low, Color.Orange);
var txt3 = Chart.DrawText("BearFase3", "", bearFractalIndex.Value, Bars[bearFractalIndex.Value].Low, Color.Orange);
var txt4 = Chart.DrawText("BearFase3_desc", "", bearFractalIndex.Value, Bars[bearFractalIndex.Value].Low, Color.Orange);
// Find fractal break (bear)
int? fractalBreak = FindBearFractalBreak(bearFractalIndex.Value);
if (fractalBreak != null)
{
BearFractalBreak[fractalBreak.Value] = Bars[fractalBreak.Value].Close;
txt2 = Chart.DrawText("BearFase2", "BREAK", fractalBreak.Value, Bars[fractalBreak.Value].Low, Color.Orange);
txt1 = Chart.DrawText("BearFase1", "FRACTAL", bearFractalIndex.Value, Bars[bearFractalIndex.Value].Low, Color.Orange);
if (bullFractalIndex != null)
{
int i = fractalBreak.Value + 1;
for (; i < this.Bars.Count; i++)
{
if (this.Bars[i].High >= this.Bars[bearFractalIndex.Value].Low && this.Bars[i].Close < this.Bars[bearFractalIndex.Value].Low && this.Bars[i].Close < this.Bars[bullFractalIndex.Value].Close && fractalBreak.Value < i && i < Bars.Count - 1)
{
// CONFIRMED -> SHORT
BearFractalBreak[i] = Bars[bearFractalIndex.Value].Low;
txt3 = Chart.DrawText("BearFase3", "SHORT", i, Bars[i].Low, Color.Orange);
txt4 = Chart.DrawText("BearFase3_desc", "CONFIRMED", fractalBreak.Value + 1, Bars[bearFractalIndex.Value].Low, Color.Orange);
shortPosition = true;
confirmationIndex = i;
break;
}
else
{
// DRAW BEAR FRACTAL RETEST LINE
if (!shortPosition)
BearFractalBreak[i] = Bars[bearFractalIndex.Value].Low;
else
BearFractalBreak[i] = double.NaN;
}
}
if (!shortPosition)
txt4 = Chart.DrawText("BearFase3_desc", "WAITING FOR CONFIRMATION", fractalBreak.Value + 1, Bars[bearFractalIndex.Value].Low, Color.Orange);
for (i += 1; i < this.Bars.Count; i++)
BearFractalBreak[i] = double.NaN;
}
}
}
}
private void ProcessBullFractalBreak()
{
bool longPosition = false;
int confirmationIndex = -1;
// Find last bear and bull fractal
int index = this.Bars.Count - 2;
int? bearFractalIndex = FindLastBearFractal(index);
int? bullFractalIndex = FindLastBullFractal(index);
if (bullFractalIndex != null)
{
// DRAW BULL FRACTAL LINE
for (int i = 0; i < Bars.Count; i++)
{
if (i < bullFractalIndex.Value)
BullFractalBreak[i] = double.NaN;
else
BullFractalBreak[i] = Bars[bullFractalIndex.Value].High;
}
var txt1 = Chart.DrawText("BullFase1", "FRACTAL (WAITING FOR BREAK)", bullFractalIndex.Value, Bars[bullFractalIndex.Value].High, Color.Turquoise);
var txt2 = Chart.DrawText("BullFase2", "", bullFractalIndex.Value, Bars[bullFractalIndex.Value].High, Color.Turquoise);
var txt3 = Chart.DrawText("BullFase3", "", bullFractalIndex.Value, Bars[bullFractalIndex.Value].High, Color.Turquoise);
var txt4 = Chart.DrawText("BullFase3_desc", "", bullFractalIndex.Value, Bars[bullFractalIndex.Value].High, Color.Turquoise);
// Find fractal break (bull)
int? fractalBreak = FindBullFractalBreak(bullFractalIndex.Value);
if (fractalBreak != null)
{
BullFractalBreak[fractalBreak.Value] = Bars[fractalBreak.Value].Close;
txt2 = Chart.DrawText("BullFase2", "BREAK", fractalBreak.Value, Bars[fractalBreak.Value].High, Color.Turquoise);
txt1 = Chart.DrawText("BullFase1", "FRACTAL", bullFractalIndex.Value, Bars[bullFractalIndex.Value].High, Color.Turquoise);
if (bearFractalIndex != null)
{
int i = fractalBreak.Value + 1;
for (; i < this.Bars.Count; i++)
{
if (this.Bars[i].Low <= this.Bars[bullFractalIndex.Value].High && this.Bars[i].Close > this.Bars[bullFractalIndex.Value].High && this.Bars[i].Close > this.Bars[bearFractalIndex.Value].Close && fractalBreak.Value < i)
{
// CONFIRMED -> LONG
BullFractalBreak[i] = Bars[bullFractalIndex.Value].High;
txt3 = Chart.DrawText("BullFase3", "LONG", i, Bars[i].High, Color.Turquoise);
txt4 = Chart.DrawText("BullFase3_desc", "CONFIRMED", fractalBreak.Value + 1, Bars[bullFractalIndex.Value].High, Color.Turquoise);
longPosition = true;
confirmationIndex = i;
break;
}
else
{
// DRAW BULL FRACTAL RETEST LINE
if (!longPosition)
BullFractalBreak[i] = Bars[bullFractalIndex.Value].High;
else
BullFractalBreak[i] = double.NaN;
}
}
if (!longPosition)
txt4 = Chart.DrawText("BullFase3_desc", "WAITING FOR CONFIRMATION", fractalBreak.Value + 1, Bars[bullFractalIndex.Value].High, Color.Turquoise);
for (i += 1; i < this.Bars.Count; i++)
BullFractalBreak[i] = double.NaN;
}
}
}
}
private int? FindBearFractalBreak(int bearFractalIndex)
{
for (int i = bearFractalIndex + 1; i < this.Bars.Count; i++)
{
if (this.Bars[i].Close < this.Bars[bearFractalIndex].Low)
return i;
}
return null;
}
private int? FindBullFractalBreak(int bullFractalIndex)
{
for (int i = bullFractalIndex + 1; i < this.Bars.Count; i++)
{
if (this.Bars[i].Close > this.Bars[bullFractalIndex].High)
return i;
}
return null;
}
private int? FindLastBearFractal(int startIndex)
{
for (int i = startIndex; i >= 4; i--)
{
if (this.Bars[i].Low > this.Bars[i - 2].Low && this.Bars[i - 1].Low > this.Bars[i - 2].Low && this.Bars[i - 3].Low > this.Bars[i - 2].Low && this.Bars[i - 4].Low > this.Bars[i - 2].Low)
return i - 2;
}
return null;
}
private int? FindLastBullFractal(int startIndex)
{
for (int i = startIndex; i >= 4; i--)
{
if (this.Bars[i].High < this.Bars[i - 2].High && this.Bars[i - 1].High < this.Bars[i - 2].High && this.Bars[i - 3].High < this.Bars[i - 2].High && this.Bars[i - 4].High < this.Bars[i - 2].High)
return i - 2;
}
return null;
}
}
}
KH
khiewkg
Joined on 14.07.2020
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: FractalBreakIndicator.algo
- Rating: 0
- Installs: 1600
- Modified: 13/10/2021 09:54
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
No comments found.