/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int period = (int)IndParam.NumParam[0].Value; int multipl = (int)IndParam.NumParam[1].Value; int prev = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = period + 2; double[] ATR = new double[Bars]; for (int bar = 1; bar < Bars; bar++) { ATR[bar] = Math.Max(High[bar], Close[bar - 1]) - Math.Min(Low[bar], Close[bar - 1]); } ATR = MovingAverage(period, 0, maMethod, ATR); double[] ATRStop = new double[Bars]; double pip = (Digits == 5 || Digits == 3) ? 10 * Point : Point; double minStop = 5 * pip; for (int bar = firstBar; bar < Bars - prev; bar++) { ATRStop[bar + prev] = Math.Max(ATR[bar] * multipl, minStop); } // Saving the components Component = new IndicatorComp[2]; Component[0] = new IndicatorComp(); Component[0].CompName = "ATR Stop margin"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].FirstBar = firstBar; Component[0].ShowInDynInfo = false; Component[0].Value = ATRStop; Component[1] = new IndicatorComp(); Component[1].CompName = "ATR Stop for the transferred position"; Component[1].DataType = IndComponentType.Other; Component[1].ShowInDynInfo = false; Component[1].FirstBar = firstBar; Component[1].Value = new double[Bars]; return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod1 = (MAMethod)IndParam.ListParam[1].Index; MAMethod maMethod2 = (MAMethod)IndParam.ListParam[2].Index; BasePrice price = (BasePrice)IndParam.ListParam[3].Index; int iPeriod1 = (int)IndParam.NumParam[0].Value; int iPeriod2 = (int)IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iPeriod1 + iPeriod2 + 1; double[] adPrice = Price(price); double[] adMA = MovingAverage(iPeriod1, 0, maMethod1, adPrice); double[] adMAPr = new double[Bars]; for (int iBar = 0; iBar < Bars; iBar++) { adMAPr[iBar] = adPrice[iBar] - adMA[iBar]; } double[] adDO = MovingAverage(iPeriod2, 0, maMethod2, adMAPr); // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Detrended Oscillator"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.LightSeaGreen; Component[0].FirstBar = iFirstBar; Component[0].Value = adDO; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Detrended Oscillator rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The Detrended Oscillator falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The Detrended Oscillator is higher than the zero line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; break; case "The Detrended Oscillator is lower than the zero line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; break; case "The Detrended Oscillator crosses the zero line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; break; case "The Detrended Oscillator crosses the zero line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; break; case "The Detrended Oscillator changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The Detrended Oscillator changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adDO, 0, 0, ref Component[1], ref Component[2], indLogic); return; }
public override void Calculate(IDataSet dataSet) { DataSet = dataSet; MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iNJaws = (int)IndParam.NumParam[0].Value; int iSJaws = (int)IndParam.NumParam[1].Value; int iNTeeth = (int)IndParam.NumParam[2].Value; int iSTeeth = (int)IndParam.NumParam[3].Value; int iNLips = (int)IndParam.NumParam[4].Value; int iSLips = (int)IndParam.NumParam[5].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; int iFirstBar = Math.Max(iNJaws + iSJaws + 2, iNTeeth + iSTeeth + 2); iFirstBar = Math.Max(iFirstBar, iNLips + iSLips + 2); // Calculation double[] adJaws = MovingAverage(iNJaws, iSJaws, maMethod, Price(basePrice)); double[] adTeeth = MovingAverage(iNTeeth, iSTeeth, maMethod, Price(basePrice)); double[] adLips = MovingAverage(iNLips, iSLips, maMethod, Price(basePrice)); double[] adUpperGator = new double[Bars]; double[] adLowerGator = new double[Bars]; for (int iBar = 0; iBar < Bars; iBar++) { adUpperGator[iBar] = Math.Abs(adJaws[iBar] - adTeeth[iBar]); adLowerGator[iBar] = -Math.Abs(adTeeth[iBar] - adLips[iBar]); } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "Upper Gator"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adUpperGator; Component[1] = new IndicatorComp(); Component[1].CompName = "Lower Gator"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Histogram; Component[1].FirstBar = iFirstBar; Component[1].Value = adLowerGator; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type. if (SlotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (SlotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } switch (IndParam.ListParam[0].Text) { case "The Gator Oscillator expands": for (int iBar = iFirstBar; iBar < Bars; iBar++) { Component[2].Value[iBar] = (adUpperGator[iBar - iPrvs] - adLowerGator[iBar - iPrvs]) > (adUpperGator[iBar - iPrvs - 1] - adLowerGator[iBar - iPrvs - 1]) + Sigma() ? 1 : 0; Component[3].Value[iBar] = (adUpperGator[iBar - iPrvs] - adLowerGator[iBar - iPrvs]) > (adUpperGator[iBar - iPrvs - 1] - adLowerGator[iBar - iPrvs - 1]) + Sigma() ? 1 : 0; } break; case "The Gator Oscillator contracts": for (int iBar = iFirstBar; iBar < Bars; iBar++) { Component[2].Value[iBar] = (adUpperGator[iBar - iPrvs] - adLowerGator[iBar - iPrvs]) < (adUpperGator[iBar - iPrvs - 1] - adLowerGator[iBar - iPrvs - 1]) - Sigma() ? 1 : 0; Component[3].Value[iBar] = (adUpperGator[iBar - iPrvs] - adLowerGator[iBar - iPrvs]) < (adUpperGator[iBar - iPrvs - 1] - adLowerGator[iBar - iPrvs - 1]) - Sigma() ? 1 : 0; } break; default: break; } }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iPeriod = (int)IndParam.NumParam[0].Value; double dLevel = IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iPeriod + 2; double[] adDeMax = new double[Bars]; double[] adDeMin = new double[Bars]; double[] adDeMarker = new double[Bars]; for (int iBar = 1; iBar < Bars; iBar++) { adDeMax[iBar] = High[iBar] > High[iBar - 1] ? High[iBar] - High[iBar - 1] : 0; adDeMin[iBar] = Low[iBar] < Low[iBar - 1] ? Low[iBar - 1] - Low[iBar] : 0; } double[] adDeMaxMA = MovingAverage(iPeriod, 0, maMethod, adDeMax); double[] adDeMinMA = MovingAverage(iPeriod, 0, maMethod, adDeMin); for (int iBar = iFirstBar; iBar < Bars; iBar++) { if (adDeMaxMA[iBar] + adDeMinMA[iBar] == 0) { adDeMarker[iBar] = 0; } else { adDeMarker[iBar] = adDeMaxMA[iBar] / (adDeMaxMA[iBar] + adDeMinMA[iBar]); } } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "DeMarker"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.RoyalBlue; Component[0].FirstBar = iFirstBar; Component[0].Value = adDeMarker; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The DeMarker rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 0.5 }; break; case "The DeMarker falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 0.5 }; break; case "The DeMarker is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { dLevel, 1 - dLevel }; break; case "The DeMarker is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { dLevel, 1 - dLevel }; break; case "The DeMarker crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { dLevel, 1 - dLevel }; break; case "The DeMarker crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { dLevel, 1 - dLevel }; break; case "The DeMarker changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 0.5 }; break; case "The DeMarker changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 0.5 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adDeMarker, dLevel, 1 - dLevel, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iPeriod = (int)IndParam.NumParam[0].Value; int iDivisor = (int)IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iPeriod + 2; double[] adAEOM = new double[Bars]; for (int iBar = 1; iBar < Bars; iBar++) { adAEOM[iBar] = iDivisor * (High[iBar] - Low[iBar]) * ((High[iBar] + Low[iBar]) / 2 - (High[iBar - 1] - Low[iBar - 1]) / 2) / Math.Max(Volume[iBar], 1); } adAEOM = MovingAverage(iPeriod, 0, maMethod, adAEOM); // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Ease of Movement"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.LightSeaGreen; Component[0].FirstBar = iFirstBar; Component[0].Value = adAEOM; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Ease of Movement rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The Ease of Movement falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The Ease of Movement changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The Ease of Movement changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adAEOM, 0, 0, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; 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 int iFirstBar = iPeriod + 2; double[] adBasePrice = Price(basePrice); double[] adCumulSum = new double[Bars]; adCumulSum[iPeriod - 1] = 0; for (int iBar = 0; iBar < iPeriod; iBar++) { adCumulSum[iPeriod - 1] += adBasePrice[iBar]; } for (int iBar = iPeriod; iBar < Bars; iBar++) { adCumulSum[iBar] = adCumulSum[iBar - 1] - adBasePrice[iBar - iPeriod] + adBasePrice[iBar]; } adCumulSum = MovingAverage(iSmooth, 0, maMethod, adCumulSum); // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Cumulative Sum"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = iFirstBar; Component[0].Value = adCumulSum; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Cumulative Sum rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The Cumulative Sum falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The Cumulative Sum changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The Cumulative Sum changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adCumulSum, 0, 0, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iPeriod = (int)IndParam.NumParam[0].Value; int iShift = (int)IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Convert to Higher Time Frame --------------------------------------------- DataPeriods htfPeriod = DataPeriods.week; double[] hfOpen = new double[Bars]; double[] hfClose = new double[Bars]; double[] hfHigh = new double[Bars]; double[] hfLow = new double[Bars]; double[] hfVolume = new double[Bars]; double[] hfPrice = new double[Bars]; int[] hIndex = new int[Bars]; int iFrame; int hBars; switch (IndParam.ListParam[4].Index) { case 0: htfPeriod = DataPeriods.hour1; break; case 1: htfPeriod = DataPeriods.min5; break; case 2: htfPeriod = DataPeriods.min15; break; case 3: htfPeriod = DataPeriods.min30; break; case 4: htfPeriod = DataPeriods.hour1; break; case 5: htfPeriod = DataPeriods.hour4; break; case 6: htfPeriod = DataPeriods.day; break; case 7: htfPeriod = DataPeriods.week; break; } int err1 = HigherTimeFrame(Period, htfPeriod, out hIndex, out hBars, out iFrame, out hfHigh, out hfLow, out hfOpen, out hfClose, out hfVolume); int err2 = HigherBasePrice(basePrice, hBars, hfHigh, hfLow, hfOpen, hfClose, out hfPrice); //------------------------------------------------------------------------ // TimeExecution if (basePrice == BasePrice.Open && iPeriod == 1 && iShift == 0) { IndParam.ExecutionTime = ExecutionTime.AtBarOpening; } // Calculation double[] adMA = MovingAverage(iPeriod, iShift, maMethod, hfPrice); // v3 -- manually shift so Position entry type logics use previous WTF value, fix bug of not using previous value if (IndParam.ListParam[0].Text == "The position opens above the Moving Average" || IndParam.ListParam[0].Text == "The position opens below the Moving Average") { adMA = MovingAverage(iPeriod, iShift + 1, maMethod, hfPrice); } else { adMA = MovingAverage(iPeriod, iShift, maMethod, hfPrice); } int iFirstBar = iPeriod + iShift + 1 + iPrvs; // Convert to Current Time Frame ---------------------------------------------- iPrvs = iPrvs * iFrame; iFirstBar = iFirstBar * iFrame; int err3 = (iFirstBar > 0.25 * Bars) ? 1 : 0; int err4 = CurrentTimeFrame(hIndex, hBars, ref adMA); // if any WTF conversion errors, return here to fail silently if (err1 == 1 || err2 == 1 || err3 == 1 || err4 == 1) { Component = new IndicatorComp[1]; Component[0] = new IndicatorComp(); Component[0].CompName = "MA Value"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].FirstBar = (int)Bars / 2; Component[0].UsePreviousBar = 0; Component[0].Value = new double[Bars]; return; } //----------------------------------------------------------------------------- // Saving the components if (slotType == SlotTypes.Open || slotType == SlotTypes.Close) { Component = new IndicatorComp[2]; Component[1] = new IndicatorComp(); Component[1].FirstBar = iFirstBar; Component[1].Value = new double[Bars]; int hBar = 0; for (int iBar = 3 * iFrame; iBar < Bars; iBar++) { // Covers the cases when the price can pass through the MA without a signal /// start WTF modfication 3 beta 1 while (hIndex[hBar] <= iBar && hBar < hBars) { hBar++; } double dValue = adMA[hIndex[hBar - 2]]; // MA value from previous HTF bar double dValue1 = adMA[hIndex[hBar - 3]]; // MA value from HTF 2 bars previous /// end WTF modfication 3 beta 1 double dTempVal = dValue; if ((dValue1 > High[iBar - 1] && dValue < Low[iBar]) || // It jumps below the current bar (dValue1 <Low[iBar - 1] && dValue> High[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.FromName(IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]); 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 Moving Average rises": IndicatorRisesLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The Moving Average falls": IndicatorFallsLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The bar opens above the Moving Average": BarOpensAboveIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The bar opens below the Moving Average": BarOpensBelowIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The bar opens above the Moving Average after opening below it": BarOpensAboveIndicatorAfterOpeningBelowLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The bar opens below the Moving Average after opening above it": BarOpensBelowIndicatorAfterOpeningAboveLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The position opens above the Moving Average": Component[0].PosPriceDependence = PositionPriceDependence.BuyHigherSellLower; 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 Moving Average": Component[0].PosPriceDependence = PositionPriceDependence.BuyLowerSelHigher; 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 Moving Average": BarClosesBelowIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "The bar closes above the Moving Average": BarClosesAboveIndicatorLogic(iFirstBar, iPrvs, adMA, ref Component[1], ref Component[2]); break; case "Draw only, no entry or exit": Component[1].CompName = "Visual Only"; Component[1].DataType = IndComponentType.NotDefined; Component[2].CompName = "Visual Only"; Component[2].DataType = IndComponentType.NotDefined; break; default: break; } } return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; BasePrice price = (BasePrice)IndParam.ListParam[2].Index; int iPeriod = (int)IndParam.NumParam[0].Value; double dLevel = IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation double[] adPrice = Price(price); double[] adMA = MovingAverage(iPeriod, 0, maMethod, adPrice); double[] adSTDV = new double[Bars]; int iFirstBar = iPeriod + 1; for (int iBar = iPeriod; iBar < Bars; iBar++) { double dSum = 0; for (int index = 0; index < iPeriod; index++) { double fDelta = (adPrice[iBar - index] - adMA[iBar]); dSum += fDelta * fDelta; } adSTDV[iBar] = Math.Sqrt(dSum / iPeriod); } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Standard Deviation"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = iFirstBar; Component[0].Value = adSTDV; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Standard Deviation rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The Standard Deviation falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The Standard Deviation is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[1] { dLevel }; break; case "The Standard Deviation is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[1] { dLevel }; break; case "The Standard Deviation crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[1] { dLevel }; break; case "The Standard Deviation crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[1] { dLevel }; break; case "The Standard Deviation changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The Standard Deviation changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } NoDirectionOscillatorLogic(iFirstBar, iPrvs, adSTDV, dLevel, ref Component[1], indLogic); Component[2].Value = Component[1].Value; return; }
/// <summary> /// Calculates a Moving Average /// </summary> /// <param name="iPeriod">Period</param> /// <param name="iShift">Shift</param> /// <param name="maMethod">Method of calculation</param> /// <param name="afSource">The array of source data</param> /// <returns>the Moving Average</returns> protected static double[] MovingAverage(int period, int shift, MAMethod maMethod, double[] sourceData) { int bar; double sum; double[] movingAverage = new double[Bars]; if (period <= 1 && shift == 0) { // There is no smoothing return(sourceData); } if (period > Bars || period + shift <= 0 || period + shift > Bars) { // Error in the parameters return(null); } for (bar = 0; bar < period + shift - 1; bar++) { movingAverage[bar] = 0; } for (bar = 0, sum = 0; bar < period; bar++) { sum += sourceData[bar]; } movingAverage[period + shift - 1] = sum / period; // Simple Moving Average if (maMethod == MAMethod.Simple) { for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = movingAverage[bar + shift - 1] + sourceData[bar] / period - sourceData[bar - period] / period; } } // Exponential Moving Average else if (maMethod == MAMethod.Exponential) { double pr = 2d / (period + 1); for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = sourceData[bar] * pr + movingAverage[bar + shift - 1] * (1 - pr); } } // Weighted Moving Average else if (maMethod == MAMethod.Weighted) { double weight = period * (period + 1) / 2d; for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { sum = 0; for (int i = 0; i < period; i++) { sum += sourceData[bar - i] * (period - i); } movingAverage[bar + shift] = sum / weight; } } // Smoothed Moving Average else if (maMethod == MAMethod.Smoothed) { for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = (movingAverage[bar + shift - 1] * (period - 1) + sourceData[bar]) / period; } } for (bar = Bars + shift; bar < Bars; bar++) { movingAverage[bar] = 0; } return(movingAverage); }
public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iPeriod = (int)IndParam.NumParam [0].Value; double dMpl = IndParam.NumParam[1].Value; int iLevel = (int)IndParam.NumParam[2].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation double[] adPrice = Price(basePrice); double[] adMA = MovingAverage(iPeriod, 0, maMethod, adPrice); double[] adPrcBB = new double[Bars]; int iFirstBar = iPeriod + iPrvs + 2; double dSum; double dStdDev; double dDelta; for (int iBar = iPeriod; iBar < Bars; iBar++) { dSum = 0; for (int i = 0; i < iPeriod; i++) { dDelta = (adPrice[iBar - i] - adMA[iBar]); dSum += dDelta * dDelta; } dStdDev = Math.Sqrt(dSum / iPeriod); double adUpBand = adMA[iBar] + dMpl * dStdDev; double adDnBand = adMA[iBar] - dMpl * dStdDev; adPrcBB[iBar] = (adPrice[iBar] - adDnBand) / (adUpBand - adDnBand) * 100; } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "%BB"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.RoyalBlue; Component[0].FirstBar = iFirstBar; Component[0].Value = adPrcBB; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The %BB rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[] { 0, 50, 100 }; break; case "The %BB falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[] { 0, 50, 100 }; break; case "The %BB is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[] { iLevel, 100 - iLevel }; break; case "The %BB is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[] { iLevel, 100 - iLevel }; break; case "The %BB crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[] { iLevel, 100 - iLevel }; break; case "The %BB crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[] { iLevel, 100 - iLevel }; break; case "The %BB changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[] { 0, 50, 100 }; break; case "The %BB changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[] { 0, 50, 100 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adPrcBB, iLevel, 100 - iLevel, ref Component[1], ref Component[2], indLogic); }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iPeriod = (int)IndParam.NumParam[0].Value; int iSmooth = (int)IndParam.NumParam[1].Value; double dLevel = IndParam.NumParam[2].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; int iFirstBar = iPrvs + iPeriod + iSmooth + 2; double[] adROC = new double[Bars]; double[] adBasePrice = Price(basePrice); for (int iBar = iPeriod; iBar < Bars; iBar++) { adROC[iBar] = adBasePrice[iBar] / adBasePrice[iBar - iPeriod]; } if (iSmooth > 0) { adROC = MovingAverage(iSmooth, 0, maMethod, adROC); } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "ROC"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Violet; Component[0].FirstBar = iFirstBar; Component[0].Value = adROC; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The ROC rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 1 }; break; case "The ROC falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 1 }; break; case "The ROC is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { dLevel, 2 - dLevel }; break; case "The ROC is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { dLevel, 2 - dLevel }; break; case "The ROC crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { dLevel, 2 - dLevel }; break; case "The ROC crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { dLevel, 2 - dLevel }; break; case "The ROC changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 1 }; break; case "The ROC changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 1 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adROC, dLevel, 2 - dLevel, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates a Moving Average /// </summary> /// <param name="iPeriod">Period</param> /// <param name="iShift">Shift</param> /// <param name="maMethod">Method of calculation</param> /// <param name="afSource">The array of source data</param> /// <returns>the Moving Average</returns> protected static double[] MovingAverage(int period, int shift, MAMethod maMethod, double[] sourceData) { int bar; double sum; double[] movingAverage = new double[Bars]; if (period <= 1 && shift == 0) { // There is no smoothing return sourceData; } if (period > Bars || period + shift <= 0 || period + shift > Bars) { // Error in the parameters return null; } for (bar = 0; bar < period + shift - 1; bar++) { movingAverage[bar] = 0; } for (bar = 0, sum = 0; bar < period; bar++) { sum += sourceData[bar]; } movingAverage[period + shift - 1] = sum / period; // Simple Moving Average if (maMethod == MAMethod.Simple) { for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = movingAverage[bar + shift - 1] + sourceData[bar] / period - sourceData[bar - period] / period; } } // Exponential Moving Average else if (maMethod == MAMethod.Exponential) { double pr = 2d / (period + 1); for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = sourceData[bar] * pr + movingAverage[bar + shift - 1] * (1 - pr); } } // Weighted Moving Average else if (maMethod == MAMethod.Weighted) { double weight = period * (period + 1) / 2d; for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { sum = 0; for (int i = 0; i < period; i++) { sum += sourceData[bar - i] * (period - i); } movingAverage[bar + shift] = sum / weight; } } // Smoothed Moving Average else if (maMethod == MAMethod.Smoothed) { for (bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = (movingAverage[bar + shift - 1] * (period - 1) + sourceData[bar]) / period; } } for (bar = Bars + shift; bar < Bars; bar++) { movingAverage[bar] = 0; } return movingAverage; }
/// <summary> /// Calculates a Moving Average /// </summary> /// <param name="period">Period</param> /// <param name="shift">Shift</param> /// <param name="maMethod">Method of calculation</param> /// <param name="source">The array of source data</param> /// <returns>the Moving Average</returns> protected double[] MovingAverage(int period, int shift, MAMethod maMethod, double[] source) { var movingAverage = new double[Bars]; if (period <= 1 && shift == 0) return source; if (period > Bars || period + shift <= 0 || period + shift > Bars) { // Wrong MovingAverage parameters return movingAverage; } for (int bar = 0; bar < period + shift - 1; bar++) movingAverage[bar] = 0; double sum = 0; for (int bar = shift; bar < period + shift; bar++) sum += source[bar]; movingAverage[period + shift - 1] = sum/period; int lastBar = Math.Min(Bars, Bars - shift); switch (maMethod) { case MAMethod.Simple: for (int bar = period; bar < lastBar; bar++) movingAverage[bar + shift] = movingAverage[bar + shift - 1] + source[bar]/period - source[bar - period]/period; break; case MAMethod.Exponential: { double pr = 2d/(period + 1); for (int bar = period; bar < lastBar; bar++) movingAverage[bar + shift] = source[bar]*pr + movingAverage[bar + shift - 1]*(1 - pr); } break; case MAMethod.Weighted: { double weight = period*(period + 1)/2d; for (int bar = period; bar < lastBar; bar++) { sum = 0; for (int i = 0; i < period; i++) sum += source[bar - i]*(period - i); movingAverage[bar + shift] = sum/weight; } } break; case MAMethod.Smoothed: for (int bar = period; bar < lastBar; bar++) movingAverage[bar + shift] = (movingAverage[bar + shift - 1]*(period - 1) + source[bar])/period; break; } for (int bar = Bars + shift; bar < Bars; bar++) movingAverage[bar] = 0; return movingAverage; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iKMin = (int)IndParam.NumParam[0].Value; int iKMax = (int)IndParam.NumParam[0].Value; int iD = (int)IndParam.NumParam[2].Value; int iLevel = (int)IndParam.NumParam[4].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iKMax + iD + 3; double[] adHighs = new double[Bars]; double[] adLows = new double[Bars]; double[] adPriceStdDev = new double[Bars]; double[] adMaxStdDev = new double[Bars]; double[] adMinStdDev = new double[Bars]; double[] adK = new double[Bars]; double dRatio; int iKAvg = (int)(iKMax + iKMin) / 2; int iK; adPriceStdDev = StdDev(Close, iKAvg); adMaxStdDev = Highest(adPriceStdDev, iKAvg); adMinStdDev = Lowest(adPriceStdDev, iKAvg); for (int iBar = iKMax; iBar < Bars; iBar++) { // Calculate Effective Length for %K Period if (adMaxStdDev[iBar] - adMinStdDev[iBar] != 0) { dRatio = (adPriceStdDev[iBar] - adMinStdDev[iBar]) / (adMaxStdDev[iBar] - adMinStdDev[iBar]); iK = (int)(iKMin + (iKMax - iKMin) * (1 - dRatio)); } else { iK = (int)iKMax; } // Calculate Highest High and Lowest Low double dMax = double.MinValue; double dMin = double.MaxValue; for (int iBack = 0; iBack < iK; iBack++) { if (High[iBar - iBack] > dMax) { dMax = High[iBar - iBack]; } if (Low[iBar - iBack] < dMin) { dMin = Low[iBar - iBack]; } } adHighs[iBar] = dMax; adLows[iBar] = dMin; // Calculate %K Value if (adHighs[iBar] == adLows[iBar]) { adK[iBar] = 50; } else { adK[iBar] = 100 * (Close[iBar] - adLows[iBar]) / (adHighs[iBar] - adLows[iBar]); } } // Calculate %D Value double[] adD = MovingAverage(iD, 0, maMethod, adK); // Saving the components Component = new IndicatorComp[4]; 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 = adK; Component[1] = new IndicatorComp(); Component[1].CompName = "%D"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].ChartColor = Color.Blue; Component[1].FirstBar = iFirstBar; Component[1].Value = adD; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; if (IndParam.ListParam[0].Text == "The %K crosses the %D upward") { SpecialValues = new double[1] { 50 }; IndicatorCrossesAnotherIndicatorUpwardLogic(iFirstBar, iPrvs, adK, adD, ref Component[2], ref Component[3]); return; } else if (IndParam.ListParam[0].Text == "The %K crosses the %D downward") { SpecialValues = new double[1] { 50 }; IndicatorCrossesAnotherIndicatorDownwardLogic(iFirstBar, iPrvs, adK, adD, ref Component[2], ref Component[3]); return; } else if (IndParam.ListParam[0].Text == "The %K is higher than the %D") { SpecialValues = new double[1] { 50 }; IndicatorIsHigherThanAnotherIndicatorLogic(iFirstBar, iPrvs, adK, adD, ref Component[2], ref Component[3]); return; } else if (IndParam.ListParam[0].Text == "The %K is lower than the %D") { SpecialValues = new double[1] { 50 }; IndicatorIsLowerThanAnotherIndicatorLogic(iFirstBar, iPrvs, adK, adD, ref Component[2], ref Component[3]); return; } else { switch (IndParam.ListParam[0].Text) { case "The %D rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 50 }; break; case "The %D falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 50 }; break; case "The %D is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The %D is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The %D crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The %D crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The %D changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 50 }; break; case "The %D changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 50 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adD, iLevel, 100 - iLevel, ref Component[2], ref Component[3], indLogic); } return; }
/// <summary> /// Calculates a Moving Average /// </summary> /// <param name="period">Period</param> /// <param name="shift">Shift</param> /// <param name="maMethod">Method of calculation</param> /// <param name="source">The array of source data</param> /// <returns>the Moving Average</returns> protected static double[] MovingAverage(int period, int shift, MAMethod maMethod, double[] source) { var movingAverage = new double[Bars]; if (period <= 1 && shift == 0) { // There is no smoothing return source; } if (period > Bars || period + shift <= 0 || period + shift > Bars) { // Error in the parameters return null; } for (int bar = 0; bar < period + shift - 1; bar++) { movingAverage[bar] = 0; } double sum = 0; for (int bar = 0; bar < period; bar++) { sum += source[bar]; } movingAverage[period + shift - 1] = sum/period; switch (maMethod) { case MAMethod.Simple: for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = movingAverage[bar + shift - 1] + source[bar]/period - source[bar - period]/period; } break; case MAMethod.Exponential: { double pr = 2d/(period + 1); for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = source[bar]*pr + movingAverage[bar + shift - 1]*(1 - pr); } } break; case MAMethod.Weighted: { double dWeight = period*(period + 1)/2d; for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { sum = 0; for (int i = 0; i < period; i++) { sum += source[bar - i]*(period - i); } movingAverage[bar + shift] = sum/dWeight; } } break; case MAMethod.Smoothed: for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = (movingAverage[bar + shift - 1]*(period - 1) + source[bar])/period; } break; } for (int bar = Bars + shift; bar < Bars; bar++) { movingAverage[bar] = 0; } return movingAverage; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iPeriod1 = (int)IndParam.NumParam[0].Value; int iPeriod2 = (int)IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iPeriod1 + iPeriod2 + 2; double[] adIndicator1 = new double[Bars]; double[] adIndicator2 = new double[Bars]; double[] adOscllator = new double[Bars]; // --------------------------------------------------------- RSI rsi1 = new RSI(slotType); rsi1.IndParam.ListParam[1].Index = IndParam.ListParam[1].Index; rsi1.IndParam.ListParam[2].Index = IndParam.ListParam[2].Index; rsi1.IndParam.NumParam[0].Value = IndParam.NumParam[0].Value; rsi1.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked; rsi1.Calculate(slotType); RSI rsi2 = new RSI(slotType); rsi2.IndParam.ListParam[1].Index = IndParam.ListParam[1].Index; rsi2.IndParam.ListParam[2].Index = IndParam.ListParam[2].Index; rsi2.IndParam.NumParam[0].Value = IndParam.NumParam[1].Value; rsi2.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked; rsi2.Calculate(slotType); adIndicator1 = rsi1.Component[0].Value; adIndicator2 = rsi2.Component[0].Value; // ---------------------------------------------------------- for (int iBar = iFirstBar; iBar < Bars; iBar++) { adOscllator[iBar] = adIndicator1[iBar] - adIndicator2[iBar]; } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Oscillator"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adOscllator; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Oscillator rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The Oscillator falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The Oscillator is higher than the zero line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; break; case "The Oscillator is lower than the zero line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; break; case "The Oscillator crosses the zero line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; break; case "The Oscillator crosses the zero line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; break; case "The Oscillator changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The Oscillator changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adOscllator, 0, 0, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iMAPeriod = (int)IndParam.NumParam[0].Value; int iLRLength = (int)IndParam.NumParam[1].Value; double dLevel = IndParam.NumParam[2].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; double[] dX = new double[Bars]; double[] dY = new double[Bars]; double dSigX; double dSigY; double dSigXY; double dSigXX; double[] adLRSlope = new double[Bars]; // Convert to Higher Time Frame --------------------------------------------- DataPeriods htfPeriod = DataPeriods.week; double[] hfOpen = new double[Bars]; double[] hfClose = new double[Bars]; double[] hfHigh = new double[Bars]; double[] hfLow = new double[Bars]; double[] hfVolume = new double[Bars]; double[] hfPrice = new double[Bars]; int[] hIndex = new int[Bars]; int iFrame; int hBars; switch (IndParam.ListParam[4].Index) { case 1: htfPeriod = DataPeriods.min5; break; case 2: htfPeriod = DataPeriods.min15; break; case 3: htfPeriod = DataPeriods.min30; break; case 4: htfPeriod = DataPeriods.hour1; break; case 5: htfPeriod = DataPeriods.hour4; break; case 6: htfPeriod = DataPeriods.day; break; case 7: htfPeriod = DataPeriods.week; break; } int err1 = HigherTimeFrame(Period, htfPeriod, out hIndex, out hBars, out iFrame, out hfHigh, out hfLow, out hfOpen, out hfClose, out hfVolume); int err2 = HigherBasePrice(basePrice, hBars, hfHigh, hfLow, hfOpen, hfClose, out hfPrice); if (err1 == 1) { return; } //------------------------------------------------------------------------ int iFirstBar = iMAPeriod + iLRLength + 2; double[] adMAPrice = MovingAverage(iMAPeriod, 0, maMethod, hfPrice); for (int iBar = iFirstBar; iBar < hBars; iBar++) { dSigX = 0; dSigY = 0; dSigXX = 0; dSigXY = 0; for (int index = 0; index < iLRLength; index++) { dSigX = dSigX + index; dSigY = dSigY + adMAPrice[iBar - index]; dSigXY = dSigXY + index * adMAPrice[iBar - index]; dSigXX = dSigXX + index * index; } adLRSlope[iBar] = -(iLRLength * dSigXY - dSigX * dSigY) / (iLRLength * dSigXX - dSigX * dSigX); } // Convert to Current Time Frame ---------------------------------------------- /// start WTF modfication 2 version 4 // copy of wider time frame array of values double[] hadLRSlope = new double[Bars]; adLRSlope.CopyTo(hadLRSlope, 0); int err3 = CurrentTimeFrame(hIndex, hBars, ref adLRSlope); // if any error, return out of calculation and indicator fails silently if (err3 == 1) { return; } /// end WTF modfication 2 version 4 //----------------------------------------------------------------------------- // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "LR Slope"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adLRSlope; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The LR Slope rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 0 }; break; case "The LR Slope falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 0 }; break; case "The LR Slope is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 0 }; break; case "The LR Slope changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 0 }; break; default: break; } /// start WTF modfication 3 version 4 // back up Bars value, reset to hBars, for performance improvement in indicator logic function int mtfBars = Data.Bars; Data.Bars = hBars; // replace very small values with 0 for performance improvement; don't know why but works for (int ctr = 0; ctr < hadLRSlope.Length; ctr++) { hadLRSlope[ctr] = (hadLRSlope[ctr] < .000000001 && hadLRSlope[ctr] > -.000000001) ? 0 : hadLRSlope[ctr]; } OscillatorLogic(iFirstBar, iPrvs, hadLRSlope, dLevel, -dLevel, ref Component[1], ref Component[2], indLogic); // resest Bars to real value Data.Bars = mtfBars; // expand component array from wtf to current time frame double[] wtfCompValue = Component[1].Value; int err4 = CurrentTimeFrame(hIndex, hBars, ref wtfCompValue); if (err4 == 1) { return; } Component[1].Value = wtfCompValue; wtfCompValue = Component[2].Value; int err5 = CurrentTimeFrame(hIndex, hBars, ref wtfCompValue); if (err5 == 1) { return; } Component[2].Value = wtfCompValue; /// end WTF modfication 3 version 4 return; }
/// <summary> /// Calculates a Moving Average /// </summary> /// <param name="period">Period</param> /// <param name="shift">Shift</param> /// <param name="maMethod">Method of calculation</param> /// <param name="source">The array of source data</param> /// <returns>the Moving Average</returns> protected static double[] MovingAverage(int period, int shift, MAMethod maMethod, double[] source) { var movingAverage = new double[Bars]; if (period <= 1 && shift == 0) { // There is no smoothing return(source); } if (period > Bars || period + shift <= 0 || period + shift > Bars) { // Error in the parameters return(null); } for (int bar = 0; bar < period + shift - 1; bar++) { movingAverage[bar] = 0; } double sum = 0; for (int bar = 0; bar < period; bar++) { sum += source[bar]; } movingAverage[period + shift - 1] = sum / period; switch (maMethod) { case MAMethod.Simple: for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = movingAverage[bar + shift - 1] + source[bar] / period - source[bar - period] / period; } break; case MAMethod.Exponential: { double pr = 2d / (period + 1); for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = source[bar] * pr + movingAverage[bar + shift - 1] * (1 - pr); } } break; case MAMethod.Weighted: { double dWeight = period * (period + 1) / 2d; for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { sum = 0; for (int i = 0; i < period; i++) { sum += source[bar - i] * (period - i); } movingAverage[bar + shift] = sum / dWeight; } } break; case MAMethod.Smoothed: for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { movingAverage[bar + shift] = (movingAverage[bar + shift - 1] * (period - 1) + source[bar]) / period; } break; } for (int bar = Bars + shift; bar < Bars; bar++) { movingAverage[bar] = 0; } return(movingAverage); }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iPeriod = (int)IndParam.NumParam[0].Value; int iSmoothing = (int)IndParam.NumParam[1].Value; int dLevel = (int)IndParam.NumParam[2].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iPeriod + iSmoothing + iPrvs + 2; double[] adR = new double[Bars]; double dMin = double.MaxValue; double dMax = double.MinValue; for (int iBar = iPeriod; iBar < Bars; iBar++) { dMin = double.MaxValue; dMax = double.MinValue; for (int index = 0; index < iPeriod; index++) { if (High[iBar - index] > dMax) { dMax = High[iBar - index]; } if (Low [iBar - index] < dMin) { dMin = Low [iBar - index]; } } adR[iBar] = -100 * (dMax - Close[iBar]) / (dMax - dMin); } double[] adRSmoothed = MovingAverage(iSmoothing, 0, maMethod, adR); // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "%R"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Teal; Component[0].FirstBar = iFirstBar; Component[0].Value = adRSmoothed; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The %R rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { -50 }; break; case "The %R falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { -50 }; break; case "The %R is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { dLevel, -100 - dLevel }; break; case "The %R is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { dLevel, -100 - dLevel }; break; case "The %R crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { dLevel, -100 - dLevel }; break; case "The %R crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { dLevel, -100 - dLevel }; break; case "The %R changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { -50 }; break; case "The %R changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { -50 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adRSmoothed, dLevel, -100 - dLevel, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iK = (int)IndParam.NumParam[0].Value; int iDFast = (int)IndParam.NumParam[1].Value; int iDSlow = (int)IndParam.NumParam[2].Value; int iLevel = (int)IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Convert to Higher Time Frame --------------------------------------------- DataPeriods htfPeriod = DataPeriods.week; double[] hfOpen = new double[Bars]; double[] hfClose = new double[Bars]; double[] hfHigh = new double[Bars]; double[] hfLow = new double[Bars]; double[] hfVolume = new double[Bars]; double[] hfPrice = new double[Bars]; int[] hIndex = new int[Bars]; int iFrame; int hBars; switch (IndParam.ListParam[4].Index) { case 1: htfPeriod = DataPeriods.min5; break; case 2: htfPeriod = DataPeriods.min15; break; case 3: htfPeriod = DataPeriods.min30; break; case 4: htfPeriod = DataPeriods.hour1; break; case 5: htfPeriod = DataPeriods.hour4; break; case 6: htfPeriod = DataPeriods.day; break; case 7: htfPeriod = DataPeriods.week; break; } int err1 = HigherTimeFrame(Period, htfPeriod, out hIndex, out hBars, out iFrame, out hfHigh, out hfLow, out hfOpen, out hfClose, out hfVolume); //int err2 = HigherBasePrice(basePrice, hBars, hfHigh, hfLow, hfOpen, hfClose, out hfPrice); if (err1 == 1) { return; } //------------------------------------------------------------------------ // Calculation int iFirstBar = iK + iDFast + iDSlow + 3; double[] adHighs = new double[Bars]; double[] adLows = new double[Bars]; for (int iBar = iK; iBar < hBars; iBar++) { double dMin = double.MaxValue; double dMax = double.MinValue; for (int i = 0; i < iK; i++) { if (hfHigh[iBar - i] > dMax) { dMax = hfHigh[iBar - i]; } if (hfLow[iBar - i] < dMin) { dMin = hfLow[iBar - i]; } } adHighs[iBar] = dMax; adLows[iBar] = dMin; } double[] adK = new double[Bars]; for (int iBar = iK; iBar < hBars; iBar++) { if (adHighs[iBar] == adLows[iBar]) { adK[iBar] = 50; } else { adK[iBar] = 100 * (hfClose[iBar] - adLows[iBar]) / (adHighs[iBar] - adLows[iBar]); } } double[] adDFast = new double[Bars]; for (int iBar = iDFast; iBar < hBars; iBar++) { double dSumHigh = 0; double dSumLow = 0; for (int i = 0; i < iDFast; i++) { dSumLow += hfClose[iBar - i] - adLows[iBar - i]; dSumHigh += adHighs[iBar - i] - adLows[iBar - i]; } if (dSumHigh == 0) { adDFast[iBar] = 100; } else { adDFast[iBar] = 100 * dSumLow / dSumHigh; } } double[] adDSlow = MovingAverage(iDSlow, 0, maMethod, adDFast); // Convert to Current Time Frame ---------------------------------------------- /// start WTF modfication 2 version 4 // copy of wider time frame array of values double[] hadK = new double[Bars]; double[] hadDFast = new double[Bars]; double[] hadDSlow = new double[Bars]; adK.CopyTo(hadK, 0); adDFast.CopyTo(hadDFast, 0); adDSlow.CopyTo(hadDSlow, 0); int err3 = CurrentTimeFrame(hIndex, hBars, ref adK); int err4 = CurrentTimeFrame(hIndex, hBars, ref adDFast); int err5 = CurrentTimeFrame(hIndex, hBars, ref adDSlow); // if any error, return out of calculation and indicator fails silently if (err3 == 1 || err4 == 1 || err5 == 1) { return; } /// end WTF modfication 2 version 4 //----------------------------------------------------------------------------- // 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 = adK; 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 = adDFast; 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 = adDSlow; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; Component[4] = new IndicatorComp(); Component[4].ChartType = IndChartType.NoChart; Component[4].FirstBar = iFirstBar; Component[4].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[3].DataType = IndComponentType.AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType.AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[3].DataType = IndComponentType.ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType.ForceCloseShort; Component[4].CompName = "Close out short position"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; /// start WTF modfication 3 version 4 // back up Bars value, reset to hBars, for performance improvement in indicator logic function int mtfBars = Data.Bars; Data.Bars = hBars; // use wtf values here, then do expansion after this if clause to cover these IndicatorCrosses cases and the OscillatorLogic cases // replace very small values with 0 for performance improvement; don't know why but works for (int ctr = 0; ctr < hadDSlow.Length; ctr++) { hadK[ctr] = (hadK[ctr] < .000000001 && hadK[ctr] > -.000000001) ? 0 : hadK[ctr]; hadDSlow[ctr] = (hadDSlow[ctr] < .000000001 && hadDSlow[ctr] > -.000000001) ? 0 : hadDSlow[ctr]; } if (IndParam.ListParam[0].Text == "The %K crosses the Slow %D upward") { SpecialValues = new double[1] { 50 }; IndicatorCrossesAnotherIndicatorUpwardLogic(iFirstBar, iPrvs, hadK, hadDSlow, ref Component[3], ref Component[4]); } else if (IndParam.ListParam[0].Text == "The %K crosses the Slow %D downward") { SpecialValues = new double[1] { 50 }; IndicatorCrossesAnotherIndicatorDownwardLogic(iFirstBar, iPrvs, hadK, hadDSlow, ref Component[3], ref Component[4]); } else if (IndParam.ListParam[0].Text == "The %K is higher than the Slow %D") { SpecialValues = new double[1] { 50 }; IndicatorIsHigherThanAnotherIndicatorLogic(iFirstBar, iPrvs, hadK, hadDSlow, ref Component[3], ref Component[4]); } else if (IndParam.ListParam[0].Text == "The %K is lower than the Slow %D") { SpecialValues = new double[1] { 50 }; IndicatorIsLowerThanAnotherIndicatorLogic(iFirstBar, iPrvs, hadK, hadDSlow, ref Component[3], ref Component[4]); } else { switch (IndParam.ListParam[0].Text) { case "The Slow %D rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 50 }; break; case "The Slow %D falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 50 }; break; case "The Slow %D is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 50 }; break; case "The Slow %D changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 50 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, hadDSlow, iLevel, 100 - iLevel, ref Component[3], ref Component[4], indLogic); } // resest Bars to real value Data.Bars = mtfBars; // expand component array from wtf to current time frame double[] wtfCompValue = Component[3].Value; int err6 = CurrentTimeFrame(hIndex, hBars, ref wtfCompValue); if (err6 == 1) { return; } Component[3].Value = wtfCompValue; wtfCompValue = Component[4].Value; int err7 = CurrentTimeFrame(hIndex, hBars, ref wtfCompValue); if (err7 == 1) { return; } Component[4].Value = wtfCompValue; /// end WTF modfication 3 version 4 return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters BasePrice basePrice = (BasePrice)IndParam.ListParam[1].Index; MAMethod fastMAMethod = (MAMethod )IndParam.ListParam[3].Index; MAMethod slowMAMethod = (MAMethod )IndParam.ListParam[4].Index; int iNFastMA = (int)IndParam.NumParam[0].Value; int iNSlowMA = (int)IndParam.NumParam[1].Value; int iSFastMA = (int)IndParam.NumParam[2].Value; int iSSlowMA = (int)IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; int iFirstBar = (int)Math.Max(iNFastMA + iSFastMA, iNSlowMA + iSSlowMA) + 2; double[] adMAFast = MovingAverage(iNFastMA, iSFastMA, fastMAMethod, Price(basePrice)); double[] adMASlow = MovingAverage(iNSlowMA, iSSlowMA, slowMAMethod, Price(basePrice)); double[] adMAOscillator = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { adMAOscillator[iBar] = adMAFast[iBar] - adMASlow[iBar]; } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "Fast Moving Average"; Component[0].ChartColor = Color.Goldenrod; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].FirstBar = iFirstBar; Component[0].Value = adMAFast; Component[1] = new IndicatorComp(); Component[1].CompName = "Slow Moving Average"; Component[1].ChartColor = Color.IndianRed; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].FirstBar = iFirstBar; Component[1].Value = adMASlow; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Fast MA crosses the Slow MA upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; break; case "The Fast MA crosses the Slow MA downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; break; case "The Fast MA is higher than the Slow MA": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; break; case "The Fast MA is lower than the Slow MA": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adMAOscillator, 0, 0, ref Component[2], ref Component[3], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iPeriod = (int)IndParam.NumParam[0].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iPeriod + 2; double[] adBOP = new double[Bars]; for (int iBar = 1; iBar < Bars; iBar++) { if (High[iBar] - Low[iBar] > Point) { adBOP[iBar] = (Close[iBar] - Open[iBar]) / (High[iBar] - Low[iBar]); } else { adBOP[iBar] = 0; } } adBOP = MovingAverage(iPeriod, 0, maMethod, adBOP); // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Balance of Power"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adBOP; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Balance of Power rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The Balance of Power falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The Balance of Power is higher than the zero line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; break; case "The Balance of Power is lower than the zero line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; break; case "The Balance of Power crosses the zero line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; break; case "The Balance of Power crosses the zero line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; break; case "The Balance of Power changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The Balance of Power changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adBOP, 0, 0, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int period = (int)IndParam.NumParam[0].Value; int prev = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = period + 2; double[] DIPos = new double[Bars]; double[] DINeg = new double[Bars]; for (int bar = 1; bar < Bars; bar++) { double trueRange = Math.Max(High[bar], Close[bar - 1]) - Math.Min(Low[bar], Close[bar - 1]); if (trueRange < Point) { trueRange = Point; } double deltaHigh = High[bar] - High[bar - 1]; double deltaLow = Low[bar - 1] - Low[bar]; if (deltaHigh > 0 && deltaHigh > deltaLow) { DIPos[bar] = 100 * deltaHigh / trueRange; } else { DIPos[bar] = 0; } if (deltaLow > 0 && deltaLow > deltaHigh) { DINeg[bar] = 100 * deltaLow / trueRange; } else { DINeg[bar] = 0; } } double[] ADIPos = MovingAverage(period, 0, maMethod, DIPos); double[] ADINeg = MovingAverage(period, 0, maMethod, DINeg); double[] ADIOsc = new double[Bars]; for (int bar = 0; bar < Bars; bar++) { ADIOsc[bar] = ADIPos[bar] - ADINeg[bar]; } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "The ADI+"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Green; Component[0].FirstBar = firstBar; Component[0].Value = ADIPos; Component[1] = new IndicatorComp(); Component[1].CompName = "The ADI-"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].ChartColor = Color.Red; Component[1].FirstBar = firstBar; Component[1].Value = ADINeg; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = firstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = firstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } switch (IndParam.ListParam[0].Text) { case "The ADI+ rises": OscillatorLogic(firstBar, prev, ADIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_rises); break; case "The ADI+ falls": OscillatorLogic(firstBar, prev, ADIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_falls); break; case "The ADI- rises": OscillatorLogic(firstBar, prev, ADINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_rises); break; case "The ADI- falls": OscillatorLogic(firstBar, prev, ADINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_falls); break; case "The ADI+ is higher than ADI-": OscillatorLogic(firstBar, prev, ADIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_is_higher_than_the_level_line); break; case "The ADI+ is lower than ADI-": OscillatorLogic(firstBar, prev, ADIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_is_lower_than_the_level_line); break; case "The ADI+ crosses the ADI- line upward": OscillatorLogic(firstBar, prev, ADIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_crosses_the_level_line_upward); break; case "The ADI+ crosses the ADI- line downward": OscillatorLogic(firstBar, prev, ADIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_crosses_the_level_line_downward); break; case "The ADI+ changes its direction upward": OscillatorLogic(firstBar, prev, ADIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_upward); break; case "The ADI+ changes its direction downward": OscillatorLogic(firstBar, prev, ADIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_downward); break; case "The ADI- changes its direction upward": OscillatorLogic(firstBar, prev, ADINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_upward); break; case "The ADI- changes its direction downward": OscillatorLogic(firstBar, prev, ADINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_downward); break; default: break; } return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int period = (int)IndParam.NumParam[0].Value; double level = Point * IndParam.NumParam[1].Value; int prev = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = period + 2; double[] ATR = new double[Bars]; for (int bar = 1; bar < Bars; bar++) { ATR[bar] = Math.Max(High[bar], Close[bar - 1]) - Math.Min(Low[bar], Close[bar - 1]); } ATR = MovingAverage(period, 0, maMethod, ATR); // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Average True Range"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = firstBar; Component[0].Value = ATR; Component[1] = new IndicatorComp(); Component[1].ChartType = IndChartType.NoChart; Component[1].FirstBar = firstBar; Component[1].Value = new double[Bars]; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = firstBar; Component[2].Value = new double[Bars]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The ATR rises": indLogic = IndicatorLogic.The_indicator_rises; break; case "The ATR falls": indLogic = IndicatorLogic.The_indicator_falls; break; case "The ATR is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[1] { level }; break; case "The ATR is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[1] { level }; break; case "The ATR crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[1] { level }; break; case "The ATR crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[1] { level }; break; case "The ATR changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The ATR changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: break; } // ATR rises equal signals in both directions! NoDirectionOscillatorLogic(firstBar, prev, ATR, level, ref Component[1], indLogic); Component[2].Value = Component[1].Value; return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int nPeriod = (int)IndParam.NumParam[0].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = 2 * nPeriod + 2; double[] ma1 = MovingAverage(nPeriod, 0, maMethod, Price(basePrice)); double[] ma2 = MovingAverage(nPeriod, 0, maMethod, ma1); double[] ma3 = MovingAverage(nPeriod, 0, maMethod, ma2); double[] adTrix = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { adTrix[iBar] = 100 * (ma3[iBar] - ma3[iBar - 1]) / ma3[iBar - 1]; } double[] adSignal = MovingAverage(nPeriod, 0, maMethod, adTrix); // adHistogram reprezents the Trix Index oscillator double[] adHistogram = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { adHistogram[iBar] = adTrix[iBar] - adSignal[iBar]; } // Saving the components Component = new IndicatorComp[5]; Component[0] = new IndicatorComp(); Component[0].CompName = "Histogram"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adHistogram; Component[1] = new IndicatorComp(); Component[1].CompName = "Signal"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].ChartColor = Color.Gold; Component[1].FirstBar = iFirstBar; Component[1].Value = adSignal; Component[2] = new IndicatorComp(); Component[2].CompName = "Trix Line"; Component[2].DataType = IndComponentType.IndicatorValue; Component[2].ChartType = IndChartType.Line; Component[2].ChartColor = Color.Blue; Component[2].FirstBar = iFirstBar; Component[2].Value = adTrix; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; Component[4] = new IndicatorComp(); Component[4].ChartType = IndChartType.NoChart; Component[4].FirstBar = iFirstBar; Component[4].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[3].DataType = IndComponentType.AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType.AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[3].DataType = IndComponentType.ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType.ForceCloseShort; Component[4].CompName = "Close out short position"; } switch (IndParam.ListParam[0].Text) { case "The Trix Index line rises": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_rises); break; case "The Trix Index line falls": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_falls); break; case "The Trix Index line is higher than zero": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_is_higher_than_the_level_line); break; case "The Trix Index line is lower than zero": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_is_lower_than_the_level_line); break; case "The Trix Index line crosses the zero line upward": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_crosses_the_level_line_upward); break; case "The Trix Index line crosses the zero line downward": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_crosses_the_level_line_downward); break; case "The Trix Index line changes its direction upward": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_changes_its_direction_upward); break; case "The Trix Index line changes its direction downward": OscillatorLogic(iFirstBar, iPrvs, adTrix, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_changes_its_direction_downward); break; case "The Trix Index line crosses the Signal line upward": OscillatorLogic(iFirstBar, iPrvs, adHistogram, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_crosses_the_level_line_upward); break; case "The Trix Index line crosses the Signal line downward": OscillatorLogic(iFirstBar, iPrvs, adHistogram, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_crosses_the_level_line_downward); break; case "The Trix Index line is higher than the Signal line": OscillatorLogic(iFirstBar, iPrvs, adHistogram, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_is_higher_than_the_level_line); break; case "The Trix Index line is lower than the Signal line": OscillatorLogic(iFirstBar, iPrvs, adHistogram, 0, 0, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_is_lower_than_the_level_line); break; default: break; } return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int iK = (int)IndParam.NumParam[0].Value; int iDFast = (int)IndParam.NumParam[1].Value; int iDSlow = (int)IndParam.NumParam[2].Value; int iLevel = (int)IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = iK + iDFast + iDSlow + 3; double[] adHighs = new double[Bars]; double[] adLows = new double[Bars]; for (int iBar = iK; iBar < Bars; iBar++) { double dMin = double.MaxValue; double dMax = double.MinValue; for (int i = 0; i < iK; i++) { if (High[iBar - i] > dMax) { dMax = High[iBar - i]; } if (Low[iBar - i] < dMin) { dMin = Low[iBar - i]; } } adHighs[iBar] = dMax; adLows[iBar] = dMin; } double[] adK = new double[Bars]; for (int iBar = iK; iBar < Bars; iBar++) { if (adHighs[iBar] == adLows[iBar]) { adK[iBar] = 50; } else { adK[iBar] = 100 * (Close[iBar] - adLows[iBar]) / (adHighs[iBar] - adLows[iBar]); } } /* * double[] adDFast = new double[Bars]; * * for (int iBar = iDFast; iBar < Bars; iBar++) * { * double dSumHigh = 0; * double dSumLow = 0; * for (int i = 0; i < iDFast; i++) * { * dSumLow += Close[iBar - i] - adLows[iBar - i]; * dSumHigh += adHighs[iBar - i] - adLows[iBar - i]; * } * if (dSumHigh == 0) * adDFast[iBar] = 100; * else * adDFast[iBar] = 100 * dSumLow / dSumHigh; * } */ double[] adDFast = MovingAverage(iDFast, 0, maMethod, adK); double[] adDSlow = MovingAverage(iDSlow, 0, maMethod, adDFast); // 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 = adK; 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 = adDFast; 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 = adDSlow; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; Component[4] = new IndicatorComp(); Component[4].ChartType = IndChartType.NoChart; Component[4].FirstBar = iFirstBar; Component[4].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[3].DataType = IndComponentType.AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType.AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[3].DataType = IndComponentType.ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType.ForceCloseShort; Component[4].CompName = "Close out short position"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; if (IndParam.ListParam[0].Text == "The %K crosses the Slow %D upward") { SpecialValues = new double[1] { 50 }; IndicatorCrossesAnotherIndicatorUpwardLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]); return; } else if (IndParam.ListParam[0].Text == "The %K crosses the Slow %D downward") { SpecialValues = new double[1] { 50 }; IndicatorCrossesAnotherIndicatorDownwardLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]); return; } else if (IndParam.ListParam[0].Text == "The %K is higher than the Slow %D") { SpecialValues = new double[1] { 50 }; IndicatorIsHigherThanAnotherIndicatorLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]); return; } else if (IndParam.ListParam[0].Text == "The %K is lower than the Slow %D") { SpecialValues = new double[1] { 50 }; IndicatorIsLowerThanAnotherIndicatorLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]); return; } else if (IndParam.ListParam[0].Text == "The Fast %D is higher than the Slow %D") { SpecialValues = new double[1] { 50 }; IndicatorIsHigherThanAnotherIndicatorLogic(iFirstBar, iPrvs, adDFast, adDSlow, ref Component[3], ref Component[4]); return; } else if (IndParam.ListParam[0].Text == "The Fast %D is lower than the Slow %D") { SpecialValues = new double[1] { 50 }; IndicatorIsLowerThanAnotherIndicatorLogic(iFirstBar, iPrvs, adDFast, adDSlow, ref Component[3], ref Component[4]); return; } else if (IndParam.ListParam[0].Text == "The %K is higher than the Level line") { SpecialValues = new double[2] { iLevel, 100 - iLevel }; OscillatorLogic(iFirstBar, iPrvs, adK, iLevel, 100 - iLevel, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_is_higher_than_the_level_line); return; } else if (IndParam.ListParam[0].Text == "The %K is lower than the Level line") { SpecialValues = new double[2] { iLevel, 100 - iLevel }; OscillatorLogic(iFirstBar, iPrvs, adK, iLevel, 100 - iLevel, ref Component[3], ref Component[4], IndicatorLogic.The_indicator_is_lower_than_the_level_line); return; } else { switch (IndParam.ListParam[0].Text) { case "The Slow %D rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 50 }; break; case "The Slow %D falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 50 }; break; case "The Slow %D is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { iLevel, 100 - iLevel }; break; case "The Slow %D changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 50 }; break; case "The Slow %D changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 50 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adDSlow, iLevel, 100 - iLevel, ref Component[3], ref Component[4], indLogic); } return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int nSlow = (int)IndParam.NumParam[0].Value; int nFast = (int)IndParam.NumParam[1].Value; double dLevel = IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = nSlow + 2; double[] adMASlow = MovingAverage(nSlow, 0, maMethod, Price(basePrice)); double[] adMAFast = MovingAverage(nFast, 0, maMethod, Price(basePrice)); double[] adAO = new double[Bars]; for (int iBar = nSlow - 1; iBar < Bars; iBar++) { adAO[iBar] = adMAFast[iBar] - adMASlow[iBar]; } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "AO"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adAO; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indicatorLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The AO rises": indicatorLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 0 }; break; case "The AO falls": indicatorLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 0 }; break; case "The AO is higher than the Level line": indicatorLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The AO is lower than the Level line": indicatorLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The AO crosses the Level line upward": indicatorLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The AO crosses the Level line downward": indicatorLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The AO changes its direction upward": indicatorLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 0 }; break; case "The AO changes its direction downward": indicatorLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 0 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adAO, dLevel, -dLevel, ref Component[1], ref Component[2], indicatorLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iNVI = (int)IndParam.NumParam[0].Value; int iPrvs = (slotType == SlotTypes.OpenFilter) ? 1 : 0; // Calculation double[] adBasePrice = Price(basePrice); int iFirstBar = iNVI + 2; double[] adVIPos = new double[Bars]; double[] adVINeg = new double[Bars]; double[] adVIOsc = new double[Bars]; double[] adVMPlus = new double[Bars]; double[] adVMMinus = new double[Bars]; double[] adVMPlusSum = new double[Bars]; double[] adVMMinusSum = new double[Bars]; double[] adTrueRange = new double[Bars]; double[] adTrueHigh = new double[Bars]; double[] adTrueLow = new double[Bars]; double[] adTRSum = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { adVMPlus[iBar] = Math.Abs(High[iBar] - Low[iBar - 1]); adVMMinus[iBar] = Math.Abs(Low[iBar] - High[iBar - 1]); if (Close[iBar - 1] > High[iBar]) { adTrueHigh[iBar] = Close[iBar - 1]; } else { adTrueHigh[iBar] = High[iBar]; } if (Close[iBar - 1] < Low[iBar]) { adTrueLow[iBar] = Close[iBar - 1]; } else { adTrueLow[iBar] = Low[iBar]; } adTrueRange[iBar] = adTrueHigh[iBar] - adTrueLow[iBar]; adVMPlusSum[iBar] = 0; adVMMinusSum[iBar] = 0; adTRSum[iBar] = 0; for (int iBack = 0; iBack < iNVI; iBack++) { adVMPlusSum[iBar] += adVMPlus[iBar - iBack]; adVMMinusSum[iBar] += adVMMinus[iBar - iBack]; adTRSum[iBar] += adTrueRange[iBar - iBack]; } if (adTRSum[iBar] != 0) { adVIPos[iBar] = adVMPlusSum[iBar] / adTRSum[iBar]; } adVINeg[iBar] = adVMMinusSum[iBar] / adTRSum[iBar]; } for (int iBar = 0; iBar < Bars; iBar++) { adVIOsc[iBar] = adVIPos[iBar] - adVINeg[iBar]; } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "The VI+"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Green; Component[0].FirstBar = iFirstBar; Component[0].Value = adVIPos; Component[1] = new IndicatorComp(); Component[1].CompName = "The VI-"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].ChartColor = Color.Red; Component[1].FirstBar = iFirstBar; Component[1].Value = adVINeg; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } switch (IndParam.ListParam[0].Text) { case "The VI+ rises": OscillatorLogic(iFirstBar, iPrvs, adVIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_rises); break; case "The VI+ falls": OscillatorLogic(iFirstBar, iPrvs, adVIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_falls); break; case "The VI- rises": OscillatorLogic(iFirstBar, iPrvs, adVINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_rises); break; case "The VI- falls": OscillatorLogic(iFirstBar, iPrvs, adVINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_falls); break; case "The VI+ is higher than VI-": OscillatorLogic(iFirstBar, iPrvs, adVIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_is_higher_than_the_level_line); break; case "The VI+ is lower than VI-": OscillatorLogic(iFirstBar, iPrvs, adVIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_is_lower_than_the_level_line); break; case "The VI+ crosses the VI- line upward": OscillatorLogic(iFirstBar, iPrvs, adVIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_crosses_the_level_line_upward); break; case "The VI+ crosses the VI- line downward": OscillatorLogic(iFirstBar, iPrvs, adVIOsc, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_crosses_the_level_line_downward); break; case "The VI+ changes its direction upward": OscillatorLogic(iFirstBar, iPrvs, adVIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_upward); break; case "The VI+ changes its direction downward": OscillatorLogic(iFirstBar, iPrvs, adVIPos, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_downward); break; case "The VI- changes its direction upward": OscillatorLogic(iFirstBar, iPrvs, adVINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_upward); break; case "The VI- changes its direction downward": OscillatorLogic(iFirstBar, iPrvs, adVINeg, 0, 0, ref Component[2], ref Component[3], IndicatorLogic.The_indicator_changes_its_direction_downward); break; default: break; } return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters BasePrice basePrice = (BasePrice)IndParam.ListParam[1].Index; MAMethod fastMAMethod = (MAMethod )IndParam.ListParam[3].Index; MAMethod slowMAMethod = (MAMethod )IndParam.ListParam[4].Index; int iNFastMA = (int)IndParam.NumParam[0].Value; int iNSlowMA = (int)IndParam.NumParam[1].Value; int iSFastMA = (int)IndParam.NumParam[2].Value; int iSSlowMA = (int)IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; string[] saColors = new string[] { "Blue", "Black", "Red", "Green", "Yellow", "Orange" }; string sFastColor = saColors[(int)IndParam.NumParam[4].Value]; string sSlowColor = saColors[(int)IndParam.NumParam[5].Value]; // Convert to Higher Time Frame --------------------------------------------- DataPeriods htfPeriod = DataPeriods.week; double[] hfOpen = new double[Bars]; double[] hfClose = new double[Bars]; double[] hfHigh = new double[Bars]; double[] hfLow = new double[Bars]; double[] hfVolume = new double[Bars]; double[] hfPrice = new double[Bars]; int[] hIndex = new int[Bars]; int iFrame; int hBars; switch (IndParam.ListParam[2].Index) { case 1: htfPeriod = DataPeriods.min5; break; case 2: htfPeriod = DataPeriods.min15; break; case 3: htfPeriod = DataPeriods.min30; break; case 4: htfPeriod = DataPeriods.hour1; break; case 5: htfPeriod = DataPeriods.hour4; break; case 6: htfPeriod = DataPeriods.day; break; case 7: htfPeriod = DataPeriods.week; break; } int err1 = HigherTimeFrame(Period, htfPeriod, out hIndex, out hBars, out iFrame, out hfHigh, out hfLow, out hfOpen, out hfClose, out hfVolume); int err2 = HigherBasePrice(basePrice, hBars, hfHigh, hfLow, hfOpen, hfClose, out hfPrice); if (err1 == 1) { return; } //----------------------------------------------------------------------- // Calculation int iFirstBar = (int)Math.Max(iNFastMA + iSFastMA, iNSlowMA + iSSlowMA) + 2; double[] adMAFast = MovingAverage(iNFastMA, iSFastMA, fastMAMethod, hfPrice); double[] adMASlow = MovingAverage(iNSlowMA, iSSlowMA, slowMAMethod, hfPrice); double[] adMAOscillator = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { adMAOscillator[iBar] = adMAFast[iBar] - adMASlow[iBar]; } // Convert to Current Time Frame ---------------------------------------------- /// start WTF modfication 2 version 4 // do in 3 blocks for adMAFast, adMASlow, to draw on chart, and adMAOscillator for signals // copy of wider time frame array of values double[] hadMAFast = new double[Bars]; adMAFast.CopyTo(hadMAFast, 0); int err3 = CurrentTimeFrame(hIndex, hBars, ref adMAFast); // if any error, return out of calculation and indicator fails silently if (err3 == 1) { return; } // copy of wider time frame array of values double[] hadMASlow = new double[Bars]; adMASlow.CopyTo(hadMASlow, 0); err3 = CurrentTimeFrame(hIndex, hBars, ref adMASlow); // if any error, return out of calculation and indicator fails silently if (err3 == 1) { return; } // copy of wider time frame array of values double[] hadMAOscillator = new double[Bars]; adMAOscillator.CopyTo(hadMAOscillator, 0); err3 = CurrentTimeFrame(hIndex, hBars, ref adMAOscillator); // if any error, return out of calculation and indicator fails silently if (err3 == 1) { return; } /// end WTF modfication 2 version 4 //----------------------------------------------------------------------------- // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "Fast Moving Average"; Component[0].ChartColor = Color.FromName(sFastColor); Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].FirstBar = iFirstBar; Component[0].Value = adMAFast; Component[1] = new IndicatorComp(); Component[1].CompName = "Slow Moving Average"; Component[1].ChartColor = Color.FromName(sSlowColor); Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].FirstBar = iFirstBar; Component[1].Value = adMASlow; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].CompName = "Close out short position"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The Fast MA crosses the Slow MA upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; break; case "The Fast MA crosses the Slow MA downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; break; case "The Fast MA is higher than the Slow MA": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; break; case "The Fast MA is lower than the Slow MA": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; break; case "Draw only, no entry or exit signals": Component[2].CompName = "Visual Only"; Component[2].DataType = IndComponentType.NotDefined; Component[3].CompName = "Visual Only"; Component[3].DataType = IndComponentType.NotDefined; break; default: break; } /// start WTF modfication 3 version 4 // back up Bars value, reset to hBars, for performance improvement in indicator logic function int mtfBars = Data.Bars; Data.Bars = hBars; // replace very small values with 0 for performance improvement; don't know why but works for (int ctr = 0; ctr < hadMAOscillator.Length; ctr++) { hadMAOscillator[ctr] = (hadMAOscillator[ctr] < .000000001 && hadMAOscillator[ctr] > -.000000001) ? 0 : hadMAOscillator[ctr]; } OscillatorLogic(iFirstBar, iPrvs, hadMAOscillator, 0, 0, ref Component[2], ref Component[3], indLogic); // resest Bars to real value Data.Bars = mtfBars; // expand component array from wtf to current time frame double[] wtfCompValue = Component[2].Value; int err4 = CurrentTimeFrame(hIndex, hBars, ref wtfCompValue); if (err4 == 1) { return; } Component[2].Value = wtfCompValue; wtfCompValue = Component[3].Value; int err5 = CurrentTimeFrame(hIndex, hBars, ref wtfCompValue); if (err5 == 1) { return; } Component[3].Value = wtfCompValue; /// end WTF modfication 3 version 4 return; }
public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int period = (int)IndParam.NumParam[0].Value; int smooth = (int)IndParam.NumParam[1].Value; double level = IndParam.NumParam[2].Value; int previous = IndParam.CheckParam[0].Checked ? 1 : 0; int firstBar = previous + period + smooth + 2; double[] adMomentum = new double[Bars]; double[] adBasePrice = Price(basePrice); for (int bar = period; bar < Bars; bar++) { adMomentum[bar] = 100 * adBasePrice[bar] / adBasePrice[bar - period]; } if (smooth > 0) { adMomentum = MovingAverage(smooth, 0, maMethod, adMomentum); } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Momentum"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = firstBar; Component[0].Value = adMomentum; Component[1] = new IndicatorComp(); Component[1].ChartType = IndChartType.NoChart; Component[1].FirstBar = firstBar; Component[1].Value = new double[Bars]; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = firstBar; Component[2].Value = new double[Bars]; // Sets the Component's type switch (SlotType) { case 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"; break; case 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"; break; default: break; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "Momentum rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[] { 100 }; break; case "Momentum falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[] { 100 }; break; case "Momentum is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[] { level, 2 * 100 - level }; break; case "Momentum is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[] { level, 2 * 100 - level }; break; case "Momentum crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[] { level, 2 * 100 - level }; break; case "Momentum crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[] { level, 2 * 100 - level }; break; case "Momentum changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[] { 100 }; break; case "Momentum changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[] { 100 }; break; default: break; } OscillatorLogic(firstBar, previous, adMomentum, level, 2 * 100 - level, ref Component[1], ref Component[2], indLogic); }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iMAPeriod = (int)IndParam.NumParam[0].Value; int iLRLength = (int)IndParam.NumParam[1].Value; double dLevel = IndParam.NumParam[2].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; double[] dX = new double[Bars]; double[] dY = new double[Bars]; double dSigX; double dSigY; double dSigXY; double dSigXX; double[] adLRSlope = new double[Bars]; int iFirstBar = iMAPeriod + iLRLength + 2; double[] adMAPrice = MovingAverage(iMAPeriod, 0, maMethod, Price(basePrice)); for (int iBar = iFirstBar; iBar < Bars; iBar++) { dSigX = 0; dSigY = 0; dSigXX = 0; dSigXY = 0; for (int index = 0; index < iLRLength; index++) { dSigX = dSigX + index; dSigY = dSigY + adMAPrice[iBar - index]; dSigXY = dSigXY + index * adMAPrice[iBar - index]; dSigXX = dSigXX + index * index; } adLRSlope[iBar] = -(iLRLength * dSigXY - dSigX * dSigY) / (iLRLength * dSigXX - dSigX * dSigX); } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "LR Slope"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = adLRSlope; 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]; // Sets the Component's type 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.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"; } // Calculation of the logic IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "The LR Slope rises": indLogic = IndicatorLogic.The_indicator_rises; SpecialValues = new double[1] { 0 }; break; case "The LR Slope falls": indLogic = IndicatorLogic.The_indicator_falls; SpecialValues = new double[1] { 0 }; break; case "The LR Slope is higher than the Level line": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope is lower than the Level line": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope crosses the Level line upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope crosses the Level line downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[2] { dLevel, -dLevel }; break; case "The LR Slope changes its direction upward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[1] { 0 }; break; case "The LR Slope changes its direction downward": indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[1] { 0 }; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adLRSlope, dLevel, -dLevel, ref Component[1], ref Component[2], indLogic); return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice price = BasePrice.Close; int nMA = (int)IndParam.NumParam[0].Value; int iATRPeriod = (int)IndParam.NumParam[1].Value; int iATRMultiplier = (int)IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation double[] adMA = MovingAverage(nMA, 0, maMethod, Price(price)); double[] adATR = new double[Bars]; double[] adUpBand = new double[Bars]; double[] adDnBand = new double[Bars]; int iFirstBar = (int)Math.Max(nMA, iATRPeriod) + iPrvs + 2; for (int iBar = 1; iBar < Bars; iBar++) { adATR[iBar] = Math.Max(Math.Abs(High[iBar] - Close[iBar - 1]), Math.Abs(Close[iBar - 1] - Low[iBar])); adATR[iBar] = Math.Max(Math.Abs(High[iBar] - Low[iBar]), adATR[iBar]); } adATR = MovingAverage(iATRPeriod, 0, maMethod, adATR); for (int iBar = nMA; iBar < Bars; iBar++) { adUpBand[iBar] = adMA[iBar] + adATR[iBar] * iATRMultiplier; adDnBand[iBar] = adMA[iBar] - adATR[iBar] * iATRMultiplier; } // Saving the components Component = new IndicatorComp[5]; Component[0] = new IndicatorComp(); Component[0].CompName = "Upper Band"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = iFirstBar; Component[0].Value = adUpBand; Component[1] = new IndicatorComp(); Component[1].CompName = "Moving Average"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].ChartColor = Color.Gold; Component[1].FirstBar = iFirstBar; Component[1].Value = adMA; Component[2] = new IndicatorComp(); Component[2].CompName = "Lower Band"; Component[2].DataType = IndComponentType.IndicatorValue; Component[2].ChartType = IndChartType.Line; Component[2].ChartColor = Color.Blue; Component[2].FirstBar = iFirstBar; Component[2].Value = adDnBand; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; Component[4] = new IndicatorComp(); Component[4].ChartType = IndChartType.NoChart; Component[4].FirstBar = iFirstBar; Component[4].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.Open) { Component[3].DataType = IndComponentType.OpenLongPrice; Component[3].CompName = "Long position entry price"; Component[4].DataType = IndComponentType.OpenShortPrice; Component[4].CompName = "Short position entry price"; } else if (slotType == SlotTypes.OpenFilter) { Component[3].DataType = IndComponentType.AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType.AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.Close) { Component[3].DataType = IndComponentType.CloseLongPrice; Component[3].CompName = "Long position closing price"; Component[4].DataType = IndComponentType.CloseShortPrice; Component[4].CompName = "Short position closing price"; } else if (slotType == SlotTypes.CloseFilter) { Component[3].DataType = IndComponentType.ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType.ForceCloseShort; Component[4].CompName = "Close out short position"; } if (slotType == SlotTypes.Open || slotType == SlotTypes.Close) { if (nMA > 1) { for (int iBar = iFirstBar; iBar < Bars; iBar++) { // Covers the cases when the price can pass through the band without a signal. double dOpen = Open[iBar]; // Current open price // Upper band double dValueUp = adUpBand[iBar - iPrvs]; // Current value double dValueUp1 = adUpBand[iBar - iPrvs - 1]; // Previous value double dTempValUp = dValueUp; if ((dValueUp1 > High[iBar - 1] && dValueUp < dOpen) || // The Open price jumps above the indicator (dValueUp1 <Low[iBar - 1] && dValueUp> dOpen) || // The Open price jumps below the indicator (Close[iBar - 1] < dValueUp && dValueUp < dOpen) || // The Open price is in a positive gap (Close[iBar - 1] > dValueUp && dValueUp > dOpen)) // The Open price is in a negative gap { dTempValUp = dOpen; // The entry/exit level is moved to Open price } // Lower band double dValueDown = adDnBand[iBar - iPrvs]; // Current value double dValueDown1 = adDnBand[iBar - iPrvs - 1]; // Previous value double dTempValDown = dValueDown; if ((dValueDown1 > High[iBar - 1] && dValueDown < dOpen) || // The Open price jumps above the indicator (dValueDown1 <Low[iBar - 1] && dValueDown> dOpen) || // The Open price jumps below the indicator (Close[iBar - 1] < dValueDown && dValueDown < dOpen) || // The Open price is in a positive gap (Close[iBar - 1] > dValueDown && dValueDown > dOpen)) // The Open price is in a negative gap { dTempValDown = dOpen; // The entry/exit level is moved to Open price } if (IndParam.ListParam[0].Text == "Enter long at the Upper Band" || IndParam.ListParam[0].Text == "Exit long at the Upper Band") { Component[3].Value[iBar] = dTempValUp; Component[4].Value[iBar] = dTempValDown; } else { Component[3].Value[iBar] = dTempValDown; Component[4].Value[iBar] = dTempValUp; } } } else { for (int iBar = 2; iBar < Bars; iBar++) { if (IndParam.ListParam[0].Text == "Enter long at the Upper Band" || IndParam.ListParam[0].Text == "Exit long at the Upper Band") { Component[3].Value[iBar] = adUpBand[iBar - iPrvs]; Component[4].Value[iBar] = adDnBand[iBar - iPrvs]; } else { Component[3].Value[iBar] = adDnBand[iBar - iPrvs]; Component[4].Value[iBar] = adUpBand[iBar - iPrvs]; } } } } else { switch (IndParam.ListParam[0].Text) { case "The bar opens below the Upper Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_below_the_Upper_Band); break; case "The bar opens above the Upper Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_above_the_Upper_Band); break; case "The bar opens below the Lower Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_below_the_Lower_Band); break; case "The bar opens above the Lower Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_above_the_Lower_Band); break; case "The bar opens below the Upper Band after opening above it": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_below_the_Upper_Band_after_opening_above_it); break; case "The bar opens above the Upper Band after opening below it": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_above_the_Upper_Band_after_opening_below_it); break; case "The bar opens below the Lower Band after opening above it": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_below_the_Lower_Band_after_opening_above_it); break; case "The bar opens above the Lower Band after opening below it": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_opens_above_the_Lower_Band_after_opening_below_it); break; case "The position opens above the Upper Band": Component[0].PosPriceDependence = PositionPriceDependence.PriceBuyHigher; Component[2].PosPriceDependence = PositionPriceDependence.PriceSellLower; Component[0].UsePreviousBar = iPrvs; Component[2].UsePreviousBar = iPrvs; Component[3].DataType = IndComponentType.Other; Component[4].DataType = IndComponentType.Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; break; case "The position opens below the Upper Band": Component[0].PosPriceDependence = PositionPriceDependence.PriceBuyLower; Component[2].PosPriceDependence = PositionPriceDependence.PriceSellHigher; Component[0].UsePreviousBar = iPrvs; Component[2].UsePreviousBar = iPrvs; Component[3].DataType = IndComponentType.Other; Component[4].DataType = IndComponentType.Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; break; case "The position opens above the Lower Band": Component[0].PosPriceDependence = PositionPriceDependence.PriceSellLower; Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyHigher; Component[0].UsePreviousBar = iPrvs; Component[2].UsePreviousBar = iPrvs; Component[3].DataType = IndComponentType.Other; Component[4].DataType = IndComponentType.Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; break; case "The position opens below the Lower Band": Component[0].PosPriceDependence = PositionPriceDependence.PriceSellHigher; Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyLower; Component[0].UsePreviousBar = iPrvs; Component[2].UsePreviousBar = iPrvs; Component[3].DataType = IndComponentType.Other; Component[4].DataType = IndComponentType.Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; break; case "The bar closes below the Upper Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_closes_below_the_Upper_Band); break; case "The bar closes above the Upper Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_closes_above_the_Upper_Band); break; case "The bar closes below the Lower Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_closes_below_the_Lower_Band); break; case "The bar closes above the Lower Band": BandIndicatorLogic(iFirstBar, iPrvs, adUpBand, adDnBand, ref Component[3], ref Component[4], BandIndLogic.The_bar_closes_above_the_Lower_Band); break; default: break; } } return; }
/// <summary> /// Calculates the indicator's components /// </summary> public override void Calculate(SlotTypes slotType) { // Reading the parameters MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index; int period = (int)IndParam.NumParam[0].Value; double level = IndParam.NumParam[1].Value; int prev = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = 2 * period + 2; double[] DIPos = new double[Bars]; double[] DINeg = new double[Bars]; for (int bar = 1; bar < Bars; bar++) { double trueRange = Math.Max(High[bar], Close[bar - 1]) - Math.Min(Low[bar], Close[bar - 1]); if (trueRange < Point) { trueRange = Point; } double deltaHigh = High[bar] - High[bar - 1]; double deltaLow = Low[bar - 1] - Low[bar]; if (deltaHigh > 0 && deltaHigh > deltaLow) { DIPos[bar] = 100 * deltaHigh / trueRange; } else { DIPos[bar] = 0; } if (deltaLow > 0 && deltaLow > deltaHigh) { DINeg[bar] = 100 * deltaLow / trueRange; } else { DINeg[bar] = 0; } } double[] ADIPos = MovingAverage(period, 0, maMethod, DIPos); double[] ADINeg = MovingAverage(period, 0, maMethod, DINeg); double[] DX = new double[Bars]; for (int bar = 0; bar < Bars; bar++) { if (ADIPos[bar] + ADINeg[bar] == 0) { DX[bar] = 0; } else { DX[bar] = 100 * Math.Abs((ADIPos[bar] - ADINeg[bar]) / (ADIPos[bar] + ADINeg[bar])); } } double[] ADX = MovingAverage(period, 0, maMethod, DX); // Saving the components Component = new IndicatorComp[5]; Component[0] = new IndicatorComp(); Component[0].CompName = "ADX"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = firstBar; Component[0].Value = ADX; Component[1] = new IndicatorComp(); Component[1].CompName = "ADI+"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].ChartColor = Color.Green; Component[1].FirstBar = firstBar; Component[1].Value = ADIPos; Component[2] = new IndicatorComp(); Component[2].CompName = "ADI-"; Component[2].DataType = IndComponentType.IndicatorValue; Component[2].ChartType = IndChartType.Line; Component[2].ChartColor = Color.Red; Component[2].FirstBar = firstBar; Component[2].Value = ADINeg; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = firstBar; Component[3].Value = new double[Bars]; Component[4] = new IndicatorComp(); Component[4].ChartType = IndChartType.NoChart; Component[4].FirstBar = firstBar; Component[4].Value = new double[Bars]; // Sets the Component's type if (slotType == SlotTypes.OpenFilter) { Component[3].DataType = IndComponentType.AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType.AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if (slotType == SlotTypes.CloseFilter) { Component[3].DataType = IndComponentType.ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType.ForceCloseShort; Component[4].CompName = "Close out short position"; } // Calculation of the logic IndicatorLogic logicRule; switch (IndParam.ListParam[0].Text) { case "The ADX rises": logicRule = IndicatorLogic.The_indicator_rises; break; case "The ADX falls": logicRule = IndicatorLogic.The_indicator_falls; break; case "The ADX is higher than the Level line": logicRule = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new double[1] { level }; break; case "The ADX is lower than the Level line": logicRule = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new double[1] { level }; break; case "The ADX crosses the Level line upward": logicRule = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new double[1] { level }; break; case "The ADX crosses the Level line downward": logicRule = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new double[1] { level }; break; case "The ADX changes its direction upward": logicRule = IndicatorLogic.The_indicator_changes_its_direction_upward; break; case "The ADX changes its direction downward": logicRule = IndicatorLogic.The_indicator_changes_its_direction_downward; break; default: logicRule = IndicatorLogic.It_does_not_act_as_a_filter; break; } // ADX rises equal signals in both directions! NoDirectionOscillatorLogic(firstBar, prev, ADX, level, ref Component[3], logicRule); Component[4].Value = Component[3].Value; return; }
/// <summary> /// Calculates a Moving Average /// </summary> /// <param name="period">Period</param> /// <param name="shift">Shift</param> /// <param name="maMethod">Method of calculation</param> /// <param name="afSource">The array of source data</param> /// <returns>the Moving Average</returns> protected static double[] MovingAverage(int period, int shift, MAMethod maMethod, double[] adSource) { double[] adTarget = new double[Bars]; if (period <= 1 && shift == 0) { // There is no smoothing return adSource; } if (period > Bars || period + shift <= 0 || period + shift > Bars) { // Error in the parameters return null; } for (int bar = 0; bar < period + shift - 1; bar++) { adTarget[bar] = 0; } double sum = 0; for (int bar = 0; bar < period; bar++) { sum += adSource[bar]; } adTarget[period + shift - 1] = sum / period; // Simple Moving Average if (maMethod == MAMethod.Simple) { for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { adTarget[bar + shift] = adTarget[bar + shift - 1] + adSource[bar] / period - adSource[bar - period] / period; } } // Exponential Moving Average else if (maMethod == MAMethod.Exponential) { double pr = 2d / (period + 1); for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { adTarget[bar + shift] = adSource[bar] * pr + adTarget[bar + shift - 1] * (1 - pr); } } // Weighted Moving Average else if (maMethod == MAMethod.Weighted) { double dWeight = period * (period + 1) / 2d; for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { sum = 0; for (int i = 0; i < period; i++) { sum += adSource[bar - i] * (period - i); } adTarget[bar + shift] = sum / dWeight; } } // Smoothed Moving Average else if (maMethod == MAMethod.Smoothed) { for (int bar = period; bar < Math.Min(Bars, Bars - shift); bar++) { adTarget[bar + shift] = (adTarget[bar + shift - 1] * (period - 1) + adSource[bar]) / period; } } for (int bar = Bars + shift; bar < Bars; bar++) { adTarget[bar] = 0; } return adTarget; }