Source »
Fractal - source code
// Forex Strategy Builder
// Copyright (c) 2006 - 2008 Miroslav Popov - All rights reserved!
// http://forexsb.com
// info(a)forexsb.com
//
// Last changed on: 2007-09-20
using System;
using System.Drawing;
namespace Forex_Strategy_Builder
{
/// <summary>
/// Indicator: Fractal
/// </summary>
public class Fractal : Indicator
{
/// <summary>
/// The default constructor.
/// </summary>
public Fractal()
{
}
/// <summary>
/// Constructor
/// </summary>
public Fractal(SlotTypes slotType)
{
sIndicatorName = "Fractal";
parameters = new IndicatorParam();
component = new IndicatorComp[] { };
bSeparatedChart = false;
bIsCalculated = false;
// The indicator name.
parameters.IndicatorName = sIndicatorName;
// The slot type.
parameters.SlotType = slotType;
// The ComboBox parameters.
parameters.ListParam[0].Caption = "Logic";
if (slotType == SlotTypes.Open)
{
parameters.ListParam[0].ItemList = new string[]
{
"Enter long at Fractal Up",
"Enter long at Fractal Down"
};
}
else if (slotType == SlotTypes.Close)
{
parameters.ListParam[0].ItemList = new string[]
{
"Exit long at Fractal Up",
"Exit long at Fractal Down"
};
}
parameters.ListParam[0].Index = 0;
parameters.ListParam[0].Text = parameters.ListParam[0].ItemList[parameters.ListParam[0].Index];
parameters.ListParam[0].Enabled = true;
parameters.ListParam[0].ToolTip = "Logic of application of the indicator";
// The CheckBox parameters.
parameters.CheckParam[0].Caption = "Is it visible";
parameters.CheckParam[0].Checked = true;
parameters.CheckParam[0].Enabled = true;
parameters.CheckParam[0].ToolTip = "Mark if you want to use the visible Fractals only";
}
/// <summary>
/// Calculates the indicator's components.
/// </summary>
/// <param name="slotType">The slot type.</param>
public override void Calculate(SlotTypes slotType)
{
if (parameters.SlotType == SlotTypes.NotDefined) return;
// Reading the parameters
bool bIsVisible = parameters.CheckParam[0].Checked;
float[] afFrUp = new float[Bars];
float[] afFrDn = new float[Bars];
for(int iBar=8; iBar<Bars-1; iBar++)
{
if(High[iBar-1]<High[iBar-2] && High[iBar]<High[iBar-2])
{
// Fractal type 1
if( High[iBar-4]<High[iBar-2] &&
High[iBar-3]<High[iBar-2] )
afFrUp[iBar+1]=High[iBar-2];
// Fractal type 2
if( High[iBar-5]< High[iBar-2] &&
High[iBar-4]< High[iBar-2] &&
High[iBar-3]==High[iBar-2] )
afFrUp[iBar+1]=High[iBar-2];
// Fractal type 3, 4
if( High[iBar-6]< High[iBar-2] &&
High[iBar-5]< High[iBar-2] &&
High[iBar-4]==High[iBar-2] &&
High[iBar-3]<=High[iBar-2] )
afFrUp[iBar+1]=High[iBar-2];
// Fractal type 5
if( High[iBar-7]< High[iBar-2] &&
High[iBar-6]< High[iBar-2] &&
High[iBar-5]==High[iBar-2] &&
High[iBar-4]< High[iBar-2] &&
High[iBar-3]==High[iBar-2] )
afFrUp[iBar+1]=High[iBar-2];
// Fractal type 6
if( High[iBar-7]< High[iBar-2] &&
High[iBar-6]< High[iBar-2] &&
High[iBar-5]==High[iBar-2] &&
High[iBar-4]==High[iBar-2] &&
High[iBar-3]< High[iBar-2] )
afFrUp[iBar+1]=High[iBar-2];
// Fractal type 7
if( High[iBar-8]< High[iBar-2] &&
High[iBar-7]< High[iBar-2] &&
High[iBar-6]==High[iBar-2] &&
High[iBar-5]< High[iBar-2] &&
High[iBar-4]==High[iBar-2] &&
High[iBar-3]< High[iBar-2] )
afFrUp[iBar+1]=High[iBar-2];
}
if(Low[iBar-1]>Low[iBar-2] && Low[iBar]>Low[iBar-2])
{
// Fractal type 1
if( Low[iBar-4]>Low[iBar-2] &&
Low[iBar-3]>Low[iBar-2] )
afFrDn[iBar+1]=Low[iBar-2];
// Fractal type 2
if( Low[iBar-5]> Low[iBar-2] &&
Low[iBar-4]> Low[iBar-2] &&
Low[iBar-3]==Low[iBar-2] )
afFrDn[iBar+1]=Low[iBar-2];
// Fractal type 3, 4
if( Low[iBar-6]> Low[iBar-2] &&
Low[iBar-5]> Low[iBar-2] &&
Low[iBar-4]==Low[iBar-2] &&
Low[iBar-3]>=Low[iBar-2] )
afFrDn[iBar+1]=Low[iBar-2];
// Fractal type 5
if( Low[iBar-7]> Low[iBar-2] &&
Low[iBar-6]> Low[iBar-2] &&
Low[iBar-5]==Low[iBar-2] &&
Low[iBar-4]> Low[iBar-2] &&
Low[iBar-3]==Low[iBar-2] )
afFrDn[iBar+1]=Low[iBar-2];
// Fractal type 6
if( Low[iBar-7]> Low[iBar-2] &&
Low[iBar-6]> Low[iBar-2] &&
Low[iBar-5]==Low[iBar-2] &&
Low[iBar-4]==Low[iBar-2] &&
Low[iBar-3]> Low[iBar-2] )
afFrDn[iBar+1]=Low[iBar-2];
// Fractal type 7
if( Low[iBar-8]> Low[iBar-2] &&
Low[iBar-7]> Low[iBar-2] &&
Low[iBar-6]==Low[iBar-2] &&
Low[iBar-5]> Low[iBar-2] &&
Low[iBar-4]==Low[iBar-2] &&
Low[iBar-3]> Low[iBar-2] )
afFrDn[iBar+1]=Low[iBar-2];
}
}
// Is visible
if (bIsVisible)
for (int iBar = 8; iBar < Bars; iBar++)
{
if (afFrUp[iBar - 1] > 0f && afFrUp[iBar] == 0f && High[iBar - 1] < afFrUp[iBar - 1])
afFrUp[iBar] = afFrUp[iBar - 1];
if (afFrDn[iBar - 1] > 0f && afFrDn[iBar] == 0f && Low [iBar - 1] > afFrDn[iBar - 1])
afFrDn[iBar] = afFrDn[iBar - 1];
}
else
for (int iBar = 8; iBar < Bars; iBar++)
{
if (afFrUp[iBar] == 0f) afFrUp[iBar] = afFrUp[iBar - 1];
if (afFrDn[iBar] == 0f) afFrDn[iBar] = afFrDn[iBar - 1];
}
// Saving the components
component = new IndicatorComp[4];
component[0] = new IndicatorComp();
component[0].ChartType = IndChartType.Level;
component[0].ChartColor = Color.SpringGreen;
component[0].FirstBar = 8;
component[0].Value = afFrUp;
component[1] = new IndicatorComp();
component[1].ChartType = IndChartType.Level;
component[1].ChartColor = Color.DarkRed;
component[1].FirstBar = 8;
component[1].Value = afFrDn;
component[2] = new IndicatorComp();
component[2].ChartType = IndChartType.NoChart;
component[2].FirstBar = 8;
component[2].Value = new float[Bars];
component[3] = new IndicatorComp();
component[3].ChartType = IndChartType.NoChart;
component[3].FirstBar = 8;
component[3].Value = new float[Bars];
// Sets the component's type.
if (slotType == SlotTypes.Open)
{
if (parameters.ListParam[0].Text == "Enter long at Fractal Up")
{
component[0].DataType = IndComponentType.OpenLongPrice;
component[1].DataType = IndComponentType.OpenShortPrice;
}
else
{
component[0].DataType = IndComponentType.OpenShortPrice;
component[1].DataType = IndComponentType.OpenLongPrice;
}
component[0].CompName = "Fractal Up";
component[1].CompName = "Fractal Down";
component[2].DataType = IndComponentType.AllowOpenLong;
component[2].CompName = "Allows long positions opening";
component[3].DataType = IndComponentType.AllowOpenShort;
component[3].CompName = "Allows short positions opening";
}
else if (slotType == SlotTypes.Close)
{
if (parameters.ListParam[0].Text == "Exit long at Fractal Up")
{
component[0].DataType = IndComponentType.CloseLongPrice;
component[1].DataType = IndComponentType.CloseShortPrice;
}
else
{
component[0].DataType = IndComponentType.CloseShortPrice;
component[1].DataType = IndComponentType.CloseLongPrice;
}
component[0].CompName = "Fractal Up";
component[1].CompName = "Fractal Down";
component[2].DataType = IndComponentType.ForceCloseLong;
component[2].CompName = "Forces long positions closing";
component[3].DataType = IndComponentType.ForceCloseShort;
component[3].CompName = "Forces short positions closing";
}
for(int iBar=1; iBar<Bars; iBar++)
{
component[2].Value[iBar] = afFrUp[iBar]>0f ? 1 : 0;
component[3].Value[iBar] = afFrDn[iBar]>0f ? 1 : 0;
}
bIsCalculated = true;
}
}
}
Top