//============================================================== // Forex Strategy Builder // Copyright (c) Miroslav Popov. All rights reserved. //============================================================== // THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE. //============================================================== using System; using System.Drawing; using ForexStrategyBuilder.Infrastructure.Entities; using ForexStrategyBuilder.Infrastructure.Enums; using ForexStrategyBuilder.Infrastructure.Interfaces; namespace ForexStrategyBuilder.Indicators.Store { public class TSI : Indicator { public TSI() { IndicatorName = "Trend Strength Indicator TSI"; PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter; SeparatedChart = true; IndicatorAuthor = "Jim Totaro"; IndicatorVersion = "1.0"; IndicatorDescription = "The Trend Strength Indicator (TSI)"; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Trade Logic"; IndParam.ListParam[0].ItemList = new string[] { "TSI line rises", "TSI line falls", "TSI line is higher than zero", "TSI line is lower than zero", "TSI line crosses the zero line upward", "TSI line crosses the zero line downward", "TSI line changes its direction upward", "TSI line changes its direction downward", }; IndParam.ListParam[0].Index = 0; IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index]; IndParam.ListParam[0].Enabled = true; IndParam.ListParam[0].ToolTip = "Trading logic of the indicator."; IndParam.ListParam[1].Caption = "First method"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod)); IndParam.ListParam[1].Index = (int)MAMethod.Exponential; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The smoothing method of the first period."; IndParam.ListParam[2].Caption = "Second method"; IndParam.ListParam[2].ItemList = Enum.GetNames(typeof(MAMethod)); IndParam.ListParam[2].Index = (int)MAMethod.Exponential; IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index]; IndParam.ListParam[2].Enabled = true; IndParam.ListParam[2].ToolTip = "The smoothing method of the second period."; IndParam.ListParam[3].Caption = "Base price"; IndParam.ListParam[3].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[3].Index = (int)BasePrice.Close; IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]; IndParam.ListParam[3].Enabled = true; IndParam.ListParam[3].ToolTip = "The price the Moving Averages are based on."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "period of the 1st EMA"; IndParam.NumParam[0].Value = 5; IndParam.NumParam[0].Min = 5; IndParam.NumParam[0].Max = 300; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "period of the 1st EMA"; IndParam.NumParam[1].Caption = "period of the 2nd EMA"; IndParam.NumParam[1].Value = 8; IndParam.NumParam[1].Min = 8; IndParam.NumParam[1].Max = 300; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "period of the 2nd EMA"; // The CheckBox parameters IndParam.CheckParam[0].Caption = "Use previous bar value"; IndParam.CheckParam[0].Enabled = true; IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar."; return; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters MAMethod shortmaMethod = (MAMethod )IndParam.ListParam[1].Index; MAMethod longmaMethod = (MAMethod )IndParam.ListParam[2].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[3].Index; int nSlow = (int)IndParam.NumParam[0].Value; int nFast = (int)IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = nSlow + nFast + 3; double[] adTSI = new double[Bars]; double[] adPrice = Price(basePrice); double[] MTM_Buffer = new double[Bars]; double[] ABSMTM_Buffer = new double[Bars]; // Calculate the TSI for (int iBar = 1; iBar < Bars; iBar++) { MTM_Buffer[iBar] = adPrice[iBar] - adPrice[iBar - 1]; ABSMTM_Buffer[iBar] = Math.Abs(MTM_Buffer[iBar]); } double[] EMA_MTM_Buffer = MovingAverage(nSlow, 0, shortmaMethod, MTM_Buffer) ; double[] EMA_ABSMTM_Buffer = MovingAverage(nSlow, 0, shortmaMethod, ABSMTM_Buffer) ; double[] EMA2_MTM_Buffer = MovingAverage(nFast, 0, longmaMethod, EMA_MTM_Buffer) ; double[] EMA2_ABSMTM_Buffer = MovingAverage(nFast, 0, longmaMethod, EMA_ABSMTM_Buffer) ; for (int iBar = iFirstBar; iBar < Bars; iBar++) { adTSI[iBar] = 100*EMA2_MTM_Buffer[iBar]/EMA2_ABSMTM_Buffer[iBar] ; } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "TSI line"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Blue; Component[0].FirstBar = iFirstBar; Component[0].Value = adTSI; 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"; } switch (IndParam.ListParam[0].Text) { case "TSI line rises": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_rises); break; case "TSI line falls": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_falls); break; case "TSI line is higher than zero": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_is_higher_than_the_level_line); break; case "TSI line is lower than zero": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_is_lower_than_the_level_line); break; case "TSI line crosses the zero line upward": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_crosses_the_level_line_upward); break; case "TSI line crosses the zero line downward": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_crosses_the_level_line_downward); break; case "TSI line changes its direction upward": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_changes_its_direction_upward); break; case "TSI line changes its direction downward": OscillatorLogic(iFirstBar, iPrvs, adTSI, 0, 0, ref Component[1], ref Component[2], IndicatorLogic.The_indicator_changes_its_direction_downward); break; default: break; } return; } /// /// Sets the indicator logic description /// public override void SetDescription() { EntryFilterLongDescription = ToString() + "; the TSI line "; EntryFilterShortDescription = ToString() + "; the TSI line "; ExitFilterLongDescription = ToString() + "; the TSI line "; ExitFilterShortDescription = ToString() + "; the TSI line "; switch (IndParam.ListParam[0].Text) { case "TSI line rises": EntryFilterLongDescription += "rises"; EntryFilterShortDescription += "falls"; ExitFilterLongDescription += "rises"; ExitFilterShortDescription += "falls"; break; case "TSI line falls": EntryFilterLongDescription += "falls"; EntryFilterShortDescription += "rises"; ExitFilterLongDescription += "falls"; ExitFilterShortDescription += "rises"; break; case "TSI line is higher than zero": EntryFilterLongDescription += "is higher than the zero line"; EntryFilterShortDescription += "is lower than the zero line"; ExitFilterLongDescription += "is higher than the zero line"; ExitFilterShortDescription += "is lower than the zero line"; break; case "TSI line is lower than zero": EntryFilterLongDescription += "is lower than the zero line"; EntryFilterShortDescription += "is higher than the zero line"; ExitFilterLongDescription += "is lower than the zero line"; ExitFilterShortDescription += "is higher than the zero line"; break; case "TSI line crosses the zero line upward": EntryFilterLongDescription += "crosses the zero line upward"; EntryFilterShortDescription += "crosses the zero line downward"; ExitFilterLongDescription += "crosses the zero line upward"; ExitFilterShortDescription += "crosses the zero line downward"; break; case "TSI line crosses the zero line downward": EntryFilterLongDescription += "crosses the zero line downward"; EntryFilterShortDescription += "crosses the zero line upward"; ExitFilterLongDescription += "crosses the zero line downward"; ExitFilterShortDescription += "crosses the zero line upward"; break; case "TSI line changes its direction upward": EntryFilterLongDescription += "changes its direction upward"; EntryFilterShortDescription += "changes its direction downward"; ExitFilterLongDescription += "changes its direction upward"; ExitFilterShortDescription += "changes its direction downward"; break; case "TSI line changes its direction downward": EntryFilterLongDescription += "changes its direction downward"; EntryFilterShortDescription += "changes its direction upward"; ExitFilterLongDescription += "changes its direction downward"; ExitFilterShortDescription += "changes its direction upward"; break; default: break; } return; } /// /// Indicator to string /// public override string ToString() { string sString = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (") + IndParam.ListParam[0].Text + ", " + // Method IndParam.ListParam[1].Text + ", " + // 1st smoothing method IndParam.ListParam[2].Text + ", " + // 2nd smoothing method IndParam.NumParam[0].ValueToString + ", " + // 1st MA period IndParam.NumParam[1].ValueToString + ")"; // 2nd MA period return sString; } } }