Bollinger Bands Adaptive Voting Trading System

Post anything related to mechanical systems and automated trading here.
Post Reply
hjelmstade
Posts: 34
Joined: Wed May 18, 2016 6:51 pm
Contact:

Bollinger Bands Adaptive Voting Trading System

Post by hjelmstade » Mon Oct 03, 2016 7:52 am

Yesterday I shared a basic bollinger bands trading system and explained how it works here: viewtopic.php?f=4&t=124

If you haven't read that post yet, you should do so before you jump into this one as this new adaptive system is based on that system so I am going to be skipping the explanation of its strategy for this post.

You can find the code for the new bollinger bands adaptive voting system here: http://www.wave59.com/library/scriptdetail.asp?id=175

I'll explain briefly how an adaptive system works but Earik does a much better job of it in his Mechanical Trading Systems book which I highly recommend you buy if you want to build your own systems as it helped me immensely.

Essentially, an adaptive system is running many miniature systems at once with different parameter sets. It keeps track of their profit and compares them over a specific look back period to determine which system is the most optimal at that point in time.

After I created the bollinger bands trading system, I noticed that there were points in the equity curve on a daily ES chart where the bollinger bands system did better in mode 2 and other times did better in mode 4 so I sought to create an adaptive system that would move between both systems as they became optimal.

After I created the first adaptive system and ran it through the optimizer to determine the best look back period, I realized I had run into the same problem again. Some look back periods did well at some points in the equity curve and others did better at other points. So then I took it one step further and implemented a voting system on top of the adaptive system. After experimenting, I determined the best system setup for a daily ES chart was having three adaptive systems with the look back periods of 5, 10 and 50 bars. So it is essentially determining which system performed the best over the last week, two weeks and 10 weeks and then returns a signal from each. This signal is counted as a vote on whether to go long or short and the system tallies the votes and trades accordingly.

As you will see later on, this resulted in the following improvements and did a great job of smoothing out the equity curve. Its not perfect but its as good as I think I can get it.

~16% ($30k) profit increase
~2% accuracy increase
~$42 avg trade increase
~3k max drawdown decrease

This system contains three files with different inputs which I will explain in turn.

Bollinger Bands EquityCurve
This function contains the same logic as the bollinger bands trading system and uses the same input as that system with the addition of the lookback and longshort variables. The lookback specifies the period for which to calculate the profit of the system and longshort is used to store whether the system is currently long, short or flat. The only difference between the logic in this function and that in the bollinger bands trading system is it was changed to return a signal rather than placing actual trades. So it is virtually keeping track of profit/loss it would have made had it actually been trading to be used for analysis.

Bollinger Adaptive EntryExit Signal
This function does the actual adaptation. You provide it with the parameters for up to two bollinger bands trading systems plus several variance parameters that determine the combination of systems to spawn. One thing I should point out is that the bollinger length and standard deviation variables are not being changed. Bollinger bands already do a decent job at adapting to volatility as their base calculation involves averages over x bars. I tried seeing if adapting these variables would lead to a better system and did not discover a single instance where this was the case. Instead what we are adapting is the profit exit and loss exit settings as these are the most vulnerable to degrade over time.

If you do not want to adapt two systems, you can set the mode setting of system 2 (s2_mode) to 0 and a second system will not be created.

INPUTS
lookback - applies to both systems and determines how many bars to look back when calculating profit and determining the best system.

There are two sets of the following variables, distinguished by the s1 or s2 in front of them for the two different systems.

bol_length - length of bollinger band
bol_sdev - standard deviation for bollinger band
bol_profit_exit_bars - if profitable, how many bars to hold before exit
bol_loss_exit_bars - if not profitable, how many bars to hold before exit
mode - see description in Bollinger_EquityCurve function
variance - how much variance you want in the profit and loss exit vars
variance_increment - how you want the profit and loss exit vars to

The two new variables are variance and variance_increment. The system takes the variance increment and begins applying that to both sides of the primary settings up to the variance total to create the number of profit exits and loss exits we wish to explore. Here are two examples of the variance settings being applied to the profit exit and loss exit settings.

profit_exit_bars: 5
loss_exit_bars: 9
variance: 2
variance_increment: 1

possible profit exits generated from input: 3, 4, 5, 6, 7
possible loss exits generated from input: 7, 8, 9, 10, 11


profit_exit_bars: 20
loss_exit_bars: 40
variance: 8
variance_increment: 2

possible profit exits: 12, 14, 16, 18, 20, 22, 24, 26, 28
possible loss exits: 32, 34, 36, 38, 40, 42, 44, 46, 48

Once the possible profit and loss exits have been generated, it spawns instances of the bollinger bands trading system for every combination of these parameters. For the first example above, it would look something like this.

3, 7
3, 8
3, 9
3, 10
3, 11
4, 7 and so on until every combination is being run on a system.

As it iterates through each system for each bar, it is determining the profit/loss for each system over the look back period and the one that has the most profit is selected and that signal is then returned.


System Bollinger Adaptive Merged Voting
This indicator then brings everything together and actually executes the trading. It deploys up to three separate adaptive systems with different look back periods and then tallies the votes from each of these adaptive systems and makes its trading decision based on the consensus.

INPUTS
contracts - number of contracts to trade (value is doubled if you enable modes #2 or #4 for any instance of the adaptive system)
lookback1, lookback2, lookback3 - how many bars to look back for each instance of the adaptive system when calculating profit

The second and third adaptive systems are optional. If you do not want to run multiple adaptive systems, you can set lookback2 and/or lookback3 to 0 and it will not create them. Lookback1 is required.

Same as before, there are two sets of system parameters you must pass in. If you do not want a second system, set s2_mode as 0 and it will not use a second system and will only adapt the first system.

bol_length - length of bollinger band
bol_sdev - standard deviation for bollinger band
bol_profit_exit_bars - if profitable, how many bars to hold before exit
bol_loss_exit_bars - if not profitable, how many bars to hold before exit
mode - see description in Bollinger_EquityCurve function
variance - how much variance you want in the profit and loss exit vars
variance_increment - how you want the profit and loss exit vars to increment up to the variance total


Default Parameters for daily ES
These are the final optimized parameters for a daily ES chart which are the default on the indicator.

lookback1: 5
lookback2: 10
lookback3: 50
s1_mode: 2
s1_bol_length: 12
s1_bol_sdev: 1.2
s1_profit_exit_bars: 4
s1_loss_exit_bars: 9
s1_exit_variance: 3
s1_exit_variance_increment: 1
s2_mode: 4
s2_bol_length: 12
s2_bol_sdev: 1.4
s2_profit_exit_bars: 3
s2_loss_exit_bars: 8
s2_exit_variance: 2
s2_exit_variance_increment: 1


Now before we get into showing how this improved the initial bollinger bands system at each step, I want to review how you could take this and set it up for a different time frame or market.

1. Run standalone bollinger bands trading system on time frame and then put into the optimizer to determine up to two workable systems. This took me a while to do as there are a lot of combinations between the length, sdev, profit exit, loss exit and mode settings so I did this in stages and only for modes 2 and 4 as I knew these were more profitable due to the contract size doubling logic if the special but rare criteria is met.

2. Once I had parameters I was happy with, I set those for s1 and s2 on the bollinger adaptive merged voting indicator. Then I ran this system through the optimizer using only the first lookback period (setting lookback2 and lookback3 to 0 to disable) and incrementally went through different values for lookback1, s1_variance, s1_variance_increment, s2_variance and s2_variance_increment.

3. Each time I found a set of parameters I liked, I wrote them down and then at the end, determined which standard variance and increment I wanted and put it through the optimizer one last time with ranges of look back periods to find the final combination.

This might seem like a lot but it really improved my standalone bollinger bands system and should allow you to go extended periods of time before you have to put it back through the optimizer if market conditions change. At least thats the idea. If someone could point out a flaw in my logic, it would be very much appreciated so I can make adjustments :)


Ok, so here are the results of the system that shows each stage and may help to visualize what is happening. I'm not sure how clear this image will be on other monitors so I'm also including the final results of the merged voting system as separate files so its easier to see. If you right click on the first image you can choose to view image which will open a new tab where you can zoom in to hopefully see things more clearly.

What I was hoping to get across with the combined image is to show how the equity curve continued to flatten after each step until we have a better equity curve in the merged system. There was an area in 2013 and 2014 where it struggled but it did eventually find its way and continue moving up. The improvements to profit, accuracy, average trade and the lower drawdown all confirm the final merged system is better and hopefully more reliable than any of its parts.

Please let me know if you have any questions and if you make improvements or optimize this for another time frame or market please share!

Thanks!
visual_results.png
Comparison of back testing results at each step
visual_results.png (383.48 KiB) Viewed 42592 times
daily_ES_backtesting_mergedvotingsummary.png
Final merged voting system summary
daily_ES_backtesting_mergedvotingsummary.png (22.22 KiB) Viewed 42592 times
daily_ES_backtesting_mergedvoting_equitycurve_small.png
Final merged voting system equity curve
daily_ES_backtesting_mergedvoting_equitycurve_small.png (14.48 KiB) Viewed 42592 times

User avatar
earik
Site Admin
Posts: 477
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by earik » Mon Oct 10, 2016 6:38 pm

Thanks for sharing! This is great stuff. :mrgreen:

Earik

hjelmstade
Posts: 34
Joined: Wed May 18, 2016 6:51 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by hjelmstade » Tue Oct 11, 2016 3:35 am

My pleasure, thanks Earik :)

sherlock
Posts: 5
Joined: Tue Jul 21, 2015 8:56 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by sherlock » Mon Oct 17, 2016 10:51 pm

Great work Hjelmstade .

I backtested your system on the @ES#C from 9/9/97 just to see if our stats would measure up.. As you will see, we are slightly out, which I can only put down to the fact you ran yours again @ES#...

Anyway, what I wanted to do was benchmark the BBAV v MVS from Eariks book. Its uncanny how close the results are:
EVS_SR.JPG
EVS_SR.JPG (59.88 KiB) Viewed 42507 times
EVS_EC.JPG
EVS_EC.JPG (46.81 KiB) Viewed 42507 times
Lets not dwell on the major drawdown (MVS) from 97-99 - You would have needed iron balls.... Anyway, that's not the point...

@Earik, do you have any comments on the similarity of results.. I've even reviewed the charts and it would seem there's a high correlation for both systems taking similar trades...However, the one big difference is the MDD......

Anyway, great coding Hjelmstade....

hjelmstade
Posts: 34
Joined: Wed May 18, 2016 6:51 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by hjelmstade » Tue Oct 18, 2016 5:23 am

Hi Sherlock,

Thank you :)

Yes, I made a mistake backtesting with @ES# so that may have been part of the difference but I also realized after the fact I did not have all of the holidays set correctly so depending on if you are including or excluding all holidays on the chart, it will be slightly different.

Also, for anyone else that reviews the code, I found a bug that surprisingly made the system slightly better. In the for loops on the AdaptiveEntryExit_Signal function, I did not subtract one from the lengths of the array while iterating through. I did not release an update to fix this as after I made the fix, the system performed worse.

This is due to the way the arrays are dynamically created. If you call on an index that doesn't hold a value, it returns 0 so when the loops iterate one too many, it returns 0 for either the profit or loss exit. What this means is that in at least several cases over the last 19 years, the adaptive function determined that the most optimal system would take profits or losses on the very next bar.

I confirmed this by fixing the for loop bug and manually adding 0 to the profit and loss exit variance arrays and the results were the same as not fixing the bug. This shouldn't hinder the system in anyway as it should adapt to whatever the optimal profit and loss exit bars are but its easy to fix if anyone is concerned.


Lastly, Sherlock since you mentioned Earik's Merged Voting System, I just shared the Bollinger Bands Signal function I'm using as an additional subsystem in my implementation of his MVS here: http://www.wave59.com/library/scriptdetail.asp?id=176

While adding to Earik's MVS, I discovered that a slightly different strategy is optimal. Rather than having a separate profit and loss exit strategy, it has been simplified to retain the signal for a certain number of bars. For some reason, returning this simplified signal meshed better with the rest of the subsystems. You will see this simplified version is not adaptive.

Here are the parameters I'm using on my merged system which are different than what was found to be optimal for the standalone system.

bol_length = 18
bol_sdev = 2.4
bol_mode = 4
signal_retention = 5

sherlock
Posts: 5
Joined: Tue Jul 21, 2015 8:56 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by sherlock » Tue Oct 18, 2016 8:16 am

Cool, I will add your BBV to the merged and run some tests..

In addition, I'm trying to add a filter into the original EMVS; however, my coding skills are lacking... After manually backtesting, it looks to me that by adding in filter will remove some of the losing trades...

Earik sent me the below, but I'm wondering whether you know how to code this....

Earik said

In general, what you have to do is get the main system into a signal variable, which I think the current code already does. Let's say it's something like:

signal = Merged_System_Function();

So what you do is create a different signal function, which has all the filters. So:

if (ema > ema2) ema_signal = 1;
if (ema < ema2) ema_signal = -1;

...then...

signal2=0;
if ((signal > 0) and (ema_signal >0)) signal2=1;
if ((signal < 0) and (ema_signal <0)) signal2= -1;

So now you are creating a "signal2" variable, which is a 1 only if the merged system and the emas are pointed up. And then a -1 if both are pointed down. Otherwise, it's just a zero. You then use signal2 to place your trades, rather than signal.
[/i]

Cheers.

hjelmstade
Posts: 34
Joined: Wed May 18, 2016 6:51 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by hjelmstade » Tue Oct 18, 2016 9:29 am

Hi Sherlock,

Yeah, I can do that. Why don't you PM me with more info regarding the exact filter you are trying to add and I'll see what I can do.

-hjelmstade

sherlock
Posts: 5
Joined: Tue Jul 21, 2015 8:56 pm
Contact:

Re: Bollinger Bands Adaptive Voting Trading System

Post by sherlock » Tue Oct 18, 2016 3:38 pm

Hi hjelmstade, I PM'd you earlier; however, for some reason its not showing in my sent folder, so I am not too sure whether you have received the msg or not.... Cheers.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests