Can anyone convert this code for me Relative strength vs index , TS
{ Search Tag: WA-Relative Strength }
{
This indicator plots the difference in percentage change between the symbol to which
the indicator is applied and a user-specified reference symbol. The percentage
change is calculated over a user-specified number of bars.
This indicator was developed to allow for comparing relative strength in
RadarScreen, where multiple data streams are not available, and to provide a simpler
solution for Charting, so that multiple data streams are not required.
}
using elsystem;
using tsdata.common;
using tsdata.marketdata;
inputs:
string ReferenceSymbol( "QQQ" ) [
DisplayName = "ReferenceSymbol",
ToolTip = "Enter the reference symbol used in the relative strength calculations."],
int Length( 10 ) [
DisplayName = "Length",
ToolTip = "Enter the number of bars over which to calculate the relative strength."],
int NumDecimalsOfPrecision( 3 ) [
DisplayName = "NumDecimalsOfPrecision",
ToolTip = "Number of Decimals of Precision. Enter the number of decimals of precision used to determine if the values are equal."],
int BelowZeroColor( Red ) [
DisplayName = "BelowZeroColor",
ToolTip = "Enter color for the difference plot when the plot value is less than zero."],
int AboveZeroColor( MyColors( "DodgerBlue" ) ) [
DisplayName = "AboveZeroColor",
ToolTip = "Enter color for the difference plot when the plot value is greater than zero."],
int LoadedStateColor( DarkGreen ) [
DisplayName = "LoadedStateColor",
ToolTip = "Enter the color for the plot of the data provider state if the state is loaded."],
int NotLoadedStateColor( Red ) [
DisplayName = "NotLoadedStateColor",
ToolTip = "Enter the color used for the plot of the data provider state if the state is NOT loaded."];
variables:
PriceSeriesProvider RefSymPrices( NULL ),
double SymROC( 0 ),
double RefROC( 0 ),
double Diff( 0 ),
intrabarpersist int DataStatePlotColor( NotLoadedStateColor ),
intrabarpersist string PSPDataState( "" );
method void CreateAndLoadPSP()
begin
RefSymPrices = new PriceSeriesProvider();
RefSymPrices.Symbol = ReferenceSymbol;
AlignPSP( RefSymPrices );
RefSymPrices.IncludeVolumeInfo = false;
RefSymPrices.IncludeTicksInfo = false;
RefSymPrices.Realtime = true;
RefSymPrices.StateChanged += RefSymPrices_StateChanged;
RefSymPrices.Updated += RefSymPriceUpdate;
RefSymPrices.LoadProvider();
end;
method void RefSymPrices_StateChanged( Object sender, StateChangedEventArgs args )
begin
PSPDataState = RefSymPrices.State.ToString();
{ once the provider loads, we'll set the plot color for the data state to
the value set in the 'LoadedStateColor' input }
if args.NewState = DataState.Loaded then
DataStatePlotColor = LoadedStateColor;
end;
{ event handler for update event of the PriceSeriesProvider RefSymPrices }
method void RefSymPriceUpdate( Object sender, PriceSeriesUpdatedEventArgs
args )
begin
UpdateRelStrengthCalc();
PlotOutputs();
end;
{ calculate rate-of-change percentages }
method void UpdateRelStrengthCalc()
begin
once ( LastBarOnChartEx )
begin
if RefSymPrices.Close.Count < Length then
throw Exception.Create( !( "Insufficient data available to calculate RateOfChange." ) );
end;
{ delay calculation and plotting until enough data is available }
if RefSymPrices.Close.Count > Length then
begin
SymROC = RateOfChange( Close, Length );
RefROC = RateOfChange( RefSymPrices.Close, Length );
Diff = Round( SymROC, NumDecimalsOfPrecision ) -
Round( RefROC, NumDecimalsOfPrecision );
end;
end;
method void PlotOutputs()
begin
Plot1( UpperStr( ReferenceSymbol ), !( "RefSym" ) );
Plot2( Diff, !( "Diff" ) );
Plot3( PSPDataState, !( "DataState" ), DataStatePlotColor );
Plot4( 0, !( "ZeroLine" ) );
if Diff < 0 then
begin
SetPlotColor( 1, BelowZeroColor );
SetPlotColor( 2, BelowZeroColor );
end
else if Diff > 0 then
begin
SetPlotColor( 1, AboveZeroColor );
SetPlotColor( 2, AboveZeroColor );
end;
end;
{ this method is used to align a PriceSeriesProvider to the data stream settings to
which the indicator is applied }
method void AlignPSP( PriceSeriesProvider PSPToAlign )
begin
{ currently, this indicator does not work when using Natural Hours for bar
building }
if AnalysisTechnique.DataStreams.DefaultStream.UsesNaturalHours then
throw Exception.Create( Name + " " + !( "cannot be used with natural hours selected." ) );
{ align the PSP bar interval and range }
PSPToAlign.Interval = DataInterval.FromCurrentSymbolData( BarType, BarInterval );
PSPToAlign.Range.FirstDate = BarDateTime[MaxBarsBack];
{ align the PSP time zone, session, and natural hours; see commentary for the
ConvertTimeZone method }
PSPToAlign.TimeZone = ConvertTimeZone();
PSPToAlign.SessionName =
AnalysisTechnique.DataStreams.DefaultStream.SessionName;
//PSPToAlign.UseNaturalHours =
// AnalysisTechnique.DataStreams.DefaultStream.UsesNaturalHours;
end;
{
the data stream TimeZone and the TimeZone used in DataProviders are from
different classes; this method is used to provide the cross-reference between the
two so that the DataProvider TimeZone can be set to the same TimeZone as the
data stream to which this indicator is applied
}
method tsdata.common.TimeZone ConvertTimeZone()
variables: elsystem.TimeZone ChartTimeZone;
begin
ChartTimeZone = AnalysisTechnique.DataStreams.DefaultStream.TimeZone;
switch ( ChartTimeZone )
begin
case elsystem.TimeZone.Exchange:
return tsdata.common.TimeZone.Exchange;
case elsystem.TimeZone.Local:
return tsdata.common.TimeZone.Local;
end;
end;
RS(vsIndex)
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
-
- Posts: 2
- Joined: 07 Feb 2022
RS(vsIndex) help with code
Relative Strength indicator for TS , easy language
WOULD REALLY LIKE THIS INDICATOR FOR MULTICHARTS!
Nearly all brokers have some form of this indicator ...it compares the strength of selected stock against index , or whatever is input
Thanks in advance!
This indicator plots the difference in percentage change between the symbol to which
the indicator is applied and a user-specified reference symbol. The percentage
change is calculated over a user-specified number of bars.
This indicator was developed to allow for comparing relative strength in
RadarScreen, where multiple data streams are not available, and to provide a simpler
solution for Charting, so that multiple data streams are not required.
WOULD REALLY LIKE THIS INDICATOR FOR MULTICHARTS!
Nearly all brokers have some form of this indicator ...it compares the strength of selected stock against index , or whatever is input
Thanks in advance!
This indicator plots the difference in percentage change between the symbol to which
the indicator is applied and a user-specified reference symbol. The percentage
change is calculated over a user-specified number of bars.
This indicator was developed to allow for comparing relative strength in
RadarScreen, where multiple data streams are not available, and to provide a simpler
solution for Charting, so that multiple data streams are not required.
Code: Select all
using elsystem;
using tsdata.common;
using tsdata.marketdata;
inputs:
string ReferenceSymbol( "QQQ" ) [
DisplayName = "ReferenceSymbol",
ToolTip = "Enter the reference symbol used in the relative strength calculations."],
int Length( 10 ) [
DisplayName = "Length",
ToolTip = "Enter the number of bars over which to calculate the relative strength."],
int NumDecimalsOfPrecision( 3 ) [
DisplayName = "NumDecimalsOfPrecision",
ToolTip = "Number of Decimals of Precision. Enter the number of decimals of precision used to determine if the values are equal."],
int BelowZeroColor( Red ) [
DisplayName = "BelowZeroColor",
ToolTip = "Enter color for the difference plot when the plot value is less than zero."],
int AboveZeroColor( MyColors( "DodgerBlue" ) ) [
DisplayName = "AboveZeroColor",
ToolTip = "Enter color for the difference plot when the plot value is greater than zero."],
int LoadedStateColor( DarkGreen ) [
DisplayName = "LoadedStateColor",
ToolTip = "Enter the color for the plot of the data provider state if the state is loaded."],
int NotLoadedStateColor( Red ) [
DisplayName = "NotLoadedStateColor",
ToolTip = "Enter the color used for the plot of the data provider state if the state is NOT loaded."];
variables:
PriceSeriesProvider RefSymPrices( NULL ),
double SymROC( 0 ),
double RefROC( 0 ),
double Diff( 0 ),
intrabarpersist int DataStatePlotColor( NotLoadedStateColor ),
intrabarpersist string PSPDataState( "" );
method void CreateAndLoadPSP()
begin
RefSymPrices = new PriceSeriesProvider();
RefSymPrices.Symbol = ReferenceSymbol;
AlignPSP( RefSymPrices );
RefSymPrices.IncludeVolumeInfo = false;
RefSymPrices.IncludeTicksInfo = false;
RefSymPrices.Realtime = true;
RefSymPrices.StateChanged += RefSymPrices_StateChanged;
RefSymPrices.Updated += RefSymPriceUpdate;
RefSymPrices.LoadProvider();
end;
method void RefSymPrices_StateChanged( Object sender, StateChangedEventArgs args )
begin
PSPDataState = RefSymPrices.State.ToString();
{ once the provider loads, we'll set the plot color for the data state to
the value set in the 'LoadedStateColor' input }
if args.NewState = DataState.Loaded then
DataStatePlotColor = LoadedStateColor;
end;
handler for update event of the PriceSeriesProvider RefSymPrices }
method void RefSymPriceUpdate( Object sender, PriceSeriesUpdatedEventArgs
args )
begin
UpdateRelStrengthCalc();
PlotOutputs();
end;
{ calculate rate-of-change percentages }
method void UpdateRelStrengthCalc()
begin
once ( LastBarOnChartEx )
begin
if RefSymPrices.Close.Count < Length then
throw Exception.Create( !( "Insufficient data available to calculate RateOfChange." ) );
end;
{ delay calculation and plotting until enough data is available }
if RefSymPrices.Close.Count > Length then
begin
SymROC = RateOfChange( Close, Length );
RefROC = RateOfChange( RefSymPrices.Close, Length );
Diff = Round( SymROC, NumDecimalsOfPrecision ) -
Round( RefROC, NumDecimalsOfPrecision );
end;
end;
method void PlotOutputs()
begin
Plot1( UpperStr( ReferenceSymbol ), !( "RefSym" ) );
Plot2( Diff, !( "Diff" ) );
Plot3( PSPDataState, !( "DataState" ), DataStatePlotColor );
Plot4( 0, !( "ZeroLine" ) );
if Diff < 0 then
begin
SetPlotColor( 1, BelowZeroColor );
SetPlotColor( 2, BelowZeroColor );
end
else if Diff > 0 then
begin
SetPlotColor( 1, AboveZeroColor );
SetPlotColor( 2, AboveZeroColor );
end;
end;
{ this method is used to align a PriceSeriesProvider to the data stream settings to
which the indicator is applied }
method void AlignPSP( PriceSeriesProvider PSPToAlign )
begin
{ currently, this indicator does not work when using Natural Hours for bar
building }
if AnalysisTechnique.DataStreams.DefaultStream.UsesNaturalHours then
throw Exception.Create( Name + " " + !( "cannot be used with natural hours selected." ) );
{ align the PSP bar interval and range }
PSPToAlign.Interval = DataInterval.FromCurrentSymbolData( BarType, BarInterval );
PSPToAlign.Range.FirstDate = BarDateTime[MaxBarsBack];
{ align the PSP time zone, session, and natural hours; see commentary for the
ConvertTimeZone method }
PSPToAlign.TimeZone = ConvertTimeZone();
PSPToAlign.SessionName =
AnalysisTechnique.DataStreams.DefaultStream.SessionName;
//PSPToAlign.UseNaturalHours =
// AnalysisTechnique.DataStreams.DefaultStream.UsesNaturalHours;
end;
{
the data stream TimeZone and the TimeZone used in DataProviders are from
different classes; this method is used to provide the cross-reference between the
two so that the DataProvider TimeZone can be set to the same TimeZone as the
data stream to which this indicator is applied
}
method tsdata.common.TimeZone ConvertTimeZone()
variables: elsystem.TimeZone ChartTimeZone;
begin
ChartTimeZone = AnalysisTechnique.DataStreams.DefaultStream.TimeZone;
switch ( ChartTimeZone )
begin
case elsystem.TimeZone.Exchange:
return tsdata.common.TimeZone.Exchange;
case elsystem.TimeZone.Local:
return tsdata.common.TimeZone.Local;
end;
end;
once
begin
{ this indicator does not work with tick/volume bars or advanced bars }
if BarType = 0 or ( BarType > 4 and BarType <> 14 ) then
throw Exception.Create( Name + " " + !( "cannot be used with tick bars, volume bars, or advanced bars." ) );
if NumDecimalsOfPrecision < 0 or NumDecimalsOfPrecision > 6 then
throw Exception.Create( !( "NumDecimalsOfPrecision input must be a number between 0 and 6." ) );
CreateAndLoadPSP();
end;
UpdateRelStrengthCalc();
PlotOutputs();
{ ** Copyright © TS Technologies, Inc. All Rights Reserved **
** TS reserves the right to modify or overwrite this analysis technique
with each release. ** }