forked from PY-Develop-Company/Signal-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice_parser.py
More file actions
200 lines (163 loc) · 7.11 KB
/
price_parser.py
File metadata and controls
200 lines (163 loc) · 7.11 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
194
195
196
197
198
199
200
from datetime import datetime, timedelta
from tvDatafeed import TvDatafeed, TvDatafeedLive, Interval
from tvDatafeed.seis import Seis
from pandas import DataFrame, read_csv
import file_manager
import interval_convertor
from my_time import now_time
trade_pause_wait_time = 600
currencies_path = "users/currencies.txt"
currencies_data_path = "currencies_data/"
currency_check_ended = "currencies_data/check_ended/"
currencies_last_analize_date = {}
tv = TvDatafeed()
tvl = TvDatafeedLive()
timeout_secs = 180
currencies_puncts = {
"EURUSD": 0.00001,
"AUDUSD": 0.00001,
"AUDCAD": 0.00001,
"EURJPY": 0.001,
"EURCAD": 0.00001,
"AUDCHF": 0.00001,
"GBPUSD": 0.00001,
"AUDJPY": 0.001,
"GBPAUD": 0.00001,
"BTCUSD": 0.01
}
class PriceData:
def __init__(self, symbol: str, exchange: str, interval: Interval):
self.symbol = symbol
self.exchange = exchange
self.interval = interval
def print(self):
print("\t", self.symbol, self.interval)
def get_real_puncts(self, puncts):
return currencies_puncts[self.symbol] * puncts
def save_chart_data(self, df: DataFrame):
interval = str(self.interval).replace(".", "")
df.to_csv(currencies_data_path + self.symbol + interval + ".csv")
with open(f"{currency_check_ended}{self.symbol}{str(self.interval).replace('.', '')}.txt", "w") as file:
time = now_time()
file.write(str(time))
def get_chart_download_time(self):
try:
with open(f"{currency_check_ended}{self.symbol}{str(self.interval).replace('.', '')}.txt", "r") as file:
res = datetime.strptime(file.read().split(".")[0], '%Y-%m-%d %H:%M:%S')
except Exception as e:
return now_time()
return res
def get_chart_data_if_exists(self):
interval = str(self.interval).replace(".", "")
path = currency_check_ended + self.symbol + interval + ".txt"
if not file_manager.is_file_exists(path):
# print("1Error chart not exists")
return None
path = currencies_data_path + self.symbol + interval + ".csv"
if not file_manager.is_file_exists(path):
# print("2Error chart not exists")
return None
try:
df = read_csv(path)
except Exception as e:
print("3Error chart not exists", path, e)
return None
try:
df["datetime"] = df.apply(lambda row: datetime.strptime(row["datetime"], '%Y-%m-%d %H:%M:%S'), axis=1)
except Exception as e:
print("Error date time is wrong formated", e, df.to_string())
return None
return df
def get_chart_data_if_exists_if_can_analize(self):
interval = str(self.interval).replace(".", "")
df = self.get_chart_data_if_exists()
last_check_date = currencies_last_analize_date.get(self.symbol + interval)
if not(df is None):
current_check_date = df.datetime[0]
if current_check_date == last_check_date:
return None
currencies_last_analize_date.update({self.symbol + interval: current_check_date})
return df
def reset_chart_data(self):
interval = str(self.interval).replace(".", "")
df = self.get_price_data(5000)
if df is None:
print("ERROR: No df")
pass
else:
print("updated pd")
path = currency_check_ended + self.symbol + interval + ".txt"
file_manager.delete_file_if_exists(path)
path = currencies_data_path + self.symbol + interval + ".csv"
file_manager.delete_file_if_exists(path)
self.save_chart_data(df)
def remove_chart_data(self):
interval = str(self.interval).replace(".", "")
path = currency_check_ended + self.symbol + interval + ".txt"
file_manager.delete_file_if_exists(path)
path = currencies_data_path + self.symbol + interval + ".csv"
file_manager.delete_file_if_exists(path)
def get_price_data(self, bars_count=5000):
try:
priceData = tvl.get_hist(symbol=self.symbol, exchange=self.exchange, interval=self.interval, n_bars=bars_count+1)
priceData = priceData.reindex(index=priceData.index[::-1]).iloc[1:].reset_index()
return priceData
except Exception as e:
print("Error cant get price data", e)
return None
def is_analize_time(self, update_date: datetime, debug=False):
minutes = interval_convertor.interval_to_int(self.interval)
if debug:
print("is_analize_time", (update_date.minute + 1), minutes, (update_date.minute + 1) % minutes)
return (update_date.minute + 1) % minutes == 0
def get_needed_chart_bar_to_analize(self, chart_bar: datetime, main_signal_interval):
minutes = interval_convertor.interval_to_int(self.interval)
main_minutes = interval_convertor.interval_to_int(main_signal_interval)
tmp = chart_bar + timedelta(minutes=main_minutes)
# print("needed bar check", main_signal_interval, minutes, chart_bar, main_minutes, "\n",
# tmp, timedelta(minutes=(tmp.hour*60 + tmp.minute) % minutes), timedelta(minutes=minutes), "\n",
# tmp - timedelta(minutes=(tmp.hour*60 + tmp.minute) % minutes) - timedelta(minutes=minutes))
chart_bar = chart_bar + timedelta(minutes=main_minutes)
res = chart_bar - timedelta(minutes=(chart_bar.hour * 60 + chart_bar.minute) % minutes) - timedelta(
minutes=minutes)
return res
def get_currencies():
currencies = []
currencies_file_content = file_manager.read_file(currencies_path)
for currency in currencies_file_content:
symbol = currency['symbol']
exchange = currency['exchange']
currencies.append((symbol, exchange))
return currencies
def get_price_data_frame_seis(seis, bars_count=5000):
price_df = seis.get_hist(n_bars=bars_count)
price_df = price_df.drop(price_df.index[len(price_df) - 1])
price_df = price_df.reindex(index=price_df.index[::-1]).reset_index()
return price_df
def create_parce_currencies_with_intervals_callbacks(pds: [PriceData]):
global tvl, tv
def update_currency_file_consumer(seis: Seis, data):
try:
price_df = get_price_data_frame_seis(seis)
pd = PriceData(seis.symbol, seis.exchange, seis.interval)
print("update file", pd.symbol, pd.interval)
pd.save_chart_data(price_df)
except Exception as e:
print("bbb", e)
print(f"creating new seis {datetime.now()}")
try:
tvl.del_tvdatafeed()
except Exception as e:
print("Error tvl", e)
tvl = TvDatafeedLive()
tv = TvDatafeed()
try:
for pd in pds:
seis = tvl.new_seis(pd.symbol, pd.exchange, pd.interval, timeout=timeout_secs)
print("seis", seis)
consumer = tvl.new_consumer(seis, update_currency_file_consumer, timeout=timeout_secs)
except ValueError as e:
print("Error1", e)
except Exception as e:
print("Error3", e)
print(f"created new seis {datetime.now()}")