Class: JIJI::Dao::TimedDataDao

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/dao/timed_data_dao.rb

Overview

時間を持つデータをCSVで記録するためのDao  データの記録、  一定期間のデータの集約結果の記録、  一定期間のデータの読み込み、 をサポート。

Constant Summary collapse

DATE_FORMAT =
"%Y-%m-%d"
MONTHLY_DATE_FORMAT =
"%Y-%m"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir, aggregator = []) ⇒ TimedDataDao

Returns a new instance of TimedDataDao.



31
32
33
34
# File 'lib/jiji/dao/timed_data_dao.rb', line 31

def initialize( root_dir, aggregator=[] )
  @root_dir = root_dir
  @aggregator = aggregator
end

Instance Attribute Details

#aggregatorObject (readonly)

Returns the value of attribute aggregator.



136
137
138
# File 'lib/jiji/dao/timed_data_dao.rb', line 136

def aggregator
  @aggregator
end

Class Method Details

.dairy?(scale) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/jiji/dao/timed_data_dao.rb', line 126

def self.dairy?(scale)
  scale.to_s =~ /^(raw|\d+[smh])$/
end

Instance Method Details

#<<(timed_data) ⇒ Object

データを追加する



66
67
68
69
70
71
72
73
# File 'lib/jiji/dao/timed_data_dao.rb', line 66

def <<(timed_data)
  @aggregator.each {|ag|
    ag_dir = ag_dir(ag)
    ag.next( timed_data ) {|data|
      out( ag, ag_dir, data )
    }
  }
end

#each_dairy_data(scale, date, start_date = nil, end_date = nil) ⇒ Object

特定の日のデータを列挙する



131
132
133
134
# File 'lib/jiji/dao/timed_data_dao.rb', line 131

def each_dairy_data( scale, date, start_date=nil, end_date=nil)
  file = "#{@root_dir}/#{scale}/#{to_date_str(date, TimedDataDao.dairy?(scale))}.csv"
  each_file_data( file, start_date, end_date )
end

#each_data(scale = :raw, start_time = nil, end_time = nil, &block) ⇒ Object

指定期間のデータを読み込む



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jiji/dao/timed_data_dao.rb', line 37

def each_data( scale=:raw, start_time=nil, end_time=nil, &block )
  sd = start_time || first_day( scale )
  ed = end_time || last_day( scale )

  it = ( !sd || !ed ) \
    ? EmptyIterator.new \
    : TimedDataIterator.new( scale, sd, ed, self )
  if block_given?
    it.each {|row|
      time = row.pop.to_i
      block.call( row, time )
    }
  else
    return it
  end
end

#first_day(scale) ⇒ Object

利用可能な最初のデータ



90
91
92
93
# File 'lib/jiji/dao/timed_data_dao.rb', line 90

def first_day( scale )
  l = list_data_files( scale )
  l.empty? ? nil : to_time( l.first[0..-5], TimedDataDao.dairy?(scale) )
end

#first_time(scale) ⇒ Object

利用可能な最初のデータ



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jiji/dao/timed_data_dao.rb', line 106

def first_time( scale )
  l = list_data_files( scale )
  return nil if l.empty?
  date = nil
  it = each_file_data( "#{@root_dir}/#{scale}/#{l.first}" )
  it.each { |row|
    date = row.last
    break
  }
  date.to_i
end

#flush(time) ⇒ Object

データを強制的に書き込む バックテストで取引が完了し場合に使用し、結果を表性的に書き出す。



56
57
58
59
60
61
62
63
# File 'lib/jiji/dao/timed_data_dao.rb', line 56

def flush( time )
  @aggregator.each {|ag|
    ag_dir = ag_dir(ag)
    ag.flush( time ) {|data|
      out( ag, ag_dir, data )
    }
  }
end

#last_day(scale) ⇒ Object

利用可能な最後のデータ



80
81
82
83
84
85
86
87
88
# File 'lib/jiji/dao/timed_data_dao.rb', line 80

def last_day( scale )
  l = list_data_files( scale )
  return nil if l.empty?
  if TimedDataDao.dairy?(scale)
    to_time( l.last[0..-5], true ) + 24*60*60
  else
    to_time( l.last[0..-5], false ) + 24*60*60*31
  end
end

#last_time(scale) ⇒ Object

利用可能な最後のデータ



95
96
97
98
99
100
101
102
103
104
# File 'lib/jiji/dao/timed_data_dao.rb', line 95

def last_time( scale )
  l = list_data_files( scale )
  return nil if l.empty?
  date = nil
  it = each_file_data( "#{@root_dir}/#{scale}/#{l.last}" )
  it.each { |row|
    date = row.last
  }
  date.to_i
end

#list_data_files(scale, pattern = "*") ⇒ Object

利用可能なデータ一覧を得る



76
77
78
# File 'lib/jiji/dao/timed_data_dao.rb', line 76

def list_data_files( scale, pattern="*" )
  Dir.glob( "#{@root_dir}/#{scale.to_s}/#{pattern}.csv" ).map{|d| File.basename(d) }.sort!
end

#to_date_str(time, dairy = true) ⇒ Object

時刻を日を示す文字列に変換する



123
124
125
# File 'lib/jiji/dao/timed_data_dao.rb', line 123

def to_date_str( time, dairy=true )
  time.strftime( dairy ? DATE_FORMAT : MONTHLY_DATE_FORMAT)
end

#to_time(str, dairy = true) ⇒ Object

文字列を時刻に変換する



118
119
120
121
# File 'lib/jiji/dao/timed_data_dao.rb', line 118

def to_time( str, dairy=true )
  d = DateTime.strptime(str, dairy ? DATE_FORMAT : MONTHLY_DATE_FORMAT)
  Time.local( d.year, d.month, d.day )
end