PureBytes Links
Trading Reference Links
|
Hi,
I have not tested for correctness. But, to do what you are trying to do, I believe that you would have to use mid level backtester (i.e. PreProcess, ProcessTradeSignals, PostProcess) not the high level backtester (i.e. Backtest).
Following your example; Get the stats at each bar and store the values in a composite, since indicator mode (i.e. charting) would not see array values populated during portfolio mode (i.e. backtest).
Finally, use Foreign to do the plotting.
Mike
SetTradeDelays( 0, 0, 0, 0 ); SetPositionSize( 5, spsPercentOfEquity );
weekDay = DayOfWeek();
Buy = weekDay == 1; BuyPrice = Open;
Sell = weekDay == 2; SellPrice = Close;
Short = weekDay == 3; ShortPrice = Open;
Cover = weekDay == 4; CoverPrice = Close;
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) { bo = GetBacktesterObject(); bo.PreProcess();
totalProfit = longProfit = shortProfit = 0;
for ( i = 0; i < BarCount; i++ ) { bo.ProcessTradeSignals( i );
stats = bo.GetPerformanceStats( 0 ); stats_long = bo.GetPerformanceStats( 1 ); stats_short = bo.GetPerformanceStats( 2 );
totalProfit[i] = stats.getValue( "EndingCapital" ) - stats.GetValue( "InitialCapital" ); longProfit[i] = stats_long.getvalue( "EndingCapital" ) - stats.GetValue( "InitialCapital" ); shortProfit[i] = stats_short.getvalue( "EndingCapital" ) - stats.GetValue( "InitialCapital" ); }
bo.PostProcess();
AddToComposite( totalProfit, "~TotalProfit", "X", atcFlagDefaults | atcFlagEnableInPortfolio ); AddToComposite( longProfit, "~LongProfit", "X", atcFlagDefaults | atcFlagEnableInPortfolio ); AddToComposite( shortProfit, "~ShortProfit", "X", atcFlagDefaults | atcFlagEnableInPortfolio ); }
Plot( Close, "Close", colorDarkGrey, styleBar );
Plot( Foreign( "~TotalProfit", "Close" ), "Total Profit", colorBlue, styleLeftAxisScale ); Plot( Foreign( "~LongProfit", "Close" ), "Long Profit", colorGreen, styleLeftAxisScale ); Plot( Foreign( "~ShortProfit", "Close" ), "Short Profit", colorRed, styleLeftAxisScale );
--- In amibroker@xxxxxxxxxxxxxxx, "j0etr4der" <j0etr4der@xxx> wrote: > > Hello, > > In the User's Guide Backtester Interface section, Stat object is says, > > "Metrics are usually calculated once backtest is completed but it is also possible to calculate metrics during backtest. To calculate current metrics and get the access to them simply call GetPerformanceStats method of Backtester object. Please note that if you calculate statistics in the middle of the backtest they will include only closed trades." > > I was hoping I could get access to the current long and short capital stats. The following code produces the correct results in AA window following optimization, but does not plot on the chart (values remain 0 ). > > Is this feasible in any way? Or do I have to figure out (not likely at my skill level) how to control trades in the backtester procedure? > > TIA, > > Joe > > ////////////////////////////////////////////////////////// > Sp = Lp = 0; > > SetCustomBacktestProc( "" ); > if ( Status( "action" ) == actionPortfolio ) > { > bo = GetBacktesterObject(); > bo.Backtest(); > > stats = bo.GetPerformanceStats( 0 ); > stats_long = bo.GetPerformanceStats( 1 ); > stats_short = bo.GetPerformanceStats( 2 ); > > Longprofit = stats_long.getvalue( "EndingCapital" ) > - stats.GetValue( "InitialCapital" ); > > bo.AddCustomMetric( "Long profit", Longprofit ); > > Shortprofit = stats_short.getvalue( "EndingCapital" ) > - stats.GetValue( "InitialCapital" ); > > bo.AddCustomMetric( "Short profit", Shortprofit ); > > for ( i = 0; i < BarCount - 1; i++ ) > { > Lp[ i ] = Longprofit; > Sp[ i ] = Shortprofit; > } > } > > Plot( Lp, "Long profit", colorBlack ); > Plot( Sp, "Short profit", colorBlack ); >
__._,_.___
**** 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/
__,_._,___
|