Hello,
I am trying to write a system that risks always 0.75% of CURRENT EQUITY per trade and uses ATR stops, which means that I need the custom backtester to access actual equity.
The problem is that I can't access the ATR value in the signal object, where I could define the position size.
Here is the description that shows how it should work when it is finished:
1.) RiskPerContract = 2*ATR(50) x PointValue x (1/TickSize);
2.) Contracts = (CurrentEquity x 0.75%) / RiskPerContract;
3.) PositionSize = Contracts x MarginDeposit;
I have already tried a lot and can't find any solution. Please help if possible. See my code below. For simplicity i have just tried to access the ATR value. It seems that AmiBroker simply ignores the value of
sig.PosSize and instead uses full equity for the trade.
Thanks in advance !
Kind regards,
Thomas
SetCustomBacktestProc(""
);
if( Status
("action") == actionPortfolio )
{
bo = GetBacktesterObject
();
bo.PreProcess();
for( bar =
0; bar < BarCount; bar++)
{
CurrentPortfolioEquity = bo.Equity;
for( sig =
bo.GetFirstSignal(bar); sig; sig = bo.GetNextSignal(bar))
{
sym = sig.Symbol;
SetForeign(sym);
MyATR = Ref
(2*
ATR(50),-
1);
RestorePriceArrays
();
if( CurrentPortfolioEquity >
0 ) sig.PosSize = MyATR[bar];
else
sig.PosSize = 0;
}
bo.ProcessTradeSignals(bar);
}
bo.PostProcess();
}
/***********************************************************************/
// sample rules:
Buy = Cross
( CCI(), 100
);
Sell = Cross
( 100, CCI
() );
Short = Cross
( -100, CCI
() );
Cover = Cross
( CCI(), -100
);
/***********************************************************************/
SetOption("InitialEquity"
,100000);
SetOption("FuturesMode"
,True);
PointValue = PointValue * (
1/TickSize);
RoundLotSize = 1
;
MarginDeposit = 1000
;
/***********************************************************************/
_SECTION_BEGIN("Price"
);
SetChartOptions(0
,chartShowArrows|chartShowDates);
_N(Title = StrFormat
("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}"
, O, H, L, C, SelectedValue
( ROC( C, 1
) ) ));
Plot( C, "Close"
, ParamColor(
"Color", colorBlack ), styleNoTitle | ParamStyle
("Style") | GetPriceStyle
() );
_SECTION_END();
/***********************************************************************/