Need help to update the Python script that can fetch a price history starting from 2008 (or the

Query: I am trying to draft a Python script that can fetch a long price history starting from 2008 (or the launch date of the RIC, whichever is later) and insert the data into our database.
However, after multiple attempts I am still facing one issue after another, and I am unable to get the script working end-to-end.
I was wondering if you could help me draft the correct version of this script or guide me with the best approach to achieve this
import pandas as pd
import win32com.client
import subprocess
import json
import requests
import re
import eikon as ek
import lseg.data as ld
from urllib.request import urlopen
from operator import itemgetter
import openpyxl
from datetime import date,timedelta,datetime
from openpyxl.drawing.text import RichTextProperties
today = date.today()#-timedelta(1)
date_time = today
date_time = date_time.strftime("%Y-%m-%d")
print('Connecting to Eikon')
#ld.set_log_level(0)
ld.open_session(app_key='REMOVED')
print('Connection Established to Eikon')
RICs = pd.read_excel(r'REMOVED\Eikon API Snapper\PKEY_PYTHON_LIBRARY.xlsx', sheet_name='List', header=None)
RICs = RICs[0].astype(str).tolist()
for RIC1 in RICs:
RIC = "'" + RIC1 + "'"
print("Checking for:", RIC1)
while True:
try:
df = ld.get_history([RIC1], start = '2007-12-31',end = '2015-12-31',interval='daily',fields = ['TRDPRC_1'])
#df = ld.get_history([RIC1], start = '2022-08-01',end = '2025-09-17',interval='daily',fields = ['TRDPRC_1'])
break
except ek.EikonError as err:
if err.code != 2504:
# request failed with other reason than 2504 error code then stop ... if 2504 then retry
break
#retry
print("retrying API snap...")
timer.wait(1)
#df = ek.get_timeseries([RIC1], start_date = '2006-12-01',end_date = '2018-12-31',interval='daily',fields = ['CLOSE'])
#df = ek.get_timeseries([RIC1], start_date='2018-12-01', end_date='2024-06-30', interval='daily', fields=['CLOSE'])
#this is for pkey to ric dictionary
Lib = pd.read_excel(r'S:\\Eikon API Snapper\PKEY_PYTHON_LIBRARY.xlsx', sheet_name='Library', header=None)
Pkey = dict(zip(Lib[0], Lib[1]))
df = df.iloc[2:]
if df.shape[1] == 1:
df.columns = ['Close']
df['Date'] = df.index
else:
df.columns = ['Date', 'Close']
df.reset_index(drop=True, inplace=True)
df['Date'] = pd.to_datetime(df.Date)
df['Date'] = df['Date'].dt.strftime("'%d%b%Y'")
df['Instrument'] = pd.Series([RIC1 for _ in range(len(df.index))])
df['Pkey'] = df['Instrument'].map(Pkey)
print(df.tail(3))
df['sql_query'] = df.apply(lambda r: "call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE(%s, %s, %s, NULL, NULL)" % (r['Pkey'], r['Date'], r['Close']), axis=1)
payload = df['sql_query']
p = '\n'.join(str(e) for e in payload)
print("Sample of Stored Procedures generated for", RIC1)
print('\n'.join(payload.tail(3).astype(str)))
headers = {'Content-Type': 'text/plain'}
url = "https://pv-marketdata-store.ihsmvals.com/sql/"
responseupdate = requests.request("POST", url, headers=headers, data=p)
print(responseupdate)
print("Data successfully inserted for RIC:", RIC1)
Answers
-
Please DO NOT post client information or credentials in these public forums !!
1 -
The provided Python script is trying to update some internal systems. It uses combination of REST API and SQL queries. We can only provide help with the LSEG APIs.
The API signature to get the historical data is correct and should be able to get the historical price points for the instruments.
ld.get_history([RIC1], start = '2007-12-31',end = '2015-12-31',interval='daily',fields = ['TRDPRC_1'])
The TRDPRC_1 is a real time field and would only be available for tradable instruments like equities etc.
1
Categories
- All Categories
- 3 Polls
- 6 AHS
- 37 Alpha
- 167 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 710 Datastream
- 1.5K DSS
- 637 Eikon COM
- 5.3K Eikon Data APIs
- 18 Electronic Trading
- 1 Generic FIX
- 7 Local Bank Node API
- 10 Trading API
- 3K Elektron
- 1.5K EMA
- 260 ETA
- 571 WebSocket API
- 42 FX Venues
- 16 FX Market Data
- 2 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 26 Messenger Bot
- 4 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 284 Open PermID
- 47 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 25 RDMS
- 2.3K Refinitiv Data Platform
- 17 CFS Bulk File/TM3
- 928 Refinitiv Data Platform Libraries
- 5 LSEG Due Diligence
- 1 LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 46 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 126 Open DACS
- 1.1K RFA
- 108 UPA
- 197 TREP Infrastructure
- 232 TRKD
- 924 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 106 Workspace SDK
- 11 Element Framework
- 5 Grid
- 19 World-Check Data File
- 1 Yield Book Analytics
- 48 中文论坛