PureBytes Links
Trading Reference Links
|
Hi Ton --
Here is some simple code that will demonstrate.
//------------------------ // // Optimization.afl // // A very simple example to illustrate // trend-following versus mean-reversion
//
// Set MALength1 = 5, MALength2 = 20 for trend following // Set MALength1 = 20, MALength2 = 5 for mean reversion
MALength1 = 20; //Optimize("MALength1",11,1,31,2); MALength2 = 5; //Optimize("MALength2",4,2,32,2);
HoldDays = 5; //Optimize("HoldDays",9,1,10,1);
MA1 = MA(C,MALength1); MA2 = MA(C,MALength2);
Buy = Cross(MA1,MA2); Sell = Cross(MA2,MA1); // OR BarsSince(Buy)>=HoldDays;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
e=Equity();
Plot(C,"C",colorBlack,styleCandle); PlotShapes(shapeUpArrow*Buy+shapeDownArrow*Sell, IIf(Buy,colorGreen,colorRed));
Plot(MA1,"MA1",colorRed,styleLine|styleLeftAxisScale);
Plot(MA2,"MA2",colorBlue,styleLine|styleLeftAxisScale);
Plot(e,"equity",colorGreen,styleLine|styleOwnScale); // //-----------------------
Set the MA lengths as suggested in the comments in the code (two runs, one for each pair of values) and click the "Apply Indicator" icon to plot everything that has a Plot statement. Note that when the "fast" MA length (MALength1) is shorter than the "slow" length (MALength2), the buys happen after upward price trends have already begun -- hoping that the trend will continue, hence a trend following system. When the "fast" length is longer than the "slow" length, the buys come after a period of price decline -- hoping that prices return to the longer term average, or mean, hence a mean reversion system. By letting an optimizer choose which works better over an in-sample period of time, the same code can give trend following systems when the price trends well, and switch to mean reversion when the prices oscillate. Examine the results in the out-of-sample period that follows to see if it works.
Run this code, with the Optimize statements enabled, using AmiBroker 5.05.1 and the new automatic walk-forward testing. Use RAR/MDD as the objective function (or UPI, K-Ratio, RRR). Set the in-sample period to one or two years of daily data, the out-of-sample period to 6 months or one year, and give it enough data so that it makes ten or more steps. Look at the values of MALength1 and MALength2 that are found to be best (at the right hand side of the walk-forward report). Depending on the price series, you will probably see some periods were trend-following and some were mean-reverting.
Thanks, Howard www.quantitativetradingsystems.com
On Fri, Mar 7, 2008 at 3:40 AM, Ton Sieverding < ton.sieverding@xxxxxxxxxx> wrote:
Correct Howard, thanks for your remark and
frankly this is one of the things I never understood. May be because I am a
typical trend follower and am using mean reversion only as warning signals. In
Dutch there is an _expression_ saying that "what the farmer doesn't like he will
not eat". So for me the MAFast must per
definition be less than the MASlow period. And yes I know that
people let this rule invert when looking to mean reverting systems. Although I
understand what they do and what it will create, I do not really understand what
is going on. Something like solving a complex partial differential equation. You
know how to do it but you don't know what you're really doing. Or to put it in
another way, I understand mean reversion when using bands like Bollinger or even
better with Hurst Envelops. I do not understand how you can get mean reverting
system when letting MASloW<MAFast. Can you
give me a hint or perhaps a source that will explain me what's going on
?
Regards, Ton.
PS : When is your new book coming
?
----- Original Message -----
Sent: Thursday, March 06, 2008 5:54
PM
Subject: Re: [amibroker] Sequential
Optimizations for independent variables
Hi Ton --
A comment on your example of insisting on fast being less
than slow in your example code ---
I understand that that might just
have been an example for ease of understanding.
But when the
moving average lengths are fast < slow, the system is trend following; when
fast > slow, the system is mean reverting. Trading system developers
might let fast and slow change relationship, which changes the type of system,
creating an adaptive trading system.
Thanks, Howard
On Tue, Mar 4, 2008 at 3:04 PM, Ton Sieverding < ton.sieverding@xxxxxxxxxx>
wrote:
Of course ... thanks.
Regards, Ton.
-----
Original Message -----
Sent:
Tuesday, March 04, 2008 10:41 PM
Subject:
Re: [amibroker] Sequential Optimizations for independent variables
Hello,
You should do this the other way round - don't generate
trades when values are conflicting:
For example, in the main code (not in custom
backtester), add the following
if( Fast > Slow )
{
PositionSize = 0; // don't enter any
trades
}
-----
Original Message -----
Sent:
Tuesday, March 04, 2008 10:33 PM
Subject:
Re: [amibroker] Sequential Optimizations for independent variables
Sure, I know Tomasz... But that doesn't
solve my problem. And my problem is that I am getting several
conflicting values from the optimizer. In the underneath mentioned code
there is a logical rule that says : Fast must be smaller than Slow. So
when I am getting as optimal values a result like
let's say Fast = 100 and Slow = 10, there is something wrong
from a logical perspective. In practice I cannot accept these values. To
avoid them I want to alter the result of the 'Objective Function' (
ObFn ) during the optimization process by simply changing the result in
a very negative value so that in the Sort it will no longer be on top of
the list. During the optimization process I need something
like : if fast>slow then ObFn = -100. And that doesn't work because
AB tells me that I am using a variable ( fast or slow ) not being
initialized. What of course is correct. Therefore I would like to get
these values with GetValue(). Hoping not to get the error
message again -)
Regards, Ton.
-----
Original Message -----
Sent:
Tuesday, March 04, 2008 9:28 PM
Subject:
Re: [amibroker] Sequential Optimizations for independent
variables
The values are available as a result of OPTIMIZE
function:
fast = Optimize("fast", 12, 5, 20, 1 ); // 'these are values of opt
variables slow = Optimize("slow", 26, 10, 25, 1 );
(note optimize calls should be on
GLOBAL level (i.e OUTSIDE "if" of custom backtest
proc)
-----
Original Message -----
Sent:
Tuesday, March 04, 2008 9:14 PM
Subject:
Re: [amibroker] Sequential Optimizations for independent
variables
Thanks for that one and something else
about optimization Thomasz/Herman. I know how to get the built-in
backtester statistics with GetValue. Let's say :
RetValue =
st.getvalue("CAR");
But how do I get the values of both
optimized values in underneath mentioned code ?
I tried the same way as above but
failed. What I did was something like :
OptFast =
st.getvalue("fast");
OptSlow =
st.getvalue("slow");
But that did gave me
errors. What's wrong with that ? Why ? The requested field
is not available ? But didn't I initialize both fields in the
Optimize instruction ? Both fields show up in the optimization list
in AA. I don't understand what going
wrong ...
SetCustomBacktestProc("");
if( Status("action") ==
actionPortfolio ) { bo = GetBacktesterObject();
bo.Backtest();
st = bo.GetPerformanceStats(0);
fast =
Optimize("fast", 12, 5, 20, 1 );
slow = Optimize("slow", 26, 10, 25, 1 );
Regards, Ton.
-----
Original Message -----
Sent:
Tuesday, March 04, 2008 2:19 PM
Subject:
Re: [amibroker] Sequential Optimizations for independent
variables
Opt =
Optimize("TotalOpt",10,1,200,1);
switch( 1 )
{
case TotalOpt <=100:
Opt1 =
TotalOpt;
Opt2 =
10;
break;
case TotalOpt > 100:
Opt1 = 10;
Opt2
= TotalOpt - 100;
break;
}
-----
Original Message -----
Sent:
Tuesday, March 04, 2008 2:10 PM
Subject:
[amibroker] Sequential Optimizations for independent
variables
Awhile back I suggested, on the feedback site, an option to
perform sequential optimizations. The response was that this is
already possible, however I can't figure out how. Can someone
explain to me how to run the following two optimizations
sequentially, that is independently, one after another?
Opt1 = Optimize("Opt1",10,1,100,1);
Opt2 = Optimize("Opt2",10,1,100,1);
Sequential optimization would only require 200 opt cycles,
while the normal opt procedure is designed for dependent
variables and requires 10,000 opt cycles. Sequential opts for
independent variables would, in this case, be almost 100 times
faster. I bet many users don't consider whether the opt
variables are independent or not... doing so could turn a two
hour opt into a 1 minute opt.
AFAIK, Opt()s cannot be if()d, and their arguments cannot be
changed dynamically, right?
I must be (again) missing something obvious.
many thanks,
herman
__._,_.___
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
__,_._,___
|