COCO研究院

 找回密碼
 註冊
搜索
12
返回列表 發新帖
樓主: omelet

回測結果的數據可以保存嗎?

[複製鏈接]
發表於 13-5-22 23:15 | 顯示全部樓層
本帖最後由 joshsmi 於 13-5-22 23:16 編輯

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.

  1. if ( Status( "action" ) == actionExplore )
  2. {
  3.     fh = fopen( "C:\\Equity.csv", "w" );

  4.     if ( fh )
  5.     {
  6.         fputs( "Symbol,Date,Time,Close\n", fh );

  7.         dn = DateNum();
  8.         tn = TimeNum();

  9.         for ( i = 0; i < BarCount; i++ )
  10.         {
  11.             Line = Name() + StrFormat( ",%06.0f,%06.0f,%g\n,",
  12.                                        dn[ i ] % 1000000,
  13.                                        tn[ i ],                                       
  14.                                        C[ i ] );
  15.             fputs( Line, fh );
  16.         }

  17.         fclose( fh );
  18.     }
  19. }

  20. Filter = Status( "lastbarinrange" );
  21. AddTextColumn( "Export done", "Status" );
複製代碼
發表於 13-5-22 23:21 | 顯示全部樓層
本帖最後由 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.


  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    AFL-Excel
  4. //  Author/Uploader: Witold Dabrowski, edited by joshsmi 2013
  5. //  E-mail:         
  6. //  Date/Time Added: 2002-08-31 06:29:37
  7. //  Origin:         
  8. //  Keywords:        
  9. //  Level:           semi-advanced
  10. //  Flags:           exploration
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=218
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=218
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  This example shows how to control Excel directly from AFL script via
  17. //  OLE-Automation interface. In this script I count %DD and plot Equity and %DD charts in Excel.
  18. //
  19. //  To run this code just click 'Scan', not 'Explore'. Exploration is displayed in Excel.
  20. //------------------------------------------------------------------------------

  21. SetOption( "FuturesMode", False );
  22. SetOption( "InitialEquity", 10000 );

  23. // test system start
  24. MA_  = MA( C, 20 );
  25. Buy  = Cover =  Cross( C, MA_ );
  26. Sell = Short = Cross( MA_, C );

  27. //PositionSize = IIf( Buy, BuyPrice, ShortPrice );
  28. SetPositionSize( 25, spsPercentOfEquity );
  29. // test system end


  30. // Equity function refers to individual equity, not portfolio equity
  31. Eq   = Equity( 1, -1 ); // flag 1: works as 0 but additionally updates buy/sell/short/cover arrays
  32.                         // so all redundant signals are removed exactly as it is done internally by the
  33.                         // backtester plus all exits by stops are applied so it is Now possible to visualise ApplyStop() stops.

  34.                         // range type -1: (default) use range set in the Automatic analysis window

  35. Nam  = Name();
  36. dn   = DateNum();
  37. tn   = TimeNum();
  38. bi   = BarIndex();
  39. fbr  = LastValue( ValueWhen( Status ( "firstbarinrange" ), bi ) );

  40. // vbscript start
  41. // remember Windows OLE and vbscript are slower
  42. EnableScript("vbscript");
  43. <%
  44. ' // Replacing the variables between AFL and VB script
  45. Eq  = AFL( "Eq" )
  46. Nam = AFL( "Nam" )
  47. dn  = AFL( "dn" )
  48. tn  = AFL( "tn" )
  49. fbr = AFL( "fbr" )

  50. ' // Starting Excel
  51. Set Excel = CreateObject( "Excel.Application" )

  52. ' // Disable confirmation messages of Excel
  53. Excel.DisplayAlerts = False

  54. ' // Setting Excel in the state of readiness
  55. Excel.SheetsInNewWorkbook = 1 ' // number of data sheets
  56. Excel.WorkBooks.Add
  57. Set Sheet = Excel.WorkBooks( 1 ).WorkSheets( 1 )

  58. ' // Writing data sheet Title
  59. Sheet.Cells( 1, 1 ) = Nam
  60. Sheet.Cells( 1, 2 ) = "DateNum"
  61. Sheet.Cells( 1, 3 ) = "TimeNum"
  62. Sheet.Cells( 1, 4 ) = "Equity"
  63. Sheet.Cells( 1, 5 ) = "DrawDown"

  64. ' // set data sheet name
  65. Excel.Activeworkbook.Sheets( 1 ).Name = Nam + " Data"

  66. ' // Initiation of Equity maximum - will be needed to calculate DrawDown
  67. MaxE = Eq( 0 )

  68. ' // Writing of Equity, DrawDown, Date and Time to Excel
  69. for i = fbr to UBound( Eq )
  70.      Y = i + 2 - fbr

  71.      ' // Datenum
  72.      Date_num = dn( i ) Mod 1000000
  73.      Sheet.Cells( Y, 2 ).Value = Date_num

  74.      ' // Timenum
  75.      Time_num = tn( i )
  76.      Sheet.Cells( Y, 3 ).Value = Time_num

  77.      ' // Equity
  78.      Sheet.Cells( Y, 4 ).Value = Eq( i )

  79.      ' // Drawdown
  80.      if Eq(i) > MaxE then
  81.         MaxE = Eq(i)
  82.      End if
  83.      Drawdown = 0 - ( ( MaxE - Eq( i ) ) / MaxE * 100 )
  84.      Sheet.Cells( Y, 5 ).Value = Drawdown
  85. Next

  86. ' // And now the charts
  87. ' // Equity chart...
  88. Sheet.Activate
  89. Sheet.Columns( 4 ).Select

  90. Excel.Charts.Add
  91. Set EquityCharts = Excel.Charts( 1 )
  92. EquityCharts.Name = Nam + " Equity"
  93. EquityCharts.Type = 1
  94. EquityCharts.ChartArea.Fill.PresetGradient 1,1,7
  95. EquityCharts.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB( 0, 150, 0 )
  96. EquityCharts.HasLegend = 0
  97. EquityCharts.HasTitle = 1
  98. EquityCharts.ChartTitle.Text = EquityCharts.Name

  99. ' // and DrawDown chart (in percent)
  100. Sheet.Activate
  101. Sheet.Columns( 5 ).Select

  102. Excel.Charts.Add
  103. Set EquityCharts = Excel.Charts( 2 )
  104. EquityCharts.Name = Nam + " DrawDown"
  105. EquityCharts.Type = 1
  106. EquityCharts.ChartArea.Fill.PresetGradient 1,1,7
  107. EquityCharts.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB( 200, 0, 0 )
  108. EquityCharts.HasLegend = 0
  109. EquityCharts.HasTitle = 1
  110. EquityCharts.ChartTitle.Text = EquityCharts.Name

  111. Excel.Visible = 1

  112. ' //Excel.Activeworkbook.SaveAs("C:\ABEquity.xls") '//Using Save As
  113. %>
複製代碼

 樓主| 發表於 13-5-23 22:57 來自手機 | 顯示全部樓層
very nice, thank joshsmi
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-12-23 17:58

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |