Cannot save message information to my database
I am currently using Elektron feed to get real-time information about prices for different RIC codes. The sample code I got from this link https://github.com/Refinitiv/websocket-api/tree/master/Applications/Examples/python
I added extra modules and modified slightly a code:
_send_market_price_request(self, ric_name) - Inserted RIC codes manually
_on_message(self, message) - I added part which should save the message information to my database
When I do not add part which saves information to my db, I can see JSON messages in the output console from this line
print(message_json)
However, if I modify it as:
print(message_json)
cur.execute("""INSERT INTO ENK_OP.ENKadhoc (DataDateTime,Name,MyValueOne)
VALUES (cast(TIMESTAMP 'NOW' as date), '{0}', '{1}')"""
.format(
message_json[0]['Key'].get('Name', 'Unknown'),
1000
))
cur.execute("commit;")
I cannot see any output from print(message_json) and the code starts relogin/get new token every 10 sec.
Could you help to identify why this problem arises, as I believe the save to my db code part should work and does not affect print() function.
My python code is available here: ELEKTRON.txt
Best Answer
-
I have run the code. The first problem is "ric" is not defined when calling the self._send_market_price_request method.
Therefore, I change it to the below.
def _send_market_price_request(self):
""" Create and send simple Market Price request """
mp_req_json = {
'ID': 2,
'Key': {
'Name': [
'JPY=', 'THB=', 'EUR='
],
},
...
def _process_login_response(self, message_json):
""" Send item request """
if message_json['State']['Stream'] != "Open" or message_json['State']['Data'] != "Ok":
print("Login failed.")
sys.exit(1)
self.logged_in = True
self._send_market_price_request()Next, some messages (such as status messages) may not have the 'Key' property. Therefore, it is better to verify it before accessing its value.
def _on_message(self, message):
""" Called when message received, parse message into JSON for processing """
print("RECEIVED on " + self.session_name + ":")
#print(message)
message_json = json.loads(message)
print(message_json)
print("Message Key")
if('Key' in message_json[0]):
print(message_json[0]['Key'].get('Name', 'Unknown'))
print("Message Key End")0
Answers
-
It may relate to the capital X letter in the code.
for x in range(len(message_json)):
cur.execute("""INSERT INTO MYDB (DATE,Name,MyValue)
VALUES (cast(TIMESTAMP 'NOW' as date), '{0}', '{1}')"""
.format(
message_json[X]['Key'].get('Name', 'Unknown'),
1000
))The for loop uses the lower case (x) but in the code block it uses the capital one (X).
0 -
Yes, indeed, there is a typo, should be lowercase 'x'. Additionally, if I try to write a message_json[0] (without loop) in order to take the first part of the message the same problem arises.
0 -
@jirapongse.phuriphanvichai
Yes, I wrote that in my code I used several RIC codes, so the RIC codes you provided also suit as an example. Yes, your modified print works well, and to save the data I also tried to slightly modify the code asif ('Key' in message_json[0]):
cur.execute("INSERT INTO ENK_OP.ENKtemptrades (DATE,Name,MyValue)VALUES (cast(TIMESTAMP 'NOW' as date), '{0}', '{1}')".format(message_json[0]['Key'].get('Name', 'Unknown'),1000))
print(message_json)So it should save 'Name' from Key, however, the same problem again (no information is saved and no messages received). Usually, under 'RECEIVED on session1' I have JSON messages (so many times this 'RECEIVED on' repeats, but in this case only once).
RECEIVED on session1: RECEIVED on session1: [{'Type': 'Ping'}] SENT on session1: {
"Type":"Pong" }
RECEIVED on session1: [{'Type': 'Ping'}] SENT on session1: {
"Type":"Pong" }Continue in the next comment....
0 -
From the code above I identified that these Ping-Pong messages come from
def _process_message(self, message_json): message_type = message_json['Type']
I tried to simply use print(message_json['Type']), but see the same behavior that it does not print anything from messages at all
def _on_message(self, message):
print("RECEIVED on " + self.session_name + ":")
message_json = json.loads(message)
print(message_json['Type'])0 -
@g.suhharukov
Please share the current code that you are using. Therefore, I can verify it.
0
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛