Source »
Heiken Ashi - 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 Heiken Ashi
/// </summary>
public class Heiken_Ashi : Indicator
{
/// <summary>
/// The default constructor.
/// </summary>
public Heiken_Ashi()
{
}
/// <summary>
/// Sets the default parameters for the designated slot type.
/// </summary>
/// <param name="slotType">The slot type.</param>
public Heiken_Ashi(SlotTypes slotType)
{
sIndicatorName = "Heiken Ashi";
parameters = new IndicatorParam();
component = new IndicatorComp[] { };
afSpecValue = new float[] { };
bSeparatedChart = false;
bIsCalculated = false;
typeOfIndicator = TypeOfIndicator.Indicator;
// 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 the H.A. High",
"Enter long at the H.A. Low",
};
else if (slotType == SlotTypes.OpenFilter)
parameters.ListParam[0].ItemList = new string[]
{
"White H.A. bar without lower shadow",
"White H.A. bar",
"Black H.A. bar",
"Black H.A. bar without upper shadow"
};
else if (slotType == SlotTypes.Close)
parameters.ListParam[0].ItemList = new string[]
{
"Exit long at the H.A. High",
"Exit long at the H.A. Low",
};
else if (slotType == SlotTypes.CloseFilter)
parameters.ListParam[0].ItemList = new string[]
{
"Black H.A. bar without upper shadow",
"Black H.A. bar",
"White H.A. bar",
"White H.A. bar without lower shadow"
};
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 Donchian Channel";
parameters.ListParam[1].Caption = "Base price";
parameters.ListParam[1].ItemList = new string[] { "High, Low, Open, Close" };
parameters.ListParam[1].Index = 0;
parameters.ListParam[1].Text = parameters.ListParam[1].ItemList[parameters.ListParam[1].Index];
parameters.ListParam[1].Enabled = true;
// The CheckBox parameters.
parameters.CheckParam[0].Caption = "Use previous bar value";
parameters.CheckParam[0].Checked = Data.Strategy.PrepareUsePrevBarValueCheckBox(slotType);
parameters.CheckParam[0].Enabled = true;
parameters.CheckParam[0].ToolTip = "Use the indicator value from the previous bar";
}
/// <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
int iPrvs = parameters.CheckParam[0].Checked ? 1 : 0;
// Calculation
float[] afHAOpen = new float[Bars];
float[] afHAHigh = new float[Bars];
float[] afHALow = new float[Bars];
float[] afHAClose = new float[Bars];
afHAOpen[0] = Open[0];
afHAHigh[0] = High[0];
afHALow[0] = Low[0];
afHAClose[0] = Close[0];
int iFirstBar = 1 + iPrvs;
for (int iBar = 1; iBar < Bars; iBar++)
{
afHAClose[iBar] = (Open[iBar] + High[iBar] + Low[iBar] + Close[iBar]) / 4;
afHAOpen[iBar] = (afHAOpen[iBar - 1] + afHAClose[iBar - 1]) / 2;
afHAHigh[iBar] = High[iBar] > afHAOpen[iBar] ? High[iBar] : afHAOpen[iBar];
afHAHigh[iBar] = afHAClose[iBar] > afHAHigh[iBar] ? afHAClose[iBar] : afHAHigh[iBar];
afHALow[iBar] = Low[iBar] < afHAOpen[iBar] ? Low[iBar] : afHAOpen[iBar];
afHALow[iBar] = afHAClose[iBar] < afHALow[iBar] ? afHAClose[iBar] : afHALow[iBar];
}
// Saving the components
component = new IndicatorComp[6];
component[0] = new IndicatorComp();
component[0].CompName = "H.A. Open";
component[0].DataType = IndComponentType.IndicatorValue;
component[0].ChartType = IndChartType.Dot;
component[0].ChartColor = Color.Green;
component[0].FirstBar = iFirstBar;
component[0].Value = afHAOpen;
component[1] = new IndicatorComp();
component[1].CompName = "H.A. High";
component[1].DataType = IndComponentType.IndicatorValue;
component[1].ChartType = IndChartType.Dot;
component[1].ChartColor = Color.Blue;
component[1].FirstBar = iFirstBar;
component[1].Value = afHAHigh;
component[2] = new IndicatorComp();
component[2].CompName = "H.A. Low";
component[2].DataType = IndComponentType.IndicatorValue;
component[2].ChartType = IndChartType.Dot;
component[2].ChartColor = Color.Blue;
component[2].FirstBar = iFirstBar;
component[2].Value = afHALow;
component[3] = new IndicatorComp();
component[3].CompName = "H.A. Close";
component[3].DataType = IndComponentType.IndicatorValue;
component[3].ChartType = IndChartType.Dot;
component[3].ChartColor = Color.Red;
component[3].FirstBar = iFirstBar;
component[3].Value = afHAClose;
component[4] = new IndicatorComp();
component[4].ChartType = IndChartType.NoChart;
component[4].FirstBar = iFirstBar;
component[4].Value = new float[Bars];
component[5] = new IndicatorComp();
component[5].ChartType = IndChartType.NoChart;
component[5].FirstBar = iFirstBar;
component[5].Value = new float[Bars];
// Sets the component's type.
if (slotType == SlotTypes.Open)
{
component[4].DataType = IndComponentType.OpenLongPrice;
component[4].CompName = "Long positions opening price";
component[5].DataType = IndComponentType.OpenShortPrice;
component[5].CompName = "Short positions opening price";
}
else if (slotType == SlotTypes.OpenFilter)
{
component[4].DataType = IndComponentType.AllowOpenLong;
component[4].CompName = "Allows long positions opening";
component[5].DataType = IndComponentType.AllowOpenShort;
component[5].CompName = "Allows short positions opening";
}
else if (slotType == SlotTypes.Close)
{
component[4].DataType = IndComponentType.CloseLongPrice;
component[4].CompName = "Long positions closing price";
component[5].DataType = IndComponentType.CloseShortPrice;
component[5].CompName = "Short positions closing price";
}
else if (slotType == SlotTypes.CloseFilter)
{
component[4].DataType = IndComponentType.ForceCloseLong;
component[4].CompName = "Forces long positions closing";
component[5].DataType = IndComponentType.ForceCloseShort;
component[5].CompName = "Forces short positions closing";
}
if (slotType == SlotTypes.Open || slotType == SlotTypes.Close)
{
for (int iBar = 2; iBar < Bars; iBar++)
{
if (parameters.ListParam[0].Text == "Enter long at the H.A. High" ||
parameters.ListParam[0].Text == "Exit long at the H.A. High")
{
component[4].Value[iBar] = afHAHigh[iBar - iPrvs];
component[5].Value[iBar] = afHALow[iBar - iPrvs];
}
else
{
component[4].Value[iBar] = afHALow[iBar - iPrvs];
component[5].Value[iBar] = afHAHigh[iBar - iPrvs];
}
}
}
else
{
switch (parameters.ListParam[0].Text)
{
case "White H.A. bar without lower shadow":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[4].Value[iBar] = afHAClose[iBar - iPrvs] > afHAOpen[iBar - iPrvs] &&
afHALow[iBar - iPrvs] == afHAOpen[iBar - iPrvs] ? 1 : 0;
component[5].Value[iBar] = afHAClose[iBar - iPrvs] < afHAOpen[iBar - iPrvs] &&
afHAHigh[iBar - iPrvs] == afHAOpen[iBar - iPrvs] ? 1 : 0;
}
break;
case "White H.A. bar":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[4].Value[iBar] = afHAClose[iBar - iPrvs] > afHAOpen[iBar - iPrvs] ? 1 : 0;
component[5].Value[iBar] = afHAClose[iBar - iPrvs] < afHAOpen[iBar - iPrvs] ? 1 : 0;
}
break;
case "Black H.A. bar":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[4].Value[iBar] = afHAClose[iBar - iPrvs] < afHAOpen[iBar - iPrvs] ? 1 : 0;
component[5].Value[iBar] = afHAClose[iBar - iPrvs] > afHAOpen[iBar - iPrvs] ? 1 : 0;
}
break;
case "Black H.A. bar without upper shadow":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[4].Value[iBar] = afHAClose[iBar - iPrvs] < afHAOpen[iBar - iPrvs] &&
afHAHigh[iBar - iPrvs] == afHAOpen[iBar - iPrvs] ? 1 : 0;
component[5].Value[iBar] = afHAClose[iBar - iPrvs] > afHAOpen[iBar - iPrvs] &&
afHALow[iBar - iPrvs] == afHAOpen[iBar - iPrvs] ? 1 : 0;
}
break;
default:
break;
}
}
bIsCalculated = true;
return;
}
}
}
Top