Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): date Nobitex daily bars by the Tehran day they cover

Rudy Celekli4146f982f584
diff --git a/agent/backtest/loaders/nobitex.py b/agent/backtest/loaders/nobitex.py
index 77ddeae1d..10f1b6361 100644
--- a/agent/backtest/loaders/nobitex.py
+++ b/agent/backtest/loaders/nobitex.py
@@ -73,6 +73,8 @@ _NOBITEX_PROBE_TIMEOUT_S = positive_env_int("NOBITEX_PROBE_TIMEOUT_S", 8)
_OUTPUT_COLUMNS = ["open", "high", "low", "close", "volume"]
+_TEHRAN_TZ = "Asia/Tehran"
+
_HEADERS = {"User-Agent": "TraderBot/vibe-trading-loader"}
@@ -192,6 +194,8 @@ class DataLoader:
"""Paginated UDF download; pages walk older batches of <=500 bars."""
deadline = time.monotonic() + _NOBITEX_FETCH_BUDGET_S
label = f"Nobitex fetch for {symbol}"
+ # The first Tehran day's bar opens before start_ts (UTC midnight).
+ request_from = start_ts - 12 * 3600 if resolution == "D" else start_ts
frames: list[pd.DataFrame] = []
page = 1
@@ -204,7 +208,7 @@ class DataLoader:
params={
"symbol": symbol,
"resolution": resolution,
- "from": start_ts,
+ "from": request_from,
"to": end_ts,
"page": page,
},
@@ -247,7 +251,7 @@ class DataLoader:
)
frame["volume"] = frame["volume"].fillna(0)
frames.append(frame)
- if len(times) < _MAX_PER_PAGE or int(times[0]) <= start_ts:
+ if len(times) < _MAX_PER_PAGE or int(times[0]) <= request_from:
break
page += 1
time.sleep(_PAGE_SLEEP_S)
@@ -257,6 +261,16 @@ class DataLoader:
df = pd.concat(frames).set_index("trade_date").sort_index()
df = df[~df.index.duplicated(keep="last")]
+ if resolution == "D":
+ # Daily bars open at Tehran midnight (20:30 UTC the day before);
+ # label each with the Tehran date it covers.
+ df.index = (
+ df.index.tz_localize("UTC")
+ .tz_convert(_TEHRAN_TZ)
+ .tz_localize(None)
+ .normalize()
+ .rename("trade_date")
+ )
start_dt = pd.Timestamp(start_ts, unit="s")
end_dt = pd.Timestamp(end_ts, unit="s")
df = df[(df.index >= start_dt) & (df.index < end_dt)]
diff --git a/agent/tests/test_nobitex_loader.py b/agent/tests/test_nobitex_loader.py
index 74129e219..557914736 100644
--- a/agent/tests/test_nobitex_loader.py
+++ b/agent/tests/test_nobitex_loader.py
@@ -136,6 +136,27 @@ class TestFetch:
assert recorder.calls[0]["params"]["resolution"] == "D"
assert recorder.calls[0]["params"]["page"] == 1
+ def test_daily_bars_carry_the_tehran_date_they_cover(self, monkeypatch):
+ # Live daily epochs: 20:30 UTC = Tehran midnight of the next date.
+ recorder = _patch(
+ monkeypatch,
+ {
+ 1: _udf_payload(
+ [1792614600, 1792701000, 1792787400], # 2026-10-21/22/23 20:30 UTC
+ ["1", "2", "3"],
+ ["2", "3", "4"],
+ ["0.5", "1", "2"],
+ ["1.5", "2.5", "3.5"],
+ ["10", "20", "30"],
+ )
+ },
+ )
+ out = nobitex.DataLoader().fetch(["BTC-IRT"], "2026-10-22", "2026-10-23")
+ df = out["BTC-IRT"]
+ assert list(df.index) == [pd.Timestamp("2026-10-22"), pd.Timestamp("2026-10-23")]
+ assert df["close"].tolist() == [1.5, 2.5]
+ assert recorder.calls[0]["params"]["from"] < 1792614600
+
def test_no_data_yields_empty_result(self, monkeypatch):
_patch(monkeypatch, {1: {"s": "no_data"}})
out = nobitex.DataLoader().fetch(["BTC-IRT"], "2024-01-01", "2024-01-03")