11---
22title : Ingest real-time financial websocket data - Set up the dataset
33excerpt : Set up a dataset so you can query financial tick data to analyze price changes
4- products : [cloud, mst, self_hosted]
5- keywords : [tutorials, finance, learn]
6- tags : [tutorials, advanced]
4+ products : [cloud]
5+ keywords : [finance, analytics, websockets, data pipeline]
6+ tags : [tutorials, intermediate]
7+ layout_components : [next_prev_large]
8+ content_group : Ingest real-time financial websocket data
79---
810
911import CreateAndConnect from "versionContent/_ partials/_ cloud-create-connect-tutorials.mdx";
@@ -26,7 +28,7 @@ regular PostgreSQL table named `company`.
2628<Collapsible heading =" Connect to the websocket server " defaultExpanded ={false} >
2729
2830When you connect to the Twelve Data API through a websocket, you create a
29- persistent connection between your computer and the websocket server.
31+ persistent connection between your computer and the websocket server.
3032You set up a Python environment, and pass two arguments to create a
3133websocket object and establish the connection.
3234
@@ -168,11 +170,11 @@ To ingest the data into your Timescale service, you need to implement the
168170` on_event` function.
169171
170172After the websocket connection is set up, you can use the ` on_event` function
171- to ingest data into the database. This is a data pipeline that ingests real-time
173+ to ingest data into the database. This is a data pipeline that ingests real-time
172174financial data into your Timescale service.
173175
174176Stock trades are ingested in real-time Monday through Friday, typically during
175- normal trading hours of the New York Stock Exchange (9:30& nbsp; AM to
177+ normal trading hours of the New York Stock Exchange (9:30& nbsp; AM to
1761784:00& nbsp; PM& nbsp; EST).
177179
178180< CreateHypertableStocks />
@@ -246,7 +248,7 @@ This function needs to:
246248 if self.conn is not None:
247249 cursor = self.conn.cursor()
248250 sql = f"""
249- INSERT INTO {self.DB_TABLE} ({' ,' .join(self.DB_COLUMNS)})
251+ INSERT INTO {self.DB_TABLE} ({' ,' .join(self.DB_COLUMNS)})
250252 VALUES %s;"""
251253 execute_values(cursor, sql, data)
252254 self.conn.commit()
@@ -274,7 +276,7 @@ This function needs to:
274276 print(f" Batch insert #{self.insert_counter}" )
275277 self.current_batch = []
276278 def start(self, symbols):
277- " " " Connect to the web socket server and start streaming real-time data
279+ " " " Connect to the web socket server and start streaming real-time data
278280 into the database.
279281
280282 Args:
@@ -287,12 +289,12 @@ This function needs to:
287289 while True:
288290 ws.heartbeat()
289291 time.sleep(10)
290- onn = psycopg2.connect(database=" tsdb" ,
291- host=" < HOST> " ,
292- user=" tsdbadmin" ,
292+ onn = psycopg2.connect(database=" tsdb" ,
293+ host=" < HOST> " ,
294+ user=" tsdbadmin" ,
293295 password=" < PASSWORD> " ,
294296 port=" < PORT> " )
295-
297+
296298 symbols = [" BTC/USD" , " ETH/USD" , " MSFT" , " AAPL" ]
297299 websocket = WebsocketPipeline(conn)
298300 websocket.start(symbols=symbols)
@@ -333,4 +335,4 @@ the energy consumption dataset.
333335< /Collapsible>
334336
335337[twelve-wrapper]: https://github.com/twelvedata/twelvedata-python
336- [psycopg2]: https://www.psycopg.org/docs/
338+ [psycopg2]: https://www.psycopg.org/docs/
0 commit comments