Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Need help with built-in indicator of Correlation RS
Here is the original code for Correlation RS. My question is below
{ Search Tag: WA-Correlation RS }
{
This indicator plots the correlation of the symbol to which the indicator is applied
and a user-specified 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" }
{ 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() + "." ) ;
if CorrelValue >= PosCorrAlert then
Alert( Symbol + ": Positive correlation alert!" )
else if CorrelValue <= NegCorrAlert then
Alert( Symbol + ": Negative correlation alert!" ) ;
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
Can you help answer these questions from other members on NexusFi?
your best approach will likely be to print the different values, as this will help you to see what values the code uses. As you noticed you can't print objects directly, but each object provides the .ToString() method that you can use.
In your example instead of trying to print the object directly using print(SecondSymPrices.Close[0].ToString()) should give you the result you are looking for.
Thanks for helping me. I am trying to follow your way of thinking. I guess the print should be from ELsystem.object.tostring.
I try to print this
print("SecondSymbol: ", SecondSymbol, ", Date: ", date, ", SecondSymPrices.[0]: ", SecondSymPrices.Close[0].tostring(), ", SecondSymPrices.[1]: ", SecondSymPrices.Close[1].tostring(), ", SecondSymPrices.open[0]: ", SecondSymPrices.open[0].tostring(), ", SecondSymPrices.open[1]: ", SecondSymPrices.open[1].tostring() );
It works and shows that in the print lot.
SecondSymbol: SPY, Date: 1151211.00, SecondSymPrices.[0]: 201.88, SecondSymPrices.[1]: 205.87, SecondSymPrices.open[0]: 203.34999999999999, SecondSymPrices.open[1]: 205.42000000000002
At this point, I guess something is wrong in the number of open because it should not be that long. But, I think it is still OK.
Then, I try to print this.
print(SecondSymPrices.Close);
An error occurs and says "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 guess it is because SecondSymPrices.Close returns different close prices in different time, so it cannot be printed. This makes sense to me. For now, I can understand the difference between SecondSymPrices.Close and SecondSymPrices.Close[0], SecondSymPrices.Close[1]......
Then, I turn to see the correlation of rate of change between symbol1 and symbol2.
I change the original function to this
CorrelValue = Correlation( Close / open - 1, SecondSymPrices.Close / SecondSymPrices.open - 1, CorrelLength );
An error of "operation not supported for these data types" and highlighted 1.
My mind goes blank. Is it because SecondSymPrices.Close and SecondSymPrices.Open are an array. So, I should try something like this
for value1 = 0 to SecondSymPrices.Count - 1 begin
ChangeOnEachBar = SecondSymPrices.close[value1] / SecondSymPrices.open[value1] - 1;
end;
the error message you are receiving points towards your study trying to access the PSP values before it's fully populated. Updating the method that computes the correlation to something like the below should do it.