Source »
Stochastics - 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-06-12
using System;
using System.Drawing;
namespace Forex_Strategy_Builder
{
/// <summary>
/// Indicator Stochastics.
/// </summary>
public class Stochastics : Indicator
{
/// <summary>
/// The default constructor.
/// </summary>
public Stochastics()
{
}
/// <summary>
/// Sets the default parameters for the designated slot type.
/// </summary>
/// <param name="slotType">The slot type.</param>
public Stochastics(SlotTypes slotType)
{
sIndicatorName = "Stochastics";
parameters = new IndicatorParam();
component = new IndicatorComp[] { };
afSpecValue = new float[] { };
fMinValue = 0f;
fMaxValue = 100f;
bSeparatedChart = true;
bIsCalculated = false;
// The indicator name.
parameters.IndicatorName = sIndicatorName;
// The slot type.
parameters.SlotType = slotType;
// The ComboBox parameters.
parameters.ListParam[0].Caption = "Logic";
parameters.ListParam[0].ItemList = new string[]
{
"The Slow %D rises",
"The Slow %D falls",
"The Slow %D is higher than the Level line",
"The Slow %D is lower than the Level line",
"The Slow %D crosses the Level line upward",
"The Slow %D crosses the Level line downward",
"The Slow %D changes its direction upward",
"The Slow %D changes its direction downward"
};
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";
parameters.ListParam[1].Caption = "Smoothing method";
parameters.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod));
parameters.ListParam[1].Index = 0;
parameters.ListParam[1].Text = parameters.ListParam[1].ItemList[parameters.ListParam[1].Index];
parameters.ListParam[1].Enabled = true;
parameters.ListParam[1].ToolTip = "The Moving Average method used for smoothing";
parameters.NumParam[0].Caption = "%K period";
parameters.NumParam[0].Value = 15;
parameters.NumParam[0].Min = 0;
parameters.NumParam[0].Max = 200;
parameters.NumParam[0].Enabled = true;
parameters.NumParam[0].ToolTip = "The smoothing period of %K";
parameters.NumParam[1].Caption = "Fast %D period";
parameters.NumParam[1].Value = 5;
parameters.NumParam[1].Min = 0;
parameters.NumParam[1].Max = 200;
parameters.NumParam[1].Enabled = true;
parameters.NumParam[1].ToolTip = "The smoothing period of Fast %D";
parameters.NumParam[2].Caption = "Slow %D period";
parameters.NumParam[2].Value = 5;
parameters.NumParam[2].Min = 0;
parameters.NumParam[2].Max = 200;
parameters.NumParam[2].Enabled = true;
parameters.NumParam[2].ToolTip = "The smoothing period of Slow %D";
parameters.NumParam[3].Caption = "Level";
parameters.NumParam[3].Value = 20;
parameters.NumParam[3].Min = 0;
parameters.NumParam[3].Max = 100;
parameters.NumParam[3].Enabled = true;
parameters.NumParam[3].ToolTip = "A critical level (for the appropriate logic)";
// 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
MAMethod maMethod = (MAMethod)Enum.GetValues(typeof(MAMethod)).GetValue(parameters.ListParam[1].Index);
int iK = (int)parameters.NumParam[0].Value;
int iDFast = (int)parameters.NumParam[1].Value;
int iDSlow = (int)parameters.NumParam[2].Value;
int fLevel = (int)parameters.NumParam[3].Value;
int iPrvs = parameters.CheckParam[0].Checked ? 1 : 0;
// Calculation
int iFirstBar = iK + iDFast + iDSlow + 2;
float[] afK = new float[Bars];
float fMin = float.MaxValue;
float fMax = float.MinValue;
for (int iBar = iK; iBar < Bars; iBar++)
{
fMin = float.MaxValue;
fMax = float.MinValue;
for (int index = 0; index < iK; index++)
{
if (High[iBar - index] > fMax) fMax = High[iBar - index];
if (Low[iBar - index] < fMin) fMin = Low[iBar - index];
}
afK[iBar] = 100 * (Close[iBar] - fMin) / (fMax - fMin);
}
float[] afDFast = MovingAverage(iDFast, 0, maMethod, afK);
float[] afDSlow = MovingAverage(iDSlow, 0, maMethod, afDFast);
// Saving the components
component = new IndicatorComp[5];
component[0] = new IndicatorComp();
component[0].CompName = "%K";
component[0].DataType = IndComponentType.IndicatorValue;
component[0].ChartType = IndChartType.Line;
component[0].ChartColor = Color.Brown;
component[0].FirstBar = iFirstBar;
component[0].Value = afK;
component[1] = new IndicatorComp();
component[1].CompName = "Fast %D";
component[1].DataType = IndComponentType.IndicatorValue;
component[1].ChartType = IndChartType.Line;
component[1].ChartColor = Color.Yellow;
component[1].FirstBar = iFirstBar;
component[1].Value = afDFast;
component[2] = new IndicatorComp();
component[2].CompName = "Slow %D";
component[2].DataType = IndComponentType.IndicatorValue;
component[2].ChartType = IndChartType.Line;
component[2].ChartColor = Color.Blue;
component[2].FirstBar = iFirstBar;
component[2].Value = afDSlow;
component[3] = new IndicatorComp();
component[3].ChartType = IndChartType.NoChart;
component[3].FirstBar = iFirstBar;
component[3].Value = new float[Bars];
component[4] = new IndicatorComp();
component[4].ChartType = IndChartType.NoChart;
component[4].FirstBar = iFirstBar;
component[4].Value = new float[Bars];
// Sets the component's type.
if (slotType == SlotTypes.OpenFilter)
{
component[3].DataType = IndComponentType.AllowOpenLong;
component[3].CompName = "Allows long positions opening";
component[4].DataType = IndComponentType.AllowOpenShort;
component[4].CompName = "Allows short positions opening";
}
else if (slotType == SlotTypes.CloseFilter)
{
component[3].DataType = IndComponentType.ForceCloseLong;
component[3].CompName = "Forces long positions closing";
component[4].DataType = IndComponentType.ForceCloseShort;
component[4].CompName = "Forces short positions closing";
}
// Calculation of the logic.
IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter;
switch (parameters.ListParam[0].Text)
{
case "The Slow %D rises":
indLogic = IndicatorLogic.The_indicator_rises;
afSpecValue = new float[1] { 50f };
break;
case "The Slow %D falls":
indLogic = IndicatorLogic.The_indicator_falls;
afSpecValue = new float[1] { 50f };
break;
case "The Slow %D is higher than the Level line":
indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
afSpecValue = new float[2] { fLevel, 100 - fLevel };
break;
case "The Slow %D is lower than the Level line":
indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
afSpecValue = new float[2] { fLevel, 100 - fLevel };
break;
case "The Slow %D crosses the Level line upward":
indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
afSpecValue = new float[2] { fLevel, 100 - fLevel };
break;
case "The Slow %D crosses the Level line downward":
indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
afSpecValue = new float[2] { fLevel, 100 - fLevel };
break;
case "The Slow %D changes its direction upward":
indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
afSpecValue = new float[1] { 50f };
break;
case "The Slow %D changes its direction downward":
indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
afSpecValue = new float[1] { 50f };
break;
default:
break;
}
bIsCalculated = OscillatorLogic(iFirstBar, iPrvs, afDSlow, fLevel, 100 - fLevel, ref component[3], ref component[4], indLogic);
}
}
}
Top