如何计算简单的移动平均线 [英] How to calculate simple moving average

查看:417
本文介绍了如何计算简单的移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,该程序使用yahoo finance api收集输入的股票数量的历史收盘数据,然后继续计算30天的数据的简单移动平均线(SMA).到目前为止,我有以下内容:

I am working on a program that uses yahoo finance api to collect the historical close data for the number of stocks entered and then go ahead and calculate simple moving average (SMA) for the data for period of 30 days. I have the following so far:

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

这段代码为我提供了指定范围内股票的收盘价.我有两个问题:

This code is giving me the close values for stocks for the specified range. I have two questions:

  1. 当前,hq.close保留所有股票的价值.如何将这些值放入数组中,以便可以对其进行计算以计算每个库存数据的SMA?

  1. Currently, hq.close is holding values for all stocks. How can I put these values in an array so that I can do a computation on it to calculate a SMA for each stock data?

我试图做这样的事情:

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

但这仅给出my_val中的第一批股票的价值.我知道我必须在这里打个圈.我尝试放

But this only gives the value of first stock in my_val. I know I have to put a loop here. I tried putting

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

  • 如何在Ruby中计算SMA?

  • How can I calculate a SMA in Ruby?

    推荐答案

    您在这里问了两个问题,所以让我们一次解决一个问题.

    You've asked two questions here, so let's address them one at a time.

    首先,此代码:

    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中产生以下哈希,据我所知,该哈希是您想要的格式:

    ... will produce the following hash in closes, which I understand is in the format you want:

    {
      "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]
    }
    

    第二,您要计算一个简单的移动平均线-对于金融应用程序来说,它只是.有一个名为simple_statistics的宝石可以做到这一点.

    Secondly, you want to calculate a simple moving average - which for financial applications is just the mean of the values. There is a Gem called simple_statistics that can do this.

    此代码:

    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中产生以下哈希:

    ... produces the following hash in averages:

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

    这篇关于如何计算简单的移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆