-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_call.py
61 lines (51 loc) · 1.99 KB
/
parse_call.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
import re
from models import Trade
from hashlib import sha256
class TradingCallParser:
def __init__(self):
pass
def tokenize(self, txt: str) -> dict[str, str]:
match = re.search(r"setup:\*\* (.*)", txt)
if match:
return {"symbol": match.group(1).upper()}
match = re.search(r"(\w+) trade", txt)
if match:
side = match.group(1).upper()
if side != "LONG" and side != "SHORT":
raise ValueError("Invalid trade type")
return {"side": "BUY" if side == "LONG" else "SELL"}
match = re.search(r"entry zone: (\d+(\.\d+)? - \d+(\.\d+)?)", txt)
if match:
return {"entry": match.group(1)}
match = re.search(r"stop-loss: (\d+\.\d+)", txt)
if match:
return {"stop_loss": match.group(1)}
match = re.search(r"target [\d]+ • (\d+\.\d+)", txt)
if match:
return {"target": match.group(1)}
return {}
def parse(self, message) -> Trade:
# Parse the text
targets = list()
entry = list()
parsed_data: dict[str, str] = dict()
for line in message.text.lower().split("\n"):
t = self.tokenize(line)
if "target" in t:
targets.append(float(t["target"]))
elif "entry" in t:
splits = t["entry"].split("-")
entry = [float(s.strip()) for s in splits]
else:
parsed_data.update(t)
# TODO: some validation to make sure it was parsed proper
return Trade(
id=message.id,
symbol=parsed_data["symbol"],
side=parsed_data["side"], # type: ignore
entry=entry, # descending for long, asc for short
stop_loss=float(parsed_data["stop_loss"]),
targets=sorted(targets), # ascending for long desc for short
timestamp=message.date,
texthash=sha256(message.text.encode("utf-8")).hexdigest(),
)