forked from Mynymoys/Xtream-UI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest
108 lines (88 loc) · 3.48 KB
/
test
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
import httpx
from candle import Candle, CandlesHistory
INTERVAL_NAME = "Hour"
INTERVAL = 60 * 60
HIGH_RSI = 75
LOW_RSI = 25
HIGH_MFI = 80
LOW_MFI = 30
MIN_CHANGE_PERCENTAGE = 4.8
NUM_CANDLES = 12
class Pair:
def __init__(self, symbol: str) -> None:
self._history: CandlesHistory | None = None
self.last_candle_start_time = 0
self.symbol = symbol
def is_inited(self):
return self._history is not None
@property
def history(self) -> CandlesHistory:
if self.is_inited():
return self._history
raise Exception("forget to call init")
@property
def current_candle(self) -> Candle:
return self.history.last
@property
def rsi(self) -> float:
return self.history.rsi
@property
def mfi(self) -> float:
return self.history.mfi
@property
def change_percent(self) -> float:
return self.current_candle.change_percent
async def _load_candles(self, start: int):
data = await self._load_data(start)
return self._parse_candles(data)
async def _load_data(self, start: int):
url = f'https://contract.mexc.com/api/v1/contract/kline/{self.symbol}'
async with httpx.AsyncClient() as client:
response = await client.get(url=url, params={
"interval": INTERVAL_NAME,
"start": start,
"limit": NUM_CANDLES,
})
response = response.json()
if "data" in response:
return response['data']
else:
print(f"Response: {response}")
raise Exception("No data in response")
def _parse_candles(self, data):
return [Candle(data, i) for (i, _) in enumerate(data["time"])]
async def init(self, server_time: int) -> None:
try:
delta_time = server_time % INTERVAL # seconds after last candle start
self.last_candle_start_time = server_time - delta_time
start = self.last_candle_start_time - ((NUM_CANDLES + 1) * INTERVAL)
candles = await self._load_candles(start)
self._history = CandlesHistory(candles)
# self._print_indicators()
except Exception as e:
print(f"Error {self.symbol}.\n{str(e)}")
async def update(self):
try:
candles = await self._load_candles(self.last_candle_start_time)
length = len(candles)
if length == 0:
print(f"No candles. start_time: {self.last_candle_start_time}")
return
self.history.update_last(candles[-1])
self.last_candle_start_time = self.current_candle.time
for candle in candles[:-1]:
self.history.add(candle)
# self._print_indicators()
except Exception as e:
print(f"Error {self.symbol}.\n{str(e)}")
def _print_indicators(self):
print(f"{self.symbol}. MFI: {self.mfi}. RSI: {self.rsi}. Percent: {self.change_percent}")
def _get_alert_msg(self, order_type: str) -> str:
# Method body goes here
pass
if order_type == "BUY":
return f"{self.symbol} - {INTERVAL_NAME} - Buy Alert: MFI {self.mfi:.2f}, RSI {self.rsi:.2f}, Change {self.change_percent:.2f}%"
elif order_type == "SELL":
return f"{self.symbol} - {INTERVAL_NAME} - Sell Alert: MFI {self.mfi:.2f}, RSI {self.rsi:.2f}, Change {self.change_percent:.2f}%"
else:
raise ValueError("Invalid order type")