⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yahoofinanceinputsynapse.java

📁 一个纯java写的神经网络源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        super.writeObjectBase(out);        if (out.getClass().getName().indexOf("xstream") == -1) {            out.writeObject(Symbol);            out.writeObject(DateStart);            out.writeObject(DateEnd);            out.writeObject(Period);        }    }        /**     *  Connects to Yahoo FInancial Services and obtains the historical data for the specifed symbol and data range.     */    protected void initInputStream() throws JooneRuntimeException {                String final_patterns = new String("");     // A buffer of final_patterns to present to the Tokenizer        int start=0;        String myUrl = new String("");     // Used to construct the URL to obtain the data                String cperiod = "d";   // Default char period to 'd' for Daily                for ( int i=0;i<frequency.length;i++) {            if ( Period.equals(frequency[i]) )                cperiod = freq_conv[i];  // Get the period character d,w or m        }                StockData = null;        StockDates = null;                if ((Symbol != null) && (!Symbol.equals(new String("")))) {            try {                CalendarStart = Calendar.getInstance();                CalendarEnd = Calendar.getInstance();                CalendarStart.setTime( date_formater.parse(DateStart) );                CalendarEnd.setTime( date_formater.parse(DateEnd) );            } catch ( ParseException ex ) {	// Could not parse one of the date strings.                log.error( "YahooFinanceInputSynapse could not parse date string. Message is : = "+ex.getMessage());	// LOG4J                CalendarStart = null;                CalendarEnd = null;                //throw new JooneRuntimeException(ex.getMessage());                if ( getMonitor() != null )                    new NetErrorManager(getMonitor(),"YahooFinanceInputSynapse could not parse date string. "+ex.getMessage());                return;            }            int addedData = 1;            if (CalendarStart != null) {                if (CalendarEnd != null) {                    if (Period != null) {                        log.info("Contacting Yahoo Finance Network.");                        try {                            while ((addedData > 0) && (addedData <= 200)) {                                myUrl = new String("http://table.finance.yahoo.com/table.csv?a="+CalendarStart.get(Calendar.MONTH)+"&b="+CalendarStart.get(Calendar.DAY_OF_MONTH)+"&c="+CalendarStart.get(Calendar.YEAR)+"&d="+CalendarEnd.get(Calendar.MONTH)+"&e="+CalendarEnd.get(Calendar.DAY_OF_MONTH)+"&f="+CalendarEnd.get(Calendar.YEAR)+"&s="+Symbol+"&g="+cperiod+"&z=200&y="+start+"&ignore=.csv");                                addedData = addURLToMemory(new URL(myUrl));                                start += 200;                            }                        } catch ( IOException ex ) {                            log.error( "Error obtaining data from YahooFInance. Error message is "+ex.getMessage());                            //throw new JooneRuntimeException(ex.getMessage());                            if ( getMonitor() != null )                                new NetErrorManager(getMonitor(),"Error obtaining data from YahooFInance."+ex.getMessage());                            return;                        }                        log.info("Loaded Yahoo Fianance data ok.");                        // Compose final pattern to give to network, also give in reverse order so oldest date first                        for ( int i=StockData[0].size()-1;i>=0;i--) {                                                        // OPEN                            final_patterns += ((Double)StockData[0].elementAt(i)).toString();                                                        // HIGH                            final_patterns += ";"+((Double)StockData[1].elementAt(i)).toString();                                                        // LOW                            final_patterns += ";"+((Double)StockData[2].elementAt(i)).toString();                                                        // CLOSE                            final_patterns += ";"+((Double)StockData[3].elementAt(i)).toString();                                                        // VOLUME                            final_patterns += ";"+((Double)StockData[4].elementAt(i)).toString();                                                        // ADJ. CLOSE                            final_patterns += ";"+((Double)StockData[5].elementAt(i)).toString();                            final_patterns += '\n';                        }                        try{                            StreamInputTokenizer sit;                            if (getMaxBufSize() > 0)                                sit = new StreamInputTokenizer(new StringReader(final_patterns), getMaxBufSize());                            else                                sit = new StreamInputTokenizer(new StringReader(final_patterns));                            super.setTokens(sit);                        } catch (IOException ex) {                            // Use Log4j                            log.error( "IOException thrown while initializing the YahooFinanceInputStream. Message is : " + ex.getMessage());	// LOG4J                            if ( getMonitor() != null )                                new NetErrorManager(getMonitor(),"Error while trying to parse Yahoo Finance data. Message is : "+ex.getMessage());                        }                    } // End of if period is null                } // End if not Year Start            } // End if not Day Start        } // end if no Symbol    } // End method initInputStream        /**     * Adds data from a URL in CSV format to Memory     * @return the number of lines added to the buffer     */    private int addURLToMemory(URL Data) throws IOException {        LineNumberReader url_reader = null;        String Line = new String("");        boolean IgnoreHeader = true;        boolean added_data = true;        boolean Docontinue = true;                log.debug(Data.getProtocol()+"://"+Data.getHost()+Data.getFile());        int initSize = 0;        // Check that the URL is not null        if ( Data != null) {            url_reader = new LineNumberReader(new InputStreamReader(new BufferedInputStream(Data.openStream())));            // Read the first line            Line = url_reader.readLine();            if ( StockDates == null)StockDates = new Vector();            if ( (StockData == null)||(StockData.length < 6))StockData = new Vector[6];            // Init Pattern list            if ( StockData[0] == null )StockData[0] = new Vector(); // Open            if ( StockData[1] == null )StockData[1] = new Vector(); // High            if ( StockData[2] == null )StockData[2] = new Vector(); // Low            if ( StockData[3] == null )StockData[3] = new Vector(); // Close            if ( StockData[4] == null )StockData[4] = new Vector(); // Volume            if ( StockData[5] == null )StockData[5] = new Vector(); // Adj.Close            initSize = StockData[0].size();            while ( (Line != null) && (!Line.equals("")) && (Docontinue == true)) {                if ( IgnoreHeader == true) // Just throw header line away                {                    if ( Line.indexOf(ColumnNames[0]) < 0) {                        if ( Line.indexOf(",")!=-1 ) // Check for bad data                        {                            StringTokenizer tokens = new StringTokenizer(Line, ",\n");                            if (tokens.countTokens() >= 7)  {                                added_data = true;                                String _data = tokens.nextToken();                                StockDates.addElement(_data); // Get Stock Date for row                                // log.debug(_data);                                StockData[0].add(Double.valueOf(tokens.nextToken()));	// Add OPEN to pattern vector                                StockData[1].add(Double.valueOf(tokens.nextToken()));	// Add HIGH to pattern vector                                StockData[2].add(Double.valueOf(tokens.nextToken()));	// Add LOW to pattern vector                                StockData[3].add(Double.valueOf(tokens.nextToken()));	// Add CLOSE to pattern vector                                StockData[4].add(Double.valueOf(tokens.nextToken()));	// Add VOLUME to pattern vector                                StockData[5].add(Double.valueOf(tokens.nextToken()));	// Add Adj.Close to pattern vector                            }                        } // End check for bad data                        else                            Docontinue = false;                    }                }                Line = url_reader.readLine();            }        }        int addedData = StockData[0].size() - initSize;        //if ( url_reader != null)        //    if ( url_reader.getLineNumber() <= 2) added_data=false; // Consider the data not added if only one line of data        return addedData;    } // End Add URL To Memory            /**     * Check that there are no errors or problems with the properties of this YahooFinanceInputSynapse.     * @return The TreeSet of errors / problems if any.     */    public TreeSet check() {        TreeSet checks = super.check();                if ( (getDateStart() != null) && (!getDateStart().equals("")) ) {            try {                date_formater.parse(DateStart);            } catch ( ParseException ex ) {                checks.add(new NetCheck(NetCheck.FATAL, "Format for Start Date is invalid." , this));            }        } else            checks.add(new NetCheck(NetCheck.FATAL, "Start Date should be populated." , this));                if ( (getDateEnd() != null) && (!getDateEnd().equals("")) ) {            try {                date_formater.parse(DateEnd);            } catch ( ParseException ex ) {                checks.add(new NetCheck(NetCheck.FATAL, "Format for End Date is invalid." , this));            }        } else            checks.add(new NetCheck(NetCheck.FATAL, "End Date should be populated." , this));                if ( getPeriod()!=null) {            if ( !getPeriod().equals("Daily") && !getPeriod().equals("Weekly") && !getPeriod().equals("Monthly") ) {                checks.add(new NetCheck(NetCheck.FATAL, "Period should be one of 'Daily' , 'Weekly' , 'Monthly'." , this));            }        } else            checks.add(new NetCheck(NetCheck.FATAL, "Period should be one of 'Daily' , 'Weekly' , 'Monthly'." , this));                if ( (getSymbol()== null)||(getSymbol().equals("")))            checks.add(new NetCheck(NetCheck.FATAL, "Symbol should be populated." , this));                        return checks;    }        public Date getStartDate() {        return startDate;    }        public void setStartDate(Date startDate) {        this.startDate = startDate;        DateStart = new String(date_formater.format(startDate));        this.resetInput();        this.setTokens(null);    }        public Date getEndDate() {        return endDate;    }        public void setEndDate(Date endDate) {        this.endDate = endDate;        DateEnd = new String(date_formater.format(endDate));        this.resetInput();        this.setTokens(null);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -