83 lines
3.8 KiB
Python
83 lines
3.8 KiB
Python
"""The exchanges Yote-In-Vest covers, with their opening hours and how each is reached.
|
||
|
||
Why these: they are the deepest, most liquid markets that are practical for a Dutch
|
||
investor trading in euros, dollars or pounds, and they overlap in time with Amsterdam.
|
||
Adding another exchange (Tokyo, Hong Kong, Zurich...) means one more entry here plus
|
||
instruments in config.py.
|
||
|
||
Opening hours are the regular continuous-trading session in local time. Public holidays
|
||
and early closes are NOT modelled. On a holiday no new candles arrive, so the strategy
|
||
stays quiet by itself, but the dashboard will still say "open" on that day.
|
||
"""
|
||
from dataclasses import dataclass
|
||
from datetime import datetime, time, timezone
|
||
from zoneinfo import ZoneInfo
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class Exchange:
|
||
id: str
|
||
name: str
|
||
country: str
|
||
currency: str # currency the shares are quoted in (London: pounds, shown as pence by Yahoo)
|
||
tz: str
|
||
opens: time | None # None = trades around the clock
|
||
closes: time | None
|
||
yahoo_suffix: str # appended to the ticker for Yahoo Finance price data
|
||
trade_via: str # "alpaca" (paper account) or "paper" (built-in simulator)
|
||
note: str
|
||
|
||
|
||
EXCHANGES: dict[str, Exchange] = {e.id: e for e in [
|
||
Exchange("AEX", "Euronext Amsterdam", "NL", "EUR", "Europe/Amsterdam",
|
||
time(9, 0), time(17, 30), ".AS", "paper",
|
||
"Home market. Alpaca does not offer Euronext yet, so orders use the built-in simulator."),
|
||
Exchange("EPA", "Euronext Paris", "FR", "EUR", "Europe/Paris",
|
||
time(9, 0), time(17, 30), ".PA", "paper",
|
||
"Same Euronext platform and hours as Amsterdam. Built-in simulator for orders."),
|
||
Exchange("XETRA", "Deutsche Börse Xetra", "DE", "EUR", "Europe/Berlin",
|
||
time(9, 0), time(17, 30), ".DE", "paper",
|
||
"Europe's biggest equity market by volume. Alpaca supports Xetra for broker partners "
|
||
"only, so orders use the built-in simulator."),
|
||
Exchange("LSE", "London Stock Exchange", "GB", "GBP", "Europe/London",
|
||
time(8, 0), time(16, 30), ".L", "paper",
|
||
"Quoted in pence; converted to pounds and then euros. Built-in simulator for orders."),
|
||
Exchange("US", "NYSE and Nasdaq", "US", "USD", "America/New_York",
|
||
time(9, 30), time(16, 0), "", "alpaca",
|
||
"The world's largest markets. Orders go to your Alpaca paper account."),
|
||
Exchange("CRYPTO", "Crypto (24/7)", "-", "USD", "UTC",
|
||
None, None, "", "alpaca",
|
||
"Trades around the clock, weekends included. Orders go to your Alpaca paper account."),
|
||
]}
|
||
|
||
|
||
def is_open(ex: Exchange, at: datetime | float | None = None) -> bool:
|
||
"""Is the exchange in its regular session at this moment (Mon to Fri, local time)?"""
|
||
if ex.opens is None or ex.closes is None:
|
||
return True
|
||
if at is None:
|
||
at = datetime.now(timezone.utc)
|
||
elif not isinstance(at, datetime):
|
||
at = datetime.fromtimestamp(at, timezone.utc)
|
||
local = at.astimezone(ZoneInfo(ex.tz))
|
||
return local.weekday() < 5 and ex.opens <= local.time() < ex.closes
|
||
|
||
|
||
def hours_label(ex: Exchange) -> str:
|
||
if ex.opens is None:
|
||
return "24/7"
|
||
return f"{ex.opens:%H:%M}–{ex.closes:%H:%M} {ex.tz.split('/')[-1].replace('_', ' ')}"
|
||
|
||
|
||
def describe(at: datetime | None = None) -> list[dict]:
|
||
"""What the dashboard shows for each exchange."""
|
||
at = at or datetime.now(timezone.utc)
|
||
out = []
|
||
for ex in EXCHANGES.values():
|
||
local = at.astimezone(ZoneInfo(ex.tz))
|
||
out.append({"id": ex.id, "name": ex.name, "country": ex.country, "currency": ex.currency,
|
||
"open": is_open(ex, at), "hours": hours_label(ex),
|
||
"local_time": None if ex.opens is None else f"{local:%H:%M}",
|
||
"trade_via": ex.trade_via, "note": ex.note})
|
||
return out
|