@@ -1017,6 +1017,13 @@ def _num(v):
10171017 chart_timezone = chart_tz ,
10181018 syminfo_timezone = str (runtime_overrides .get ("timezone" ) or "" ) or None ,
10191019 syminfo_session = str (runtime_overrides .get ("session" ) or "" ) or None ,
1020+ syminfo_type = str (runtime_overrides .get ("type" ) or "" ) or None ,
1021+ syminfo_strings = {
1022+ k : str (runtime_overrides .get (k ))
1023+ for k in ("ticker" , "tickerid" , "currency" , "basecurrency" ,
1024+ "description" , "volumetype" )
1025+ if runtime_overrides .get (k )
1026+ } or None ,
10201027 syminfo_metadata = syminfo_metadata ,
10211028 syminfo_mintick = _num (runtime_overrides .get ("mintick" )),
10221029 syminfo_pointvalue = _num (runtime_overrides .get ("pointvalue" )),
@@ -1125,6 +1132,16 @@ def _setup_signatures(self) -> None:
11251132 if hasattr (L , "strategy_set_syminfo_session" ):
11261133 L .strategy_set_syminfo_session .argtypes = [ctypes .c_void_p , ctypes .c_char_p ]
11271134 L .strategy_set_syminfo_session .restype = None
1135+ # syminfo.type ("forex"/"stock"/"crypto"/...) + the generic string
1136+ # member setter (ticker/tickerid/currency/basecurrency/...). Older
1137+ # .so builds predate these exports — hasattr-guarded like the rest.
1138+ if hasattr (L , "strategy_set_syminfo_type" ):
1139+ L .strategy_set_syminfo_type .argtypes = [ctypes .c_void_p , ctypes .c_char_p ]
1140+ L .strategy_set_syminfo_type .restype = None
1141+ if hasattr (L , "strategy_set_syminfo_string" ):
1142+ L .strategy_set_syminfo_string .argtypes = [
1143+ ctypes .c_void_p , ctypes .c_char_p , ctypes .c_char_p ]
1144+ L .strategy_set_syminfo_string .restype = ctypes .c_int
11281145 if hasattr (L , "strategy_set_syminfo_metadata" ):
11291146 L .strategy_set_syminfo_metadata .argtypes = [
11301147 ctypes .c_void_p , ctypes .c_char_p , ctypes .c_double ]
@@ -1151,6 +1168,8 @@ def run(self, bars_csv: Path, params: dict | None = None,
11511168 chart_timezone : str | None = None ,
11521169 syminfo_timezone : str | None = None ,
11531170 syminfo_session : str | None = None ,
1171+ syminfo_type : str | None = None ,
1172+ syminfo_strings : dict | None = None ,
11541173 syminfo_metadata : dict | None = None ,
11551174 syminfo_mintick : float | None = None ,
11561175 syminfo_pointvalue : float | None = None ,
@@ -1243,6 +1262,17 @@ def run(self, bars_csv: Path, params: dict | None = None,
12431262 self .lib .strategy_set_syminfo_timezone (state , str (syminfo_timezone ).encode ())
12441263 if syminfo_session and hasattr (self .lib , "strategy_set_syminfo_session" ):
12451264 self .lib .strategy_set_syminfo_session (state , str (syminfo_session ).encode ())
1265+ # Instrument class (syminfo.type). Unset keeps the engine's
1266+ # "crypto" default byte-identical; per-symbol datasets pass
1267+ # TV's value ("forex" for OANDA:EURUSD, "stock" for NASDAQ:AAPL).
1268+ if syminfo_type and hasattr (self .lib , "strategy_set_syminfo_type" ):
1269+ self .lib .strategy_set_syminfo_type (state , str (syminfo_type ).encode ())
1270+ if syminfo_strings and hasattr (self .lib , "strategy_set_syminfo_string" ):
1271+ for skey , sval in syminfo_strings .items ():
1272+ if sval is None or str (sval ) == "" :
1273+ continue
1274+ self .lib .strategy_set_syminfo_string (
1275+ state , str (skey ).encode (), str (sval ).encode ())
12461276 if syminfo_metadata and hasattr (self .lib , "strategy_set_syminfo_metadata" ):
12471277 for mkey , mval in syminfo_metadata .items ():
12481278 try :
@@ -1515,6 +1545,32 @@ def _filter_trace_to_window(trace: list[dict], window: tuple[int, int] | None) -
15151545 ]
15161546
15171547
1548+ def format_trade_qty (qty : float ) -> str :
1549+ """Lot-faithful quantity text for the TV-alignable export.
1550+
1551+ The previous ``f"{qty:g}"`` kept only 6 significant digits, so any
1552+ quantity with more digits than that was silently rewritten on the way
1553+ out: an OANDA:EURUSD all-in lot of 923941.16 units (TV export, 0.01 lot
1554+ step) printed as ``923941``, 897902.68 printed as ``897903`` (rounded UP),
1555+ 92293.36 as ``92293.4``, and anything >= 1e6 as ``1e+06``. The ledger held
1556+ the exact value; only the CSV lied, which put a spurious 0.01-0.5 unit
1557+ "qty miss" on 1933/2708 entries of the EURUSD KI-52 all-in probe and on
1558+ every giua64 entry while the fills themselves were exact.
1559+
1560+ Eight decimals cover every lot step in use (1 share, 0.01 unit, 0.0001
1561+ contract, satoshi-scale 1e-8) and absorb binary noise from
1562+ floor(q/step)*step (0.30000000000000004 -> ``0.3``); trailing zeros are
1563+ trimmed the way TradingView prints its ``Size (qty)`` column, so every
1564+ value the old formatter rendered faithfully renders byte-identically.
1565+ """
1566+ if not math .isfinite (qty ):
1567+ return f"{ qty :g} "
1568+ text = f"{ qty :.8f} " .rstrip ("0" ).rstrip ("." )
1569+ if text in ("" , "-0" , "-" ):
1570+ return "0"
1571+ return text
1572+
1573+
15181574def write_engine_trades_csv (trades : list [dict ], path : Path ) -> None :
15191575 """Emit one row per trade *side* (exit then entry) in reverse-chronological
15201576 order — byte-for-byte alignable with TradingView's `trades.csv` export.
@@ -1552,7 +1608,7 @@ def write_engine_trades_csv(trades: list[dict], path: Path) -> None:
15521608 n , side ,
15531609 _fmt_time_utc (t [time_key ]),
15541610 f"{ t [price_key ]:.6f} " ,
1555- f" { t [ ' qty' ]:g } " ,
1611+ format_trade_qty ( float ( t [ " qty" ])) ,
15561612 f"{ t ['pnl' ]:.6f} " ,
15571613 f"{ t ['pnl_pct' ]:.4f} " ,
15581614 f"{ t ['max_runup' ]:.6f} " ,
@@ -1677,6 +1733,10 @@ def _run_via_docker(strategy_dir: Path, ohlcv_path: Path, params: dict,
16771733 syminfo ["timezone" ] = run_kwargs ["syminfo_timezone" ]
16781734 if run_kwargs .get ("syminfo_session" ):
16791735 syminfo ["session" ] = run_kwargs ["syminfo_session" ]
1736+ if run_kwargs .get ("syminfo_type" ):
1737+ syminfo ["type" ] = run_kwargs ["syminfo_type" ]
1738+ for skey , sval in (run_kwargs .get ("syminfo_strings" ) or {}).items ():
1739+ syminfo [skey ] = sval
16801740 if run_kwargs .get ("syminfo_mintick" ) is not None :
16811741 syminfo ["mintick" ] = run_kwargs ["syminfo_mintick" ]
16821742 if run_kwargs .get ("syminfo_pointvalue" ) is not None :
0 commit comments