As for storing to external DB via ODBC plugin:
In connection
variable you would have to insert DSN, driver, password, DB name, port,....
Columns ID
, MetricName
,MetricValue
have to exist. But you may change to other names of your table in odbcExecuteSQL
line.
/// @link https://www.amibroker.com/guide/a_custombacktest.html
/// @link http://www.amibroker.com/kb/2015/01/06/how-to-display-interest-gains-in-the-backtest-report/
/// @link https://forum.amibroker.com/t/is-it-possible-to-get-result-from-backtest-and-optimization/12570/2
/// @link https://forum.amibroker.com/t/saving-the-backtest-result/26201/7
/// by fxshrat@gmail.com
EnableTextOutput(0);
metrics_list = "MaxTradeDrawdown,"+
"MaxTradeDrawdownPercent,"+
"MaxSystemDrawdown,"+
"MaxSystemDrawdownPercent,"+
"RecoveryFactor,"+
"CAR/MDD,"+
"RAR/MDD,"+
"ProfitFactor,"+
"PayoffRatio,"+
"StandardError,"+
"RRR," + //Risk-Reward Ratio
"UlcerIndex,"+
"UlcerPerformanceIndex,"+
"SharpeRatio,"+
"KRatio";
rows = StrCount(metrics_list, ",")+1;
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) {
bo = GetBacktesterObject();
bo.Backtest(); // run default backtest procedure
mat = Matrix(rows, 1);
stats = bo.GetPerformanceStats(0);
for ( i = 0; i < rows; i++ ) {
mat[i][0] = stats.GetValue(StrExtract(metrics_list, i));
}
StaticVarSet( "CBT_StatsMat", mat);
}
// Dummy system
SetPositionSize(1, spsShares);
m = MA( Close, 20 );
Buy = Cross( Close, m );
Sell = Cross( m, Close );
Short = Cover = 0;
// Dummy system end
// Call metrics elsewhere AFTER backtest
// Exploration output or anything else...
mat = StaticVarGet( "CBT_StatsMat" );
SetOption("NoDefaultColumns",1);
Filter = 0;
AddColumn(Null, "ID", 1);
AddtextColumn("", "Metric", 1);
AddColumn(Null, "Value", 1);
explore = Status("action") == actionExplore;
odbcTrigger = explore OR ParamTrigger("Store to external DB", "CLICK HERE");
connection = "ODBC;"+
"DSN=YOUR_DSN;"+
"DRIVER={YOUR_DRIVER};"+ // e.g {MariaDB ODBC 3.1 Driver}
"TCPIP=1;"+
"SERVER=127.0.0.1;"+
"UID=YOUR_UID;"+
"PWD=YOUR_PASSWORD;"+
"DATABASE=YOUR_DB;"+
"PORT=YOUR_PORT";
table_name = "YOUR_TABLE";// table name of external DB
odbcOpenDatabase(connection);
if ( typeof( mat ) == "matrix" ) {
if ( Status("stocknum") == 0 ) {
if ( odbcTrigger)
odbcExecuteSQL("DELETE FROM "+table_name+";");
//
for ( i = 0; i < rows; i++ ) {
//printf("%s: %g\n", StrExtract(metrics_list, i), mat[i][0]);
AddRow(StrFormat("%g\t%s\t%g", i+1, StrExtract(metrics_list, i), mat[i][0]));
//
if ( odbcTrigger ) {
odbcExecuteSQL(StrFormat("Insert INTO "+table_name+" (ID, MetricName, MetricValue) VALUES ( %1.0f, '%s', %g )",
i+1, StrExtract(metrics_list, i), mat[i][0]) );
}
}
}
}
Left part of picture shows MariaDB table after program execution in AmiBroker.
