@djjazzy213, I forgot to mention that the format file for AFL code of post #3 has to be modified to:
$NOQUOTES 1
$AUTOADD 1
$OVERWRITE 1
$SEPARATOR ,
$FORMAT TICKER,SHARES_SHORT
see SHARES_SHORT
field
But instead of import via ASCII importer you may alternatively import via OLE.
So below is 3rd (alternative) code (doing that)...
In addition the code should work for most single fundamental (number) to be imported.
Right now you can select from following list via ParamList:
You have to read here https://www.amibroker.com/guide/objects.html if you want to add more.
As well as you have investigate source code of key statistic page
e.g. https://finance.yahoo.com/quote/AAPL/key-statistics
Hint: (ATM) at line 57 of page's source code after defaultKeyStatistics
.
Format:
"statistic":{"raw":....
If you want to import more statistic then you have to modify line #15 and switch statement beginning at line #50
Another note, it is recommended to use the code in Exploration to enable UI refresh after completion.
But you may also click " View - Refresh all" or menu bar afterwards if applying the code in chart pane.
And... symbols of ticker_list
variable (line 10) have to exists in your DB otherwise you will get Error 18!
/// Import SINGLE "key statistics" from Yahoo finance via OLE and save to text file (optionally)
/// Replace ticker_list by your own symbol list or single symbol
/// If you want to import different statistic then you have to modify line #15 and switch statement beginning at line #50
/// Also see bottom of https://www.amibroker.com/guide/objects.html
/// As well as investigate source code of key statistic page for statistic_list e.g. https://finance.yahoo.com/quote/AAPL/key-statistics
/// AFL code by fxshrat@gmail.com
/// @link https://forum.amibroker.com/t/adding-a-yahoo-fundamental-data-piece-to-norgate-database/17549/5
EnableTextOutput(0);
listnum = 0;//GetOption( "FilterIncludeWatchlist"); // or instead use some hardcoded number etc.
ticker_list = "AAPL,MSFT";//CategoryGetSymbols(categoryWatchlist, listnum);
/// modify list if required
/// statistics names of source code from e.g.
/// @link https://finance.yahoo.com/quote/AAPL/key-statistics
statistic_list = "beta,bookValue,ebitda,floatShares,pegRatio,sharesOutstanding,sharesShort,sharesShortPriorMonth";
statistic = ParamList("Choose Statistic", statistic_list, 6);
///
export_to_file = True;// export to file in addition -> True/False setting
Filter = 0;
if ( GetChartID() > 0 )
chart_trigger = ParamTrigger( "Import Statistic", "CLICK HERE" );
else
chart_trigger = 0;
trigger = chart_trigger OR Status("action") == actionExplore;
SetOption("RefreshWhenCompleted", True);// works in analysis only
if ( trigger ) {
if ( Status( "stocknum" ) == 0 ) {
yhoo_out = "";
what = statistic+"\":";
AB = CreateObject( "Broker.Application" );
for ( i = 0; ( nm = StrExtract( ticker_list, i, ',' ) ) != ""; i++ ) {
ih = InternetOpenURL( "https://finance.yahoo.com/quote/"+nm+"/key-statistics" );
if ( ih ) {
str_repl = "";
while ( ( str = InternetReadString( ih ) ) != "" ) {
if ( StrFind(str, what) )
str_repl = StrReplace(str, what, "\n");
}
InternetClose( ih );
str_ext = StrExtract(str_repl, 1, '\n');
str_ext = StrExtract(str_ext, 1, ':');
value = StrToNum(StrExtract(str_ext, 0, ','));
yhoo_out += nm + "," + value + "\n";
/// @link https://www.amibroker.com/guide/objects.html
st = AB.Stocks( nm );
switch( statistic ) {
case "beta": st.Beta = value; break;
case "bookValue": st.BookValuePerShare = value; break;
case "ebitda": st.EBITDAPerShare = value; break;
case "floatShares": st.SharesFloat = value; break;
case "pegRatio": st.PEGRatio = value; break;
case "sharesOutstanding": st.SharesOut = value; break;
case "sharesShort": st.SharesShort = value; break;
case "sharesShortPriorMonth": st.SharesShortPrevMonth = value; break;
default: break;
}
}
}
_TRACE(statistic);
_TRACE(yhoo_out);
if ( export_to_file ) {
// save to file
fh = fopen("AmiQuote\\Download\\"+statistic+".txt", "w" );
if ( fh ) {
fputs( yhoo_out, fh );
fclose( fh );
} else
Error( "ERROR: file can not be open" );
}
PopupWindow("Import of "+statistic+" finished!\n\n"+
"Click 'View - Refresh All' of Menu bar\n"+
"if import not being visible yet.", "Status", 5);
}
}