Amibroker 自动导入国内股票日数据的JavaScript脚本

Discussion in 'AmiBroker' started by zhaowd2001, Aug 29, 2010.

  1. 从yahoo下载到股票日数据后,用JavaScript导入Amibroker的时候遇到点问题(日期格式问题)。
    所以把写好的JS程序共享一下,希望能帮助大家。

    以下是 abimport.js:
    Code:
    /*
    Usage:
    600877.csv is the data file downloaded from yahoo web.
    cscript //nologo abimport.js 600877.csv
    
    2010-08-27 comment:  Purpose: import daily stock data into Amibroker
    The input data is download from yahoo.
    The data format is:
    Date,Open,High,Low,Close,Volume,Adj Close
    2010-08-27,13.27,13.78,13.22,13.53,7397234,13.53
    
    The date format 2010-08-27 can't be imported into Amibroker.
    It seems like the date format expected by Amibroker is 08-27-2010
    Sample code:
    datefld = "08-27-2010"; //correct date format in JavaScript.
    datefld = "2010-08-27";//invalid date format in JavaScript
    date = new Date( datefld );
    quote = stock.Quotations.Add( date.getVarDate() );
    
    So, this JS file converts format of date from '2010-08-27' to '08-27-2010' before adding them into Amibroker.
    
    2010-08-27 comment:  How to download stock data:
    1.Download history stock data from yahoo: (600877 is the stock number)
    http://table.finance.yahoo.com/table.csv?s=600877.ss
    
    2.Download today stock data from yahoo:(600877 is the stock number)
    http://hq.sinajs.cn/list=sh600877
    
    OLD comments:
    ** AmiBroker/Win32 scripting Example
    **
    ** File:	 Import2.js
    ** Created:	 Tomasz Janeczko, February 2nd, 2000
    ** Purpose:	 Import quotes from Yahoo's CSV history file
    ** Language: JScript (Windows Scripting Host)
    **
    ** The data is stored in lines with following format
    ** Date,Open,High,Low,Close,Volume
    **
    */
    bdebug = false
    stock_csv = WScript.Arguments.Item(0);
    
    ods( "Importing data into Amibroker ...");
    ods( "Data file: " + stock_csv );
    ImportCSV( stock_csv	);
    
    function ods(v)
    {
    	    WScript.Echo( v );
    }
    
    function debug(oQuote)
    {
      var oDate = new Date( oQuote.Date );
    
    	    ods( 
    		//stock.Ticker + "," + 
    	       oDate.getFullYear() + "-" + 
    		   (
    		   parseInt(oDate.getMonth()) + 1 
    		   )
    		   + "-" + oDate.getDate() + "," + 
    	       oQuote.Close + "," + 
    	       oQuote.Open + "," +
    	       oQuote.High + "," +
    	       oQuote.Low + "," + 
    	       oQuote.Volume );
    }
    
    function ImportCSV( filename )
    {
    	var fso, f, r;
    	var ForReading = 1;
    	var AmiBroker;
    	var ticker;
    	var date;
    	var quote;
    	var fields;
    	var stock;
    	
    	var date1 = 0;
    	var date2 = 0;
    
    	/* Create AmiBroker app object */
    	AmiBroker = new ActiveXObject( "Broker.Application" );
    
    	/* ... and file system object */
    	fso = new ActiveXObject( "Scripting.FileSystemObject" );
    
    	/* we use file name ( without extension ) as a ticker name */
    	ticker = fso.GetBaseName( filename ).toUpperCase();
        ticker = ticker.split(".");
    	ticker = ticker[0];
    
        /* add a ticker - this is safe operation, in case that	 */
        /* ticker already exists, AmiBroker returns existing one */
    	/* we are doing this outside loop since the file contains */
    	/* quotes of single stock only */
        stock = AmiBroker.Stocks.Add(  ticker ); 
    
    	/* open ASCII file */
    	f = fso.OpenTextFile( filename, ForReading);
    
        /* notify the user */
    	ods( "Stock ticker: " + stock.Ticker );
    
    	/* skip first line which contains format definition */
    	f.SkipLine(); 
    
    	/* read the file line by line */
    	while ( !f.AtEndOfStream )
    	{  
    		  r =  f.ReadLine();
    		  //WScript.Echo( r );
    		  
    		  /* split the lines using comma as a separator */
    		  fields = r.split(","); 
    		  
    		  /* split date at - separator */
    		  var datefld = fields[ 0 ].split("-");
    		  var datefld2 = fields[ 0 ].split("-");
    
    		  /* ensure Y2K compliance by converting year to 4 digit number */
    		  //var year = parseInt( datefld[ 2 ] );
    		  //year += ( year < 50 ) ? 2000 : 1900;
    		  
    		  //datefld[ 2 ] = year.toString();
    		  datefld[0] = datefld2[1];//month
    		  datefld[1] = datefld2[2];//day
    		  datefld[2] = datefld2[0];//year
    
    		  /* put date back all together */
    		  datefld = datefld[0] + "-" + datefld[1] + "-" + datefld[2];
    		  
    		  //save first date
    		  if( date1 == 0 )
    			date1 = datefld;
    		  date2 = datefld;
    
    		  date = new Date( datefld );
    		  //
    		  var msg  = "Line:" + r; 
    		      msg += datefld;
    			  msg += "Date:" + date.getVarDate();
    		  //ods(msg);
    
    		  /* add a new quotation */
    		  quote = stock.Quotations.Add( date.getVarDate() );
    		  
    		  /* put data into it */
    		  quote.Open = parseFloat( fields[ 1 ] );
    		  quote.High  = parseFloat( fields[ 2 ] );
    		  quote.Low   = parseFloat( fields[ 3 ] );
    		  quote.Close = parseFloat( fields[ 4 ] );
    		  quote.Volume = parseInt( fields[ 5 ] );
    		  
    		  if( bdebug )
    			debug( quote );
    	}
    
    	/* refresh ticker list and windows */
    	AmiBroker.RefreshAll();
    
    	/* notify the user */
    	if( date1 == date2 )
    		ods( "Finished:Date of data is " + date2);
    	else
    		ods( "Finished:" + "Date of data is from " + date2 + " to " + date1);
    
    }
    
    
     
  2. 謝謝樓主, 雖然我不炒a股.但真的值得借鑑.
     
  3. 谢谢楼主的辛勤劳动,我个人建议最好不要使用yahoo财经的A股日线。

    首先是数据迟到1天,最关键的日线很多数据有错误,可能过几天又好了,你也不知道什么时候,我原来也用yahoo财经,后来不得不放弃了。
     
  4. 我是玩港股的,也发觉雅虎的数据很差, 股票的分拆,合并,供股等都没有调整. 错漏百出,但是免费的数据很少.
     
  5. 多谢两位的建议。目前我只是做测试用,还可以将就一下。
    你们大家有用Amibroker AFL做买卖分析的吗?有成功的思路吗
    谢谢
     
  6. 谢谢楼主的分享!