-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
209 lines (174 loc) · 6.68 KB
/
components.py
File metadata and controls
209 lines (174 loc) · 6.68 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
201
202
203
204
205
206
207
208
209
import time
import queue
import requests
import threading
import cbpro
import pandas as pd
import plots as pl
import utils as ut
import client as cl
import data_tools as dt
class MyWebsocketClient(cbpro.WebsocketClient):
def __init__(self, currency, channels="ticker"):
super().__init__(products=currency, channels=[channels])
self.should_print = False
self.message = None
def __enter__(self):
self.start()
return self
def on_message(self, msg):
self.message = msg
def __exit__(self, exc_type, exc_value, exc_traceback):
self.close()
class Trends:
def __init__(self, currency, public_client=None,
frequency=60, event=None, buy_the_dip=False, buy_obj=None):
self.thread = None
self.event = event
self.frequency = frequency # in secs
self.was_joined = False
self.currency = currency
self.buy_the_dip = buy_the_dip
self.buy_obj = buy_obj
self.out_queue = queue.Queue()
self.public_client = public_client
if self.buy_obj.frequency:
self.frequency = int(self.frequency *buy_obj.frequency)
def __enter__(self):
thread_args = {
'target': self.get_trends,
'args': (self.currency, self.out_queue),
'kwargs': {
"frequency": self.frequency,
"event": self.event
}
}
self.thread = threading.Thread(**thread_args)
self.thread.daemon = True
self.thread.start()
return self
def join(self):
self.thread.join()
self.was_joined = True
return self.out_queue.get()
def __exit__(self, exc_type, exc_value, exc_traceback):
print(ut.term.home + ut.term.clear)
if not self.was_joined:
try:
self.thread.join()
except Exception as e:
raise RuntimeError(f"Current trends process failed. Message: {e}")
def get_trends(self, currency, out_queue=None, frequency=None, event=None):
def _get_trends():
response = requests.request(
"GET",
f"https://api.pro.coinbase.com/products/{currency}/candles",
headers={'Cache-Control': 'no-cache', "Pragma": "no-cache"},
data={'granularity': 60},
timeout=5
)
if response.status_code != 200:
raise ConnectionError("request failed")
historic_rates = response.json()
historic_rates_df = pd.DataFrame(
historic_rates,
columns=['time', 'low', 'high', 'open', 'close', 'volume']
)
historic_rates_df.drop(
['time', 'low', 'high', 'open', 'volume'],
axis=1, inplace=True
)
historic_rates_df = historic_rates_df.iloc[::-1] # invert row order
historic_rates_df.reset_index(inplace=True)
minute_mark = historic_rates_df.index
closing_price = historic_rates_df.close
lowess_df = dt.Lowess.get_lowess_trend(
x=minute_mark,
y=closing_price
)
lowess_trendlines = dt.Lowess.get_lowess_trendlines(lowess_df)
width, height, last_sign, last_change = pl.Plotter.plot(
x=minute_mark,
y=closing_price,
trendlines=lowess_trendlines,
title=currency,
xaxis_title="minutes",
yaxis_title="price",
_type="terminal"
)
if out_queue:
out_queue.put((width, height))
return width, height, last_sign, last_change
_, height, last_sign, last_change = _get_trends()
if self.buy_the_dip:
ut.Messages.next_purchase(height, self.frequency)
if frequency and event:
counter = 0
mins_elapsed = 0
initial_time = time.time()
client = cl.Coinbase(self.currency.replace('-USD', ""))
available_balance = client.usd_balance
while event.is_set():
if time.time() - initial_time > frequency:
_get_trends()
if self.buy_the_dip and mins_elapsed % self.buy_obj.frequency == 0:
counter = 0
if last_sign == '-':
response = client.buy(self.buy_obj.amount)
available_balance = client.usd_balance
ut.Messages.buy(height, response)
else:
ut.Messages.not_dipping(height)
initial_time = time.time()
mins_elapsed += 1
time.sleep(1)
counter += 1
if self.buy_the_dip:
ut.Messages.next_purchase(height, self.frequency - counter)
ut.Messages.available_balance(height, available_balance)
class CurrentPrice:
def __init__(self, currency, event, term_x_pos=0, term_y_pos=0):
self.thread = None
self.event = event
self.was_joined = False
self.currency = currency
self.term_x_pos = term_x_pos
self.term_y_pos = term_y_pos
def __enter__(self):
thread_args = {
'target': self.get_current_price,
'kwargs': {
'currency': self.currency,
"event": self.event
}
}
self.thread = threading.Thread(**thread_args)
self.thread.daemon = True
self.thread.start()
return self
def join(self):
self.thread.join()
self.was_joined = True
def __exit__(self, exc_type, exc_value, exc_traceback):
if not self.was_joined:
try:
self.thread.join()
print(ut.term.home + ut.term.clear)
except Exception as e:
raise RuntimeError(f"Current trends process failed. Message: {e}")
def get_current_price(self, currency, event):
with MyWebsocketClient(currency) as ws_client:
try:
while event.is_set():
if ws_client.error:
print("socket error") # TODO: add logs
break
if ws_client.message:
price = f'{float(ws_client.message["price"]):,}'
ut.Messages.current_price(self.term_y_pos, currency, price)
time.sleep(1)
ws_client.close()
except Exception as e:
raise RuntimeError(e)
finally:
event.clear()