[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Code help please... Optimize with CMAE



PureBytes Links

Trading Reference Links

Dennis -

I don't post here much, but was about to post something else and saw
this.  Sometimes I can't resist a challenge.  It looks like you want
to experiment with CMAE as a general purpose function optimizer. 
First, I concur with others - the best way is to write a DLL.  But, if
you just want to play ...

Warning, this is a hack - and hack is not being used in the good way
here.  But it will allow you to play.

1. Setup AA to current symbol and set the Optimization target to
"objective" in AA Settings / Walk-Forward

2. Optimize the following code.  It is set for CMAE, but you might
also find the commented out SPSO interesting.  I've also done my best
to re-express your objective function to what I think you meant. 
Check the comments.
_____

//REMEMBER - Set the Optimization target to "objective" in AA Settings
/ Walk-Forward

//OptimizerSetEngine( "spso" );
//OptimizerSetOption( "Runs", 2 );
//OptimizerSetOption( "MaxEval", 1000 );

OptimizerSetEngine("cmae");

X			= Optimize( "X", 1, 1, 100, 0.1 );
Y			= Optimize( "Y", 1, 1, 100, 0.1 );

Buy		= Sell = Short = Cover = 0;

SetOption("UseCustomBacktestProc", True );

if ( Status( "action" ) == actionPortfolio )
{
	bo			= GetBacktesterObject( );
	bo.Backtest( );
	//  The original goal appeared to be to minimize the error of 100 - X*Y, 
	//  AND to minimize the difference between X and Y.  In other words, a 
	//  result of X=10 and Y=10.
	//  Note that this must be expressed as an objective to be maximized.
	Objective	= - abs( 100 - X * Y ) - abs( X - Y );
	bo.AddCustomMetric( "objective", Objective );
}
_____

It should be obvious how to setup any parameters and an objective. 
Hack doesn't seem strong enough, but it was an interesting diversion.

-- Bruce


--- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <groups@xxx> wrote:
>
> Hello,
> 
> In that case just look at the CMAE docs (the read me is inside "cmaes")
> If this is too complicated, you may need to use SciLab for example
> www.scilab.org
> 
> 
> Best regards,
> Tomasz Janeczko
> amibroker.com
>   ----- Original Message ----- 
>   From: Dennis Brown 
>   To: amibroker@xxxxxxxxxxxxxxx 
>   Sent: Monday, July 07, 2008 4:46 PM
>   Subject: Re: [amibroker] Code help please... Optimize with CMAE
> 
> 
>   Thank you both for continuing to provide help.
> 
> 
>   I really do not need to solve an exhaustive search for two
variables with a simplistic objective.  I really need to solve
problems that have many variables and a very complex objective formula
that is not suitable for an exhaustive search.
> 
> 
>   I only provided this simplest of all cases so the the problem
would not get in the way of my question of how to connect this simple
case to the CMAE engine.
> 
> 
>   If that is too complicated to address on this list, then I will
have to look elsewhere for help.
> 
> 
>   Again, thank you for your responses,
> 
> 
>   Dennis
> 
> 
>   On Jul 7, 2008, at 9:19 AM, Tomasz Janeczko wrote:
> 
> 
>     Hello,
> 
>     Here is the simplest exhaustive search sample:
> 
>     function Objective( x, y ) 
>     { 
>       return sin(x) * cos(y); 
>     } 
> 
>     xmin = 0; 
>     xmax = 100; 
>     xstep = 1; 
>     xbest = Null; 
>     ymin = 0; 
>     ymax = 100; 
>     ystep = 1; 
>     ybest = Null; 
> 
> 
>     best = -1e9; 
> 
>     for( x = xmin; x <= xmax; x += xstep ) 
>     for( y = ymin; y <= ymax; y += ystep ) 
>     { 
>        f = Objective( x, y ); 
> 
>        if( f > best ) 
>        { 
>             best = f; 
>             xbest = x; 
>             ybest = y; 
>        } 
>     } 
> 
>     printf("Best f = %g, at x = %g, y = %g", best, xbest, ybest );
> 
>     Best regards,
>     Tomasz Janeczko
>     amibroker.com
>       ----- Original Message -----
>       From: Paul Ho
>       To: amibroker@xxxxxxxxxxxxxxx
>       Sent: Monday, July 07, 2008 2:29 PM
>       Subject: RE: [amibroker] Code help please... Optimize with CMAE
> 
> 
>       I think using an exhaustive search would be a way to start. 
since you only have 2 parameters, you can also put a little bit of
smart in there yourself.
>       for example, if X*Y == 100 or close to that. there can be a
constraint in place to restrict the range of  Y searched for a
particular value of X, and vice versa,
>       Cheers
>       Paul.
> 
> 
> 
> ------------------------------------------------------------------------
>         From: amibroker@xxxxxxxxxxxxxxx
[mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of Dennis Brown
>         Sent: Monday, 7 July 2008 10:17 PM
>         To: amibroker@xxxxxxxxxxxxxxx
>         Subject: Re: [amibroker] Code help please... Optimize with CMAE
> 
> 
>         Tomasz and Paul,
> 
>         Thank you for responding.
> 
>         Yes, I also had looked at those sources, but being that I am
able to 
>         only stumble along with C++, it is Greek to me.
> 
>         I think I would be better off trying to understand how to
modify the 
>         AmiBroker plugin DLL to call back my AFL objective function.
> 
>         It is still Greek, but at least it is a simpler and
meaningful Greek 
>         phrase to start with.
> 
>         I would still welcome any specific advice or hints in areas
that I am 
>         likely to stumble over.
> 
>         Best regards,
>         Dennis
> 
>         > On Jul 7, 2008, at 7:10 AM, Tomasz Janeczko wrote:
>         >
>         > Yes, these sources are actually included in what you
already have on 
>         > your hard disk
>         > under ADK\CMAE\cmaes
>         >
>         > Best regards,
>         > Tomasz Janeczko
>         > amibroker.com
>         >> ----- Original Message -----
>         >> From: Paul Ho
>         >> To: amibroker@xxxxxxxxxxxxxxx
>         >> Sent: Monday, July 07, 2008 12:22 PM
>         >> Subject: RE: [amibroker] Code help please... Optimize
with CMAE
>         >>
>         >> Go to the guy's site where Tomasz download his source
code from, 
>         >> download his source code, and stare at that one instead,
I think 
>         >> its a lot closer to what you want.
http://www.bionik.tu-berlin.de/user/niko/cmaes_c.tar.gz
> 
>         >>
>         >>
>         >> On Jul 7, 2008, at 4:05 AM, Tomasz Janeczko wrote:
>         >>
>         >> Dennis,
>         >>
>         >> The optimizer plugin architecture uses backtester. You
can't go 
>         >> without using backtester.
>         >> Call to pfEvaluateFunc invokes full-blown backtest for given 
>         >> parameter set.
>         >>
>         >> If you would like to optimize "general purpose" functions
without 
>         >> using backtester,
>         >> you would need to take sources provided and write your
own plugin 
>         >> that won't
>         >> use backtesting engine at all.
>         >>
>         >> Best regards,
>         >> Tomasz Janeczko
>         >> amibroker.com
>         >> ----- Original Message -----
>         >> From: "Dennis Brown" <see3d@xxx>
>         >> To: <amibroker@xxxxxxxxxxxxxxx>
>         >> Sent: Monday, July 07, 2008 3:30 AM
>         >> Subject: [amibroker] Code help please... Optimize with CMAE
>         >>
>         >>
>         >>> Hello,
>         >>>
>         >>> I have been staring at the CMAE DLL stuff for days and I
really need
>         >>> some help to figure out how to use it in a particular way.
>         >>>
>         >>> I would like to use the optimizer in a generic sense to
do the
>         >>> following from AFL without using the internal
backtester, meaning 
>         >>> only
>         >>> AFL in indicator mode:
>         >>>
>         >>> Initialize 2 items:
>         >>> item 1 is X and has a default,min,max,step,current,best
values
>         >>> 1,1,1000,1,1,1
>         >>> item 2 is Y and has a default,min,max,step,current,best
values
>         >>> 1,1,1000,1,1,1
>         >>>
>         >>> The objective is to optimize X and Y so that X*Y==100
>         >>>
>         >>> objective function in AFL:
>         >>> function Objective()
>         >>> {
>         >>> return 100 - X*Y;
>         >>> }
>         >>>
>         >>> The steps I would need to take as I understand them are:
>         >>>
>         >>> 1. Initialize the X and Y OptimizeItems by calling
>         >>> OptimizerInit( with bunch of arguments) --most arguments are
>         >>> irrelevant to this test.
>         >>>
>         >>> 2. Start the optimizer engine by calling 
>         >>> pfEvaluateFunc( pContext ) --
>         >>> there really is no context that I understand for this test.
>         >>>
>         >>> 3. The DLL calls back for the objective AFL function
>         >>>
>         >>> 4. It runs step 3 a number of times to find the solution
of X=Y=10
>         >>>
>         >>> 5. OptimizerFinalize(same bunch of arguments as step 1)
>         >>>
>         >>> Of course I would prefer that step 3 is AFL calling the
optimizer 
>         >>> DLL
>         >>> instead (simple mode), but I did not think that is how
the CMAE 
>         >>> works.
>         >>>
>         >>> Anyway, if I could get this simple case to work, I am
sure I could
>         >>> figure out how do do much more complicated cases after
that on my 
>         >>> own.
>         >>>
>         >>> Of course if there is no way to use the existing DLL
without 
>         >>> changing
>         >>> it, I would like to know that also. I should be able to
make modest
>         >>> changes to the DLL myself.
>         >>>
>         >>> Please any hints or AFL code is appreciated.
>         >>>
>         >>> Best regards,
>         >>> Dennis
>         >>>
>



------------------------------------

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
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/