152 lines
8.4 KiB
Python
152 lines
8.4 KiB
Python
"""All settings in one place. Values come from the .env file."""
|
|
import os
|
|
from dataclasses import dataclass, field
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from .exchanges import EXCHANGES
|
|
|
|
load_dotenv()
|
|
|
|
|
|
def _f(name: str, default: float) -> float:
|
|
return float(os.getenv(name, default))
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Instrument:
|
|
tv_symbol: str # ticker as TradingView sends it, e.g. "BTCUSD" or "ASML"
|
|
exchange: str # key in exchanges.EXCHANGES
|
|
broker: str # "alpaca" or "paper"
|
|
broker_symbol: str # symbol at the broker, e.g. "BTC/USD"
|
|
news_query: str # what to search news for
|
|
currency: str # currency the price is quoted in
|
|
asset_class: str = "stock" # "stock" or "crypto"
|
|
yahoo: str = "" # Yahoo Finance symbol, used for real prices
|
|
|
|
|
|
def _stock(ticker: str, exchange: str, news_query: str) -> Instrument:
|
|
ex = EXCHANGES[exchange]
|
|
return Instrument(ticker, exchange, ex.trade_via, ticker, news_query, ex.currency,
|
|
"stock", ticker + ex.yahoo_suffix)
|
|
|
|
|
|
def _crypto(ticker: str, pair: str, yahoo: str, news_query: str) -> Instrument:
|
|
return Instrument(ticker, "CRYPTO", EXCHANGES["CRYPTO"].trade_via, pair, news_query, "USD",
|
|
"crypto", yahoo)
|
|
|
|
|
|
# Map TradingView tickers to instruments. Shares that trade on several exchanges (ASML, SAP,
|
|
# Shell, AstraZeneca) are listed here ONCE, on the exchange named below, so make the
|
|
# TradingView alert on that exchange (e.g. EURONEXT:ASML, XETR:SAP, LSE:SHEL).
|
|
_CRYPTO = [
|
|
("BTCUSD", "BTC/USD", "BTC-USD", "Bitcoin"), ("ETHUSD", "ETH/USD", "ETH-USD", "Ethereum"),
|
|
("SOLUSD", "SOL/USD", "SOL-USD", "Solana crypto"), ("DOGEUSD", "DOGE/USD", "DOGE-USD", "Dogecoin"),
|
|
("AVAXUSD", "AVAX/USD", "AVAX-USD", "Avalanche crypto"), ("LINKUSD", "LINK/USD", "LINK-USD", "Chainlink crypto"),
|
|
("LTCUSD", "LTC/USD", "LTC-USD", "Litecoin"), ("DOTUSD", "DOT/USD", "DOT-USD", "Polkadot crypto"),
|
|
("BCHUSD", "BCH/USD", "BCH-USD", "Bitcoin Cash"),
|
|
]
|
|
|
|
# ticker -> what to search the news for. Yahoo symbol = ticker + the exchange's suffix.
|
|
_STOCKS = {
|
|
"AEX": { # Euronext Amsterdam
|
|
"ASML": "ASML Holding", "ADYEN": "Adyen", "INGA": "ING Groep", "PHIA": "Philips",
|
|
"AD": "Ahold Delhaize", "HEIA": "Heineken", "UNA": "Unilever", "WKL": "Wolters Kluwer",
|
|
"PRX": "Prosus", "AKZA": "Akzo Nobel", "MT": "ArcelorMittal", "NN": "NN Group",
|
|
"ASM": "ASM International", "BESI": "BE Semiconductor", "IMCD": "IMCD", "SBMO": "SBM Offshore",
|
|
"RAND": "Randstad", "DSFIR": "DSM-Firmenich", "KPN": "KPN telecom", "REN": "RELX",
|
|
"ABN": "ABN AMRO", "EXO": "Exor", "ASRNL": "ASR Nederland", "AALB": "Aalberts",
|
|
},
|
|
"EPA": { # Euronext Paris
|
|
"MC": "LVMH", "TTE": "TotalEnergies", "OR": "L'Oreal", "SAN": "Sanofi", "AIR": "Airbus",
|
|
"BNP": "BNP Paribas", "SU": "Schneider Electric", "AI": "Air Liquide", "CS": "AXA",
|
|
"RMS": "Hermes", "KER": "Kering", "DG": "Vinci", "SAF": "Safran", "BN": "Danone",
|
|
"EL": "EssilorLuxottica", "STLAP": "Stellantis", "ORA": "Orange telecom",
|
|
"CAP": "Capgemini", "GLE": "Societe Generale", "ACA": "Credit Agricole",
|
|
},
|
|
"XETRA": { # Frankfurt
|
|
"SAP": "SAP SE", "SIE": "Siemens AG", "ALV": "Allianz", "DTE": "Deutsche Telekom",
|
|
"MBG": "Mercedes-Benz", "BMW": "BMW", "BAS": "BASF", "BAYN": "Bayer", "ADS": "Adidas",
|
|
"IFX": "Infineon", "MUV2": "Munich Re", "DBK": "Deutsche Bank", "VOW3": "Volkswagen",
|
|
"DHL": "DHL Group", "RWE": "RWE", "EOAN": "E.ON", "HEN3": "Henkel",
|
|
},
|
|
"LSE": { # London
|
|
"SHEL": "Shell plc", "AZN": "AstraZeneca", "HSBA": "HSBC Holdings", "BP": "BP plc",
|
|
"ULVR": "Unilever", "GSK": "GSK", "RIO": "Rio Tinto", "DGE": "Diageo",
|
|
"BATS": "British American Tobacco", "LSEG": "London Stock Exchange Group", "BARC": "Barclays",
|
|
"LLOY": "Lloyds Banking", "VOD": "Vodafone", "NG": "National Grid", "REL": "RELX",
|
|
"GLEN": "Glencore", "CPG": "Compass Group",
|
|
},
|
|
"US": { # NYSE and Nasdaq
|
|
"SPY": "S&P 500 stock market", "QQQ": "Nasdaq 100 stock market", "IWM": "Russell 2000 small caps",
|
|
"DIA": "Dow Jones stock market", "AAPL": "Apple stock", "MSFT": "Microsoft stock",
|
|
"NVDA": "Nvidia stock", "AMZN": "Amazon stock", "GOOGL": "Alphabet Google stock",
|
|
"META": "Meta Platforms stock", "TSLA": "Tesla stock", "AVGO": "Broadcom stock",
|
|
"JPM": "JPMorgan stock", "V": "Visa stock", "MA": "Mastercard stock", "UNH": "UnitedHealth stock",
|
|
"XOM": "Exxon Mobil stock", "JNJ": "Johnson & Johnson stock", "WMT": "Walmart stock",
|
|
"COST": "Costco stock", "NFLX": "Netflix stock", "AMD": "AMD stock", "DIS": "Disney stock",
|
|
"KO": "Coca-Cola stock", "PEP": "PepsiCo stock", "BAC": "Bank of America stock",
|
|
"CVX": "Chevron stock", "LLY": "Eli Lilly stock", "ORCL": "Oracle stock", "CRM": "Salesforce stock",
|
|
},
|
|
}
|
|
_LIST = [_crypto(*c) for c in _CRYPTO] + [
|
|
_stock(t, ex, q) for ex, names in _STOCKS.items() for t, q in names.items()
|
|
]
|
|
INSTRUMENTS: dict[str, Instrument] = {i.tv_symbol: i for i in _LIST}
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Settings:
|
|
webhook_secret: str = os.getenv("WEBHOOK_SECRET", "change-me")
|
|
|
|
# Demo mode: built-in strategy, fake money. Nothing is ever sent to a broker.
|
|
demo_mode: bool = os.getenv("DEMO_MODE", "false").lower() == "true"
|
|
# "real": live prices from Yahoo Finance + real news headlines (default)
|
|
# "simulated": made-up prices and news, works fully offline
|
|
demo_data: str = os.getenv("DEMO_DATA", "real").lower()
|
|
demo_interval: str = os.getenv("DEMO_INTERVAL", "1h") # candle size for real data: 15m, 1h, 1d
|
|
demo_poll_seconds: float = _f("DEMO_POLL_SECONDS", 900) # how often to fetch new prices
|
|
demo_tick_seconds: float = _f("DEMO_TICK_SECONDS", 5) # simulated mode: one candle every N seconds
|
|
db_path: str = os.getenv("DB_PATH", "bot.db")
|
|
|
|
# Brokers
|
|
alpaca_key: str = os.getenv("ALPACA_API_KEY", "")
|
|
alpaca_secret: str = os.getenv("ALPACA_SECRET_KEY", "")
|
|
# Safety: live trading is impossible unless this is explicitly "true".
|
|
alpaca_live: bool = os.getenv("ALPACA_LIVE", "false").lower() == "true"
|
|
paper_start_cash: float = _f("PAPER_START_CASH", 10000)
|
|
|
|
# News sources (see app/news.py). Finnhub needs a free key from finnhub.io.
|
|
news_sources: tuple = tuple(x.strip().lower() for x in
|
|
os.getenv("NEWS_SOURCES", "google,ecb,fed,finnhub").split(",") if x.strip())
|
|
finnhub_key: str = os.getenv("FINNHUB_API_KEY", "")
|
|
# Central-bank feeds. If a feed ever moves, change the address here instead of the code.
|
|
ecb_feed_url: str = os.getenv("NEWS_ECB_URL", "https://www.ecb.europa.eu/rss/press.xml")
|
|
fed_feed_url: str = os.getenv("NEWS_FED_URL", "https://www.federalreserve.gov/feeds/press_all.xml")
|
|
|
|
# DeepSeek (OpenAI-compatible API)
|
|
deepseek_key: str = os.getenv("DEEPSEEK_API_KEY", "")
|
|
deepseek_model: str = os.getenv("DEEPSEEK_MODEL", "deepseek-chat")
|
|
deepseek_url: str = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
|
|
|
|
# Risk rules
|
|
max_position_pct: float = _f("MAX_POSITION_PCT", 0.10) # max 10% of equity per trade
|
|
max_order_eur: float = _f("MAX_ORDER_EUR", 50) # hard cap per buy, in euros
|
|
# Alpaca accounts are in US dollars, so the euro cap is converted with this rate.
|
|
# Update it now and then (or set it a bit low to stay safely under the cap).
|
|
eur_usd_rate: float = _f("EUR_USD_RATE", 1.10)
|
|
# Fallback rate (pounds per euro), only used when the live rate can't be fetched.
|
|
eur_gbp_rate: float = _f("EUR_GBP_RATE", 0.87)
|
|
# Skip buys while the instrument's exchange is closed (Mon to Fri, regular hours).
|
|
enforce_market_hours: bool = os.getenv("ENFORCE_MARKET_HOURS", "true").lower() == "true"
|
|
max_open_positions: int = int(os.getenv("MAX_OPEN_POSITIONS", 4))
|
|
daily_loss_limit_pct: float = _f("DAILY_LOSS_LIMIT_PCT", 0.03) # stop buying after -3% today
|
|
veto_confidence: float = _f("VETO_CONFIDENCE", 0.6) # bearish news at >=60% confidence blocks a buy
|
|
# US stock buys that arrive while Wall Street is closed: "skip" (default) or "queue"
|
|
# (queue = Alpaca fills at the next opening price, which may differ a lot from the signal).
|
|
us_after_hours: str = os.getenv("US_AFTER_HOURS", "skip").lower()
|
|
block_buys_if_filter_fails: bool = os.getenv("BLOCK_BUYS_IF_FILTER_FAILS", "true").lower() == "true"
|
|
|
|
|
|
settings = Settings()
|