Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Correlation Indicator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Correlation Indicator

    Hi,
    Here's a correlation indicator with a configurable window setting.



    The indicator is referencing the open source mathlib found on this site:


    Not sure it's a good idea to distribute everything as one pack from a licensing perspective so the install is a bit ugly as it requires some manual copying of files.

    Instructions:
    1. Download the C sharp pack from http://www.alglib.net/ (free for personal use as I understand it)
    2. Unzip and dig out the following files and copy them into NinjaTrader7\bin\Custom\Indicator\
    (you could also build a dll that you can reference, I did this but there were some problems with compiling it in NT7 so this is a bit of a dirty workaround...):
    ap.cs
    correlationtests.cs
    conv.cs
    corr.cs
    correlation.cs
    fft.cs
    ftbase.cs
    gammafunc.cs
    ibetaf.cs
    normaldistr.cs
    studenttdistr.cs
    3. Copy the attached indicator file into the same dir.
    4. Open KJCorrelation through: Tools=>Edit NinjaScript =>
    5. Compile
    6. Add the indicator on a chart with a contract, configure by setting SymbolA to whatever contract you want compare the key contract with and the correlation window.

    That's it. Again, sorry it's a bit of a hack... It should work though.
    Attached Files

    #2
    klassej,

    Thank you for sharing your indicator.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi classej,

      thanks for sharing. Is this for NT7? I'm asking because it uses e.g. "GridCategory", "CurrentsBars[1]" etc. and so it does not compile in NT6.

      Best,
      Tomas

      Comment


        #4
        Originally posted by Tomas View Post
        Hi classej,

        thanks for sharing. Is this for NT7? I'm asking because it uses e.g. "GridCategory", "CurrentsBars[1]" etc. and so it does not compile in NT6.
        Yep, only tested in NT7 so far.

        Comment


          #5
          I've also been working on multi-instrument indicators. One issue that's been difficult to deal with is synchronization, especially with indicators such as correlation which have lookbacks. NT7 doesn't help with synchronization, so if either instrument is missing any bars, Closes[0][x] and Closes[1][x] will refer to different times, which will cause the correlation routine to produce bad results. This is true even for historical data, and there are additional issues with live data because bars don't close until the first tick of the next bar, so the bars of the two instruments close at different times, and possibly quite late. There are also issues relating to daily bars.

          If you're only working with historical data for instruments which never have missing bars, then you don't have to worry about it. But if you're working with instruments that might have missing bars, or working with live data, then it's an issue.

          I've devised a solution which synchronizes correctly in the face of missing bars, for historical and real-time data. It works in indicators, but there is currently an NT7 bug which prevents it from working in multi-timeframe strategies. I've attached a copy of my version of a correlation indicator which uses it.
          Attached Files
          Last edited by kdoren; 02-06-2010, 03:40 PM. Reason: Removed Correlation.zip file, please see post #9 for later version

          Comment


            #6
            Thanks for that comment on synchronisation and the indicator, it seems like you put in a lot of effort into this. As you've probably noticed I just ignore the corr coefficient if there's only data for one of the input series and move on. I thought that would work. Also, I was inspired by an indicator called spread that does something similar (executes on each barupdate though, no loops, http://www.ninjatrader-support2.com/...ght=nt7+spread).

            Here's a comparison with proper data on a 1m chart of the Sterling/Euro correlation. (Your correlation indicator is black):


            Here's what happens when there's missing data (I deleted 30mins of BP6 data):



            So I guess you're right. Do you reckon it's academic though or a real issue if one uses a data feed like IQfeed?

            ==EDIT==

            Added a plot from CQG as well with the same window size(10). Doesn't match exactly...

            Click image for larger version

Name:	eu6bp5corr_CQG.jpg
Views:	1
Size:	94.1 KB
ID:	854080
            Last edited by klassej; 01-19-2010, 03:03 PM.

            Comment


              #7
              If you're using only historical data, on heavily-traded instruments during regular trading hours, you probably don't need to worry about synchronization at all, especially with longer bar times, because there shouldn't be any missing bars.

              On live data, the two instruments' bars will close at different times. Which bar closes first depends on tick order, and the one that closes first will produce a bad correlation output because Closes[0][x] and Closes[1][x] will refer to different times. Depending on tick order, that bad output could be the one permanently displayed by your indicator.

              To work right on live data, try running your calculations only if (Times[0][0] == Times[1][0]); I think that might work right.

              This isn't an academic exercise for me; I trade mostly stock pairs, where missing bars are possible due to light trading, short bar time, late opening, trading halts, or datafeed issues (the STX chart I posted with my correlation indicator has missing 1-min bars). I actually first noticed the problem on daily bars, because one issue had a trade reported on a holiday and the other didn't, so it was a missing bar. I also realized that some of my backtesting results were wrong because missing bars in some stocks had caused bad correlation results which in turn caused some trades to not execute.

              FYI, your correlation indicator uses the Spearman rank correlation; mine uses the Pearson method. That might explain any difference in output.

              The spread indicator you refer to is mine; the method I used works well in most cases, but it has issues with live data that has missing bars. I have a newer version which uses my synchronization methods; I'll post it soon.

              Comment


                #8
                Correlation indicator

                Originally posted by klassej View Post
                Hi,
                Here's a correlation indicator with a configurable window setting.



                The indicator is referencing the open source mathlib found on this site:


                Not sure it's a good idea to distribute everything as one pack from a licensing perspective so the install is a bit ugly as it requires some manual copying of files.

                Instructions:
                1. Download the C sharp pack from http://www.alglib.net/ (free for personal use as I understand it)
                2. Unzip and dig out the following files and copy them into NinjaTrader7\bin\Custom\Indicator\
                (you could also build a dll that you can reference, I did this but there were some problems with compiling it in NT7 so this is a bit of a dirty workaround...):
                ap.cs
                correlationtests.cs
                conv.cs
                corr.cs
                correlation.cs
                fft.cs
                ftbase.cs
                gammafunc.cs
                ibetaf.cs
                normaldistr.cs
                studenttdistr.cs
                3. Copy the attached indicator file into the same dir.
                4. Open KJCorrelation through: Tools=>Edit NinjaScript =>
                5. Compile
                6. Add the indicator on a chart with a contract, configure by setting SymbolA to whatever contract you want compare the key contract with and the correlation window.

                That's it. Again, sorry it's a bit of a hack... It should work though.
                It did not work for me:
                got 2 compiling error regarding:
                ADX-copy and ADX in 'variables'
                would appreciate a zip file top import in NT 7 very much
                thanks
                harry
                Last edited by hjahend; 02-06-2010, 01:59 PM. Reason: error found: had to delete ADX -copy from the custom indicator set and after that it compliled correct

                Comment


                  #9
                  Here's an updated correlation indicator.

                  It offers 4 choices for type of correlation calculation: Pearson (the most common), Rank (also called Spearman), PctChange (correlation of the returns) and Residual (correlation of the error, which is the difference between the input and a linear regression line through it).

                  With this, and all multi-instrument indicators, currently you must add the secondary data series to the chart before applying the indicator. There are currently bugs in NT7 which will cause problems if an indicator refers to instrument data series which are not displayed on the chart.

                  The indicator's "Symbol2" parameter defaults to the secondary instrument displayed on the chart, so you don't need to enter the symbol again.

                  Update 4/7/10: Please see the Indicators area for an updated version:
                  http://www.ninjatrader-support2.com/...d=1&linkid=291
                  Attached Files
                  Last edited by kdoren; 04-07-2010, 03:41 AM. Reason: Removed Correlation.zip file, please see Indicators area for updated version

                  Comment


                    #10
                    Originally posted by kdoren View Post
                    Here's an updated correlation indicator.

                    It offers 4 choices for type of correlation calculation: Pearson (the most common), Rank (also called Spearman), PctChange (correlation of the returns) and Residual (correlation of the error, which is the difference between the input and a linear regression line through it).
                    Is this a replacement of the previous KJcorrelation? If so which CS files should be deleted first in NT7 befor importing this new one?
                    With thanks
                    Harry

                    Comment


                      #11
                      This is a different correlation indicator, so it doesn't use any of the CS files from KJCorrelation. I posted mine in this thread because of the topic. I think it's just about stable enough to post in the indicators forum, please let me know any problems or comments with this version.

                      Comment


                        #12
                        Originally posted by kdoren View Post
                        This is a different correlation indicator, so it doesn't use any of the CS files from KJCorrelation. I posted mine in this thread because of the topic. I think it's just about stable enough to post in the indicators forum, please let me know any problems or comments with this version.
                        When importing your last correlation indicator in NT7 I get a message that this indicator already exists (see: the correlation.cs file from Alglib in your earlier message).
                        That was the reason of my question. Please advise.
                        thanks
                        harry

                        Comment


                          #13
                          I haven't installed KJCorrelation, but if it uses the same filename (correlation.cs) you will have to either change the name of the old one or delete it if you no longer need it; or else you could copy the code from mine into the NT editor, save as a different name, and compile it.

                          Comment


                            #14
                            correlation indicator in NT7

                            Originally posted by kdoren View Post
                            I haven't installed KJCorrelation, but if it uses the same filename (correlation.cs) you will have to either change the name of the old one or delete it if you no longer need it; or else you could copy the code from mine into the NT editor, save as a different name, and compile it.
                            That was the solution, thanks
                            Harry

                            Comment


                              #15
                              I've posted an updated Correlation indicator in the Indicators area:

                              http://www.ninjatrader-support2.com/...d=1&linkid=291

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by gentlebenthebear, Today, 01:30 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post gentlebenthebear  
                              Working...
                              X