Skip to content

Instantly share code, notes, and snippets.

@smnuman
Last active August 22, 2022 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smnuman/1dc381981b3361bdb569 to your computer and use it in GitHub Desktop.
Save smnuman/1dc381981b3361bdb569 to your computer and use it in GitHub Desktop.
Data Export to a .csv file from AmiBroker database.

#EOD-Data export script Tomasz Janeczko described -- in AmiBroker kb -- a simple way to export AB data to a .csv file.

The easiest way to export quotes to CSV file is to use the below formula from Automatic Analysis window: (Analysis -> Automatic Analysis)

Filter=1; 
AddColumn(O,"Open"); 
AddColumn(H,"High"); 
AddColumn(L,"Low"); 
AddColumn(C,"Close"); 
AddColumn(V,"Volume",1.0); 
AddColumn(OI,"Open Interest",1.0); 
  • Open: Analysis->Formula Editor
  • Paste the above formula into formula window
  • Choose Tools->Send to Auto-Analysis menu in the Formula Editor
  • In Automatic Analysis window select Apply to: All Stocks, Range: All quotations (or any other time range or filter, depending on what you need to export)
  • Press Explore button
  • Press Export button, specify the name and press OK

A better modification for Re-Importing

Tomasz also described in a reply to a comment to the above script that --

"... this code sample was designed to be easy and output was not intended to be re-imported to AmiBroker. There are other ways to export of course, including using file functions as shown in the code below that does what you asked for (exports date in fixed YYMMDD format and times in HHMMSS 24-hour format)."

fh = fopen( "DataFor"+Name()+".csv", "w" );
if( fh )
{
  fputs("Symbol,Date,Time,Open,High,Low,Close,OpenInt,Volume\n", fh );
  dn = DateNum();
  tn = TimeNum();
 
  for( i = 0; i < BarCount; i++ )
  {
   Line = Name() +
   StrFormat(",%06.0f,%06.0f,%g,%g,%g,%g,%g,%g\n",
   dn[ i ] % 1000000,
   tn[ i ],
   Open[ i ],
   High[ i ],
   Low[ i ],
   Close[ i ],
   OpenInt[ i ],
   Volume[ i ] );
  
   fputs( Line, fh );
  }
 
  fclose( fh );
}
Buy=Sell=0; // for scan
Filter = Status("lastbarinrange");
AddTextColumn("Export done", "Status");

FYI

Checkout another primitive version of this kind of script in my collection

Hope this collection helps all in exporting AB EOD-Data. Happy trading!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment