Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:16:15+00:00 2026-06-13T01:16:15+00:00

I am working on a program that uses yahoo finance api to collect the

  • 0

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. 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?

    I tried doing something like this:

    "#{hq.close}" my_val = [hq.close]
    puts 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
    

    But this gives me an error:

    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. How can I calculate a SMA in Ruby?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:16:16+00:00Added an answer on June 13, 2026 at 1:16 am

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

    First, this code:

    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
    

    … 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]
    }
    

    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.

    This code:

    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
    

    … produces the following hash in averages:

    { "AAPL" => 634.548, "MSFT" => 29.238, "RHT" => 53.946 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a Android Web Services program that uses a number
I am working on a program that uses IE to display its help pages.
Greetings, Forum. I'm working on a program in Python that uses Twisted to manage
I'm working on a program that asks for input, then calculates and posts the
I'm working on a program that will need to delete a folder (and then
I am working on a program that extracts real time quote for 900+ stocks
Working with a program that uses 16bytes 4v4 one byte matrices : unsigned char
I'm working on a program that uses HTML/CSS/Javascript/JQuery for its user interface. One of
I am working on a C program that uses a Union. The union definition
I have a (C#) genetic program that uses financial time-series data and it's currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.