hello @richpach
Below afl is one way to solve the problem.
Here I use AFL and one Batch file to import the data.
i hope you like it
/**
/// @link https://forum.amibroker.com/t/how-to-import-plain-csv-files-with-amiquote/27363/7
How to use:
to download the data from internet
1) Open parameters window in Amibroker
To import the data
2) you must Create a Batch file
3) write the Path to your Batch File
"e:\\AmiBroker\\Batch\\BatchFileName.abb", "" ); // << Path to your Batch File
///////////////////////////////////////////////////////////////
This is the output of this afl:
$FORMAT Ticker,Date_DMY,Open,Close,Volume
CORE,03/09/2021,2.1012,2.0997,2.89
BALANCED,03/09/2021,4.7099,4.7075,1.93
BALANCEDIDX,03/09/2021,1.3739,1.3728,3.41
CAPITAL_STABLE,03/09/2021,3.7327,3.7308,1.36
DIVERSIFIED,03/09/2021,6.2783,6.2739,2.68
HIGH_GROWTH,03/09/2021,7.3194,7.3143,3.21
Write your PATH
PathToSave = "R:\\"; //<<< PATH to save the file
FileName = "Rest_Com.txt"; // <<< the File name, to save in your hard drive
*/
_SECTION_BEGIN( "Download From Rest.com.au" );
PathToSave = "R:\\"; //<<< PATH to save the file
FileName = "Rest_Com.txt"; // <<< the File name, to save in your hard drive
FromDay=ParamDate("Start Date", "01/09/2021",1); printf( "\n Start Date =\t" + FromDay );
ToDay=ParamDate("End Date","03/09/2021",1 ); printf( "\n End Date =\t" + toDay );
EnableTextOutput(False);
CORE ="https://rest.com.au/Client/Templates/Rest/InvestmentSection/csvexport/csvexport.aspx?id=1&action=unitprice&dateFrom="+FromDay+"&dateTo="+ToDay+"&itemId=1";
BALANCED ="https://rest.com.au/Client/Templates/Rest/InvestmentSection/csvexport/csvexport.aspx?id=3&action=unitprice&dateFrom="+FromDay+"&dateTo="+ToDay+"&itemId=3";
BALANCEDIDX ="https://rest.com.au/Client/Templates/Rest/InvestmentSection/csvexport/csvexport.aspx?id=4&action=unitprice&dateFrom="+FromDay+"&dateTo="+ToDay+"&itemId=4";
CAPITAL_STABLE="https://rest.com.au/Client/Templates/Rest/InvestmentSection/csvexport/csvexport.aspx?id=5&action=unitprice&dateFrom="+FromDay+"&dateTo="+ToDay+"&itemId=5";
DIVERSIFIED ="https://rest.com.au/Client/Templates/Rest/InvestmentSection/csvexport/csvexport.aspx?id=6&action=unitprice&dateFrom="+FromDay+"&dateTo="+ToDay+"&itemId=6";
High_GROWTH ="https://rest.com.au/Client/Templates/Rest/InvestmentSection/csvexport/csvexport.aspx?id=7&action=unitprice&dateFrom="+FromDay+"&dateTo="+ToDay+"&itemId=7";
EnableTextOutput(True);
function GetSymbolName( x )
{
return StrExtract( "CORE,BALANCED,BALANCEDIDX,CAPITAL_STABLE,DIVERSIFIED,HIGH_GROWTH", x );
}
PressToDownload= ParamTrigger( "Press to Start Download", "Press Once");
if( PressToDownload )
{
URLList= CORE +"\n"+BALANCED+"\n"+BALANCEDIDX+"\n"+CAPITAL_STABLE+"\n"+DIVERSIFIED+"\n"+HIGH_GROWTH;
for( i = 0; ( symbol = StrExtract( URLList, i,'\n' ) ) != ""; i++ )
{
ih = InternetOpenURL( symbol );
fhLots = fopen( PathToSave + FileName, "a" );
// below line is Optional, if you dont like the Header just comment out this below line
if( fhLots ) fputs( "$FORMAT Ticker,Date_DMY,Open,Close,Volume\n", fhLots );
if( ih && fhLots )
{
while( ( str = InternetReadString( ih ) ) != "" )
{
if( StrFind( Str, "2022," ) OR StrFind( Str, "2021," ) OR StrFind( Str, "2020," )) // if year found do something
{
Date_ = StrExtract( str , 0 ); //_TRACE("#, Year = "+ Date_ );
Buy_ = StrExtract( str , 1 ); // Buy, map as Open
Sell_ = StrExtract( str , 2 ); // Sell, map as Close
Vol_ = StrExtract( str , 3 ); // Extract the 3rd column
Line = ""+GetSymbolName(i) +"," + Date_ +","+ Buy_ +","+ Sell_ +","+ Vol_ ;
_TRACE("#, My extracted lines = " + Line );
fputs( Line, fhLots ); // save the Clean data file txt
}
}
InternetClose( ih );
if( fhLots ) fclose( fhLots );
}
}
Say("File is ready",1);
}
_SECTION_END();
///////////////////////////////////////////////////////////////
// Import a CSV file using Batch
//////////////////////////////////////////////////////////////
_SECTION_BEGIN( "Import file using Batch" );
// run batch
if( ParamTrigger( "Import a CSV file with Batch", "Press Once to Import" ) )
{
ShellExecute( "runbatch", "e:\\AmiBroker\\Batch\\BatchFileName.abb", "" ); // << Path to your Batch File
}
_SECTION_END();
Now. If you still want to import CSV file that is in your hard drive. Is already covered in AmiQuote readme
Below afl is to add / insert in the bottom of the above code ( but to work you need to read first )
///////////////////////////////////////////////////////////////
// Import a CSV file with AmiQouote
// C:\MyDataFolder\{symbol}.txt
//////////////////////////////////////////////////////////////
if( ParamTrigger( "AQ Import a CSV file", "Press Once to Import" ) )
{
AQ = CreateObject( "AmiQuote.Document" );
AQ.AddSymbols("Rest_Com"); // "Rest_Com" << FileName, your hard drive
AQ.Source = 16; // <<< change as needed
//AQ.From = dt_From; //"2018-01-01 00:00:00";
AQ.To = Now();
AQ.AutoImport = 1; //False; //True;
AQ.Download();
while( AQ.DownloadInProgress() OR AQ.ImportInProgress() )
{
ThreadSleep( 20 );
}
}