public override void Calculate(SlotTypes slotType)
{
double dAFMin = IndParam.NumParam[0].Value;
double dAFInc = IndParam.NumParam[1].Value;
double dAFMax = IndParam.NumParam[2].Value;
// Reading the parameters
int intDirNew;
double dAF;
double dPExtr;
double dPSARNew = 0;
int[] aiDir = new int[Bars];
double[] adPSAR = new double[Bars];
//---- Calculating the initial values
adPSAR[0] = 0;
dAF = dAFMin;
intDirNew = 0;
if (Close[1] > Open[0])
{
aiDir[0] = 1;
aiDir[1] = 1;
dPExtr = Math.Max(High[0], High[1]);
adPSAR[1] = Math.Min(Low[0], Low[1]);
}
else
{
aiDir[0] = -1;
aiDir[1] = -1;
dPExtr = Math.Min(Low[0], Low[1]);
adPSAR[1] = Math.Max(High[0], High[1]);
}
for (int iBar = 2; iBar < Bars; iBar++)
{
//---- PSAR for the current period
if (intDirNew != 0)
{
// The direction was changed during the last period
aiDir[iBar] = intDirNew;
intDirNew = 0;
adPSAR[iBar] = dPSARNew + dAF * (dPExtr - dPSARNew);
}
else
{
aiDir[iBar] = aiDir[iBar - 1];
adPSAR[iBar] = adPSAR[iBar - 1] + dAF * (dPExtr - adPSAR[iBar - 1]);
}
// PSAR has to be out of the previous two bars limits
if (aiDir[iBar] > 0 && adPSAR[iBar] > Math.Min(Low[iBar - 1], Low[iBar - 2]))
adPSAR[iBar] = Math.Min(Low[iBar - 1], Low[iBar - 2]);
else if (aiDir[iBar] < 0 && adPSAR[iBar] < Math.Max(High[iBar - 1], High[iBar - 2]))
adPSAR[iBar] = Math.Max(High[iBar - 1], High[iBar - 2]);
//---- PSAR for the next period
// Calculation of the new values of flPExtr and flAF
// if there is a new extreme price in the PSAR direction
if (aiDir[iBar] > 0 && High[iBar] > dPExtr)
{
dPExtr = High[iBar];
dAF = Math.Min(dAF + dAFInc, dAFMax);
}
if (aiDir[iBar] < 0 && Low[iBar] < dPExtr)
{
dPExtr = Low[iBar];
dAF = Math.Min(dAF + dAFInc, dAFMax);
}
// Whether the price reaches PSAR
if (Low[iBar] <= adPSAR[iBar] && adPSAR[iBar] <= High[iBar])
{
intDirNew = -aiDir[iBar];
dPSARNew = dPExtr;
dAF = dAFMin;
if (intDirNew > 0)
dPExtr = High[iBar];
else
dPExtr = Low[iBar];
}
}
int iFirstBar = 8;
// Saving the components
Component = new IndicatorComp[1];
Component[0] = new IndicatorComp();
Component[0].CompName = "PSAR value";
if (slotType == SlotTypes.Close)
Component[0].DataType = IndComponentType.ClosePrice;
else
Component[0].DataType = IndComponentType.IndicatorValue;
Component[0].ChartType = IndChartType.Dot;
Component[0].ChartColor = Color.Violet;
Component[0].FirstBar = iFirstBar;
Component[0].PosPriceDependence = PositionPriceDependence.BuyHigherSellLower;
Component[0].Value = adPSAR;
return;
}