Skip to content

Commit ac86dd4

Browse files
committed
added sma and ema in indicators
1 parent 5fcc84b commit ac86dd4

File tree

5 files changed

+50
-168
lines changed

5 files changed

+50
-168
lines changed

algorithms/live algo/__init__.py

Whitespace-only changes.

algorithms/live algo/algorithm.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

algorithms/live algo/main.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

algorithms/live algo/portfolio.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

technical analysis/indicators.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# TODO: Request data from binance servers and create a standard moving average using numpy
2+
3+
import numpy as np
4+
from pprint import pprint
5+
import matplotlib.pyplot as plt
6+
7+
from data import closes
8+
9+
a = np.array(closes)
10+
11+
# Simple Moving Average with a rolling window of 'w'
12+
def sma(arr, window):
13+
return np.convolve(arr, np.ones(window), 'valid') / window
14+
15+
# Exponential Weighted Moving Average
16+
def ewma(arr, alpha, window):
17+
'''
18+
:arr: array
19+
:param alpha: specify decay [0, 1]
20+
:window: length of ewma
21+
'''
22+
arr = arr[-(len(arr) - window + 1):] # Re-adjusting length of arr so it has the same len as sma
23+
ewma_arr = np.zeros_like(arr) # returns an array of zeros the same length as arr
24+
ewma_arr[0] = arr[0] # first value in list ewma_arr is equal to first value in list arr
25+
for t in range(1, arr.shape[0]):
26+
ewma_arr[t] = alpha * arr[t] + (1 - alpha) * ewma_arr[t - 1]
27+
28+
return ewma_arr
29+
30+
sma = sma(
31+
arr=a,
32+
window=20
33+
)
34+
35+
ewma = ewma(
36+
arr=a,
37+
alpha=0.33,
38+
window=20
39+
)
40+
41+
a = a[-481:]
42+
43+
fig, ax = plt.subplots()
44+
45+
ax.plot(a, label='BTCUSDT close')
46+
ax.plot(sma, label='sma 20 periods')
47+
ax.plot(ewma, label='ewma 20 periods')
48+
leg = ax.legend()
49+
50+
plt.show()

0 commit comments

Comments
 (0)