Polygon.io is a financial market data platform for Stock APIs, Forex, Cryptocurrency etc. It works with both RESTful API & WebSockets.
#1. Polygon.io with WebSockets
Polygon.io provides a standardized interface for streaming real-time stock, forex, and crypto data using the WebSocket protocol. Users can specify which WebSocket channels they want to consume by sending instructions in the form of actions. Our WebSockets emit events to notify the user when an event has occurred in a subscribed channel.
WebSockets can be used with multiple programming languages like Python, Javascript, Go, C# etc. You can check some of the sample Polygon.io applications.
Below is a sample code with Javascript.
const WebSocket = require('ws') const APIKEY = process.env.POLY_API_KEY || 'YOUR_API_KEY' const ws = new WebSocket('wss://socket.polygon.io/forex') // Connection Opened: ws.on('open', () => { console.log('Connected!') ws.send(`{"action":"auth","params":"${APIKEY}"}`) ws.send(`{"action":"subscribe","params":"C.AUD/USD,C.USD/EUR,C.USD/JPY"}`) }) // Per message packet: ws.on('message', ( data ) => { data = JSON.parse( data ) data.map(( msg ) => { if( msg.ev === 'status' ){ return console.log('Status Update:', msg.message) } console.log('Tick:', msg) }) }) ws.on('error', console.log)
Here’s also a sample Python code.
import time from polygon import WebSocketClient, STOCKS_CLUSTER def my_custom_process_message(message): print("this is my custom message processing", message) def my_custom_error_handler(ws, error): print("this is my custom error handler", error) def my_custom_close_handler(ws): print("this is my custom close handler") def main(): key = 'your api key' my_client = WebSocketClient(STOCKS_CLUSTER, key, my_custom_process_message) my_client.run_async() my_client.subscribe("T.MSFT", "T.AAPL", "T.AMD", "T.NVDA") time.sleep(1) my_client.close_connection() if __name__ == "__main__": main()
#2. Polygon.io with RESTful APIs
The Polygon.io API is organized around REST. The API has predictable resource-oriented URLs, accepts form-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs.
Like WebSockets, RESTful API implementation can be done with Python, Javascript, PHP, Java & Kotlin.
Here’s a sample REST demo for Python.
from polygon import RESTClient def main(): key = "your api key" # RESTClient can be used as a context manager to facilitate closing the underlying http session # https://requests.readthedocs.io/en/master/user/advanced/#session-objects with RESTClient(key) as client: resp = client.stocks_equities_daily_open_close("AAPL", "2018-03-02") print(f"On: {resp.from_} Apple opened at {resp.open} and closed at {resp.close}") if __name__ == '__main__': main()
#3. Pricing
There are multiple pricing options. If you’re just looking to get started, you can go for the free plan. The free plan allows you to make 5 API calls/minute, end of day data and 2 years of historical data.
So, that’s it for this post on Fintech. Stay tuned for more.