rivella50 / talib-ruby

Ruby Wrapper for the Technical Analysis Library ta-lib
55 stars 31 forks source link

Patch 1217 #11

Closed mephistobooks closed 10 years ago

mephistobooks commented 10 years ago

Hello, rivella50-san.

I found one more. It seems that the index of results are unshifted. So there is a difference from C/C++. https://gist.github.com/mephistobooks/7992785

Could you please check the above gist text?

The suggestion of mine for this is correcting a for-statement in ta_func_call() in talib.c.

thank you, mephistobooks

mephistobooks commented 10 years ago

rivella50-san, I'm sorry but patch-1217 still has bugs. Please forget this. (I did not get the meaning of begind and nbelem well..., but it seems there is a problem.)

rivella50 commented 10 years ago

Hi there, for MACD you could try with this real world example code:

require 'rubygems'
require 'talib_ruby'
require 'date'
require 'time'
require 'uri'
require 'net/http'

  class Macd

    # ascending ordered data
    def load_stock_history(share_id)
      url_share = "http://ichart.finance.yahoo.com/table.csv?s=SHARE_ID&TODAY&g=d&a=0&b=2&c=2000&ignore=.csv"
      response = Net::HTTP.get_response(URI.parse(url_share.sub('SHARE_ID', share_id)))
      response.body
    end

    def build_daily_chart_items(data, horizon)
      items = []
      lines = data.split("\n")
      (1..(lines.length-1)).each do |i|
        line_items = lines[i].split(',')
        item_date = DateTime.strptime(line_items[0], '%Y-%m-%d')
        break if item_date < horizon
        item = { :date => DateTime.new(item_date.year, item_date.month, item_date.day, 23, 59, 59),
                        :open => line_items[1].to_f, :high => line_items[2].to_f,
                        :low => line_items[3].to_f, :close => line_items[4].to_f }
        items << item
      end
      items.reverse
    end

    # items are asc sorted
    def build_closes(items)
      closes = []

      items.each do |item|
        closes << item[:close]
      end

      return closes
    end

  end

slower_period = 12
longer_period = 26
macd_period   = 9
l = TaLib::Function.new("MACD")
stoch = Macd.new
data =  stoch.load_stock_history("ABBN.VX")
items = stoch.build_daily_chart_items(data, (Date.today.<< 12))

values = stoch.build_closes(items)
values_length = values.length

out_k    = Array.new(values_length)
out_d    = Array.new(values_length)
out_hist = Array.new(values_length)

l.in_real(0, values)
l.out_real(0, out_k)
l.out_real(1, out_d)
l.out_real(2, out_hist)
l.opt_int(0, slower_period)
l.opt_int(1, longer_period)
l.opt_int(2, macd_period)
p "lookback is #{l.lookback}"
l.call(0, values_length-1)

p "k: #{out_k.join(',')}"
p "d: #{out_d.join(',')}"
p "hist: #{out_hist.join(',')}"

For the printed outputs you can see that talib leaves more than one empty slot at the beginning. Since this is filled by talib i wouldn't change that in the wrapper as well. It would be interesting which outputs you get when you use the same input data as in the example above. Thanks for your efforts.

mephistobooks commented 10 years ago

Thank you for your confirming, rivella50-san.

But what I would like to say is: