Regular visitors will have noticed ever increasing amounts of spam on the Trading Gurus Community Forum. On November 24th 2012 we implemented some changes:

1. New users will be moderated 2. Registered users who have a post count of zero will not be able to view attachments or download code. 3. Registered users who have a post count of zero will not be able to create a new thread

As an additional countermeasure we anticipate introducing a modest paywall in the near future.

If this is your first visit here our apologies for the inconvenience.

Existing users may wish to contribute their ideas to this thread.

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: Trading using Support Vector Classification

  1. #1
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139

    Trading using Support Vector Classification

    A place to post code and discuss trading algorithms based upon the use of the magical sounding "support vector machines"!

  2. #2
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139

    A naive SVM trader in MatLab

    Hi,

    Following the discussion here, I've put together some basic MatLab code that uses the LibSVM library (available here) and trades on hourly bars downloaded via MetaTrader 4. Initial result were interesting/ promising. The inputs are log returns for the previous 6 hours.

    I haven't rigorously checked the code, so there is a reasonable possibility that the code is data snooping, although I have tried to prevent this. Code is attached for anyone to check and modify.

    A 'Points balance' curve is shown for the period of April 2010-Jan 2012.... The y axis is the cumulative sum of points won/lost in each trade and the x-axis is just the trade number.

    Incidentally, this approach does use a similar basic approach to that used in the original RAI code (see posts in this thread), that showed a positive expectancy over the year range 1999-2006.

    Clearly much more testing is needed, but it might be worth writing an MT4 EA to do a bit of forward testing. The cost and complexity parameters for the SVM training also need to be further optimised.

    Anyway, enjoy having a play with this...

    Cheers,

    Dr T
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Dr_T; 04-14-2012 at 05:58 PM. Reason: Code bug fix!!!

  3. #3
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139
    So, so here is the cumulative points balance using same algorithm as above run on GBPUSD (same as above) over the years 1999-2012.

    In summary, total points won = 530000
    Mean winning trade size = 416 points
    Mean trade results (winners and losers) = 12 points

    Num Wins/Num Losses = 0.75

    Personally, I don't believe this. Usually a consistently positive result means that there is a problem in the code - most probably in the trading simulation. So, the challenge is to find and correct the bug! (I'm skeptical and cynical about pretty much everything, with a hint of pessimism for good measure!)

    If I have time, I might implement the strategy as an MT4 EA and back test/forward test on a demo account.

    Cheers,

    Dr T
    Attached Images Attached Images

  4. #4
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139

    The dangers of buggy code!

    Found it!

    There was 24 hours worth of data snooping going on. Fixed and... a slightly more realistic curve!

    Dr T
    Attached Images Attached Images

  5. #5
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139
    Quote Originally Posted by Dr_T View Post
    Found it!

    There was 24 hours worth of data snooping going on. Fixed and... a slightly more realistic curve!

    Dr T
    So, having fixed this and changed the settings slightly (see updated code attached), SVMTrader managed to return a positive balance in backtesting on the 2010-2012 GBPUSD period. This version of the code also uses time as an input.

    If this is real (personally I suspect yet another bug!!), then it's worth looking at the size of those drawdowns.

    Cheers,

    Dr T
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Dr_T; 04-15-2012 at 06:42 PM.

  6. #6
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139

    1999-2012

    So, the 1999-2012 period looks reasonably profitable with the updated code. However, note the initial drawdown of nearly 10,000 points over the first approx. 3 years of trading!

    But as I suggested previously, this is all probably due to some kind of erroneous bias anyway.

    Cheers,

    Dr T
    Attached Images Attached Images

  7. #7
    Administrator Jim's Avatar
    Join Date
    Apr 2010
    Location
    South West England
    Posts
    494
    Hi Dr. T,

    Thanks once again for sharing the fruits of your hard labour with us.

    Perhaps I could take this opportunity to restate a question I originally posed in the RAI thread. Can you see any mileage in an approach that used a SVM to tune the parameters of an artificially stupid, but nonetheless currently profitable, algorithm such as Ray Robot 2?

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  8. #8
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139
    Hi Jim,

    Quote Originally Posted by Jim View Post
    Can you see any mileage in an approach that used a SVM to tune the parameters of an artificially stupid, but nonetheless currently profitable, algorithm such as Ray Robot 2?
    Good question! I did run some simulations to see if an SVM could identify the profitable trades from a moving average cross-over strategy, where I just generated the batch of MA parameters using Monte Carlo. On that occasion, SVM didn't appear to offer an 'edge'. But then, perhaps that is because the MA strategy was a long term loser anyway and I didn't provide much price information for training. The results posted above may suggest that SVM offers some enhancement if a strategy is based upon specific 'price action' (or if the losses occur due to some detectable price pattern). I'd certainly be interested in giving it a go. I think it's been said many times before, but having a good set of inputs to the model seems to be key.

    Cheers for now,

    Dr T

  9. #9
    Senior Member
    Join Date
    Dec 2011
    Location
    London, UK
    Posts
    139

    Svm ea

    Hi again,

    I've created an interface/ wrapper dll for libsvm to interface with MT4. The resulting creation is called SVMLib.dll. A compiled version and source code are attached. Once again, experimental code, so please used with extreme caution. I haven't yet been able to verify that it works correctly. I think there might be a memory leak in there too. All that I can say for sure is that when I call it, it runs and returns some numbers.

    I've written a simple EA based upon the Matlab code posted above. Interestingly, with exactly the same settings, the EA always loses in backtesting. Is this an interesting feature of the MT4 backtesting routines, a bug in the Matlab code or evidence that the wrapper dll is not working correctly. Quite possibly all three! Only more testing will reveal the answers.

    In which case, you are invited to test, modify and improve the attached code.

    I'm running the MT4 optimizer at the moment to try to discover if there any optimal cost and complexity parameters. I'll post here if this turns up anything interesting.

    NOTE:
    The attached files should be placed in the following folders of your MT4 installation

    MetaTrader4 Installation Folder\libsvm.dll - original libsvm library, source can be downloaded and compiled from http://www.csie.ntu.edu.tw/~cjlin/libsvm/

    MetaTrader4 Installation Folder\experts\libraries\SVMLib.dll - libsvm wrapper dll
    MetaTrader4 Installation Folder\experts\libraries\ANNLib.dll - provides a normalisation function, so not strictly needed, but it is used in the example EA

    MetaTrader4 Installation Folder\experts\SVMTrader_v1_20120417.mq4 - SVM example EA

    I'm afraid the EA code is very untidy and inefficient but I'm not trying to win any coding contests here.


    Cheers,

    Dr T
    Attached Files Attached Files

  10. #10
    Administrator Jim's Avatar
    Join Date
    Apr 2010
    Location
    South West England
    Posts
    494
    Hi Dr. T,

    Blimey, you have been busy! Thanks yet again.

    Quote Originally Posted by Dr_T View Post
    I'd certainly be interested in giving it a go. I think it's been said many times before, but having a good set of inputs to the model seems to be key.
    One thought that occurs to me - It's been said many times before that knowing whether a market is "ranging" or "trending" would be a fairly close approximation to the fabled "Holy Grail". RR2 is the proud possessor of a "FadeEntry" switch. Set "True" if market is "ranging", and "False" if market is "trending".

    Can you see any (preferably stupidly simple!) way of using your shiny new code to classify a market, and then flick Ray's switch appropriately?

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

Page 1 of 4 123 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •