2012-10-14 31 views
2

我正在使用雅虎財經API收集輸入股票數量的歷史收盤數據的程序,然後繼續計算數據的簡單移動平均線(SMA) 30天。我目前有以下幾種:如何計算簡單移動平均線

require 'rubygems' 
require 'yahoofinance' 

array = [] 
while line = gets 
    break if line.chomp =~ /N/ #exit when 'N' is entered 
    array << line.chomp 
end 
puts "Values: #{array.join(',')}" #joining all the elements with a comma 

array.each do |s| 
    print "\n______\n" 
    puts s 

    YahooFinance::get_HistoricalQuotes(s, 
            Date.parse('2012-10-06'), 
            Date.today()) do |hq| 
    puts "#{hq.close}" 
    end 
end 

這段代碼給了我指定範圍股票的接近值。我有兩個問題:

  1. 目前,hq.close持有所有股票的價值。我怎樣才能把這些值放在一個數組中,以便我可以對它進行計算來爲每個股票數據計算一個SMA?

    我試圖做這樣的事情:

    "#{hq.close}" my_val = [hq.close] 
    puts my_val 
    

    但這只是給第一股的my_val值。我知道我必須在這裏放一個循環。我試圖把

    while(!hq.close.emply?) 
        my_val = [hq.close] 
        puts my_val 
    end 
    

    但是,這給了我一個錯誤:

    C:/Users/Muktak/workspace/RubySample/sample_program.rb:23:in block (2 levels) in <main>': undefined methodemplty?' for 19.52:Float (NoMethodError) from 
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/yahoofinance-1.2.2/lib/yahoofinance.rb:491:in block in get_HistoricalQuotes' from 
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/yahoofinance-1.2.2/lib/yahoofinance.rb:456:inblock in get_historical_quotes' from 
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/yahoofinance-1.2.2/lib/yahoofinance.rb:456:in each' from 
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/yahoofinance-1.2.2/lib/yahoofinance.rb:456:inget_historical_quotes' from 
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/yahoofinance-1.2.2/lib/yahoofinance.rb:489:in get_HistoricalQuotes' from 
    C:/Users/Muktak/workspace/RubySample/sample_program.rb:19:inblock in ' from 
    C:/Users/Muktak/workspace/RubySample/sample_program.rb:13:in each' from 
    C:/Users/Muktak/workspace/RubySample/sample_program.rb:13:in' 
    Values: FB,GOOG 
    
  2. 我如何計算在Ruby SMA?

+2

沒有''emply方法,''空而另一方面...? –

+0

嗨,感謝您指出錯字錯誤。但即使在改正之後,我仍然會遇到同樣的錯誤。我正在考慮另一種方法。由於hq.close包含股票代碼及其結束值的列表,我可以在這個對象上做兩個循環,第一個用於遍歷該對象以獲取列表數量,第二個獲取列表中的相應值。我知道這在Java中是非常有可能的。有沒有辦法在ruby中做到這一點?請舉一個例子 – user1745117

回答

2

你在這裏問了兩個問題,所以讓我們一次解決一個問題。

首先,此代碼:

require 'rubygems' 
require 'yahoofinance' 

stock_names = %w{MSFT RHT AAPL} 
start = Date.parse '2012-10-06' 
finish = Date.today 
closes = {} 

stock_names.each do |stock_name| 
    quotes = YahooFinance::get_HistoricalQuotes stock_name, start, finish 
    closes[stock_name] = quotes.collect { |quote| quote.close } 
end 

...會產生closes以下散,你想我理解的格式爲:

{ 
    "AAPL" => [629.71, 628.1, 640.91, 635.85, 638.17], 
    "RHT"=> [53.69, 53.77, 53.86, 54.0, 54.41], 
    "MSFT"=> [29.2, 28.95, 28.98, 29.28, 29.78] 
} 

其次,要計算一個簡單的移動平均線 - 這對於金融應用來說只是the mean of the values。有一種叫做simple_statistics的Gem可以做到這一點。

此代碼:

require 'rubygems' 
require 'yahoofinance' 
require 'simple_statistics' 

stock_names = %w{MSFT RHT AAPL} 
start = Date.parse '2012-10-06' 
finish = Date.today 
averages = {} 

stock_names.each do |stock_name| 
    quotes = YahooFinance::get_HistoricalQuotes stock_name, start, finish 
    closes = quotes.collect { |quote| quote.close } 
    averages[stock_name] = closes.mean 
end 

...產生以下散列averages

{ "AAPL" => 634.548, "MSFT" => 29.238, "RHT" => 53.946 }