-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
227 lines (204 loc) · 11.3 KB
/
functions.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from torch.utils.data import TensorDataset, DataLoader
import numpy as np
import torch
from model import LSTMModel
import torch.nn as nn
import matplotlib.pyplot as plt
import os
import pandas as pd
import talib
def search_null_value(df):
columns_with_null = df.columns[df.isnull().any()]
print('columns_with_null:',columns_with_null)
for column in columns_with_null:
print(f"Null values in column '{column}':")
print(df[df[column].isnull()])
def prepare_data_forlstm(data):
data = data.dropna(how = "any", ignore_index = True)
data = data.sort_values(["Date"]).reset_index(drop=True)
return np.array(data['Close']), np.array(data['rsi_14'])
def detect_outliers_iqr(data, threshold=1.5):
q1 = np.percentile(data, 25)
q3 = np.percentile(data, 75)
iqr = q3 - q1
lower_bound = q1 - (threshold * iqr)
upper_bound = q3 + (threshold * iqr)
return len(np.where((data < lower_bound) | (data > upper_bound))[0])
def predict_nanvalue_lstm(data, column_name, model_path, device, default_value = 0):
if pd.isna(data[column_name]) or data[column_name] in [None,np.nan,""]:
close = float(data['Close'])
model = LSTMModel()
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
x = np.array([[[close]]])
x = torch.tensor(x, dtype=torch.float32).to(device)
x = x.to(torch.float32)
with torch.no_grad():
prediction = model(x).item()
if np.isnan(prediction):
prediction = default_value
return prediction
else:
return data[column_name]
def predict_lstm_single(close_value, model_path, device, default_value = 0):
# print("device",device)
close = float(close_value)
model = LSTMModel()
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
x = np.array([[[close]]])
x = torch.tensor(x, dtype=torch.float32).to(device)
x = x.to(torch.float32)
with torch.no_grad():
prediction = model(x).item()
if np.isnan(prediction):
prediction = default_value
return prediction
def predict_lstm_multiple(data:pd.Series, model_path:str, device, default_value = 0):
model = LSTMModel(input_size=len(data.keys()))
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
x = np.array([[data]])
x = torch.tensor(x,dtype=torch.float32).to(device)
x = x.to(torch.float32)
with torch.no_grad():
prediction = model(x).item()
if np.isnan(prediction):
prediction = default_value
return prediction
# must fix parameter model and parameter data
def predict_nanvalue_lstm_vwma(data, column_name, model_path, device, default_value = 0):
if pd.isna(data[column_name]) or data[column_name] in [None,np.nan,""]:
close = float(data['Close'])
volumn = float(data['Volume'])
model = LSTMModel()
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
x = np.array([[[close,volumn]]])
x = torch.tensor(x,dtype=torch.float32).to(device)
x = x.to(torch.float32)
with torch.no_grad():
prediction = model(x).item()
if np.isnan(prediction):
prediction = default_value
return prediction
else:
return data[column_name]
def predict_nanvalue_lstm_ichimoku(data, column_name, model_path, device, default_value = 0):
if pd.isna(data[column_name]) or data[column_name] in [None,np.nan,""]:
close = float(data['Close'])
high = float(data['High'])
low = float(data['Low'])
model = LSTMModel()
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
x = np.array([[[close,high,low]]])
x = torch.tensor(x,dtype=torch.float32).to(device)
x = x.to(torch.float32)
with torch.no_grad():
prediction = model(x).item()
if np.isnan(prediction):
prediction = default_value
return prediction
else:
return data[column_name]
def refill_missingvalue(data, column_name, default_value = 0):
if pd.isna(data[column_name]) or data[column_name] in [None,np.nan,""]:
return default_value
else:
return data[column_name]
def return_candle_pattern(data):
# 'Open', 'High', 'Low', 'Close', 'Volume'
data['CDL2CROWS'] = talib.CDL2CROWS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDL3BLACKCROWS'] = talib.CDL3BLACKCROWS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDL3INSIDE'] = talib.CDL3INSIDE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDL3LINESTRIKE'] = talib.CDL3LINESTRIKE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDL3OUTSIDE'] = talib.CDL3OUTSIDE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDL3STARSINSOUTH'] = talib.CDL3STARSINSOUTH(data['Open'], data['High'], data['Low'], data['Close'])
data['CDL3WHITESOLDIERS'] = talib.CDL3WHITESOLDIERS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLABANDONEDBABY'] = talib.CDLABANDONEDBABY(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLADVANCEBLOCK'] = talib.CDLADVANCEBLOCK(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLBELTHOLD'] = talib.CDLBELTHOLD(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLBREAKAWAY'] = talib.CDLBREAKAWAY(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLCLOSINGMARUBOZU'] = talib.CDLCLOSINGMARUBOZU(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLCONCEALBABYSWALL'] = talib.CDLCONCEALBABYSWALL(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLCOUNTERATTACK'] = talib.CDLCOUNTERATTACK(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLDARKCLOUDCOVER'] = talib.CDLDARKCLOUDCOVER(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLDOJI'] = talib.CDLDOJI(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLDOJISTAR'] = talib.CDLDOJISTAR(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLDRAGONFLYDOJI'] = talib.CDLDRAGONFLYDOJI(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLENGULFING'] = talib.CDLENGULFING(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLEVENINGDOJISTAR'] = talib.CDLEVENINGDOJISTAR(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLGRAVESTONEDOJI'] = talib.CDLGRAVESTONEDOJI(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHAMMER'] = talib.CDLHAMMER(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHANGINGMAN'] = talib.CDLHANGINGMAN(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHARAMI'] = talib.CDLHARAMI(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHARAMICROSS'] = talib.CDLHARAMICROSS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHIGHWAVE'] = talib.CDLHIGHWAVE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHIKKAKE'] = talib.CDLHIKKAKE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHIKKAKEMOD'] = talib.CDLHIKKAKEMOD(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLHOMINGPIGEON'] = talib.CDLHOMINGPIGEON(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLIDENTICAL3CROWS'] = talib.CDLIDENTICAL3CROWS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLINNECK'] = talib.CDLINNECK(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLINVERTEDHAMMER'] = talib.CDLINVERTEDHAMMER(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLKICKING'] = talib.CDLKICKING(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLKICKINGBYLENGTH'] = talib.CDLKICKINGBYLENGTH(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLLADDERBOTTOM'] = talib.CDLLADDERBOTTOM(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLLONGLEGGEDDOJI'] = talib.CDLLONGLEGGEDDOJI(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLLONGLINE'] = talib.CDLLONGLINE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLMARUBOZU'] = talib.CDLMARUBOZU(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLMATCHINGLOW'] = talib.CDLMATCHINGLOW(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLMATHOLD'] = talib.CDLMATHOLD(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLMORNINGDOJISTAR'] = talib.CDLMORNINGDOJISTAR(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLMORNINGSTAR'] = talib.CDLMORNINGSTAR(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLONNECK'] = talib.CDLONNECK(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLPIERCING'] = talib.CDLPIERCING(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLRICKSHAWMAN'] = talib.CDLRICKSHAWMAN(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLRISEFALL3METHODS'] = talib.CDLRISEFALL3METHODS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLSEPARATINGLINES'] = talib.CDLSEPARATINGLINES(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLSHOOTINGSTAR'] = talib.CDLSHOOTINGSTAR(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLSHORTLINE'] = talib.CDLSHORTLINE(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLSPINNINGTOP'] = talib.CDLSPINNINGTOP(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLSTALLEDPATTERN'] = talib.CDLSTALLEDPATTERN(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLSTICKSANDWICH'] = talib.CDLSTICKSANDWICH(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLTAKURI'] = talib.CDLTAKURI(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLTASUKIGAP'] = talib.CDLTASUKIGAP(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLTHRUSTING'] = talib.CDLTHRUSTING(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLTRISTAR'] = talib.CDLTRISTAR(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLUNIQUE3RIVER'] = talib.CDLUNIQUE3RIVER(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLUPSIDEGAP2CROWS'] = talib.CDLUPSIDEGAP2CROWS(data['Open'], data['High'], data['Low'], data['Close'])
data['CDLXSIDEGAP3METHODS'] = talib.CDLXSIDEGAP3METHODS(data['Open'], data['High'], data['Low'], data['Close'])
return data
# calculate indicator
def cal_rsi(value):
value = float(value)
if value >= 0.7:
return 1
elif value <= 0.3:
return -1
else:
return 0
def cal_storsi(value):
value = float(value)
if value >= 0.8:
return 1
elif value <= 0.2:
return -1
else:
return 0
def cal_tema(value, min_tema, max_tema):
tema_min = value[f'tema_{min_tema}']
tema_max = value[f'tema_{max_tema}']
tema_min = float(tema_min)
tema_max = float(tema_max)
if tema_min > tema_max:
return 1
elif tema_min < tema_max:
return -1
else:
return 0