📄 yahoofinanceinputsynapse.java
字号:
package org.joone.io;import java.io.*;import java.text.*;import java.net.URL;import java.util.*;import org.joone.log.*;import org.joone.net.NetCheck;import org.joone.exception.JooneRuntimeException;import org.joone.engine.NetErrorManager;/** * <P>The YahooFinanceInputSynapse provides support for financial data input from financial markets. The * synapse contacts YahooFinance services and downloads historical data for the chosen symbol and date range. * Finally the data is presented to the network in reverse date order i.e oldest first. </P> * <P>This synapse provides the following info .. </P> * <P>Open as column 1</P> * <P>High as column 2</P> * <P>Low as column 3</P> * <P>Close as column 4.</P> * <P>Volume as column 5.</P> * <P>Adj.Close as column 6.</P> * <P>For the particular stock symbol.</P> * <BR> * <P> Developer Notes : </P> * <P> This YahooFinanceInputSynapse uses the following format to extract stock financial information from the Yahoo Network</P>. * <P>http://table.finance.yahoo.com/table.csv?a=8&b=1&c=2002&d=11&e=3&f=2002&s=tsco.l&y=0&g=d&ignore=.csv</P> * <BR> * <P>a = From Month 0 - 11</P> * <P>b = From Day 1-31</P> * <P>c = From Year XXXX</P> * <P>d = To Month 0-11</P> * <P>e = To Day 1-31</P> * <P>f = To Year XXXX</P> * <P>s = Symbol</P> * <P>y = [record] from record to + 200 records</P> * <P>g=[d] or[m] or [y] - daily or monthly or yearly</P> * <P>ignore = .csv</P> * */public class YahooFinanceInputSynapse extends StreamInputSynapse { /** The object used when logging debug,errors,warnings and info. */ private static final ILogger log = LoggerFactory.getLogger(YahooFinanceInputSynapse.class); String [] months = new String [] {"January","February","March","April","May","June","July","August","September","October","November","December"}; String [] frequency = new String [] {"Daily","Weekly","Monthly"}; String [] freq_conv = new String [] {"d","w","m"}; String Symbol = new String(""); DateFormat date_formater = DateFormat.getDateInstance(DateFormat.MEDIUM); Calendar CalendarStart = Calendar.getInstance(); Calendar CalendarEnd = Calendar.getInstance(); String DateStart = new String(date_formater.format(CalendarStart.getTime())); String DateEnd = new String(date_formater.format(CalendarEnd.getTime())); String Period = new String("Daily"); private transient Date startDate = CalendarStart.getTime(); private transient Date endDate = CalendarEnd.getTime(); private Vector StockData [] = new Vector [6]; // Holds all the data + optional data, oldest first. private Vector StockDates = new Vector(); // Holds the Dates associated with each row of data. String [] ColumnNames = new String [] {"Date","Open","High","Low","Close","Volume","Adj. Close"}; // Ignore lines with these names in static final long serialVersionUID = 1301769209320717393L; /** * Constructor for the YahooFinanceInputSynapse object */ public YahooFinanceInputSynapse() { super(); } /** * Gets the name of the symbol * * @return The Symbol name */ public String getSymbol() { return Symbol; } /** * Gets year to start data retrieval from. * * @return The year to start from * @deprecated Use getStartDate instead */ public String getDateStart() { return DateStart; } /** * Gets year to end data retrieval from * * @return The year to end on * @deprecated Use getEndDate instead */ public String getDateEnd() { return DateEnd; } /** * Gets the period for data retrieval. * * @return The month to end on */ public String getPeriod() { return Period; } /** * Gets the dates associated with each row of data. */ public Vector getStockDates() { return(StockDates); } /** * <P>Gets the stock data retrieived by this synapse. Returns the data in a Vector array of length 5.</P> * <P>In column 0 Open data.</P> * <P>In column 1 High</P> * <P>In column 2 Low</P> * <P>In column 3 Close</P> * <P>In column 4 Volume</P> * <P>In column 5 Adj.Close</P> * */ public Vector [] getStockData() { return(StockData); } /** * Reads this YahooFinanceInputSynapse object into memory from the specified object stream. * * @param in The object input stream that this object should be read from * @exception IOException The Input Output Exception * @exception ClassNotFoundException The class not found exception */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { super.readObjectBase(in); if (in.getClass().getName().indexOf("xstream") == -1) { Symbol = (String) in.readObject(); DateStart = (String) in.readObject(); DateEnd = (String) in.readObject(); Period = (String) in.readObject(); } if (!isBuffered() || (getInputVector().size() == 0)) { initInputStream(); } try { startDate = date_formater.parse(DateStart); endDate = date_formater.parse(DateEnd); } catch (ParseException ex) { log.error("Invalid Date: start:"+DateStart+" end:"+DateEnd); } } /** * Sets the name of the database jdbc driver. * * @param newSymbol The new stock symbol to retrieve the data with. * @deprecated Use setEndDate instead */ public void setSymbol(java.lang.String newSymbol) { // Only set if it actually has changed if ( !Symbol.equals(newSymbol)) { Symbol = newSymbol; this.resetInput(); this.setTokens(null); } } /** * Gets the the data from which data is retrieved. * * @param newDataStart The data from which data is retrieved. * @deprecated Use setStartDate instead */ public void setDateStart(String newDateStart) { // Only set if it actually has changed if (!DateStart.equals(newDateStart)) { DateStart = newDateStart; this.resetInput(); this.setTokens(null); } } /** * Gets the the data to which data is retrieved. * * @param newDateEnd The date to which data is retrieved. */ public void setDateEnd(String newDateEnd) { // Only set if it actually has changed if (!DateEnd.equals(newDateEnd)) { DateEnd = newDateEnd; this.resetInput(); this.setTokens(null); } } /** * Sets the period with which to retrieve data should be one of "Daily" or "Monthly" or "Yearly". * * @param newPeriod The period with which data is retieved. */ public void setPeriod(String newPeriod) { // Only set if it actually has changed if (!Period.equals(newPeriod)) { Period = newPeriod; this.resetInput(); this.setTokens(null); } } /** * Writes this YahooFInanceSynapseInput object to the ObjectOutputStream out. * * @param out The ObjectOutputSteeam that this object should be written to * @exception IOException The Input Output Exception if any */ private void writeObject(ObjectOutputStream out) throws IOException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -