-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
1605 lines (1348 loc) · 62.2 KB
/
Copy pathbot.py
File metadata and controls
1605 lines (1348 loc) · 62.2 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Automated Trading Chart Vision Bot.
Receives a chart screenshot on Telegram, extracts trade parameters with
Gemini Flash vision, calculates profit/loss percentages in code (never
trusting the AI with math), and replies with a formatted signal:
[ASSET] [ACTION] [ORDER_TYPE]
ENTRY: [VALUE]
SL: [VALUE]
TP: [VALUE]
Profit: +[X]% / Loss: -[Y]%
Live trade monitoring (GoldAPI for spot metals, Yahoo Finance for
forex/indices/oil, Bybit for crypto) alerts when a pending order fills, when
to move SL to breakeven once price covers 30% of the distance to TP, and on
TP or SL. Chart assets are matched against each provider's real instrument
list rather than a guessed suffix.
Levels are tested against the high and low of each polling interval, not the
last traded price, so a wick through TP or SL between polls is still caught.
"""
import asyncio
import json
import logging
import os
import random
import re
import time
from datetime import datetime, timedelta
from datetime import timezone as dt_timezone
from pathlib import Path
from typing import Literal, NamedTuple, Optional
import httpx
from dotenv import load_dotenv
from google import genai
from google.genai import types as genai_types
from pydantic import BaseModel
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
)
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
# Optional settings
STATE_FILE = Path(os.environ.get("STATE_FILE", "state.json"))
# Fraction of the entry->TP distance that triggers the breakeven alert
BREAKEVEN_FRACTION = 0.30
# How often to check live prices for active trades (seconds)
MONITOR_INTERVAL = 60
# Drop a monitored trade after this many hours so stale setups don't pile up
# (0 disables expiry)
TRADE_TTL_HOURS = float(os.environ.get("TRADE_TTL_HOURS", "72"))
# Entry within this fraction of the current price counts as a market order.
# Deliberately tight (1 basis point). The two misreadings are not equally
# costly: calling a market order "pending" is cheap, because price is already
# sitting on the entry and the fill fires on the next poll a minute later.
# Calling a limit order "market" is expensive — the bot believes you are in a
# position you never opened and starts sending breakeven and TP/SL alerts for
# it. So when in doubt, treat it as pending.
MARKET_ORDER_TOLERANCE = 0.0001
# Relative tolerance for treating a re-sent chart as the same trade
DUPLICATE_TOLERANCE = 0.001
BYBIT_TICKERS_URL = "https://api.bybit.com/v5/market/tickers"
BYBIT_KLINE_URL = "https://api.bybit.com/v5/market/kline"
# Bybit market categories to search for a pair, in order of preference:
# linear = USDT perpetual futures (most leveraged pairs), spot = spot market
BYBIT_CATEGORIES = ("linear", "spot")
# Tried in order — first one that responds wins. The newest Flash models on the
# free tier intermittently return 503 (high demand), so we keep fallbacks.
# Ordered by measured availability and latency on the free tier, not by
# version number. Benchmarked with a real vision+schema call: 3.6-flash
# answered in 2.7s and 3.1-flash-lite in 1.1s, while 3-flash-preview took
# 29.8s and the rest returned 429 (quota exhausted) inside a second.
# Exhausted models fail fast, so they cost almost nothing to skip and stay on
# as fallbacks for when the quota window rolls over.
GEMINI_MODELS = [
"gemini-3.6-flash",
"gemini-3.1-flash-lite",
"gemini-3-flash-preview",
"gemini-flash-latest",
"gemini-3.5-flash",
]
# When every model is busy the whole list is retried after a pause. Free-tier
# 503s are explicitly temporary ("spikes in demand are usually temporary"), so
# waiting a few seconds usually beats failing the user's chart outright.
GEMINI_BACKOFF = (0, 4, 10)
# Never spend longer than this on one image, so a reply always arrives
GEMINI_MAX_WAIT = 75
# Errors worth retrying: overloaded, rate-limited, or a transient server fault
GEMINI_RETRY_CODES = (429, 500, 502, 503, 504)
def is_retryable(error: Exception) -> bool:
"""True if this Gemini failure is transient and worth another attempt."""
code = getattr(error, "code", None) or getattr(error, "status_code", None)
if isinstance(code, int):
return code in GEMINI_RETRY_CODES
text = str(error)
return any(str(c) in text for c in GEMINI_RETRY_CODES) or "UNAVAILABLE" in text
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
gemini_client = genai.Client(api_key=GEMINI_API_KEY)
# ---------------------------------------------------------------------------
# Persistent state: subscribed chats + active trades
# ---------------------------------------------------------------------------
def utcnow_iso() -> str:
return datetime.now(dt_timezone.utc).isoformat()
def load_state() -> dict:
data: dict = {}
if STATE_FILE.exists():
try:
data = json.loads(STATE_FILE.read_text())
except (json.JSONDecodeError, OSError):
logger.warning("Could not read %s, starting fresh", STATE_FILE)
data.setdefault("chats", [])
data.setdefault("trades", [])
data.pop("last_sent", None) # retired: the scheduled motivation texts
# Migrate trades written by older versions, which lacked these fields.
# Every key monitor_trades reads is defaulted here: a trade missing one
# used to raise mid-cycle and abort the checks for every other trade too.
now = utcnow_iso()
for trade in data["trades"]:
trade.setdefault("status", "active")
trade.setdefault("created_at", now)
trade.setdefault("provider", "bybit")
trade.setdefault("category", "linear")
trade.setdefault("fill_direction", None)
trade.setdefault("be_alerted", False)
trade.setdefault("decimals", 2)
trade.setdefault("profit_pct", 0.0)
trade.setdefault("loss_pct", 0.0)
return data
def save_state() -> None:
"""Persist state, writing through a temp file so a crash mid-write can't
truncate it — a half-written state.json loses every monitored trade.
"""
try:
STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
tmp = STATE_FILE.with_suffix(STATE_FILE.suffix + ".tmp")
tmp.write_text(json.dumps(state, indent=2))
tmp.replace(STATE_FILE)
except OSError:
logger.exception("Could not save state to %s", STATE_FILE)
state = load_state()
def register_chat(chat_id: int) -> None:
if chat_id not in state["chats"]:
state["chats"].append(chat_id)
save_state()
# ---------------------------------------------------------------------------
# Vision extraction (Gemini) — returns raw values only, no math, no prose
# ---------------------------------------------------------------------------
class ActivePosition(BaseModel):
"""A second position tool the chart shows as already running.
Reported for information only — the bot signals and monitors the most
recent setup, not this one.
"""
direction: Literal["LONG", "SHORT"]
entry: Optional[float] = None
stop_loss: Optional[float] = None
take_profit: Optional[float] = None
class ChartAnalysis(BaseModel):
"""Raw values Gemini extracts from the image. All math happens in code.
is_trading_chart gates everything: when False the bot stays silent.
"""
is_trading_chart: bool
asset: Optional[str] = None
direction: Optional[Literal["LONG", "SHORT"]] = None
entry: Optional[float] = None
stop_loss: Optional[float] = None
take_profit: Optional[float] = None
current_price: Optional[float] = None
# Where the entry line sits relative to the current price, judged
# visually. Reading two prices off a chart and subtracting them is far
# more error-prone than seeing which line is higher, and this is the only
# thing that can classify the order when no feed carries the asset.
entry_placement: Optional[Literal["above", "below", "at"]] = None
# How many position tools are drawn on the chart. When more than one, the
# values above describe the most recent (right-most) of them.
position_count: Optional[int] = None
# A different position tool that price is currently inside — a trade
# already running, as opposed to the setup being signalled.
active_position: Optional[ActivePosition] = None
class TradeData(BaseModel):
"""A complete, validated trade setup extracted from a chart."""
asset: str
direction: Literal["LONG", "SHORT"]
entry: float
stop_loss: float
take_profit: float
current_price: Optional[float] = None
entry_placement: Optional[Literal["above", "below", "at"]] = None
position_count: int = 1
VISION_PROMPT = """\
You are analyzing an image that should be a screenshot of a trading chart \
(e.g. TradingView) with a long/short position tool drawn on it.
First decide: is this actually a trading chart with a visible position tool \
(entry, stop loss, and take profit levels)? If it is NOT — any other kind of \
image, or a chart without a position tool, or a chart whose price levels are \
unreadable — set is_trading_chart to false and leave every other field null.
If it IS such a chart, set is_trading_chart to true and extract the values.
WHICH POSITION TO READ — this matters most:
A chart often has several position tools drawn on it from earlier setups, plus \
other drawings (trendlines, rectangles, fibs, notes). You must extract exactly \
ONE position: the MOST RECENT one.
The most recent position is the one furthest to the RIGHT on the chart — time \
runs left to right, so the right-most position tool is the newest. Judge this \
by where each tool's box STARTS (its left edge / the entry line's anchor): the \
tool whose box starts furthest right is the most recent, even if an older tool \
is taller or stretches further right. If two start at the same place, take the \
one nearest the last (right-most) candle.
Ignore every other position tool on the chart completely — do not average them, \
do not blend their levels, and do not pick the largest or most obvious one. \
Set position_count to the total number of position tools you can see (1 if \
there is only one), and return the levels of the right-most one only.
How to read the chart:
- The asset name is usually in the top-left corner (e.g. "Bitcoin / U.S. Dollar" \
means the asset symbol is BTCUSD). Return the compact ticker symbol.
- The position tool draws two shaded boxes. The RED shaded box is the Stop Loss \
zone. The GREEN or BLUE shaded box is the Take Profit zone. The horizontal line \
separating them is the Entry price.
- Read exact price values from the labels on the position tool or the price \
axis on the right.
- direction: "SHORT" if the red (stop loss) box is ABOVE the entry line, \
"LONG" if the red box is BELOW the entry line.
- current_price is the price the market is currently trading at: the \
highlighted/coloured label on the right price axis, level with the last candle \
on the right edge. If you genuinely cannot see it, set it to null rather than \
guessing.
IS IT A PENDING ORDER OR A MARKET EXECUTION:
Set entry_placement by LOOKING at the chart, not by comparing numbers. Find the \
horizontal entry line of the position you returned, and find the level of the \
current price (the last candle on the right edge / the highlighted price-axis \
label). Then:
- "above" - the entry line is drawn clearly ABOVE the current price level
- "below" - the entry line is drawn clearly BELOW the current price level
- "at" - the entry line sits ON the current price, level with the last \
candle, so the trade would execute immediately
Judge this visually. Seeing which line is higher is reliable; reading two \
prices off the axis and subtracting them is not. Most drawn setups are pending \
orders placed away from the current price — only answer "at" when the entry \
line genuinely touches the latest candle's price level.
IS ANOTHER POSITION ALREADY RUNNING:
Besides the most recent position, the chart may show a DIFFERENT position tool \
that is currently live: price has already passed its entry line and the candles \
are now travelling between that entry and its take profit or stop loss, with \
the tool extending to the right edge alongside the latest candles. If such a \
position exists AND it is not the same one you returned above, describe it in \
active_position (its direction, and its entry/stop_loss/take_profit if you can \
read them). If there is no such position, or the position you returned above is \
itself the running one, leave active_position null.
Extract only the raw values. Do NOT calculate anything. Do NOT write a message. \
Return only the structured data.
"""
def extract_chart_analysis(image_bytes: bytes, mime_type: str) -> ChartAnalysis:
"""Call Gemini vision with enforced structured JSON output (sync).
Tries each model in GEMINI_MODELS until one responds — free-tier models
intermittently return 503 (high demand) or 429 (quota).
"""
deadline = time.monotonic() + GEMINI_MAX_WAIT
last_error: Exception | None = None
for attempt, delay in enumerate(GEMINI_BACKOFF):
if delay:
if time.monotonic() + delay >= deadline:
break
logger.info("All models busy, retrying in %ss", delay)
time.sleep(delay)
for model in GEMINI_MODELS:
try:
response = gemini_client.models.generate_content(
model=model,
contents=[
genai_types.Part.from_bytes(
data=image_bytes, mime_type=mime_type
),
VISION_PROMPT,
],
config=genai_types.GenerateContentConfig(
response_mime_type="application/json",
response_schema=ChartAnalysis,
temperature=0,
),
)
except Exception as error: # noqa: BLE001 - try the next model
last_error = error
if not is_retryable(error):
# A bad key or malformed request won't fix itself
logger.error("Model %s failed permanently: %s", model, error)
raise
logger.warning("Model %s busy (attempt %d): %s",
model, attempt + 1, error)
if time.monotonic() >= deadline:
break
continue
if attempt:
logger.info("Model %s answered on attempt %d", model, attempt + 1)
parsed = response.parsed
if isinstance(parsed, ChartAnalysis):
return parsed
return ChartAnalysis.model_validate_json(response.text)
if time.monotonic() >= deadline:
break
raise last_error if last_error else RuntimeError("No Gemini model available")
def to_trade_data(analysis: ChartAnalysis) -> Optional[TradeData]:
"""Return a complete TradeData, or None if the image isn't a usable chart."""
if not analysis.is_trading_chart:
return None
required = (analysis.asset, analysis.direction, analysis.entry,
analysis.stop_loss, analysis.take_profit)
if any(value is None for value in required):
return None
return TradeData(
asset=analysis.asset,
direction=analysis.direction,
entry=analysis.entry,
stop_loss=analysis.stop_loss,
take_profit=analysis.take_profit,
current_price=analysis.current_price,
entry_placement=analysis.entry_placement,
position_count=max(1, analysis.position_count or 1),
)
# ---------------------------------------------------------------------------
# Deterministic math & formatting (never done by the AI)
# ---------------------------------------------------------------------------
def calculate_percentages(data: TradeData) -> tuple[float, float]:
"""Profit/loss percentages, calculated deterministically (never by the AI)."""
entry = data.entry
if data.direction == "SHORT":
profit = (entry - data.take_profit) / entry * 100
loss = (data.stop_loss - entry) / entry * 100
else: # LONG
profit = (data.take_profit - entry) / entry * 100
loss = (entry - data.stop_loss) / entry * 100
return round(profit, 2), round(loss, 2)
def breakeven_price(data: TradeData) -> float:
"""Price at which 30% of the entry->TP distance is covered."""
return data.entry + (data.take_profit - data.entry) * BREAKEVEN_FRACTION
def reference_price(data: TradeData, live_price: Optional[float] = None) -> Optional[float]:
"""The price the entry is judged against to classify the order.
The live feed wins when we have it. Gemini reads the chart's own price
label well enough most of the time, but it is a screenshot of a moment
that has already passed, and when the label is small or occluded the model
returns null — which used to make every such setup look like a market
execution. The feed is both current and always numeric, so it decides;
the chart's reading is only the fallback for assets no feed carries.
"""
if live_price is not None and live_price > 0:
return live_price
if data.current_price is not None and data.current_price > 0:
return data.current_price
return None
def entry_fill_direction(
data: TradeData, live_price: Optional[float] = None
) -> Optional[str]:
"""Which way price must travel to reach entry: 'up', 'down', or None.
None means the order executes immediately at market: entry sits on the
current price.
A numeric comparison against a real price wins when one is available.
Failing that we fall back to where the model *saw* the entry line sitting
relative to the last candle — judging which of two lines is higher is a
far easier visual task than reading both prices off the axis correctly,
and it is the only signal left for an asset no feed carries.
"""
current = reference_price(data, live_price)
if current is not None:
if abs(data.entry - current) / data.entry < MARKET_ORDER_TOLERANCE:
return None
return "up" if data.entry > current else "down"
if data.entry_placement == "above":
return "up"
if data.entry_placement == "below":
return "down"
if data.entry_placement == "at":
return None
# Nothing to compare against at all — assume pending rather than claim a
# position is open. See MARKET_ORDER_TOLERANCE for why that way round.
return "unknown"
def determine_order_type(
data: TradeData, live_price: Optional[float] = None
) -> str:
"""Classify the setup as a market execution or a pending LIMIT/STOP order.
Entry at the current price is a market execution — the position is open
now. Entry away from the current price is a pending order that only
becomes a position once price travels to it, and whether it is a LIMIT or
a STOP depends on which side of the market it sits.
"""
action = "SELL" if data.direction == "SHORT" else "BUY"
fill = entry_fill_direction(data, live_price)
if fill is None:
return f"{action} MARKET"
if fill == "unknown":
# Pending, but with no price reference there is no way to say which
# side of the market the entry sits on, and LIMIT vs STOP is exactly
# that question. Better to name neither than to guess wrong.
return action
if data.direction == "SHORT":
# Selling above the market waits for price to rise -> LIMIT
order = "LIMIT" if fill == "up" else "STOP"
else:
# Buying below the market waits for price to fall -> LIMIT
order = "LIMIT" if fill == "down" else "STOP"
return f"{action} {order}"
def _natural_decimals(value: float) -> int:
"""Number of decimals needed to represent the price without trailing zeros."""
max_decimals = 5 if value >= 1 else 8
text = f"{value:.{max_decimals}f}".rstrip("0")
return len(text.split(".")[1]) if "." in text else 0
def signal_decimals(data: TradeData) -> int:
prices = (data.entry, data.stop_loss, data.take_profit)
return max(2, *(_natural_decimals(p) for p in prices))
def build_signal_message(data: TradeData, live_price: Optional[float] = None) -> str:
profit, loss = calculate_percentages(data)
decimals = signal_decimals(data)
entry, sl, tp = (
f"{p:.{decimals}f}" for p in (data.entry, data.stop_loss, data.take_profit)
)
header = f"{data.asset.upper()} {determine_order_type(data, live_price)}"
return (
f"{header}\n"
f"ENTRY: {entry}\n"
f"SL: {sl}\n"
f"TP: {tp}\n"
f"Profit: +{profit}% / Loss: -{loss}%"
)
def describe_chart_context(
analysis: ChartAnalysis, data: TradeData, decimals: int
) -> Optional[str]:
"""What else was on the chart besides the setup being signalled.
Only one thing is worth saying out loud: that another position on the
chart is a trade already running. The count of position tools is logged
rather than messaged — the model is still asked for it, because counting
them makes it look at all of them before choosing, but reporting it back
is noise on a chart that always has several.
"""
lines = []
if data.position_count > 1:
logger.info(
"%s: %d position tools on the chart, read the most recent",
data.asset.upper(), data.position_count,
)
running = analysis.active_position
if running:
levels = " · ".join(
f"{label} {value:.{decimals}f}"
for label, value in (
("entry", running.entry),
("SL", running.stop_loss),
("TP", running.take_profit),
)
if value is not None and value > 0
)
lines.append(
f"⚡ A {running.direction} on that chart is already running"
+ (f" — {levels}" if levels else "")
+ ".\nThat one is not monitored: the signal above is for the most "
"recent setup. Send the running trade as its own chart if you want "
"breakeven and TP/SL alerts for it too."
)
return "\n\n".join(lines) if lines else None
def validate(data: TradeData) -> Optional[str]:
"""Sanity-check the extracted values; return an error message or None."""
if min(data.entry, data.stop_loss, data.take_profit) <= 0:
return "Extracted prices were invalid (zero or negative)."
if data.direction == "SHORT":
if not (data.stop_loss > data.entry > data.take_profit):
return (
"Values don't look like a valid SHORT setup "
"(expected SL above entry and TP below entry)."
)
else:
if not (data.stop_loss < data.entry < data.take_profit):
return (
"Values don't look like a valid LONG setup "
"(expected SL below entry and TP above entry)."
)
return None
# ---------------------------------------------------------------------------
# Live prices: Bybit public API (no key, no account)
#
# Bybit names its pairs its own way: a chart labelled BTCUSD or XAUUSD is
# BTCUSDT / XAUUSDT here. Rather than guess a suffix and hope, the bot
# downloads Bybit's real symbol list per market category and matches against
# it — the list is authoritative and catches renames and new listings.
#
# The list is only used to resolve a chart to a pair. Per-cycle pricing stays
# a targeted per-symbol call, because the full linear ticker payload is ~550KB
# and fetching that every minute would be absurd.
# ---------------------------------------------------------------------------
# Cache the symbol list this long before refetching (seconds)
BYBIT_SYMBOLS_TTL = 6 * 3600
# Never ask a provider for more than this much history in one poll. Bounds the
# catch-up after the host has been asleep for hours.
MAX_LOOKBACK_MINUTES = 180
class PriceSample(NamedTuple):
"""What price did over an interval, not just where it ended up.
Comparing TP/SL against the last traded price only catches a level if
price is still beyond it at the instant we poll. A wick that spikes
through the stop and snaps back inside the same minute is invisible that
way, and the trade runs on as if nothing happened. Carrying the high and
low of the interval means any touch counts, however brief.
"""
last: float
high: float
low: float
@classmethod
def point(cls, price: float) -> "PriceSample":
"""A sample from a feed that only publishes a spot price, no range."""
return cls(price, price, price)
def lookback_minutes(since: Optional[datetime], now: datetime) -> int:
"""How many 1-minute candles to request to cover the gap since `since`."""
if since is None:
return 2
gap = (now - since).total_seconds()
return max(2, min(MAX_LOOKBACK_MINUTES, int(gap // 60) + 2))
_bybit_symbols: dict[str, set[str]] = {}
_bybit_symbols_at: dict[str, float] = {}
def normalize_asset(asset: str) -> str:
return re.sub(r"[^A-Z0-9]", "", asset.upper())
def bybit_symbol_candidates(asset: str) -> list[str]:
"""Bybit pair names to try for an asset like 'BTCUSD' or 'XAUUSD'.
Order matters: the first candidate that Bybit actually lists wins.
"""
compact = normalize_asset(asset)
candidates = []
if compact.endswith("USDT"):
base = compact[:-4]
candidates.append(compact)
elif compact.endswith("USDC"):
base = compact[:-4]
candidates.append(compact)
elif compact.endswith("USD"):
base = compact[:-3]
# BTCUSD -> BTCUSDT is the usual perpetual; the plain USD pair exists
# for a few inverse contracts, so keep it as a second choice.
candidates.extend([compact + "T", compact])
else:
base = compact
candidates.extend([base + "USDT", base + "USDC"])
return [c for c in dict.fromkeys(candidates) if c]
async def bybit_symbols(http: httpx.AsyncClient, category: str) -> set[str]:
"""Every pair Bybit lists in a category, cached for BYBIT_SYMBOLS_TTL.
Diagnostics only — resolution probes individual symbols instead, because
this payload is ~550KB and too slow to sit on the request path.
"""
cached = _bybit_symbols.get(category)
if cached and time.monotonic() - _bybit_symbols_at.get(category, 0) < BYBIT_SYMBOLS_TTL:
return cached
try:
response = await http.get(BYBIT_TICKERS_URL, params={"category": category})
payload = response.json()
except (httpx.HTTPError, ValueError) as error:
logger.warning("Could not fetch the Bybit %s pair list: %s", category, error)
return cached or set()
if payload.get("retCode") != 0:
logger.warning("Bybit %s pair list returned retCode %s",
category, payload.get("retCode"))
return cached or set()
names = {
row["symbol"]
for row in (payload.get("result") or {}).get("list") or []
if row.get("symbol")
}
if not names:
return cached or set()
_bybit_symbols[category] = names
_bybit_symbols_at[category] = time.monotonic()
logger.info("Bybit lists %d %s pairs", len(names), category)
return names
async def fetch_bybit_price(
http: httpx.AsyncClient, symbol: str, category: str
) -> Optional[float]:
"""Last traded price for a Bybit symbol, or None if unavailable."""
try:
r = await http.get(
BYBIT_TICKERS_URL, params={"category": category, "symbol": symbol}
)
payload = r.json()
except (httpx.HTTPError, ValueError):
return None
# Bybit returns HTTP 200 even for unknown symbols; retCode signals success
if payload.get("retCode") != 0:
return None
tickers = (payload.get("result") or {}).get("list") or []
if not tickers:
return None
try:
return float(tickers[0]["lastPrice"])
except (KeyError, ValueError):
return None
async def fetch_bybit_sample(
http: httpx.AsyncClient, symbol: str, category: str, minutes: int
) -> Optional[PriceSample]:
"""High/low/close over the last `minutes` 1-minute candles on Bybit."""
try:
r = await http.get(
BYBIT_KLINE_URL,
params={
"category": category,
"symbol": symbol,
"interval": "1",
"limit": minutes,
},
)
payload = r.json()
except (httpx.HTTPError, ValueError):
return None
if payload.get("retCode") != 0:
return None
# Rows are [start_ms, open, high, low, close, volume, turnover], newest first
rows = (payload.get("result") or {}).get("list") or []
highs, lows = [], []
last: Optional[float] = None
for row in rows:
try:
high, low, close = float(row[2]), float(row[3]), float(row[4])
except (IndexError, TypeError, ValueError):
continue
if last is None:
last = close
highs.append(high)
lows.append(low)
if last is None:
# Klines unavailable (a brand-new listing, say) — fall back to the
# ticker so the trade is still checked, just without wick coverage.
price = await fetch_bybit_price(http, symbol, category)
return PriceSample.point(price) if price else None
return PriceSample(last, max(highs), min(lows))
# ---------------------------------------------------------------------------
# Live prices: Gold API (public spot metals — keyless, no account needed)
#
# Covers real spot gold (XAUUSD -> XAU) and spot silver (XAGUSD -> XAG).
# Free, unmetered public JSON API: https://api.gold-api.com/price/{symbol}
# ---------------------------------------------------------------------------
GOLDAPI_BASE_URL = "https://api.gold-api.com/price"
def goldapi_symbol_candidates(asset: str) -> list[str]:
compact = normalize_asset(asset)
if compact in ("XAUUSD", "GOLD", "XAU"):
return ["XAU"]
if compact in ("XAGUSD", "SILVER", "XAG"):
return ["XAG"]
return []
async def fetch_goldapi_price(
http: httpx.AsyncClient, symbol: str
) -> Optional[float]:
"""Current spot price for XAU (Gold) or XAG (Silver) from gold-api.com."""
try:
response = await http.get(
f"{GOLDAPI_BASE_URL}/{symbol.upper()}",
headers={"User-Agent": "Mozilla/5.0"},
)
if response.status_code != 200:
return None
payload = response.json()
price = float(payload["price"])
return price if price > 0 else None
except (httpx.HTTPError, KeyError, ValueError, TypeError) as error:
logger.warning("GoldAPI price check failed for %s: %s", symbol, error)
return None
# ---------------------------------------------------------------------------
# Live prices: Yahoo Finance (public market data — keyless, no account needed)
#
# Covers forex pairs (EURUSD=X), indices (^DJI, ^GSPC, ^IXIC, ^GDAXI, ^FTSE),
# and commodities/oil (CL=F, BZ=F).
# ---------------------------------------------------------------------------
def yahoo_symbol_candidates(asset: str) -> list[str]:
"""Yahoo Finance symbol names to try for an asset like 'EURUSD' or 'US30'."""
compact = normalize_asset(asset)
candidates = []
# Indices & Commodities
index_map = {
"US30": "^DJI", "DJI": "^DJI", "DOW": "^DJI",
"US500": "^GSPC", "SPX500": "^GSPC", "SPX": "^GSPC", "SP500": "^GSPC",
"US100": "^IXIC", "NAS100": "^IXIC", "NASDAQ": "^IXIC",
"GER40": "^GDAXI", "GER30": "^GDAXI", "DAX": "^GDAXI",
"UK100": "^FTSE", "FTSE": "^FTSE",
"USOIL": "CL=F", "WTI": "CL=F",
"UKOIL": "BZ=F", "BRENT": "BZ=F",
}
if compact in index_map:
candidates.append(index_map[compact])
# Forex pairs (e.g., EURUSD -> EURUSD=X)
if len(compact) == 6 and compact.isalpha():
candidates.append(f"{compact}=X")
# Try raw as a fallback
candidates.append(asset.strip())
return list(dict.fromkeys(candidates))
async def fetch_yahoo_chart(
http: httpx.AsyncClient, symbol: str
) -> Optional[dict]:
"""Raw 1-minute chart payload for a Yahoo Finance symbol."""
url = f"https://query2.finance.yahoo.com/v8/finance/chart/{symbol}"
try:
r = await http.get(
url,
params={"interval": "1m", "range": "1d"},
headers={"User-Agent": "Mozilla/5.0"},
)
if r.status_code != 200:
return None
return (r.json()["chart"]["result"] or [None])[0]
except (httpx.HTTPError, KeyError, IndexError, TypeError, ValueError):
return None
async def fetch_yahoo_price(
http: httpx.AsyncClient, symbol: str
) -> Optional[float]:
"""Current regularMarketPrice for a Yahoo Finance symbol, or None if unavailable."""
result = await fetch_yahoo_chart(http, symbol)
if not result:
return None
try:
price = float(result["meta"]["regularMarketPrice"])
except (KeyError, TypeError, ValueError):
return None
return price if price > 0 else None
async def fetch_yahoo_sample(
http: httpx.AsyncClient, symbol: str, minutes: int
) -> Optional[PriceSample]:
"""High/low/last over the last `minutes` 1-minute candles on Yahoo."""
result = await fetch_yahoo_chart(http, symbol)
if not result:
return None
try:
last = float(result["meta"]["regularMarketPrice"])
except (KeyError, TypeError, ValueError):
return None
if last <= 0:
return None
try:
quote = result["indicators"]["quote"][0]
highs = [h for h in (quote.get("high") or [])[-minutes:] if h]
lows = [low for low in (quote.get("low") or [])[-minutes:] if low]
except (KeyError, IndexError, TypeError):
highs, lows = [], []
# The quote arrays go quiet outside market hours; the last price alone is
# still a valid (if range-less) reading.
if not highs or not lows:
return PriceSample.point(last)
return PriceSample(last, max(max(highs), last), min(min(lows), last))
# ---------------------------------------------------------------------------
# Provider routing: Yahoo Finance for metals/forex/indices/oil, Bybit for crypto
# ---------------------------------------------------------------------------
async def resolve_market(asset: str) -> Optional[dict]:
"""Pick where to source live prices for a charted asset.
GoldAPI goes first for spot metals: Gold/Silver (XAUUSD, XAGUSD).
Yahoo Finance goes next for traditional markets: forex pairs, stock indices, and oil.
Bybit goes next: it covers crypto (BTCUSD, ETHUSD, SOLUSD...).
"""
async with httpx.AsyncClient(timeout=20) as http:
for candidate in goldapi_symbol_candidates(asset):
if await fetch_goldapi_price(http, candidate) is not None:
return {"provider": "goldapi", "symbol": candidate}
for candidate in yahoo_symbol_candidates(asset):
if await fetch_yahoo_price(http, candidate) is not None:
return {"provider": "yahoo", "symbol": candidate}
# Ask Bybit about each candidate directly rather than downloading the
# whole ticker table. The full linear list is ~550KB and takes ~9s on
# a good connection, which times out on a free-tier host and silently
# rejects every asset; a single-symbol probe is ~1KB and answers in
# milliseconds. Bybit is still the authority on whether a pair exists.
for candidate in bybit_symbol_candidates(asset):
for category in BYBIT_CATEGORIES:
if await fetch_bybit_price(http, candidate, category) is not None:
return {
"provider": "bybit",
"symbol": candidate,
"category": category,
}
return None
async def fetch_market_price(
http: httpx.AsyncClient, market: dict
) -> Optional[float]:
"""Current price for a resolved market, whichever provider carries it."""
provider = market["provider"]
if provider == "bybit":
return await fetch_bybit_price(
http, market["symbol"], market.get("category", "linear")
)
if provider == "goldapi":
return await fetch_goldapi_price(http, market["symbol"])
if provider == "yahoo":
return await fetch_yahoo_price(http, market["symbol"])
return None
async def fetch_trade_sample(
http: httpx.AsyncClient, trade: dict, minutes: int
) -> Optional[PriceSample]:
"""What price did over the last `minutes` for a monitored trade."""
provider = trade.get("provider", "bybit")
if provider == "bybit":
return await fetch_bybit_sample(
http, trade["symbol"], trade.get("category", "linear"), minutes
)
if provider == "yahoo":
return await fetch_yahoo_sample(http, trade["symbol"], minutes)
if provider == "goldapi":
# gold-api.com publishes a spot price and nothing else — no OHLC
# endpoint — so spot metals are the one feed still sampled pointwise
# and can miss a wick that reverses inside the polling interval.
price = await fetch_goldapi_price(http, trade["symbol"])
return PriceSample.point(price) if price else None
logger.warning(
"Trade on %s uses retired provider %r — it will expire on its own",
trade.get("asset"), provider,
)
return None
def check_trade(trade: dict, sample: PriceSample) -> Optional[str]:
"""Return the event for this trade over this interval, or None.
Levels are tested against the interval's high and low rather than its
closing price, so a level that price only touched still counts.
A pending order (LIMIT/STOP) is not a position yet, so it can only report
'entry' (price touched the entry level, order filled) or 'missed' (price
ran all the way to TP without ever filling — the setup is void).
Once filled, the trade reports 'tp', 'sl' or 'breakeven'.
"""
long = trade["direction"] == "LONG"
# The level a move in each direction reaches first
favourable = sample.high if long else sample.low
adverse = sample.low if long else sample.high
def reached(level: float, extreme: float) -> bool:
return extreme >= level if long else extreme <= level
reached_tp = reached(trade["tp"], favourable)
if trade.get("status") == "pending":
# Entry can sit either side of the market, so test the extreme that
# travels towards it rather than assuming a direction.
fill = trade.get("fill_direction")
if fill == "down":
touched = sample.low <= trade["entry"]
elif fill == "up":
touched = sample.high >= trade["entry"]
else: