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

[amibroker] Re: Custom Optimization Metric



PureBytes Links

Trading Reference Links

I should probably add that the challenge now is not calculating the SD but how to load up an array with only monthly returns...



--- In amibroker@xxxxxxxxxxxxxxx, "bh.hicks" <bh.hicks@xxx> wrote:
>
> Mike,
> You are the man! Thank you! I have posted the code I ended up with to the bottom of this email in hopes it will help someone else someday but please be aware I am the worst "programmer" on the planet before using.  
> 
> Now onto StdDev of monthly returns.  Traders Studio has an object that stores monthly returns that I access like this...
> 
> NOTE: NOT AMIBROKER CODE
> 
> objReport = thisSession.MonthlyReport
> ReDim(Mreturn,objReport.ItemCount)
> 
> For i = 0 To objReport.ItemCount - 1
>     objPeriod = objReport.Period(i)
>     Mreturn[i]=objPeriod.PercentReturn
> Next
> 
> AvgMreturn=Average(Mreturn,i,0)
> StDevMreturn=StdDevS(Mreturn,i,0)
> Msharpe=(AvgMreturn-.0025)/StDevMreturn
> 
> I haven't been able to find something similar in AB.  Does it exist or is there another method I need to use to get at this data? Thanks again for any bones that can be thrown my way. I will do my best to leave the results behind for the next newbie who follows me.
> 
> --------------------------------------------------------------------
> MY AB Code to calculate Standard Deviation of Trades and use it to calculate T-score.
> 
> SetCustomBacktestProc("");
> 
> if( Status("action") == actionPortfolio )
> {
> bo = GetBacktesterObject();
> bo.Backtest(); 
> st = bo.GetPerformanceStats(0);
> 
> //Get Variables
> AvgTrade=st.GetValue("AllAvgProfitLossPercent");
> 
> // iterate through closed trades
> X = 0;
> N = 0;
> SumX = 0;
> SumX2 = 0;
> 
>    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
>    {
> 		X[N] = trade.GetPercentProfit();
> 		SumX=SumX+X[N];
> 		SumX2=SumX2+(X[N]^2);
>    		N++;
> 	}
> 
> // Custom Metric Caluclations
> TradeStdDev=sqrt((SumX2-((SumX)*(SumX)/N))/(N-1));
> TScore=sqrt(N)*AvgTrade/TradeStdDev;
> 
> // Here we add custom metric to backtest report
> bo.AddCustomMetric( "TradeStdDev",TradeStdDev);
> bo.AddCustomMetric( "T-Score",TScore);
> }
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "Mike" <sfclimbers@> wrote:
> >
> > I haven't verified any of this, but...
> > 
> > When you create your own arrays (e.g. TradeReturn[]), I believe that they get aligned with the market data of the current symbol (~~~Equity in this case). Assuming that you had a total of 20 trades over 1000 bars, you would have an array with values for the first 20 elements and zeroes (or Nulls?) for the remaining 980 bars. Your StDev call is operating on the last 3 bars, all of which will be zero, giving zero.
> > 
> > I believe that the answer would be for you to manually calculate the std. deviation of the trades yourself.
> > 
> > Then again, the problem might just be related to trying to add an array as custom metric as opposed to a scaler, in which case LastValue(TradeStdDev) would be the way to go. Experiment and see what you come up with.
> > 
> > Mike
> > 
> > P.S. Great start for someone new to AmiBroker!
> > 
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "bh.hicks" <bh.hicks@> wrote:
> > >
> > > Ok, I am making progress on this but getting hung up on the standard deviation calculations.  I have been through most of all 800+ posts that mention standard deviation but haven't been able to figure this out.  If anyone can offer some insight into why my TradeStDev is coming back empty, I would very much appreciate it as I barely have any hair left.
> > > 
> > > 
> > > SetCustomBacktestProc("");
> > > 
> > > if( Status("action") == actionPortfolio )
> > > {
> > > bo = GetBacktesterObject();
> > > bo.Backtest(); 
> > > st = bo.GetPerformanceStats(0);
> > > 
> > > //Get Variables
> > > WLratio=st.GetValue("PayoffRatio");
> > > WinPercent=st.GetValue("WinnersPercent")/100;
> > > PF=st.GetValue("ProfitFactor");
> > > NetProfit=st.GetValue("NetProfit");
> > > MaxDD=st.GetValue("MaxSystemDrawdown");
> > > TestYears=(EndValue( BarIndex() ) - BeginValue( BarIndex() ))/252;
> > > AvgTrade=st.GetValue("AllAvgProfitLossPercent");
> > > 
> > > // iterate through closed trades
> > > TradeReturn = 0;
> > > NumTrades = 0;
> > > 
> > >    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
> > >    {
> > > 		TradeReturn[NumTrades] = trade.GetPercentProfit();
> > > 		NumTrades++;
> > >    }
> > > 
> > > // Metrics
> > > Fscore=(((WLratio+1)*WinPercent)-1)/WLratio;
> > > MyF=Fscore*PF;
> > > DDR=(NetProfit/-MaxDD)*(TestYears/20);
> > > TradeStdDev=StDev(TradeReturn,3); 
> > > 
> > > // Here we add custom metric to backtest report
> > > bo.AddCustomMetric( "W/L Ratio",WLratio);
> > > bo.AddCustomMetric( "Win %",WinPercent);
> > > bo.AddCustomMetric( "ProfitFactor",PF);
> > > bo.AddCustomMetric( "F-Score",Fscore);
> > > bo.AddCustomMetric( "MyF",MyF);
> > > bo.AddCustomMetric( "DDR",DDR);
> > > bo.AddCustomMetric( "AvgTrade%",AvgTrade);
> > > bo.AddCustomMetric( "Test0",TradeReturn[0]);
> > > bo.AddCustomMetric( "Test1",TradeReturn[NumTrades-1]);
> > > bo.AddCustomMetric( "TradeStDev",TradeStdDev);
> > > }
> > > 
> > > 
> > > --- In amibroker@xxxxxxxxxxxxxxx, "bh.hicks" <bh.hicks@> wrote:
> > > >
> > > > I have been a long time Traders Studio user and am beginning the process of migrating everything over to AmiBroker.  One of the more useful pieces of code I wrote for Traders Studio was a custom optimization metric that I would like to reproduce in AmiBroker.  I am a little overwhelmed at all the information available and would very much appreciate it if someone could point me towards some sources of info that will allow me to accomplish this.
> > > > 
> > > > The ranking metric does the following:
> > > > 
> > > > Msharpe=(AvgMreturn-.0025)/StDevMreturn (m refers to monthly)
> > > > Tscore=Sqr(TradeCount)* AvgTrade/TradeStdDev (std dev. is of % returns of each trade)
> > > > Fscore=(((WLratio+1)*WinPercent)-1)/WLratio (WLratio is W/L ratio)
> > > > MyF=Fscore*ProfitFactor
> > > > DDR=(NetProfit/MaxDD)*(YearsTested/20)
> > > > 
> > > > CustomScore=Msharpe*MyF*Tscore*DDR
> > > > 
> > > > This did a pretty good job allowing me to quickly rank optimization runs on the criteria that is important to me.
> > > > 
> > > > So although new to AmiBroker, I can work my myself around some moderately difficult code and Trader Studio's object-oriented back end but I really just don't know where to start to dig up how to access this data during backtests an optimization runs. 
> > > > 
> > > > Thanks for any help.
> > > > B
> > > >
> > >
> >
>




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

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

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/