public override void Calculate(SlotTypes slotType)
{
// Reading the parameters
BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index;
int iPeriod = (int)IndParam.NumParam[0].Value;
int iSmooth = (int)IndParam.NumParam[1].Value;
int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0;
// Calculation
double[] adBasePrice = Price(basePrice);
double[] adMA = new double[Bars];
int iFirstBar = iPeriod + iSmooth + 1 + iPrvs;
// Calculating Chande Momentum Oscillator
double[] adCMO1 = new double[Bars];
double[] adCMO2 = new double[Bars];
double[] adCMO1Sum = new double[Bars];
double[] adCMO2Sum = new double[Bars];
double[] adCMO = new double[Bars];
for (int iBar = 1; iBar < Bars; iBar++)
{
adCMO1[iBar] = 0;
adCMO2[iBar] = 0;
if (adBasePrice[iBar] > adBasePrice[iBar - 1])
adCMO1[iBar] = adBasePrice[iBar] - adBasePrice[iBar - 1];
if (adBasePrice[iBar] < adBasePrice[iBar - 1])
adCMO2[iBar] = adBasePrice[iBar - 1] - adBasePrice[iBar];
}
for (int iBar = 0; iBar < iPeriod; iBar++)
{
adCMO1Sum[iPeriod - 1] += adCMO1[iBar];
adCMO2Sum[iPeriod - 1] += adCMO2[iBar];
}
for (int iBar = iPeriod; iBar < Bars; iBar++)
{
adCMO1Sum[iBar] = adCMO1Sum[iBar - 1] + adCMO1[iBar] - adCMO1[iBar - iPeriod];
adCMO2Sum[iBar] = adCMO2Sum[iBar - 1] + adCMO2[iBar] - adCMO2[iBar - iPeriod];
if (adCMO1Sum[iBar] + adCMO2Sum[iBar] == 0)
adCMO[iBar] = 100;
else
adCMO[iBar] = 100 * (adCMO1Sum[iBar] - adCMO2Sum[iBar]) / (adCMO1Sum[iBar] + adCMO2Sum[iBar]);
}
double SC = 2.0 / (iSmooth + 1);
for (int iBar = 0; iBar < iPeriod; iBar++)
adMA[iBar] = adBasePrice[iBar];
for (int iBar = iPeriod; iBar < Bars; iBar++)
{
double dAbsCMO = Math.Abs(adCMO[iBar]) / 100;
adMA[iBar] = SC * dAbsCMO * adBasePrice[iBar] + (1 - SC * dAbsCMO) * adMA[iBar - 1];
}
// Saving the components
if (slotType == SlotTypes.Open || slotType == SlotTypes.Close)
{
Component = new IndicatorComp[2];
Component[1] = new IndicatorComp();
Component[1].Value = new double[Bars];
for (int iBar = 2; iBar < Bars; iBar++)
{ // Covers the cases when the price can pass through the MA without a signal
double dValue = adMA[iBar - iPrvs]; // Current value
double dValue1 = adMA[iBar - iPrvs - 1]; // Previous value
double dTempVal = dValue;
if ((dValue1 > High[iBar - 1] && dValue < Open[iBar]) || // It jumps below the current bar
(dValue1 < Low[iBar - 1] && dValue > Open[iBar]) || // It jumps above the current bar
(Close[iBar - 1] < dValue && dValue < Open[iBar]) || // Positive gap
(Close[iBar - 1] > dValue && dValue > Open[iBar])) // Negative gap
dTempVal = Open[iBar];
Component[1].Value[iBar] = dTempVal;
}
}
else
{
Component = new IndicatorComp[3];
Component[1] = new IndicatorComp();
Component[1].ChartType = IndChartType.NoChart;
Component[1].FirstBar = iFirstBar;
Component[1].Value = new double[Bars];
Component[2] = new IndicatorComp();
Component[2].ChartType = IndChartType.NoChart;
Component[2].FirstBar = iFirstBar;
Component[2].Value = new double[Bars];
}
Component[0] = new IndicatorComp();
Component[0].CompName = "MA Value";
Component[0].DataType = IndComponentType.IndicatorValue;
Component[0].ChartType = IndChartType.Line;
Component[0].ChartColor = Color.Red;
Component[0].FirstBar = iFirstBar;
Component[0].Value = adMA;
if (slotType == SlotTypes.Open)
{
Component[1].CompName = "Position opening price";
Component[1].DataType = IndComponentType.OpenPrice;
}
else if (slotType == SlotTypes.OpenFilter)
{
Component[1].DataType = IndComponentType.AllowOpenLong;
Component[1].CompName = "Is long entry allowed";
Component[2].DataType = IndComponentType.AllowOpenShort;
Component[2].CompName = "Is short entry allowed";
}
else if (slotType == SlotTypes.Close)
{
Component[1].CompName = "Position closing price";
Component[1].DataType = IndComponentType.ClosePrice;
}
else if (slotType == SlotTypes.CloseFilter)
{
Component[1].DataType = IndComponentType.ForceCloseLong;
Component[1].CompName = "Close out long position";
Component[2].DataType = IndComponentType.ForceCloseShort;
Component[2].CompName = "Close out short position";
}
if (slotType == SlotTypes.OpenFilter || slotType == SlotTypes.CloseFilter)
{
switch (IndParam.ListParam[0].Text)
{
case "The Vidya Moving Average rises":
IndicatorRisesLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The Vidya Moving Average falls":
IndicatorFallsLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The bar opens above the Vidya Moving Average":
BarOpensAboveIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The bar opens below the Vidya Moving Average":
BarOpensBelowIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The bar opens above the Vidya Moving Average after opening below it":
BarOpensAboveIndicatorAfterOpeningBelowLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The bar opens below the Vidya Moving Average after opening above it":
BarOpensBelowIndicatorAfterOpeningAboveLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The position opens above the Vidya Moving Average":
Component[0].PosPriceDependence = PositionPriceDependence.BuyHigherSellLower;
Component[0].UsePreviousBar = iPrvs;
Component[1].DataType = IndComponentType.Other;
Component[1].ShowInDynInfo = false;
Component[2].DataType = IndComponentType.Other;
Component[2].ShowInDynInfo = false;
break;
case "The position opens below the Vidya Moving Average":
Component[0].PosPriceDependence = PositionPriceDependence.BuyLowerSelHigher;
Component[0].UsePreviousBar = iPrvs;
Component[1].DataType = IndComponentType.Other;
Component[1].ShowInDynInfo = false;
Component[2].DataType = IndComponentType.Other;
Component[2].ShowInDynInfo = false;
break;
case "The bar closes below the Vidya Moving Average":
BarClosesBelowIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
case "The bar closes above the Vidya Moving Average":
BarClosesAboveIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]);
break;
default:
break;
}
}
return;
}