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

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



PureBytes Links

Trading Reference Links

Ozzy, all -

I got a little sidetracked this weekend but had said that I'd post a
solution to detecting IS (In-sample) and OOS (out-of-sample).  So,
here it is.  I would appreciate that it not be posted elsewhere,
because I plan to mod it slightly and post it along with some signal
tutorials to another site in the near future.  And, I really hope that
Tomasz makes this status property available in the near future and
does away with the need for this.  But, I had to do this a while back
for market timing signal work that I was doing.

First, I'll post the include file that contains the function
OptSampleType().  This is meant to be called in every pass and returns
a code to indicate IS or OOS.  I'll make a follow up post that shows a
simple 2 MA signal example that uses it to trace buy/sell signals of
the OOS segments.

For the code geeks, I'll mention briefly that it works using the
ActionEx codes and date ranges to drive a simple state machine model
of the optimization passes.  I'd really suggest just looking at it as
a black-box function that returns the codes documented in the
comments, though.


//--------------------------------------------------------------------------------------------------
//
//  OptSampleType - Bruce R
//
//  Routine to return a test type for walk forward testing -
//
//       0 - Unknown
//       1 - In sample / out of sample range setup
//       2 - In sample
//       3 - Out of sample
//
//  This is a bit of a hack since Amibroker does not return an
extended status
//  for this.  It is based on a simple state machine that tracks the
following 
//  sequence for an in sample / out of sample pass
//  
//       1. Setup for the in sample date range
//       2. Backtest on the in sample date range for each ticker
//       3. Backtest on the in sample date range for the ~~~EQUITY symbol 
//       4. Repeat 2 - 3 multiple times for optimization
//       5. Backtest on the out of sample date range for each ticker
//       6. Backtest on the out of sample date range for the ~~~EQUITY
symbol
//       7. Repeat 1 - 6 for each walk forward period 
//
//
//--------------------------------------------------------------------------------------------------

function OptSampleType( )
{
	actex			= Status( "actionex" );

	//  Set the default result
	res				= 0;

	//  Process the action type - 13, 14, 15
	switch ( actex )
	{
		//  Setup this optimization phase
		case actionExOptimizeSetup:
			StaticVarSet( "_optsamptrig", True );
			StaticVarSet( "_lastsampfrom", 0 );
			StaticVarSet( "_lastsampto", 0 );
			res				= 1;
			break;

		//  May be alternating backtest / custom backtest procedure
		case actionExOptimizeBacktest:
		case actionExOptimizePortfolio:
			//  Get the current date range
			rangefrom			= Status( "rangefromdate" );
			rangeto			= Status( "rangetodate" );
			//  Was it a backtest after a setup trigger
			if ( StaticVarGet( "_optsamptrig" ) )
			{
				//  In sample
				StaticVarSet( "_optsamptrig", False );
				res			= 2;
			}
			else
			{
				//  Was the date range different
				if ( rangefrom != StaticVarGet( "_lastsampfrom" )
						AND rangeto != StaticVarGet( "_lastsampto" ) )
				{
					//  Out of sample
					res			= 3;
				}
				else
				{
					//  Same sample phase
					res			= StaticVarGet( "_lastres" );
				}
			}
			
			//  Reset the trigger
			StaticVarSet( "_optsamptrig", False );

			//  Set the last date range
			StaticVarSet( "_lastsampfrom", rangefrom );
			StaticVarSet( "_lastsampto", rangeto );
			break;
	}

	StaticVarSet( "_lastres", res );
	return res;
}



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

**** 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/