/
cron_chart.rb
59 lines (48 loc) · 1.36 KB
/
cron_chart.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require './config.rb'
require './stooq_db.rb'
require 'fileutils'
require './plot_chart.rb'
def make_filename(symbol, datetime, size)
path = File.join(TNST_CHART_PATH, datetime.strftime("%Y/%m/%d"))
fn = "%s_%s_%s.gif" % [symbol.downcase, datetime.strftime("%Y%m%d%H%M%S"), size]
File.join(path, fn)
end
def save_chart(symbol, datetime, size, data)
path = File.join(TNST_CHART_PATH, datetime.strftime("%Y/%m/%d"))
FileUtils.mkdir_p(path)
fname = make_filename(symbol, datetime, size)
File.open(fname, "wb"){|io| io.write(data) }
# $stderr.puts fname
end
def plot_chart(symbol, datetime)
fetcher = Candlizer.new(@mysql)
fetcher.symbol = symbol
fetcher.date = Date.today
plotter = ChartPlotter.new(fetcher)
plotter.output = nil
plotproc = proc{|pl|
IO.popen("-", "r+") do |io|
if io then
# parent
io.read
else
# child
Gnuplot.open do |gp|
pl.gnuplot = gp
pl.plot
end
end
end
}
plotter.config = ChartConfigPreset::Large
img = plotproc.call(plotter)
save_chart(symbol, datetime, 'l', img)
plotter.config = ChartConfigPreset::Medium
img = plotproc.call(plotter)
save_chart(symbol, datetime, 'm', img)
plotter.config = ChartConfigPreset::Small
img = plotproc.call(plotter)
save_chart(symbol, datetime, 's', img)
rescue
p [$!, $@, __FILE__, __LINE__]
end