2012-10-14 6 views
2

나는 yahoo finance 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. 가 어떻게 루비에서 SMA를 계산할 수 있습니까?

+2

빈에는'emply'방법''다른 한편으로는 없습니다 ... –

+0

안녕하세요, 오타 오류를 지적 해 주셔서 감사합니다. 그러나 수정 후에도 여전히 동일한 오류가 발생합니다. 나는 다른 접근법 하나를 생각하고있다. hq.close에는 닫는 값을 가진 주식 기호 목록이 있기 때문에이 객체에 대해 두 개의 루프를 수행 할 수 있습니다. 먼저 객체 수를 얻기 위해 객체를 반복하고 목록에서 각각의 값을 가져 오는 두 번째 루프를 수행 할 수 있습니까? 나는 이것이 자바에서 매우 가능하다는 것을 알고있다. 루비에서이 작업을 수행 할 수있는 방법이 있습니까? 예제를 제공해주세요. – 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라는 보석이 있습니다.

이 코드 :

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 } 
관련 문제