Search the web
Sign In
New User? Sign Up
amibroker · AmiBroker User's List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
how to add custom colume in backtest repor   Message List  
Reply | Forward Message #139980 of 144687 |
Re: how to add custom colume in backtest repor

dear mike,

thank you for your help to clarify the matter.
but still i am getting blank colom in bactest report.
i am tring it for single symbol only.
help me.

asit.

--- In amibroker@yahoogroups.com, "Mike" <sfclimbers@...> wrote:
>
>
> 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@yahoogroups.com, "asitasu" <asitasu@> 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)*ShortPri\
> ce,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@yahoogroups.com, "Mike" sfclimbers@ wrote:
> > >
> > > See "custom metrics" in the user guide:
> > >
> > > http://www.amibroker.com/guide/a_custommetrics.html
> > >
> > > Mike
> > >
> > > --- In amibroker@yahoogroups.com, "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.
> > > >
> > >
> >
>





Mon Jul 6, 2009 4:12 am

asitasu
Offline Offline
Send Email Send Email

Forward
Message #139980 of 144687 |
Expand Messages Author Sort by Date

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 ...
asitasu
Offline Send Email
Jul 4, 2009
3:40 am

See "custom metrics" in the user guide: http://www.amibroker.com/guide/a_custommetrics.html Mike...
Mike
sfclimbers
Offline Send Email
Jul 4, 2009
4:17 am

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...
asitasu
Offline Send Email
Jul 5, 2009
5:16 am

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...
Mike
sfclimbers
Offline Send Email
Jul 5, 2009
7:08 pm

dear mike, thank you for your help to clarify the matter. but still i am getting blank colom in bactest report. i am tring it for single symbol only. help me. ...
asitasu
Offline Send Email
Jul 6, 2009
4:12 am

Hi, I ran the code on symbol AAV from 1/1/07 to 12/31/08 and it generated the extra columns with values. I have not made any attempt to validate the values,...
Mike
sfclimbers
Offline Send Email
Jul 6, 2009
7:27 am

dear mike, 10000 thank you for clarifing my concept. with your idea with small change as follow i got the desired result. if ( trade.IsLong() ) { SetForeign(...
asitasu
Offline Send Email
Jul 6, 2009
12:17 pm

Great, glad to hear that it is now working for you. However, you should not have had to use C[entryBar]. It is OK to use it, but you should not have had to....
Mike
sfclimbers
Offline Send Email
Jul 6, 2009
5:13 pm

Are you sure you are actually getting results for each trade, or the same result for each trade? I would have thought you would be only getting the last values...
Graham
kavemanperth
Offline Send Email
Jul 7, 2009
12:48 am

dear graham, i am getting purfect output for individula trades. asit....
asitasu
Offline Send Email
Jul 7, 2009
3:53 am

Graham, When passing true as the last argument to SetForeign, you actually do get ButPrice and ShortPrice (that's what the doc says anyway). The other arrays...
Mike
sfclimbers
Offline Send Email
Jul 7, 2009
4:56 am

OK, did not realise that would also work in CBT Live and learn, a continuous process -- Cheers Graham Kav AFL Writing Service http://www.aflwriting.com...
Graham
kavemanperth
Offline Send Email
Jul 7, 2009
6:19 am

http://www.youtube.com/watch?v=dd4_nqCWihA O:-)...
Mubashar Virk
chmav
Offline Send Email
Jul 7, 2009
7:09 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help