-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsi_algorithm.rb
More file actions
193 lines (168 loc) · 5.07 KB
/
Copy pathrsi_algorithm.rb
File metadata and controls
193 lines (168 loc) · 5.07 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Dir["./models/*.rb"].each {|file| require file }
require('./bot')
require('linear-regression')
require('matrix')
class RsiAlgorithm
attr_reader :price_history
attr_reader :last_rsi
def rsi_is_a_valley?(rsiArray, index)
if rsiArray[index - 1] && rsiArray[index + 1]
first = rsiArray[index - 1]
last = rsiArray[index + 1]
if first >= rsiArray[index] && last >= rsiArray[index]
true
else
false
end
else
false
end
end
def rsi_is_a_peak?(rsiArray, index)
if rsiArray[index - 1] && rsiArray[index + 1]
first = rsiArray[index - 1]
last = rsiArray[index + 1]
if first <= rsiArray[index] && last <= rsiArray[index]
true
else
false
end
else
false
end
end
def initialize(rsiTolerance: 10, price_history: [], buy_zone: 30, sell_zone: 70)
@rsiTolerance = rsiTolerance
@price_history = price_history
@buy_zone = buy_zone
@sell_zone = sell_zone
@last_rsi = ""
end
# format the number to make sure I dont run into floating point issues
def format_number_to_be_larger_than_one(number)
if (number < 1 && number > 0) || (number < 0 && number > -1)
number = number * 10
format_number_to_be_larger_than_one(number)
else
number.to_f
end
end
def macd_recently_crossed?(macdArray)
# formatting the last and 2nd to last histogram height
firstMacd = format_number_to_be_larger_than_one(macdArray[macdArray.length - 2][0])
nextMacd = format_number_to_be_larger_than_one(macdArray[macdArray.length - 1][0])
# divide the 2 to see if there was a switch in sign.
# If it went from negative to positive, then the result of the division would negative
# If it went from positive to negative, then the result of the division would also be negative
# If it stayed the same sign, then the result of the division would be positive
if ((firstMacd / nextMacd) < 0)
true
else
false
end
end
def average_slope_of_array(array)
slopes = []
array.each_with_index do |a, index|
if index != 0
slope = (array[index] - array[index - 1]).to_f
slopes.push(slope)
end
end
average_slope = slopes.reduce(:+) / slopes.count
format_number_to_be_larger_than_one(average_slope)
end
def regress x, y, degree
x_data = x.map { |xi| (0..degree).map { |pow| (xi**pow).to_f } }
mx = Matrix[*x_data]
my = Matrix.column_vector(y)
((mx.t * mx).inv * mx.t * my).transpose.to_a[0]
end
# just simply calculating whether or not the last 4 rsi numbers crossed into either threshold
# rsi will always cross before macd which is why I test a few places backwards
def rsi_recently_crossed_threshold?(rsiArray)
crossed = false
buy = false
sell = false
last_index = rsiArray.count - 1
buyRsiToInspect = []
sellRsiToInspect = []
# Inspect the last indexes in rsiArray(up to @tolerance) to see if they crossed the
# buy and sell zones
rsiToInspect = rsiArray[(last_index - @rsiTolerance)..last_index]
rsiToInspect.each_with_index do |rsi, index|
if rsi >= @sell_zone && rsi_is_a_peak?(rsiToInspect, index)
crossed = true
sell = true
sellRsiToInspect.push(rsi)
elsif rsi <= @buy_zone && rsi_is_a_valley?(rsiToInspect, index)
buy = true
crossed = true
buyRsiToInspect.push(rsi)
end
end
if buy
if buyRsiToInspect.count > 2
# is it trending up? Then buy
xs = (1..buyRsiToInspect.count).map { |n| n }
# quad_regression_sign = regress(xs, buyRsiToInspect, 2).last
linear_regression = Regression::Linear.new(xs, buyRsiToInspect)
if linear_regression.slope > 0
# if quad_regression_sign > 0
{ crossed: true, sell: false, buy: true }
else
{ crossed: true, sell: false, buy: false }
end
else
{ crossed: true, sell: false, buy: false }
end
elsif sell
if sellRsiToInspect.count > 2
# is it trending down? Then sell
# average_slope = average_slope_of_array(sellRsiToInspect)
xs = (1..sellRsiToInspect.count).map { |n| n }
# quad_regression_sign = regress(xs, sellRsiToInspect, 2).last
linear_regression = Regression::Linear.new(xs, sellRsiToInspect)
# if quad_regression_sign < 0
if linear_regression.slope < 0
{ crossed: true, sell: true, buy: false }
else
{ crossed: true, sell: false, buy: false }
end
else
{ crossed: true, sell: false, buy: false }
end
else
{ crossed: true, sell: false, buy: false }
end
end
def analyze
# Initialize Indicator Class
data = Indicators::Data.new(@price_history)
# calculate MACD
macdArray = data.calc(:type => :macd, :params => [12, 26, 9]).output
# reference to rsi so I know whether to buy or sell
# rsiAlert = rsi_recently_crossed_threshold?(rsi)
# test to see if rsi went to the buy/sell zones and a macd cross
if true #macd_recently_crossed?(macdArray)
# calculate rsi
rsi = data.calc(:type => :rsi, :params => 14).output
rsiAlert = rsi_recently_crossed_threshold?(rsi)
if rsiAlert[:crossed]
if rsiAlert[:buy]
puts "RSI Buy Alert: #{rsiAlert}"
"buy"
elsif rsiAlert[:sell]
puts "RSI Sell Alert: #{rsiAlert}"
"sell"
else
"wait"
end
else
"wait"
end
else
"wait"
end
end
end