PureBytes Links
Trading Reference Links
|
Hi,
There are a few errors with your second example.
The first error is that in your second example "risk" is an array. When calling "trade.addcustommetric(...)", you must pass a scaler (i.e. a single value) not an array. Notice that in your first example risk was a scaler.
The second error is that in your second example "risk" gets recalculated for every symbol. So, when referring to "risk" in your custom backtester code, which symbol's "risk" are you expecting to get? The active symbol during custom backtesting is "~~~Equity", so you will be getting the "risk" for ~~~~Equity, which is not what you want. Notice that in your first example "risk" was a fixed value.
To solve both problems, you must recalculate risk for each trade using the symbol of that trade, and using the values from the entry bar of that trade (i.e. calculate using scalers instead of entire array).
Your custom backtester code would then look something like this:
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) { bo = GetBacktesterObject(); bo.backtest( 1 ); Sumprofitperrisk = 0; numtrades = 0; bars = BarIndex(); dates = DateTime();
for ( trade = bo.getfirsttrade(); trade; trade = bo.getnexttrade() ) { entryBar = LastValue( ValueWhen( trade.EntryDateTime == dates, bars ) );
if (trade.IsLong()) { SetForeign(trade.Symbol, true, true); pw = TimeFrameGetPrice( "l", inWeekly, -1 ); qw = TimeFrameGetPrice( "l", inWeekly, -2 ); wqs = Min( pw, qw ); bstopamount = BuyPrice - ( wqs - 1 ); risk = bstopamount[entryBar] * BuyPrice[entryBar]; RestorePriceArrays(true); } else { SetForeign(trade.Symbol, true, true); yw = TimeFrameGetPrice( "h", inWeekly, -1 ); zw = TimeFrameGetPrice( "h", inWeekly, -2 ); wzs = Max( yw, zw ); sstopamount = ( wzs + 1 ) - ShortPrice; risk = sstopamount[entryBar] * ShortPrice[entryBar]; RestorePriceArrays(true); }
rmultiple = trade.getprofit() / risk; trade.addcustommetric( "initial risk $" , risk ); trade.addcustommetric( "R-multiple" , rmultiple ); Sumprofitperrisk = Sumprofitperrisk + rmultiple ; numtrades++; }
expectancy3 = Sumprofitperrisk / numtrades ;
bo.addcustommetric( "Expectancy (per risk)", expectancy3 ); bo.listtrades(); }
I have not tested the code above, so make sure to test it out before accepting it. Also, I have not tried to verify whether or not your calculations are valid. I just copied what you already had and moved the calculation of "risk" to inside the backtester.
Mike
--- In amibroker@xxxxxxxxxxxxxxx, "asitasu" <asitasu@xxx> wrote: > > dear mike, > > i understood where you want me to direct. for you reference i am giving full afl for my system which works fine giving me two extra coloms of initial risk & expectancy/trade coloumwise. but when i try to change risk perameter as shown in following afl i get blnak colom of initial risk & expectancy/trade. so i wnt to know how to get initial risk coloum when using risk = enty - stop value. > > afl. > Capital = 100000; > SetOption("InitialEquity", Capital ); > RoundLotSize = 1; > possize = 0.8*Capital; > allocationrisk = 0.8; // max capital employed per trade > risk = 0.05*Capital; // % max risk per trade > /* calling custom backtest*/ > SetCustomBacktestProc(""); > if( Status("action") == actionPortfolio ) > { > bo = GetBacktesterObject(); > bo.backtest(1); > Sumprofitperrisk = 0; > numtrades = 0; > // iterate for closed trades > for( trade = bo.getfirsttrade(); trade; trade = bo.getnexttrade() ) > { > rmultiple = trade.getprofit()/risk; > trade.addcustommetric("initial risk $" , risk ); > trade.addcustommetric("R-multiple" , rmultiple ); > Sumprofitperrisk = Sumprofitperrisk + rmultiple ; > numtrades++; > } > expectancy3 = Sumprofitperrisk / numtrades ; > bo.addcustommetric( "Expectancy (per risk)", expectancy3 ); > bo.listtrades(); > } > > xb = IIf(C > EMA(C,7),20,0); > xs = IIf(C < EMA(C,7),-20,0); > y = TimeFrameGetPrice("h",inMonthly,-1); > z = TimeFrameGetPrice("h",inMonthly,-2); > p = TimeFrameGetPrice("l",inMonthly,-1); > q = TimeFrameGetPrice("l",inMonthly,-2); > r = TimeFrameGetPrice("h",inMonthly); > s = TimeFrameGetPrice("l",inMonthly); > wz = Max(y,z); > zb = IIf(r > wz ,30,0); > wq = Min(p,q); > zs = IIf(s < wq,-30,0); > yw = TimeFrameGetPrice("h",inWeekly,-1); > zw = TimeFrameGetPrice("h",inWeekly,-2); > pw = TimeFrameGetPrice("l",inWeekly,-1); > qw = TimeFrameGetPrice("l",inWeekly,-2); > rw = TimeFrameGetPrice("h",inWeekly); > sw = TimeFrameGetPrice("l",inWeekly); > wzs = Max(yw,zw); > yb = IIf(rw > wzs ,25,0); > wqs = Min(pw,qw); > ys = IIf(sw < wqs,-25,0); > score = xb+yb+zb+xs+ys+zs; > avrage = MA(V,30); > diffvol = (V-avrage)/avrage; > Buy = C > Ref(HHV(H,2),-1) AND C>O AND score > 70 ; > BuyPrice = C; > bstopamount = BuyPrice-(wqs - 1); > Sell = L < wqs; > SellPrice = wqs - 1; > ExRem(Buy,Sell); > Short = C < Ref(LLV(L,2),-1) AND C<O AND score < -70 ; > ShortPrice = C ; > sstopamount = (wzs +1) - ShortPrice; > Cover = rw > wzs ; > CoverPrice = wzs + 1; > SetPositionSize( 100, spsShares ) ; > ExRem(Short,Cover); > //PositionSize =IIf(Buy, Min((risk/bstopamount)*BuyPrice,possize),Min((risk/sstopamount)*ShortPrice,possize)); > //PositionScore = PositionSize ; //(V- MA(V,7))/MA(V,7) ; OR ma(v,5)/ma(v,20); prefer stocks that High vol thrust; > //ApplyStop(0,1,4,1); > > thisk works fine. > > but following chane gives blank coloum. > > Capital = 100000; > SetOption("InitialEquity", Capital ); > RoundLotSize = 1; > xb = IIf(C > EMA(C,7),20,0); > xs = IIf(C < EMA(C,7),-20,0); > y = TimeFrameGetPrice("h",inMonthly,-1); > z = TimeFrameGetPrice("h",inMonthly,-2); > p = TimeFrameGetPrice("l",inMonthly,-1); > q = TimeFrameGetPrice("l",inMonthly,-2); > r = TimeFrameGetPrice("h",inMonthly); > s = TimeFrameGetPrice("l",inMonthly); > wz = Max(y,z); > zb = IIf(r > wz ,30,0); > wq = Min(p,q); > zs = IIf(s < wq,-30,0); > yw = TimeFrameGetPrice("h",inWeekly,-1); > zw = TimeFrameGetPrice("h",inWeekly,-2); > pw = TimeFrameGetPrice("l",inWeekly,-1); > qw = TimeFrameGetPrice("l",inWeekly,-2); > rw = TimeFrameGetPrice("h",inWeekly); > sw = TimeFrameGetPrice("l",inWeekly); > wzs = Max(yw,zw); > yb = IIf(rw > wzs ,25,0); > wqs = Min(pw,qw); > ys = IIf(sw < wqs,-25,0); > score = xb+yb+zb+xs+ys+zs; > avrage = MA(V,30); > diffvol = (V-avrage)/avrage; > Buy = C > Ref(HHV(H,2),-1) AND C>O AND score > 70 ; > BuyPrice = C; > bstopamount = BuyPrice-(wqs - 1); > Sell = L < wqs; > SellPrice = wqs - 1; > ExRem(Buy,Sell); > Short = C < Ref(LLV(L,2),-1) AND C<O AND score < -70 ; > ShortPrice = C ; > sstopamount = (wzs +1) - ShortPrice; > Cover = rw > wzs ; > CoverPrice = wzs + 1; > SetPositionSize( 100, spsShares ) ; > ExRem(Short,Cover); > risk = IIf(Buy, bstopamount*BuyPrice,sstopamount*ShortPrice); // max risk per trade > /* calling custom backtest*/ > SetCustomBacktestProc(""); > if( Status("action") == actionPortfolio ) > { > bo = GetBacktesterObject(); > bo.backtest(1); > Sumprofitperrisk = 0; > numtrades = 0; > // iterate for closed trades > for( trade = bo.getfirsttrade(); trade; trade = bo.getnexttrade() ) > { > rmultiple = trade.getprofit()/risk; > trade.addcustommetric("initial risk $" , risk ); > trade.addcustommetric("R-multiple" , rmultiple ); > Sumprofitperrisk = Sumprofitperrisk + rmultiple ; > numtrades++; > } > expectancy3 = Sumprofitperrisk / numtrades ; > bo.addcustommetric( "Expectancy (per risk)", expectancy3 ); > bo.listtrades(); > } > > can any one help. > > asit. > > --- In amibroker@xxxxxxxxxxxxxxx, "Mike" sfclimbers@ wrote: > > > > See "custom metrics" in the user guide: > > > > http://www.amibroker.com/guide/a_custommetrics.html > > > > Mike > > > > --- In amibroker@xxxxxxxxxxxxxxx, "asitasu" <asitasu@> wrote: > > > > > > dear frieds, > > > > > > i want to add extra collume of risk(no % risk) in backtest report how to add this by use of afl? i want risk to be calculated on base of trade > > > entry & stop loss price. say for example i enter at Rs 2515 and at time of entry my stop loss is Rs 2375.50, than risk per unit is 139.5(ie 2515 - 2375.50). > > > > > > help me. > > > > > > asit. > > > > > >
__._,_.___
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
__,_._,___
|