File size: 9,329 Bytes
acaeaf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FVG-Delta Forex Signal Engine v6.0 — Instrument Universe (generated)
===================================================================

The universe is GENERATED so the scanner returns the FULL liquid market after
filtering (100+ instruments), not a tiny hand list:

  major   : FX pairs containing USD          (EUR/USD, USD/JPY, ...)
  minor   : FX crosses of the 8 majors       (EUR/JPY, GBP/JPY, EUR/GBP, ...)
  exotic  : USD/EUR/GBP vs SGD/NOK/SEK/ZAR/MXN/TRY/CNH/... and exotic crosses
  metal   : XAU, XAG, XPT, XPD, Copper
  energy  : WTI, Brent, NatGas, Heating Oil, Gasoline
  commodity: Corn, Wheat, Soybean, Coffee, Sugar, Cocoa, Cotton
  index   : NAS100, SPX500, US30, RUT, DAX, FTSE, CAC, EU50, IBEX, JPN225,
            HK50, AUS200, SWI20, IN50, DXY

Each instrument carries: yahoo (yfinance symbol), tv (TradingView symbol), cat,
digits, pip, spread (pips, typical), csize (units/std lot), pipval ($/pip/std lot),
sess (clock bucket). FX digits/pip/pipval are derived from the quote currency
(JPY-quoted => 3 digits, 0.01 pip; else 5 digits, 0.0001 pip). pipval is a
broker-approx default and is overridable via FVG_PIPVAL_<PAIR> (and FVG_SPREAD_<PAIR>).
Filter categories with FVG_UNIVERSE_INCLUDE / FVG_UNIVERSE_EXCLUDE.
"""

from __future__ import annotations
import os
from itertools import combinations
from typing import Dict, List, Optional

_FIELDS = ("key", "yahoo", "tv", "cat", "digits", "pip", "spread", "csize", "pipval", "sess")

# Conventional precedence: base = earlier in this list, quote = later. JPY always
# quote, USD mid so EUR/USD..NZD/USD and USD/CAD..USD/JPY come out conventional.
_RANK = ["EUR", "GBP", "AUD", "NZD", "USD", "CAD", "CHF",
         "NOK", "SEK", "DKK", "SGD", "HKD", "CNH", "PLN", "HUF", "CZK",
         "TRY", "ZAR", "MXN", "THB", "INR", "ILS", "RON", "JPY"]
_RIDX = {c: i for i, c in enumerate(_RANK)}

_MAJ8 = ["EUR", "GBP", "AUD", "NZD", "USD", "CAD", "CHF", "JPY"]

# exotic counterparts paired against a base currency
_USD_EXO = ["SGD", "HKD", "CNH", "NOK", "SEK", "DKK", "PLN", "HUF", "CZK",
            "TRY", "ZAR", "MXN", "THB", "INR", "ILS", "RON"]
_EUR_EXO = ["NOK", "SEK", "DKK", "PLN", "HUF", "CZK", "TRY", "ZAR", "SGD", "RON"]
_GBP_EXO = ["NOK", "SEK", "PLN", "ZAR", "SGD", "TRY"]
_MISC_EXO = ["NOKSEK", "NOKJPY", "SEKJPY", "ZARJPY", "MXNJPY", "TRYJPY",
             "SGDJPY", "AUDSGD", "CADNOK", "CHFSGD", "EURMXN", "GBPTRY"]


def _order(a: str, b: str) -> tuple:
    return (a, b) if _RIDX.get(a, 99) <= _RIDX.get(b, 99) else (b, a)


def _fx_row(base: str, quote: str) -> Optional[Dict]:
    if base == quote:
        return None
    base, quote = _order(base, quote)
    key = f"{base}/{quote}"
    jpy = quote == "JPY"
    digits = 3 if jpy else 5
    pip = 0.01 if jpy else 0.0001
    if quote == "USD":
        cat, pipval = "major", 10.0
    elif "USD" in (base, quote):
        cat, pipval = "major", (6.7 if jpy else 10.0)
    elif base in _MAJ8 and quote in _MAJ8:
        cat, pipval = "minor", (6.7 if jpy else 10.0)
    else:
        cat, pipval = "exotic", (6.7 if jpy else 10.0)
    spread = {"major": 1.2, "minor": 2.2, "exotic": 12.0}[cat]
    return dict(zip(_FIELDS, (key, f"{base}{quote}=X", f"OANDA:{base}{quote}",
                             cat, digits, pip, spread, 100000, pipval, "fx")))


def _gen_fx() -> List[Dict]:
    seen = set()
    rows: List[Dict] = []

    def add(a: str, b: str):
        r = _fx_row(a, b)
        if r and r["key"] not in seen:
            seen.add(r["key"])
            rows.append(r)

    for a, b in combinations(_MAJ8, 2):       # 28 major/minor crosses
        add(a, b)
    for x in _USD_EXO:
        add("USD", x)
    for x in _EUR_EXO:
        add("EUR", x)
    for x in _GBP_EXO:
        add("GBP", x)
    for pair in _MISC_EXO:
        add(pair[:3], pair[3:])
    return rows


# Non-FX instruments (explicit metadata).
_NONFX = [
    # key,        yahoo,      tv,            cat,        digits, pip,   spread, csize, pipval, sess
    ("XAU/USD",   "GC=F",     "OANDA:XAUUSD", "metal",     2, 0.1,    2.5,  100,   10.0,  "metal"),
    ("XAG/USD",   "SI=F",     "OANDA:XAGUSD", "metal",     3, 0.01,   3.0,  5000,  50.0,  "metal"),
    ("XPT/USD",   "PL=F",     "TVC:PLATINUM", "metal",     2, 0.1,    5.0,  100,   10.0,  "metal"),
    ("XPD/USD",   "PA=F",     "TVC:PALLADIUM","metal",     2, 0.1,    8.0,  100,   10.0,  "metal"),
    ("COPPER",    "HG=F",     "CAPITALCOM:COPPER","metal",  4, 0.0001, 4.0,  25000, 12.5,  "metal"),
    ("WTI/USD",   "CL=F",     "TVC:USOIL",    "energy",    2, 0.01,   3.0,  1000,  10.0,  "energy"),
    ("BRENT/USD", "BZ=F",     "TVC:UKOIL",    "energy",    2, 0.01,   3.5,  1000,  10.0,  "energy"),
    ("NATGAS",    "NG=F",     "TVC:NATGAS",   "energy",    3, 0.001,  5.0,  10000, 10.0,  "energy"),
    ("HEATOIL",   "HO=F",     "NYMEX:HO1!",   "energy",    4, 0.0001, 6.0,  42000, 4.2,   "energy"),
    ("GASOLINE",  "RB=F",     "NYMEX:RB1!",   "energy",    4, 0.0001, 6.0,  42000, 4.2,   "energy"),
    ("CORN",      "ZC=F",     "CBOT:ZC1!",    "commodity", 2, 0.25,   3.0,  50,    12.5,  "commodity"),
    ("WHEAT",     "ZW=F",     "CBOT:ZW1!",    "commodity", 2, 0.25,   3.5,  50,    12.5,  "commodity"),
    ("SOYBEAN",   "ZS=F",     "CBOT:ZS1!",    "commodity", 2, 0.25,   4.0,  50,    12.5,  "commodity"),
    ("COFFEE",    "KC=F",     "ICEUS:KC1!",   "commodity", 2, 0.05,   5.0,  375,   18.75, "commodity"),
    ("SUGAR",     "SB=F",     "ICEUS:SB1!",   "commodity", 2, 0.01,   4.0,  112000,11.2,  "commodity"),
    ("COCOA",     "CC=F",     "ICEUS:CC1!",   "commodity", 0, 1.0,    5.0,  10,    10.0,  "commodity"),
    ("COTTON",    "CT=F",     "ICEUS:CT1!",   "commodity", 2, 0.01,   4.0,  50000, 5.0,   "commodity"),
    ("NAS100",    "^NDX",     "TVC:NDX",      "index",     1, 1.0,    1.0,  1,     1.0,   "idx_us"),
    ("SPX500",    "^GSPC",    "TVC:SPX",      "index",     1, 1.0,    0.5,  1,     1.0,   "idx_us"),
    ("US30",      "^DJI",     "TVC:DJI",      "index",     0, 1.0,    2.0,  1,     1.0,   "idx_us"),
    ("RUSSELL",   "^RUT",     "TVC:RUT",      "index",     1, 1.0,    1.0,  1,     1.0,   "idx_us"),
    ("GER40",     "^GDAXI",   "TVC:DAX",      "index",     0, 1.0,    1.0,  1,     1.1,   "idx_eu"),
    ("UK100",     "^FTSE",    "TVC:UKX",      "index",     0, 1.0,    1.0,  1,     1.3,   "idx_eu"),
    ("FRA40",     "^FCHI",    "TVC:CAC40",    "index",     0, 1.0,    1.0,  1,     1.1,   "idx_eu"),
    ("EU50",      "^STOXX50E","TVC:SX5E",     "index",     0, 1.0,    1.5,  1,     1.1,   "idx_eu"),
    ("ESP35",     "^IBEX",    "TVC:IBC",      "index",     0, 1.0,    2.0,  1,     1.1,   "idx_eu"),
    ("JPN225",    "^N225",    "TVC:NI225",    "index",     0, 1.0,    7.0,  1,     0.007, "idx_jp"),
    ("HK50",      "^HSI",     "TVC:HSI",      "index",     0, 1.0,    5.0,  1,     0.13,  "idx_jp"),
    ("AUS200",    "^AXJO",    "ASX:XJO",      "index",     0, 1.0,    2.0,  1,     0.65,  "idx_jp"),
    ("SWI20",     "^SSMI",    "TVC:SMI",      "index",     0, 1.0,    2.0,  1,     1.2,   "idx_eu"),
    ("IN50",      "^NSEI",    "NSE:NIFTY",    "index",     1, 1.0,    2.0,  1,     0.012, "idx_jp"),
    ("DXY",       "DX-Y.NYB", "TVC:DXY",      "index",     3, 0.001,  2.0,  1000,  10.0,  "idx_us"),
]


def _build() -> Dict[str, Dict]:
    out: Dict[str, Dict] = {}
    rows = _gen_fx() + [dict(zip(_FIELDS, r)) for r in _NONFX]
    for d in rows:
        tag = d["key"].replace("/", "")
        try:
            d["spread"] = float(os.getenv(f"FVG_SPREAD_{tag}", d["spread"]))
        except Exception:
            pass
        try:
            d["pipval"] = float(os.getenv(f"FVG_PIPVAL_{tag}", d["pipval"]))
        except Exception:
            pass
        out[d["key"]] = d
    return out


INSTRUMENTS: Dict[str, Dict] = _build()
_BY_YAHOO = {v["yahoo"]: v for v in INSTRUMENTS.values()}


def universe(include: Optional[str] = None, exclude: Optional[str] = None) -> List[str]:
    """Ordered instrument keys, filtered by category (major,minor,exotic,metal,
    energy,commodity,index) via FVG_UNIVERSE_INCLUDE / FVG_UNIVERSE_EXCLUDE."""
    inc = {c.strip().lower() for c in (include or "").split(",") if c.strip()}
    exc = {c.strip().lower() for c in (exclude or "").split(",") if c.strip()}
    keys = []
    for k, v in INSTRUMENTS.items():
        cat = v["cat"]
        if inc and cat not in inc:
            continue
        if cat in exc:
            continue
        keys.append(k)
    return keys


def meta(key: str) -> Optional[Dict]:
    return INSTRUMENTS.get(key)


def by_yahoo(sym: str) -> Optional[Dict]:
    return _BY_YAHOO.get(sym)


def yahoo_symbol(key: str) -> Optional[str]:
    m = INSTRUMENTS.get(key)
    return m["yahoo"] if m else None


def tv_symbol(key: str) -> str:
    m = INSTRUMENTS.get(key)
    return m["tv"] if m else key.replace("/", "")


def digits(key: str) -> int:
    m = INSTRUMENTS.get(key)
    return int(m["digits"]) if m else 5


def pip_size(key: str) -> float:
    m = INSTRUMENTS.get(key)
    return float(m["pip"]) if m else 0.0001


if __name__ == "__main__":
    u = universe()
    by_cat: Dict[str, int] = {}
    for k in u:
        by_cat[INSTRUMENTS[k]["cat"]] = by_cat.get(INSTRUMENTS[k]["cat"], 0) + 1
    print(f"Universe: {len(u)} instruments")
    print("By category:", by_cat)
    print("Sample:", u[:12], "...", u[-6:])