-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcandlestick.rb
167 lines (143 loc) · 4.16 KB
/
candlestick.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# ruby candlestick.rb postgres://user:pass@host:port/db_name
# @see https://timescale.github.io/timescaledb-ruby/toolkit_candlestick/
require 'bundler/inline' #require only what you need
gemfile(true) do
gem 'timescaledb', path: '../..'
gem 'pry'
gem 'puma'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'sinatra-reloader'
end
ActiveRecord::Base.establish_connection ARGV.first
def db(&block)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.connection.instance_exec(&block)
ActiveRecord::Base.logger = nil
end
class Tick < ActiveRecord::Base
extend Timescaledb::ActsAsHypertable
include Timescaledb:: ContinuousAggregatesHelper
acts_as_hypertable time_column: "time",
segment_by: "symbol",
value_column: "price"
scope :plotly_candlestick, -> (from: nil) do
data = ohlcv.to_a
{
type: 'candlestick',
xaxis: 'x',
yaxis: 'y',
x: data.map(&:time),
open: data.map(&:open),
high: data.map(&:high),
low: data.map(&:low),
close: data.map(&:close),
volume: data.map(&:volume)
}
end
continuous_aggregates(
timeframes: [:minute, :hour, :day, :month],
scopes: [:_candlestick]
)
descendants.each do |cagg|
cagg.class_eval do
self.time_vector_options = time_vector_options.merge(value_column: :close)
[:open, :high, :low, :close].each do |attr|
attribute attr, :decimal, precision: 10, scale: 2
end
[:volume, :vwap].each do |attr|
attribute attr, :integer
end
[:open_time, :high_time, :low_time, :close_time].each do |attr|
attribute attr, :time
end
scope :ohlcv, -> do
unscoped
.from("(#{to_sql}) AS candlestick")
.select(time_column, *segment_by_column,
"open(candlestick),
high(candlestick),
low(candlestick),
close(candlestick),
open_time(candlestick),
high_time(candlestick),
low_time(candlestick),
close_time(candlestick),
volume(candlestick),
vwap(candlestick)")
end
end
end
end
db do
if true
#Tick.drop_continuous_aggregates
#drop_table :ticks, if_exists: true, force: :cascade
hypertable_options = {
time_column: "time",
chunk_time_interval: "1 day",
compress_segmentby: "symbol",
compress_orderby: "time",
compress_after: "1 week"
}
create_table :ticks, id: false, hypertable: hypertable_options, if_not_exists: true do |t|
t.timestamptz :time, null: false
t.string :symbol, null: false
t.decimal :price
t.integer :volume
end
add_index :ticks, [:time, :symbol], if_not_exists: true
end
execute(ActiveRecord::Base.sanitize_sql_for_conditions( [<<~SQL, {from: 1.week.ago.to_date, to: 1.day.from_now.to_date}]))
INSERT INTO ticks
SELECT time, 'SYMBOL', 1 + (random()*30)::int, 100*(random()*10)::int
FROM generate_series(TIMESTAMP :from,
TIMESTAMP :to,
INTERVAL '10 second') AS time;
SQL
Tick.create_continuous_aggregates
Tick.refresh_aggregates
end
if ARGV.include?("--pry")
Pry.start
return
end
require 'sinatra/base'
require "sinatra/json"
class App < Sinatra::Base
register Sinatra::Reloader
get '/candlestick.js' do
send_file 'candlestick.js'
end
get '/daily_close_price' do
json({
title: "Daily",
data: Tick::CandlestickPerDay.previous_week.plotly_candlestick
})
end
get '/candlestick_1m' do
json({
title: "Candlestick 1 minute last hour",
data: Tick::CandlestickPerMinute.last_hour.plotly_candlestick
})
end
get '/candlestick_1h' do
json({
title: "Candlestick yesterday hourly",
data:Tick::CandlestickPerHour.yesterday.plotly_candlestick
})
end
get '/' do
<<~HTML
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src='https://cdn.plot.ly/plotly-2.17.1.min.js'></script>
<script src='/candlestick.js'></script>
</head>
<body>
<div id='charts'>
</body>
HTML
end
end
App.run!