forked from warp-id/solana-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
technicalAnalysis.ts
188 lines (142 loc) · 6.04 KB
/
technicalAnalysis.ts
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
import { BotConfig } from "./bot";
export class TechnicalAnalysis {
constructor(public botConfig: BotConfig) { }
public calculateEMAs = (prices: number[]): { EMA_3: number, EMA_18: number, prevEMA_3: number } => {
const shortPeriod = 3;
const longPeriod = 18;
if (prices.length < longPeriod - 1) {
return { EMA_3: null, EMA_18: null, prevEMA_3: null };
}
const shortMultiplier = 2 / (shortPeriod + 1);
const longMultiplier = 2 / (longPeriod + 1);
let shortEMA = prices.slice(0, shortPeriod).reduce((acc, val) => acc + val, 0) / shortPeriod;
let longEMA = prices.slice(0, longPeriod).reduce((acc, val) => acc + val, 0) / longPeriod;
let prevEMA = shortEMA;
prices.forEach(price => {
prevEMA = shortEMA;
shortEMA = (price - shortEMA) * shortMultiplier + shortEMA;
longEMA = (price - longEMA) * longMultiplier + longEMA;
});
return {
EMA_3: shortEMA,
EMA_18: longEMA,
prevEMA_3: prevEMA
};
}
public calculateMACDv2 = (
prices: number[],
_shortPeriod : number = null,
_longPeriod: number = null,
_signalPeriod: number = null
): { macd: number, signal: number } => {
const shortPeriod = _shortPeriod ?? this.botConfig.MACDShortPeriod;
const longPeriod = _longPeriod ?? this.botConfig.MACDLongPeriod;
const signalPeriod = _signalPeriod ?? this.botConfig.MACDSignalPeriod;
if (prices.length < longPeriod + signalPeriod - 1) {
return { macd: null, signal: null };
}
const shortMultiplier = 2 / (shortPeriod + 1);
const longMultiplier = 2 / (longPeriod + 1);
let shortEMA = prices.slice(0, shortPeriod).reduce((acc, val) => acc + val, 0) / shortPeriod;
let longEMA = prices.slice(0, longPeriod).reduce((acc, val) => acc + val, 0) / longPeriod;
const macdLine: number[] = [];
for (let i = longPeriod; i < prices.length; i++) {
shortEMA = (prices[i] - shortEMA) * shortMultiplier + shortEMA;
longEMA = (prices[i] - longEMA) * longMultiplier + longEMA;
const macdValue = shortEMA - longEMA;
macdLine.push(macdValue);
}
let sum = 0;
for (let i = 0; i < signalPeriod; i++) {
sum += macdLine[i];
}
let signalEMA = sum / signalPeriod;
const signal: number[] = [signalEMA];
const signalMultiplier = 2 / (signalPeriod + 1);
for (let i = signalPeriod; i < macdLine.length; i++) {
signalEMA = (macdLine[i] - signalEMA) * signalMultiplier + signalEMA;
signal.push(signalEMA);
}
return {
macd: macdLine[macdLine.length - 1],
signal: signal[signal.length - 1]
};
}
public calculateRSIv2 = (prices: number[]): number => {
const period = this.botConfig.RSIPeriod;
const delta: number[] = [];
let gainSum = 0;
let lossSum = 0;
for (let i = 1; i < prices.length; i++) {
delta.push(prices[i] - prices[i - 1]);
}
for (let i = 0; i < period; i++) {
if (delta[i] > 0) {
gainSum += delta[i];
} else {
lossSum += Math.abs(delta[i]);
}
}
const initialAvgGain = gainSum / period;
const initialAvgLoss = lossSum / period;
let prevAvgGain = initialAvgGain;
let prevAvgLoss = initialAvgLoss;
let cRSI = 0;
for (let i = period; i < prices.length; i++) {
const gain = delta[i] > 0 ? delta[i] : 0;
const loss = delta[i] < 0 ? Math.abs(delta[i]) : 0;
const avgGain = ((prevAvgGain * (period - 1)) + gain) / period;
const avgLoss = ((prevAvgLoss * (period - 1)) + loss) / period;
const RS = avgGain / avgLoss;
cRSI = 100 - (100 / (1 + RS));
prevAvgGain = avgGain;
prevAvgLoss = avgLoss;
}
return cRSI;
}
public calculateRSIv3 = (prices: number[], _period: number = null): { RSI: number, RSI_EMA_11: number, RSI_prevEMA_11: number } => {
const period = _period ?? this.botConfig.RSIPeriod;
const emaPeriod = 11;
const delta: number[] = [];
const rsiValues: number[] = [];
let gainSum = 0;
let lossSum = 0;
for (let i = 1; i < prices.length; i++) {
delta.push(prices[i] - prices[i - 1]);
}
for (let i = 0; i < period; i++) {
if (delta[i] > 0) {
gainSum += delta[i];
} else {
lossSum += Math.abs(delta[i]);
}
}
const initialAvgGain = gainSum / period;
const initialAvgLoss = lossSum / period;
let prevAvgGain = initialAvgGain;
let prevAvgLoss = initialAvgLoss;
let cRSI = 0;
for (let i = period; i < prices.length; i++) {
const gain = delta[i] > 0 ? delta[i] : 0;
const loss = delta[i] < 0 ? Math.abs(delta[i]) : 0;
const avgGain = ((prevAvgGain * (period - 1)) + gain) / period;
const avgLoss = ((prevAvgLoss * (period - 1)) + loss) / period;
const RS = avgGain / avgLoss;
cRSI = 100 - (100 / (1 + RS));
prevAvgGain = avgGain;
prevAvgLoss = avgLoss;
rsiValues.push(cRSI);
}
if (rsiValues.length < emaPeriod) {
return { RSI: cRSI, RSI_EMA_11: NaN, RSI_prevEMA_11: NaN };
}
const emaMultiplier = 2 / (emaPeriod + 1);
let ema_11 = rsiValues.slice(0, emaPeriod).reduce((acc, val) => acc + val, 0) / emaPeriod;
let prevEMA_11 = ema_11;
for (let i = emaPeriod; i < rsiValues.length; i++) {
prevEMA_11 = ema_11;
ema_11 = (rsiValues[i] - ema_11) * emaMultiplier + ema_11;
}
return { RSI: cRSI, RSI_EMA_11: ema_11, RSI_prevEMA_11: prevEMA_11 };
}
}