Harmony Search Algorithm

Discussions about the testing and simulation of mechanical trading systems using historical data and other methods. Trading Blox Customers should post Trading Blox specific questions in the Customer Support forum.
Post Reply
Eventhorizon
Roundtable Knight
Roundtable Knight
Posts: 229
Joined: Thu Jul 08, 2010 2:36 pm
Location: Boulder, CO
Contact:

Harmony Search Algorithm

Post by Eventhorizon » Tue Jan 18, 2011 12:10 pm

In the Feature Requests forum, Bobsyd recntly requested some intelligence on the part of TBB in optimizing system parameters. This got me researching search algorithms.

I have successfully implemented a genetic search algorithm called the Hamony Search Algorithm in TBB. I would like to share it with the community, but it is not a simple matter of downloading a blox adding it and away you go! Only users with TBB could implement it as modification of the blox is required. It also requires a fair amount of effort and de-bugging. I have used it on three systems. The example here is a simple system and took over an hour to configure, so I am not sure how much interest there would be - hence this introductory post.

I have worked up step-by-step instructions - 6 pages including example code using the algorithm with the ATR Channel Breakout system. I would need to upload the HS Blox itself plus the instructions plus the example system (made up of a PM, MM, 1 Entry/Exit, 1 Exit blox plus the Harmony Search Auxiliary blox). I would think this is enough for some determined persons to emulate the method for their own systems. Can I upload an entire system, or do I have to upload it in bits for you to assemble? Maybe I should zip it all together into 1 file.

To see the benefits of using such a methodology, consider the chart of a 5000 cycle search using MAR as the objective function shown below.

This search space involved 6 parameters: atrDays (1 - 100 x units of 1), closeAverageDays (1 - 600 x units of 1), entryThreshold and exitThreshold (0 - 5 continuous values), riskPerTrade (0.1% - 2% continuous), tradeDirection (0, 1, 2). If I were to try to step search simultaneously, this amounts to 100 x 600 x inf x inf x inf x 3 = a lot. More reasonably let's call it 20 x 24 x 20 x 20 x 20 x 3 = 11.52 x 10^6 combinations = a bit less than a lot but still too many to do on my machine! I think the search algorithm does a pretty good job in 5000 tests.

I wrote a couple of detailed blogs on the subject. My first was a general introduction to the topic. The second was more about the mechanics. Let me know if there is interest in this and how best to share the work.
Attachments
HS_ATR_BO_5000.png
Notice how the MAR ration generally improves moving from left to right - the paramter sets are "evolving"
HS_ATR_BO_5000.png (28.83 KiB) Viewed 8879 times

sluggo
Roundtable Knight
Roundtable Knight
Posts: 2986
Joined: Fri Jun 11, 2004 2:50 pm

Post by sluggo » Tue Jan 18, 2011 12:30 pm

I'd suggest you make a .zip archive with hierarchy, and include subfolders
  • Blox (files with suffix .tbx)
  • Simulation Suites (files with suffix .vts)
  • Systems (files with suffix .tbs)
This way you can easily upload the Blox Basic code AND the parameter settings (which are saved in the .vts suite) needed to run it.

Personally, I'm leery of using a non-smooth goal function (such as MAR_ratio) to guide an adaptive search like the genetic algorithm. MAR ratio is just too jaggedy { it's discontinuous, so d(MAR)/d(parameter) goes to +/- Infinity } for me to be comfy with it as an objective. I myself would be a lot less nervous if the goal were something like Rsquared, or Sharpe, or (RAR * %BarsProfitable / StDev).

PNW_Trader
Senior Member
Senior Member
Posts: 37
Joined: Wed Jul 21, 2010 1:00 am
Location: Portland, OR

Post by PNW_Trader » Tue Jan 18, 2011 4:47 pm

Looks like great work and I'd love to see the TBB code!

bobsyd
Roundtable Knight
Roundtable Knight
Posts: 376
Joined: Wed Nov 05, 2008 2:49 pm

Post by bobsyd » Wed Jan 19, 2011 1:57 am

Ditto.

Also interested in Sluggo's comment about appropriate goodness function when using an adaptive search method. Makes some intuitive sense but can't profess to understand all the logic links. Also, curious what the concept is behind (RAR * %BarsProfitable/StDev) and what is meant by %BarsProfitable. If using daily bars, would it be % of Days when Total Equity increases?

Eventhorizon
Roundtable Knight
Roundtable Knight
Posts: 229
Joined: Thu Jul 08, 2010 2:36 pm
Location: Boulder, CO
Contact:

Post by Eventhorizon » Wed Jan 19, 2011 3:22 pm

Well, here it is. The zip file contains a list of files, and some instructions in pdf form.

I apologize in advance for the amateurish nature of my coding and documentation - I have not yet learned the discipline needed to write beautiful code like Roger Rines!

The system that is included (thanks for the suggestions, Sluggo - I tried to faithfully follow them) implements the example in the instructions i.e. all the recommended code changes have been done, so you just need to follow along. It would be good to compare the system as shipped with TBB with my version to understand how it works. I would say it is worth at least skimming the document before "playing" with the system.

I will try to answer questions promptly - I am sure to have missed some really basic things!

EDIT: While I think about it -

Sluggo, I chose MAR to make for a simple example as it is something everyone understands. The user can implement any objective function they care to dream up, and they can impose any filtering constraints on the results set (such as minimum number of trades, minimum r-sq value, etc) they desire.

Interestingly, one of the features of genetic search is its resiliance in the face of discontinuities - the roughness of the surface doesn't cause the problems you get with hill-climbing and other derivitive-based methods.

I am in the process of figuring out how to handle the desire for other solution characteristics such as preferring a lower plateau over a higher pinnacle. In "genetic" terms I am thinking of islands in the ocean - the HSA puts everything on one island, but I want to be able to see what solutions might have arisen if a slower-to-evolve-but-ultimately-better solution had got a foothold on another island. Something in the area of "cluster analysis" might work I think.
Attachments
HarmonyMemory.zip
Example Harmony Memory Search Optimization
(54.68 KiB) Downloaded 290 times

bobsyd
Roundtable Knight
Roundtable Knight
Posts: 376
Joined: Wed Nov 05, 2008 2:49 pm

Post by bobsyd » Fri Jan 21, 2011 8:34 pm

Many thanks Ian, IMHO an awesome and valuable contribution. And as far as “amateurish natureâ€
Attachments
Pinnacle vs Plateau.xls
(38 KiB) Downloaded 331 times

bluefightingcat
Roundtable Knight
Roundtable Knight
Posts: 349
Joined: Fri Nov 20, 2015 3:09 pm

Re: Harmony Search Algorithm

Post by bluefightingcat » Tue Feb 23, 2016 6:21 am

I just recently started using TradingBlox and came across this Topic. Considering that TradingBlox hasn't implemented Genetic Testing (or similar) yet I found this post very interesting. I just ran a stepped test using 5 parameters that meant about 9600 cycles. It my machine about 36 hours to do. Needless to say I was getting a bit impatient.

I was wondering whether you have done any work on this algorithm? I read the two posts mentioned in the original post but that is quite old? Any progress made on this?

Would be very interested to test this out.

Tim Arnold
Site Admin
Site Admin
Posts: 8540
Joined: Tue Apr 06, 2004 1:41 pm
Location: Boston, MA
Contact:

Re: Harmony Search Algorithm

Post by Tim Arnold » Wed Feb 24, 2016 8:05 am

I have not tried this. I don't use this approach and have not found genetic or brute force optimization strategies very useful.

bluefightingcat
Roundtable Knight
Roundtable Knight
Posts: 349
Joined: Fri Nov 20, 2015 3:09 pm

Re: Harmony Search Algorithm

Post by bluefightingcat » Wed Feb 24, 2016 8:49 am

and have not found genetic or brute force optimization strategies very useful
Care to share how you go about designing a system and then optimizing it? I'm a relatively new to all this, so any advice on how to improve and test my system design would be much appreciated.

Tim Arnold
Site Admin
Site Admin
Posts: 8540
Joined: Tue Apr 06, 2004 1:41 pm
Location: Boston, MA
Contact:

Re: Harmony Search Algorithm

Post by Tim Arnold » Wed Feb 24, 2016 10:38 am


bluefightingcat
Roundtable Knight
Roundtable Knight
Posts: 349
Joined: Fri Nov 20, 2015 3:09 pm

Re: Harmony Search Algorithm

Post by bluefightingcat » Thu Sep 29, 2016 6:16 am

If anybody is interested, I tried this out and it seems to work fine.

Post Reply