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

[amibroker] Re: Detecting In-Smaple and Out-of-Sample Walk Forward



PureBytes Links

Trading Reference Links

Part II -

I mentioned that I had needed to detect IS/OOS in walk-forward testing
for signal work.  It is actually needed for several applications, and
again I hope that Tomasz adds the status property.  Below is a brief,
demo example that might be of use.  Side note - Yahoo really sucks for
doing this !  At least, I've met my yearly posting target for 2009 :-)

It is a very simple 2MA crossover optimization that was used to
initially test OptSampleType().  It is NOT meant to be practical.  It
can be run, for example, on the SP500 in "Easy mode" as follows -

Start - 12/31/2001
End - 12/31/2002
Last - 9/30/2003
Step - 3 months
NOT anchored
Optimization target - CAR/MDD

These parameters don't have to be used, but this example shows some
interesting tidbits.  For example, it show the limitations in
transitioning from a bear to a bull period with limited lookback.  It
also shows some boundary effects in the walk-forward mode.  But these
are stories for another day.

Anyway, here's a snippet of the _Trace output -

Name = SP-CP , ActionEx = 14 , From = 6/30/2003 , To = 9/30/2003 ,
OptSampleType = 3
  Fast = 45 , Slow = 49
  Signals - 
    BUY  - 6/30/2003
    SELL - 8/15/2003
    BUY  - 8/27/2003
    SELL - 9/10/2003
    BUY  - 9/17/2003
Name = SP-CP , ActionEx = 14 , From = 9/30/2003 , To = 12/31/2003 ,
OptSampleType = 3
  Fast = 97 , Slow = 75
  Signals - 
    SELL - 9/30/2003

Finally, the code -

//--------------------------------------------------------------------------------------------------
//
//  Simple 2MA Optimization.afl - 1/18/09
//
//  Example to show use of OptSampleType() function.  Note that this
is meant as a
//  non-practical example that demonstrates a special case and a
boundary condition
//  related to signals in a walk-forward optimization.
//
//--------------------------------------------------------------------------------------------------

#include <OptSampleType.afl>

//  Get the optimization pass type - this must be outside of any
conditionals
samptype			= OptSampleType( );

//  Test routine for OptSampleType

//  Choose an engine and set a simple, short optimization
OptimizerSetEngine( "spso" );			//  cmae, trib
OptimizerSetOption( "Runs", 2 );
OptimizerSetOption( "MaxEval", 500 );

//  Simple 2 MA crossover system
fast			= Optimize( "Fast MA", 13, 1, 100, 1 );
slow			= Optimize( "Slow MA", 55, 1, 100, 1 );
Buyimp			= Cross( MA( C, fast ), MA( C, slow ) );
Sellimp		= Cross( MA( C, slow ), MA( C, fast ) );
Short 			= Cover = 0;

//  Handle a signal in progress at the start of the period - note that
//  this will introduce a few redundant signals (if first signal of OOS
//  period is the same as the last signal of the last OOS period).  These
//  will have to be filtered later, but it is the easiest way for this
demo
Buystate		= Flip( Buyimp, Sellimp );
Sellstate		= NOT Buystate;
fbir			= Status( "firstbarinrange" );
Buy			= Buyimp OR fbir * Buystate;
Sell			= Sellimp OR fbir * Sellstate;
 //--------------------------------------------------------------------------------------------------

//  Debug output
if ( samptype == 3 )
{
	_TRACE	( "Name = " + StrLeft( Name( ) + "     ", 5 )
				+ " , ActionEx = " + Status( "actionex" )
				+ " , From = " + NumToStr( DateTimeConvert( 2, Status(
"rangefromdate" ) ), formatDateTime )
				+ " , To = " + NumToStr( DateTimeConvert( 2, Status( "rangetodate"
) ), formatDateTime )
				+ " , OptSampleType = " + samptype
	 		);
	_TRACE( "  Fast = " + fast + " , Slow = " + slow );
}

//  Show the signals for the out-of-sample period
if ( samptype == 3 )
{
	bi			= BarIndex( );
	bi			= bi - bi[ 0 ];
	dt			= DateTime( );
	fbi			= LastValue( ValueWhen( Status( "firstbarinrange"), bi ) );
	lbi			= LastValue( ValueWhen( Status( "lastbarinrange" ), bi ) );
	_TRACE( "  Signals - " );
	for ( i = fbi; i <= Lbi; i++ )
	{
		if ( Buy[ i ] )
			_TRACE( "    BUY  - " + NumToStr( dt[ i ], formatDateTime ) );
		if ( Sell[ i ] )
			_TRACE( "    SELL - " + NumToStr( dt[ i ], formatDateTime ) );
	}
}

//--------------------------------------------------------------------------------------------------



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

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL 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/