Repository navigation
Expand file tree
/
Copy pathindicators.py
More file actions
95 lines (69 loc) · 3.29 KB
/
Copy pathindicators.py
File metadata and controls
95 lines (69 loc) · 3.29 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
import numpy as np
#implement a simple moving average indicator
#import function from loadata.py (create_close_price_numpy_array)
import load_data
def simple_moving_average(data, window_size):
#import close prices here
weights = np.ones(window_size) / window_size
sma = np.convolve(data, weights, mode='valid')
return sma
def exponential_moving_average(data, window_size):
#import close prices here as well (call the import close prices function)
ema = np.zeros_like(data)
alpha = 2 / (window_size + 1)
ema[0] = data[0]
for i in range(1, len(data)):
ema[i] = alpha * data[i] + (1 - alpha) * ema[i - 1]
return ema
#import close prices here to have them returned
def import_close_prices(file_path):
load_data.create_close_price_array(file_path)
#read numpy array from npy file
close_prices = np.load('./close_prices.npy')
return close_prices
def plot_close_prices_vs_sma(close_prices, sma):
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.plot(close_prices, label='Close Prices')
plt.plot(range(len(sma)), sma, label='Simple Moving Average', color='orange')
plt.legend()
plt.show()
def plot_close_prices_vs_ema(close_prices, ema):
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.plot(close_prices, label='Close Prices')
plt.plot(range(len(ema)), ema, label='Exponential Moving Average', color='green')
plt.legend()
plt.show()
#varibles for fast and slow ma, will be set in cli tool later
fast = 10
slow = 100
def run_indicators(file_path, fast, slow):
close_prices = import_close_prices(file_path) #needs to be chnaged to variable path for cli tool
sma_fast= simple_moving_average(close_prices, fast) #sma_fast/slow, ema_fast/slow functions need to be imported into strategy.py
sma_slow= simple_moving_average(close_prices, slow)
ema_fast= exponential_moving_average(close_prices, fast)
ema_slow= exponential_moving_average(close_prices, slow)
return close_prices, sma_fast, sma_slow, ema_fast, ema_slow
if __name__ == "__main__":
close_prices, sma_fast, sma_slow, ema_fast, ema_slow = run_indicators('/home/destiny/Programming/Kraken_OHLCVT/1INCHEUR_1440.csv')
#save sma_10/ema_10 to a npy file
np.save('/home/destiny/Programming/pycpp-indicator-engine/sma_fast.npy', sma_fast)
np.save('/home/destiny/Programming/pycpp-indicator-engine/ema_fast.npy', ema_fast)
np.save('/home/destiny/Programming/pycpp-indicator-engine/sma_slow.npy', sma_slow)
np.save('/home/destiny/Programming/pycpp-indicator-engine/ema_slow.npy', ema_slow)
plot_close_prices_vs_sma(close_prices, sma_fast)
plot_close_prices_vs_ema(close_prices, ema_fast)
plot_close_prices_vs_sma(close_prices, sma_slow)
plot_close_prices_vs_ema(close_prices, ema_slow)
# def compare_lengths_sma_close_prices(close_prices, sma):
# len_close = len(close_prices)
# len_sma = len(sma)
# print(f"Length of Close Prices: {len_close}")
# print(f"Length of SMA: {len_sma}")
# if len_close > len_sma:
# print(f"Close Prices is longer by {len_close - len_sma} elements.")
# elif len_sma > len_close:
# print(f"SMA is longer by {len_sma - len_close} elements.")
# else:
# print("Both arrays are of equal length.")