Source »
Pivot Points - 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-07-30
using System;
using System.Drawing;
namespace Forex_Strategy_Builder
{
/// <summary>
/// Indicator Pivot Points.
/// </summary>
public class Pivot_Points : Indicator
{
/// <summary>
/// The default constructor.
/// </summary>
public Pivot_Points()
{
}
/// <summary>
/// Sets the default parameters for the designated slot type.
/// </summary>
/// <param name="slotType">The slot type.</param>
public Pivot_Points(SlotTypes slotType)
{
sIndicatorName = "Pivot Points";
parameters = new IndicatorParam();
component = new IndicatorComp[] { };
afSpecValue = new float[] { };
bSeparatedChart = false;
bIsCalculated = false;
typeOfIndicator = TypeOfIndicator.Additional;
// 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 R3 (short at S3)",
"Enter long at R2 (short at S2)",
"Enter long at R1 (short at S1)",
"Enter the market at the Pivot Point",
"Enter long at S1 (short at R1)",
"Enter long at S2 (short at R2)",
"Enter long at S3 (short at R3)",
};
else if (slotType == SlotTypes.Close)
parameters.ListParam[0].ItemList = new string[]
{
"Exit long at R3 (short at S3)",
"Exit long at R2 (short at S2)",
"Exit long at R1 (short at S1)",
"Exit the market at the Pivot Point",
"Exit long at S1 (short at R1)",
"Exit long at S2 (short at R2)",
"Exit long at S3 (short at R3)",
};
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 = "Base price";
parameters.ListParam[1].ItemList = new string[] { "Previous bar range" };
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 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
int iFirstBar = 2;
float[] afPivot = new float[Bars];
float[] afR1 = new float[Bars];
float[] afR2 = new float[Bars];
float[] afR3 = new float[Bars];
float[] afS1 = new float[Bars];
float[] afS2 = new float[Bars];
float[] afS3 = new float[Bars];
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
afPivot[iBar] = (High[iBar] + Low[iBar] + Close[iBar]) / 3;
afR1[iBar] = 2 * afPivot[iBar] - Low[iBar];
afS1[iBar] = 2 * afPivot[iBar] - High[iBar];
afR2[iBar] = afPivot[iBar] + High[iBar] - Low[iBar];
afS2[iBar] = afPivot[iBar] - High[iBar] + Low[iBar];
afR3[iBar] = High[iBar] + 2 * (afPivot[iBar] - Low[iBar]);
afS3[iBar] = Low[iBar] - 2 * (High[iBar] - afPivot[iBar]);
}
component = new IndicatorComp[9];
for (int iComp = 0; iComp < 7; iComp++)
{
component[iComp] = new IndicatorComp();
component[iComp].ChartType = IndChartType.Dot;
component[iComp].ChartColor = Color.Violet;
component[iComp].DataType = IndComponentType.IndicatorValue;
component[iComp].FirstBar = iFirstBar;
}
component[3].ChartColor = Color.Blue;
component[0].Value = afR3;
component[1].Value = afR2;
component[2].Value = afR1;
component[3].Value = afPivot;
component[4].Value = afS1;
component[5].Value = afS2;
component[6].Value = afS3;
component[0].CompName = "Resistance 3";
component[1].CompName = "Resistance 2";
component[2].CompName = "Resistance 1";
component[3].CompName = "Pivot point";
component[4].CompName = "Support 1";
component[5].CompName = "Support 2";
component[6].CompName = "Support 3";
component[7] = new IndicatorComp();
component[7].ChartType = IndChartType.NoChart;
component[7].FirstBar = iFirstBar;
component[7].Value = new float[Bars];
component[8] = new IndicatorComp();
component[8].ChartType = IndChartType.NoChart;
component[8].FirstBar = iFirstBar;
component[8].Value = new float[Bars];
// Sets the component's type.
if (slotType == SlotTypes.Open)
{
component[7].DataType = IndComponentType.OpenLongPrice;
component[7].CompName = "Long positions opening price";
component[8].DataType = IndComponentType.OpenShortPrice;
component[8].CompName = "Short positions opening price";
}
else if (slotType == SlotTypes.Close)
{
component[7].DataType = IndComponentType.CloseLongPrice;
component[7].CompName = "Long positions closing price";
component[8].DataType = IndComponentType.CloseShortPrice;
component[8].CompName = "Short positions closing price";
}
if (slotType == SlotTypes.Open)
{
switch (parameters.ListParam[0].Text)
{
case "Enter the market at the Pivot Point":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afPivot[iBar - iPrvs];
component[8].Value[iBar] = afPivot[iBar - iPrvs];
}
break;
case "Enter long at S1 (short at R1)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afS1[iBar - iPrvs];
component[8].Value[iBar] = afR1[iBar - iPrvs];
}
break;
case "Enter long at S2 (short at R2)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afS2[iBar - iPrvs];
component[8].Value[iBar] = afR2[iBar - iPrvs];
}
break;
case "Enter long at S3 (short at R3)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afS3[iBar - iPrvs];
component[8].Value[iBar] = afR3[iBar - iPrvs];
}
break;
case "Enter long at R1 (short at S1)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afR1[iBar - iPrvs];
component[8].Value[iBar] = afS1[iBar - iPrvs];
}
break;
case "Enter long at R2 (short at S2)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afR2[iBar - iPrvs];
component[8].Value[iBar] = afS2[iBar - iPrvs];
}
break;
case "Enter long at R3 (short at S3)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afR3[iBar - iPrvs];
component[8].Value[iBar] = afS3[iBar - iPrvs];
}
break;
default:
break;
}
}
else if (slotType == SlotTypes.Close)
{
switch (parameters.ListParam[0].Text)
{
case "Exit the market at the Pivot Point":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afPivot[iBar - iPrvs];
component[8].Value[iBar] = afPivot[iBar - iPrvs];
}
break;
case "Exit long at R1 (short at S1)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afR1[iBar - iPrvs];
component[8].Value[iBar] = afS1[iBar - iPrvs];
}
break;
case "Exit long at R2 (short at S2)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afR2[iBar - iPrvs];
component[8].Value[iBar] = afS2[iBar - iPrvs];
}
break;
case "Exit long at R3 (short at S3)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afR3[iBar - iPrvs];
component[8].Value[iBar] = afS3[iBar - iPrvs];
}
break;
case "Exit long at S1 (short at R1)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afS1[iBar - iPrvs];
component[8].Value[iBar] = afR1[iBar - iPrvs];
}
break;
case "Exit long at S2 (short at R2)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afS2[iBar - iPrvs];
component[8].Value[iBar] = afR2[iBar - iPrvs];
}
break;
case "Exit long at S3 (short at R3)":
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
component[7].Value[iBar] = afS3[iBar - iPrvs];
component[8].Value[iBar] = afR3[iBar - iPrvs];
}
break;
default:
break;
}
}
bIsCalculated = true;
}
}
}
Top