//+--------------------------------------------------------------------+ //| Copyright: (C) 2017, Miroslav Popov - All rights reserved! | //| Author: Jim Totaro | //| Website: http://forexsb.com/ | //| Support: http://forexsb.com/forum/ | //| License: Proprietary under the following circumstances: | //| | //| This code is a part of Forex Strategy Builder. It is free for | //| use as an integral part of Forex Strategy Builder. | //| One can modify it in order to improve the code or to fit it for | //| personal use. This code or any part of it cannot be used in | //| another applications without a permission. Contact information | //| cannot be changed. | //| | //| NO LIABILITY FOR CONSEQUENTIAL DAMAGES | //| | //| In no event shall the author be liable for any damages whatsoever | //| (including, without limitation, incidental, direct, indirect and | //| consequential damages, damages for loss of business profits, | //| business interruption, loss of business information, or other | //| pecuniary loss) arising out of the use or inability to use this | //| product, even if advised of the possibility of such damages. | //+--------------------------------------------------------------------+ #property copyright "Copyright 2014, Miroslav Popov" #property link "http://forexsb.com" #property version "1.00" #property strict #include #include //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class TrendStrengthIndicatorTSI : public Indicator { public: TrendStrengthIndicatorTSI(SlotTypes slotType) { SlotType=slotType; IndicatorName="Trend Strength Indicator TSI"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = true; IsDiscreteValues = false; IsDeafultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void TrendStrengthIndicatorTSI::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); // Reading the parameters MAMethod shortmaMethod=(MAMethod)ListParam[1].Index; MAMethod longmaMethod =(MAMethod)ListParam[2].Index; BasePrice basePrice=(BasePrice)ListParam[3].Index; int nSlow = (int)NumParam[0].Value; int nFast = (int)NumParam[1].Value; int iPrvs=CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar=nSlow+nFast+3; double basePrc[]; Price(basePrice,basePrc); double adMASlow[]; MovingAverage(nSlow,0,shortmaMethod,basePrc,adMASlow); double adMAFast[]; MovingAverage(nFast,0,longmaMethod,basePrc,adMAFast); double adTSI[]; ArrayResize(adTSI,Data.Bars); ArrayInitialize(adTSI, 0); double MTM_Buffer[]; ArrayResize(MTM_Buffer,Data.Bars); ArrayInitialize(MTM_Buffer, 0); double ABSMTM_Buffer[]; ArrayResize(ABSMTM_Buffer,Data.Bars); ArrayInitialize(ABSMTM_Buffer, 0); double EMA_MTM_Buffer[]; ArrayResize(EMA_MTM_Buffer,Data.Bars); ArrayInitialize(EMA_MTM_Buffer, 0); double EMA_ABSMTM_Buffer[]; ArrayResize(EMA_ABSMTM_Buffer,Data.Bars); ArrayInitialize(EMA_ABSMTM_Buffer, 0); double EMA2_MTM_Buffer[]; ArrayResize(EMA2_MTM_Buffer,Data.Bars); ArrayInitialize(EMA2_MTM_Buffer, 0); double EMA2_ABSMTM_Buffer[]; ArrayResize(EMA2_ABSMTM_Buffer,Data.Bars); ArrayInitialize(EMA2_ABSMTM_Buffer, 0); //--- calculate the moving average on arrays for (int iBar = 1; iBar < Data.Bars; iBar++) { MTM_Buffer[iBar] = basePrc[iBar]-basePrc[iBar-1]; ABSMTM_Buffer[iBar] = fabs(MTM_Buffer[iBar]); } MovingAverage(nSlow,0,shortmaMethod,MTM_Buffer,EMA_MTM_Buffer); MovingAverage(nSlow,0,shortmaMethod,ABSMTM_Buffer,EMA_ABSMTM_Buffer); MovingAverage(nFast,0,longmaMethod,EMA_MTM_Buffer,EMA2_MTM_Buffer); MovingAverage(nFast,0,longmaMethod,EMA_ABSMTM_Buffer,EMA2_ABSMTM_Buffer); for (int iBar = iFirstBar; iBar < Data.Bars; iBar++) { adTSI[iBar] = 100*EMA2_MTM_Buffer[iBar]/EMA2_ABSMTM_Buffer[iBar] ; } // Saving the components ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "TSI Line"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = iFirstBar; ArrayCopy(Component[0].Value,adTSI); ArrayResize(Component[1].Value,Data.Bars); Component[1].FirstBar=iFirstBar; ArrayResize(Component[2].Value,Data.Bars); Component[2].FirstBar=iFirstBar; // 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"; } if(ListParam[0].Text=="TSI line rises") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_rises); } else if(ListParam[0].Text=="TSI line falls") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_falls); } else if(ListParam[0].Text=="TSI line is higher than zero") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_is_higher_than_the_level_line); } else if(ListParam[0].Text=="TSI line is lower than zero") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_is_lower_than_the_level_line); } else if(ListParam[0].Text=="TSI line crosses the zero line upward") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_crosses_the_level_line_upward); } else if(ListParam[0].Text=="TSI line crosses the zero line downward") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_crosses_the_level_line_downward); } else if(ListParam[0].Text=="TSI line changes its direction upward") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_changes_its_direction_upward); } else if(ListParam[0].Text=="TSI line changes its direction downward") { OscillatorLogic(iFirstBar,iPrvs,adTSI,0,0,Component[1],Component[2],IndicatorLogic_The_indicator_changes_its_direction_downward); } } //+------------------------------------------------------------------+