NexusFi: Find Your Edge


Home Menu

 





Need help with built-in indicator of Correlation RS


Discussion in TradeStation

Updated
    1. trending_up 2,475 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

Need help with built-in indicator of Correlation RS

  #1 (permalink)
tony1124
new york
 
Posts: 2 since Dec 2015
Thanks Given: 0
Thanks Received: 0

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" }

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." ) ;

// set-up 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

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
Exit Strategy
NinjaTrader
PowerLanguage & EasyLanguage. How to get the platfor …
EasyLanguage Programming
REcommedations for programming help
Sierra Chart
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Spoo-nalysis ES e-mini futures S&P 500
28 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

tony1124,

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.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
  #3 (permalink)
tony1124
new york
 
Posts: 2 since Dec 2015
Thanks Given: 0
Thanks Received: 0



ABCTG View Post
tony1124,

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.

Regards,

ABCTG

Hi ABCTG,

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;

Thanks.

Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

tony1124;

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.

 
Code
// calculate correlation
method void UpdateCorrelCalc()
begin

if SecondSymPrices.Close.Count >= PSPNeededCount and PSPNeededCount  > 0 then
begin 
CorrelValue = Correlation( Close, SecondSymPrices.Close, CorrelLength ) ;
PlotOutputs() ;
end ;
end ;
Regards,

ABCTG

Follow me on Twitter Reply With Quote




Last Updated on December 14, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts