If you want to export portfolio equity to file then use this
Set symbol to ~~~EQUITY and choose periodicity in backtest preferences
Then click explore. Output is in C:\ drive.
if ( Status( "action" ) == actionExplore )
{
fh = fopen( "C:\\Equity.csv", "w" );
if ( fh )
{
fputs( "Symbol,Date,Time,Close\n", fh );
dn = DateNum();
tn = TimeNum();
for ( i = 0; i < BarCount; i++ )
{
Line = Name() + StrFormat( ",%06.0f,%06.0f,%g\n,",
dn[ i ] % 1000000,
tn[ i ],
C[ i ] );
fputs( Line, fh );
}
fclose( fh );
}
}
Filter = Status( "lastbarinrange" );
AddTextColumn( "Export done", "Status" ); 本帖最後由 joshsmi 於 13-5-22 23:32 編輯
To export individual equity to Excel then use similar code to the below one.
Choose periodcity in Backtest preferences, choose From-To range and click 'Scan'
Code tested with Office 2010! You will have 2 charts and one data sheet in Excel.
Keep in mind that if you have large data set then output is slow because of Microsoft OLE and vbscript.
//------------------------------------------------------------------------------
//
//Formula Name: AFL-Excel
//Author/Uploader: Witold Dabrowski, edited by joshsmi 2013
//E-mail:
//Date/Time Added: 2002-08-31 06:29:37
//Origin:
//Keywords:
//Level: semi-advanced
//Flags: exploration
//Formula URL: http://www.amibroker.com/library/formula.php?id=218
//Details URL: http://www.amibroker.com/library/detail.php?id=218
//
//------------------------------------------------------------------------------
//
//This example shows how to control Excel directly from AFL script via
//OLE-Automation interface. In this script I count %DD and plot Equity and %DD charts in Excel.
//
//To run this code just click 'Scan', not 'Explore'. Exploration is displayed in Excel.
//------------------------------------------------------------------------------
SetOption( "FuturesMode", False );
SetOption( "InitialEquity", 10000 );
// test system start
MA_= MA( C, 20 );
Buy= Cover =Cross( C, MA_ );
Sell = Short = Cross( MA_, C );
//PositionSize = IIf( Buy, BuyPrice, ShortPrice );
SetPositionSize( 25, spsPercentOfEquity );
// test system end
// Equity function refers to individual equity, not portfolio equity
Eq = Equity( 1, -1 ); // flag 1: works as 0 but additionally updates buy/sell/short/cover arrays
// so all redundant signals are removed exactly as it is done internally by the
// backtester plus all exits by stops are applied so it is Now possible to visualise ApplyStop() stops.
// range type -1: (default) use range set in the Automatic analysis window
Nam= Name();
dn = DateNum();
tn = TimeNum();
bi = BarIndex();
fbr= LastValue( ValueWhen( Status ( "firstbarinrange" ), bi ) );
// vbscript start
// remember Windows OLE and vbscript are slower
EnableScript("vbscript");
<%
' // Replacing the variables between AFL and VB script
Eq= AFL( "Eq" )
Nam = AFL( "Nam" )
dn= AFL( "dn" )
tn= AFL( "tn" )
fbr = AFL( "fbr" )
' // Starting Excel
Set Excel = CreateObject( "Excel.Application" )
' // Disable confirmation messages of Excel
Excel.DisplayAlerts = False
' // Setting Excel in the state of readiness
Excel.SheetsInNewWorkbook = 1 ' // number of data sheets
Excel.WorkBooks.Add
Set Sheet = Excel.WorkBooks( 1 ).WorkSheets( 1 )
' // Writing data sheet Title
Sheet.Cells( 1, 1 ) = Nam
Sheet.Cells( 1, 2 ) = "DateNum"
Sheet.Cells( 1, 3 ) = "TimeNum"
Sheet.Cells( 1, 4 ) = "Equity"
Sheet.Cells( 1, 5 ) = "DrawDown"
' // set data sheet name
Excel.Activeworkbook.Sheets( 1 ).Name = Nam + " Data"
' // Initiation of Equity maximum - will be needed to calculate DrawDown
MaxE = Eq( 0 )
' // Writing of Equity, DrawDown, Date and Time to Excel
for i = fbr to UBound( Eq )
Y = i + 2 - fbr
' // Datenum
Date_num = dn( i ) Mod 1000000
Sheet.Cells( Y, 2 ).Value = Date_num
' // Timenum
Time_num = tn( i )
Sheet.Cells( Y, 3 ).Value = Time_num
' // Equity
Sheet.Cells( Y, 4 ).Value = Eq( i )
' // Drawdown
if Eq(i) > MaxE then
MaxE = Eq(i)
End if
Drawdown = 0 - ( ( MaxE - Eq( i ) ) / MaxE * 100 )
Sheet.Cells( Y, 5 ).Value = Drawdown
Next
' // And now the charts
' // Equity chart...
Sheet.Activate
Sheet.Columns( 4 ).Select
Excel.Charts.Add
Set EquityCharts = Excel.Charts( 1 )
EquityCharts.Name = Nam + " Equity"
EquityCharts.Type = 1
EquityCharts.ChartArea.Fill.PresetGradient 1,1,7
EquityCharts.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB( 0, 150, 0 )
EquityCharts.HasLegend = 0
EquityCharts.HasTitle = 1
EquityCharts.ChartTitle.Text = EquityCharts.Name
' // and DrawDown chart (in percent)
Sheet.Activate
Sheet.Columns( 5 ).Select
Excel.Charts.Add
Set EquityCharts = Excel.Charts( 2 )
EquityCharts.Name = Nam + " DrawDown"
EquityCharts.Type = 1
EquityCharts.ChartArea.Fill.PresetGradient 1,1,7
EquityCharts.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB( 200, 0, 0 )
EquityCharts.HasLegend = 0
EquityCharts.HasTitle = 1
EquityCharts.ChartTitle.Text = EquityCharts.Name
Excel.Visible = 1
' //Excel.Activeworkbook.SaveAs("C:\ABEquity.xls") '//Using Save As
%>
very nice, thank joshsmi
頁:
1
[2]