Trend Strength Indicator (TSI) by jetaro
62788 downloads / 9501 views / Created: 27.01.2017 Average Rating: 0
Indicator Description
Here's the FSB version I created of the TSI indicator. Any issues please post it here.
//==============================================================
// 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;
}
}
}
//+--------------------------------------------------------------------+ //| 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 <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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); } } //+------------------------------------------------------------------+
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2024, Forex Software Ltd.;
Copyright © 2006 - 2024, Forex Software Ltd.;