Hi,
I wonder how i could code the correlation between an indicator and price? I hope you can help me, and if so please add a concrete example.
For example, RSI correlation with price, how should i code this?
I want to use this with my own personal indicators.
Thanks in advance.
WingParticipant
Veteran
There is no built in correlation function I think. Here is how I calculate the relevance of an indicator:
To measure for example RSI impact on future price movements, you need to divide these movements into two categories, negative and positives. For each bar, look at the price ten bars ago (close[10]), and the RSI value ten bars ago. Calculate how the price has changed. This is either a positive or a negative number. If positive add this change times the RSI value to a sum variable. This will weight the collected RSI value based on how big of a price impact it precedes. It can also be weighted for time/trade number to reflect changing market conditions, if you want to.
You will also have another variable called PositiveNumber, which is the total number of posititive instances. The same for negative instances. So for each you have a sum, which is the sum of all respective positive or negative price changes times the RSI indicator values, divided by the total number of instances. This way, you will see the average of the RSI before a positive change and a negative. You can use these values in your trading system, or to see the indicator’s importance. Take the difference between the negative and positive average divided values, which could be around 15 units let’s say. Divide this difference, with the difference between the high[800] and low[800] for RSI, and you will have a value for the indicators prediction strength. To fully utilize it, you need to compare RSI prediction strength with strength of other indicators.
I have a system based on this, but I don’t want to share the code.
In here maybe there is a working example: http://www.prorealcode.com/topic/correlation-coefficient/
Thank you very much Wing for great answer! So this code will show if indicator Y correlates with Price X? So i could just use different indicators in “Y” if they act like a line that could be compared with price movements?
I think this is a very strong tool that i would like to learn more about. I have really been missing a code to test my indicators relevance. Correlation vs relevance ought to be much the same?
Is this code accurate and the same as you described in your post? if not, could you show how to code what you described?
And so if i have a indicator that i want to compare to price i type that indicator in “Y”?
Thanks in advance, keep up the good work.
period = 12
if barindex>period then
X = (close/close[1])
Y = (volume/volume[1])
sqX = square(X)
sqY = square(Y)
varianceX = average[period](sqX)-(average[period](X)*average[period](X))
varianceY = average[period](sqY)-(average[period](Y)*average[period](Y))
covariance = (summation[period]((X-average[period](X))*(Y-average[period](Y))))/period
correlation = covariance/SQRT(varianceX*varianceY)
endif
RETURN correlation as "Correlation", 0 as "0"
Regarding the code i posted above, how do i incorporate it and understand it? If “0” then indicator and price is balanced and correlated? and otherwise it’s not? What about horizontal movements in the indicator, seems like it’s in balance then, or will this (if working) want to reach 0 again?
How does a high “Period” affect the result in the indicator, lagging or something? Or just reflecting a bigger period of correlation?
I wish for so much information as possible so i can learn how to code this.
Thanks in advance!
WingParticipant
Veteran
What I described is not really a correlation with price, it is a correlation between the indicator and price CHANGE. Yes, I think that is the relevance.
I have not used the code example you are talking about, I just found it by searching. If you want to do what I do you must code what I describe in the first post. My code is based on what I described, but I have added additional weighting factors on it, so the whole system is many hundreds lines of code. It would be a big hassle to show the simple version. Note that most of the code for a system like this will be in an indicator, because it can check price history before you start trading. The indicator is then used by a trading system. It returns two variables to the system for each indicator being compared to price change: The indicator value when prices will go down, and the indicator value when price will go up. The system can then check if the current value is below/above these values and place trades.
It is the best system I have created so far, and I am only 50 % done with it, so you are right that it is a very interesting approach. This is because the program looks at price history and learns the behaviour of price compared to indicators.
If you are new to programming or PRT this can be hard, but until you are able to program it yourself I’m afraid you will not be able to utilize it in a good way. If you want to learn more about these methods, look up “Machine learning” and “Regression analysis”. PRT is not powerful enough to do machine learning like ‘real’ programming languages but methods like the one I describe can be done.
I see!
I wish it will work out for you and you’ll manage to create something great.
Been coding abit, but mostly know the fundamentals, but i’m making progress, often easier to understand already created codes than to create them by myself tho. But i understand, keep up the great work.
Thanks for the tips, will look into it!
Is it possible to code correlation between different instruments? If so, do you know how?
WingParticipant
Veteran
Correlation between different instruments is available in a few built-in indicators in PRT, but none of these are accessible when coding. Why they have limited this function I don’t know, but right now you can’t make any system based on several instruments.
Okey i see, that’s to bad.
So i have made som coding and would love some input if you can spare the time! I have not yet implented everything that you described, etc what you described as weight, got some problems with that, and the last part.
My ability has taken me here.
I tried some with *RSI and so on, and to use a count to divide the number of instances but as i’m not truly sure what i should do i took away that.
Thanks in advance
c1 = average[100](RSI)
c2 = average[100](close)
if c1-c1[10] > 0 then
posy=posy+1
elsif c1-c1[10] < 0 then
negy=negy-1
endif
posnegy = posy/negy
posnegyMA =average[50](posnegy)
if c2-c2[10] > 0 then
posx=posx+1
elsif c2-c2[10] < 0 then
negx=negx-1
endif
posnegx = posx/negx
posnegxma = average[50](posnegx)
correlation = posnegy-posnegx
return correlation as "corr diff", posnegx as "price", posnegy as "indicator", posnegyMA as "IndicatorMa", Posnegxma as "PriceMa"
WingParticipant
Veteran
I did not specify exactly how I did, so at last I have a small example for you to try on for example the DAX 30 min timeframe. Note that this one does not check every ten bars, it checks between two times. In this example ADX is used, not RSI. I also did not mention, that not only is the indicator muliplied, the price change is squared. Some time weighting is included here, which you can try. Comparing the two returned values to the maximum/minmum over past periods to see relevance is not included here. In addition to this, you probably need to do this with several indicators for a good system. Enjoy.
rs=ADX[14]
whe=50 // minimum price changed accounted for
endtime=170000
once wei1=1
once wei1s=1
once wei2=1
once wei2s=1
once weic=0 // weighter. 0 means no time weighting. Recommended: 0.001-0.02
once downnum=1
once downsum=1
once upnum=1
once upsum=1
once go=0
if time>110000 and barindex>10 and go=0 then
place=rs
price=open
go=1
endif
if time>endtime and go=1 then
if close-price>whe then
wei1=wei1*(1+weic)
wei1s=wei1s+wei1
upsum=upsum+(place*(wei1*square((close-price))))
upnum=upnum+1*(wei1*square((close-price)))
endif
go=0
if close-price<-whe then
wei2=wei2*(1+weic)
wei2s=wei2s+wei2
downsum=downsum+place*(wei2*square((-(close-price))))
downnum=downnum+1*(wei2*square((-(close-price))))
endif
endif
return downsum/downnum, upsum/upnum
Thanks alot Wing! Will look into this and try to learn from the code.