Need help with builtin indicator of Correlation RS 
December 10th, 2015, 09:51 PM  #1 (permalink) 
Trading Apprentice
new york
Futures Experience: Beginner
Platform: TradeStation
Favorite Futures: emini
Posts: 2 since Dec 2015
Thanks: 0 given,
0
received

Need help with builtin indicator of Correlation RS
Here is the original code for Correlation RS. My question is below
{ Search Tag: WACorrelation RS } { This indicator plots the correlation of the symbol to which the indicator is applied and a userspecified second symbol. The correlation is calculated over a user specified number of bars. This study can be used only with daily, weekly, or monthly bars. } using elsystem ; using tsdata.common ; using tsdata.marketdata ; inputs: string SecondSymbol( "SPY" ), { the correlation between this symbol and the symbol to which the indicator is applied will be calculated } int CorrelLength( 14 ), { number of bars over which to calculate the correlation } double PosCorrAlert( 0.7 ), { correlation values equal to or above this value will trigger an alert, if alerts are enabled } double NegCorrAlert( 0.7 ), { correlation values equal to or below this value will trigger an alert, if alerts are enabled } int LoadedStateColor( DarkGreen ), { color to use in grid applications for plotting of the State of the PriceSeriesProvider when the State is "loaded" } int NotLoadedStateColor( DarkRed ) ; { color to use in grid applications for plotting of the State of the PriceSeriesProvider when the State is something other than "loaded" } variables: intrabarpersist int PSPNeededCount( 0 ), intrabarpersist double CorrelValue( 0 ) ; // event handler for indicator's Initialized event method void Initialize( Object InitSender, InitializedEventArgs InitArgs ) begin if Bartype < 2 or BarType > 4 then throw Exception.Create( "Relative Strength can be used only with daily, " + "weekly, or monthly bars." ) ; // setup the "aligned" PriceSeriesProvider SecondSymPrices.Interval = DataInterval.FromCurrentSymbolData( BarType, BarInterval ) ; SecondSymPrices.Range.FirstDate = DateTime.FromELDateAndTime( Date[CorrelLength], Time[CorrelLength] ) ; SecondSymPrices.Load = true ; { calculate the value of the Count property of the PSP that will be required for the correlation calculation; this value will be used when the correlation is calculated, below, to ensure that the PSP contains enough data to perform the correlation calculation } PSPNeededCount = CorrelLength + 1 ; end ; // event handler for update event of the PriceSeriesProvider SecondSymPrices method void SecondSymPriceUpdate( Object SecondSymPriceSender, PriceSeriesUpdatedEventArgs SecondSymPriceUpdateArgs ) begin UpdateCorrelCalc() ; end ; // calculate correlation method void UpdateCorrelCalc() begin if SecondSymPrices.Close.Count >= PSPNeededCount then CorrelValue = Correlation( Close, SecondSymPrices.Close, CorrelLength ) else throw Exception.Create( "Insufficient data available to calculate" + " correlation. Required bars = " + NumToStr( PSPNeededCount, 0 ) + ". Available bars = " + SecondSymPrices.Close.Count.ToString() + "." ) ; PlotOutputs() ; end ; method void PlotOutputs() begin Plot1( UpperStr( SecondSymbol ), "2ndSym" ) ; Plot2( SecondSymPrices.State.ToString(), "DataState", iff( SecondSymPrices.State = DataState.Loaded, LoadedStateColor, NotLoadedStateColor ) ) ; Plot3( CorrelValue, "Correlation" ) ; if CorrelValue >= PosCorrAlert then Alert( Symbol + ": Positive correlation alert!" ) else if CorrelValue <= NegCorrAlert then Alert( Symbol + ": Negative correlation alert!" ) ; end ; UpdateCorrelCalc() ; { ** Copyright © TradeStation Technologies, Inc. All Rights Reserved ** ** TradeStation reserves the right to modify or overwrite this analysis technique with each release. ** } Question I am new to use PriceSeriesProvider. I am trying to see the correlation between the rate of change of two stocks. So, I try to change the calculation CorrelValue = Correlation( rateofchange(Close, 1), rateofchange(SecondSymPrices.Close, 1), CorrelLength ) But RadarScreen shows an error of "invalid index used to access element of a collection" I tried to search something related to PriceSeriesProvider. SecondSymPrices.Close[0] is the closing price of current bar and SecondSymPrices.Close[1] is the closing price of 1 bar before. Then, I try CorrelValue = Correlation( Close / close[1]  1, SecondSymPrices.Close / SecondSymPrices.Close[1]  1, CorrelLength ) Unfortunately, the number I get is wrong. Then, I try to print(SecondSymPrices.Close). An error of "attempting to print an object reference. please use the '.' operator and select the object member you wish to use(ex. object.doublevalue, object.tostring(), etc" I have no idea about that. So, I try CorrelValue = Correlation( Close / open  1, SecondSymPrices.Close /SecondSymPrices.open  1 , CorrelLength ) It shows the error of "operation not supported for these data types" and highlighted "1". Then, I change it to CorrelValue = Correlation( Close / open, SecondSymPrices.Close /SecondSymPrices.open , CorrelLength ) Error again and force me to close my EL. It seems due to infinite loop. I am not sure why. Thank you for your help 