-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_timestamps.py
More file actions
64 lines (50 loc) · 1.99 KB
/
add_timestamps.py
File metadata and controls
64 lines (50 loc) · 1.99 KB
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
import os
import time
import eikon as ek
import pandas as pd
import pandas_market_calendars as mcal
from dotenv import load_dotenv
if __name__ == "__main__":
load_dotenv()
ek.set_app_key(os.getenv("EIKON_APP_KEY"))
directory = os.path.join(__file__, "..", "data", "prices")
for file in os.listdir(directory):
if not file.endswith(".csv"):
continue
# Get the instrument RIC
ric = file.split(".")[0].replace("-", ".")
print(f"Processing RIC: {ric}")
# Get the exchange for the RIC
success = False
while not success:
try:
exchange = ek.get_data(
instruments=[ric],
fields=["TR.ExchangeMarketIdCode"],
field_name=True,
)[0]["TR.EXCHANGEMARKETIDCODE"][0]
print(f"\tFound exchange: {exchange}")
success = True
except Exception as e:
print(f"\tError getting exchange for {ric}: {e}")
print("\tRetrying...")
# Sleep for 3 seconds before retrying
time.sleep(3)
# Handle edge cases
if exchange == "MTAA":
exchange = "XMIL"
file_path = os.path.join(directory, file)
df = pd.read_csv(file_path, parse_dates=["Date"], index_col="Date")
# Get the trading calendar for the exchange
sched = mcal.get_calendar(exchange)
extended_sched = sched.schedule(start_date="2023-10-23", end_date="2025-06-11")
# Use market close times for timestamps
trading_days = extended_sched["market_close"]
# Build a map from plain dates to close‐of‐day timestamps
td = trading_days.copy()
td.index = td.index.normalize()
# Replace the date‐only index with the corresponding timestamps
df.index = df.index.map(td.to_dict())
# Save the df
df.to_csv(file_path, index=True)
print(f"Processed {file} with timestamps.")