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

[amibroker] Help Code Please - Regular vs. Custom Backtester



PureBytes Links

Trading Reference Links

I know the Custom Backtester Interface "is provided for advanced users
only", which I'm obviously not.

But as a learning exercise I'd like to replicate the result of
following positionsizing method using the Custom Backtester Interface:

PositionSize = MarginDeposit * ( -100 * RiskPercent * 0.01 ) /  ( MA(
ATR( 1 ), ATRLength ) * PointValue );

>From the previous Tomasz's post, I learned :

"There are TWO phases" and I have to use "a variable that is
calculated in phase 2 (i.e. inside "if( status("action") ==
actionPortfolio
statement)"

So I calculated ATR inside "if( status("action") == actionPortfolio
statement)".

As an indicator, my poorly-written code matches MA( ATR( 1 ), ATRLength ).

But I got "Out of Range" error message in local language when I ran
the following code.

Is anybody kind enough to correct my code ?

=============================

RiskPercent = 2;
ATRLength = 14;

// PositionSizing

SetOption( "UseCustomBacktestProc", True );

if ( Status("action") == actionPortfolio )
{
	bo = GetBacktesterObject();
	bo.PreProcess();

	NumContracts = 0;
	ATRn = 0;
	TR = 0;
	SumTR = 0;

	for (bar = 1; bar < BarCount; bar++)
	{

		// ATR ( Simple Moving Average ) Calculation
		TR[ bar ] = Max( High[ bar ] - Low[ bar ], Max( Close[ bar - 1 ] -
Low[ bar ], High[ bar ] - Close[ bar - 1 ] ) );
		for ( jj = 0; bar > ATRLength AND jj < ATRLength; jj++ )
		{
			SumTR[ bar ] = TR[ bar - jj ] + SumTR[ bar ];
		}
		ATRn[ bar ] = SumTR[ bar ] / ATRLength;



		for (sig = bo.GetFirstSignal(bar); sig; sig = bo.GetNextSignal(bar))
		{

			if ( sig.isEntry() )
			{

				EQ[ bar ] = bo.equity;

				/*
				if ( bar <= ATRLength )
					ATRn[ bar ] = 999999;
				*/

				NumContracts[ bar ] = ( EQ[ bar ] * RiskPercent * 0.01 ) / ( ATRn[
bar ] * sig.PointValue );

				if ( NumContracts[ bar ] > 1 )
					NumContracts[ bar ] = floor( NumContracts[ bar ] );
				else
					NumContracts[ bar ] = 1;

				sig.PosSize    = sig.MarginDeposit * NumContracts[ bar ];
			}
		}
		bo.ProcessTradeSignals(bar);
	}
	bo.PostProcess();
}

// Trading System 

Top = BBandTop( Close, 20, 2 );
Bot = BBandBot( Close, 20, 2 );

BuyCondition = Cross( Close, Top );
ShortCondition = Cross( Bot, Close );

Buy = BuyCondition;
Short = ShortCondition;
Cover = Buy;
Sell = Short;

BuyPrice = Open;
ShortPrice = Open;
SellPrice = Open;
CoverPrice = Open;

//PositionSize = MarginDeposit * ( -100 * RiskPercent * 0.01 ) /  (
MA( ATR( 1 ), ATRLength ) * PointValue );

Equity( 1 );





------------------------ Yahoo! Groups Sponsor --------------------~--> 
<font face=arial size=-1><a href="http://us.ard.yahoo.com/SIG=12hm6khoh/M=362343.6886682.7839641.1493532/D=groups/S=1705632198:TM/Y=YAHOO/EXP=1123754723/A=2894354/R=0/SIG=11qvf79s7/*http://http://www.globalgiving.com/cb/cidi/c_darfur.html";>Help Sudanese refugees rebuild their lives through GlobalGiving</a>.</font>
--------------------------------------------------------------------~-> 

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

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