Connect to a Polygon websocket to receive messages for AAPL trades and stream them into AWS Kinesis.
import boto3
from boto import kinesis
import getpass
import json
import time
from polygon import WebSocketClient, STOCKS_CLUSTER
Use the access and secret keys for your AWS account
ACCESS_KEY = getpass.getpass()
SECRET_KEY = getpass.getpass()
Generate a session token with the AWS CLI command:
aws sts get-session-token --duration-seconds 129600
SESSION_TOKEN = getpass.getpass()
Input your KEY_ID for Alpaca/Polygon
# Alpaca/Polygon KEY_ID
KEY_ID = getpass.getpass()
client = boto3.client(
'kinesis',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN
)
client = boto3.client('kinesis')
kinesis = kinesis.connect_to_region("us-east-1")
kinesis.describe_stream("polygon-trades-aapl")
kinesis.list_streams()
# This function runs everytime I receive a message from the websocket
def process_event(message):
event_list = json.loads(message)
# there can be one or more events per message
for event in event_list:
if event['ev'] == 'T':
kinesis.put_record("polygon-trades-aapl", json.dumps(event), "partitionkey")
my_client = WebSocketClient(STOCKS_CLUSTER, KEY_ID, process_event)
my_client.run_async()
# subscribe to the AAPL trade channel for 5 seconds.
my_client.subscribe("T.AAPL")
time.sleep(5)
# close the websocket after 100 seconds
my_client.close_connection()