class Iqfeed::HistoryClient
Attributes
end_session[RW]
max_tick_number[RW]
old_to_new[RW]
start_session[RW]
ticks_per_send[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/iqfeed/history_client.rb, line 100 def initialize(options = {}) parse_options(options) @request_id = 0 end
Public Instance Methods
close()
click to toggle source
# File lib/iqfeed/history_client.rb, line 183 def close @socket.close end
format_request_id(type)
click to toggle source
# File lib/iqfeed/history_client.rb, line 144 def format_request_id(type) type.to_s + @request_id.to_s.rjust(7, '0') end
get_daily_range(options, observer)
click to toggle source
# File lib/iqfeed/history_client.rb, line 166 def get_daily_range(options, observer) @socket.printf "HDT,%s,%s,%s,%07d,%d,2%07d,%07d\r\n", options[:symbol], options[:from].strftime("%Y%m%d %H%M%S"), options[:to].strftime("%Y%m%d %H%M%S"), @max_tick_number, @old_to_new, @request_id, @ticks_per_send add_observer(observer) end
get_ohlc_range(options, observer)
click to toggle source
# File lib/iqfeed/history_client.rb, line 173 def get_ohlc_range(options, observer) printf "HIT,%s,%07d,%s,%s,%07d,%s,%s,%d,1%07d,%07d\r\n", options[:symbol], options[:duration], options[:from].strftime("%Y%m%d %H%M%S"), options[:to].strftime("%Y%m%d %H%M%S"), @max_tick_number, @start_session, @end_session, @old_to_new, @request_id, @ticks_per_send @socket.printf "HIT,%s,%07d,%s,%s,%07d,%s,%s,%d,1%07d,%07d\r\n", options[:symbol], options[:duration], options[:from].strftime("%Y%m%d %H%M%S"), options[:to].strftime("%Y%m%d %H%M%S"), @max_tick_number, @start_session, @end_session, @old_to_new, @request_id, @ticks_per_send add_observer(observer) end
get_tick_range(options, observer)
click to toggle source
# File lib/iqfeed/history_client.rb, line 156 def get_tick_range(options, observer) printf "HTT,%s,%s,%s,%07d,%s,%s,%d,0%07d,%07d\r\n", options[:symbol], options[:from].strftime("%Y%m%d %H%M%S"), options[:to].strftime("%Y%m%d %H%M%S"), @max_tick_number, @start_session, @end_session, @old_to_new, @request_id, @ticks_per_send @socket.printf "HTT,%s,%s,%s,%07d,%s,%s,%d,0%07d,%07d\r\n", options[:symbol], options[:from].strftime("%Y%m%d %H%M%S"), options[:to].strftime("%Y%m%d %H%M%S"), @max_tick_number, @start_session, @end_session, @old_to_new, @request_id, @ticks_per_send add_observer(observer) end
open()
click to toggle source
# File lib/iqfeed/history_client.rb, line 116 def open @socket = TCPSocket.open @host, @port @socket.puts "S,SET CLIENT NAME," + @name end
parse_options(options)
click to toggle source
# File lib/iqfeed/history_client.rb, line 105 def parse_options(options) @host = options[:host] || 'localhost' @port = options[:port] || 9100 @name = options[:name] || 'DEMO' @max_tick_number = options[:max_tick_number] || 5000000 @start_session = options[:start_session] || '000000' @end_session = options[:end_session] || '235959' @old_to_new = options[:old_to_new] || 1 @ticks_per_send = options[:ticks_per_send] || 500 end
process_request() { |procs[fields[0].to_i].call(line)| ... }
click to toggle source
# File lib/iqfeed/history_client.rb, line 121 def process_request procs = [] exception = nil procs[0] = Proc.new{|line| Iqfeed::Tick.parse(line)} procs[1] = Proc.new{|line| Iqfeed::OHLC.parse(line)} procs[2] = Proc.new{|line| Iqfeed::DWM.parse(line)} while line = @socket.gets fields = line.match(/^([^,]+),(.*)/) line = fields[2] if line =~ /^E,/ exception = 'No Data' elsif line =~ /!ENDMSG!,/ break end yield procs[fields[1][0].to_i].call(line) end if exception raise NoDataError.new("No Data") end end
run()
click to toggle source
# File lib/iqfeed/history_client.rb, line 148 def run process_request do |line| changed notify_observers(line) end @request_id = @request_id + 1 end