Skip to content

Instantly share code, notes, and snippets.

@charlesalexanderlee
Created April 19, 2020 16:53
Show Gist options
  • Save charlesalexanderlee/dd0d49dabe4b86836e148519bd38b517 to your computer and use it in GitHub Desktop.
Save charlesalexanderlee/dd0d49dabe4b86836e148519bd38b517 to your computer and use it in GitHub Desktop.
import praw
import requests
import simplejson
from datetime import datetime
from praw.models import MoreComments
from time import sleep
from requests_oauthlib import OAuth1
from config import (api_key,
secret,
oath_token,
oath_secret,
client_id,
client_secret)
# Initializes Reddit API
reddit = praw.Reddit(client_id=client_id,
client_secret=client_secret,
user_agent='wallstreetbets')
# Authentication for Ally API
auth = OAuth1(api_key, secret, oath_token, oath_secret)
# Gets submission
submission = reddit.submission(id='g48v33')
# Top level comments only
submission.comments.replace_more(limit=None)
# Iterates through each top level comment
for comment in submission.comments:
# Splits comment into list of words
option = comment.body.upper().split()
ticker = ''
# Only gets comments with 3 parts
if len(option) == 3:
# Ticker
if option[0][0] == '$':
ticker += option[0][1:]
else:
ticker += option[0]
# Date
date = option[2].split('/')
if len(date) == 3:
ticker += date[2][0:2]
ticker += date[0].zfill(2)
ticker += date[1].zfill(2)
# Direction
if option[1][-1] == 'C':
ticker += 'C'
elif option[1][-1] == 'P':
ticker += 'P'
else:
break
# Strike
strike = ''
if option[1][0] == '$':
strike = option[1][1:-1]
else:
strike = option[1][0:-1]
if '.' in strike:
idx = strike.index('.')
ticker += strike[0:idx].zfill(5)
ticker += strike[idx+1:].zfill(3)
else:
ticker += strike.zfill(5) + '000'
try:
print(comment.body)
print(ticker)
url = f'https://api.tradeking.com/v1/market/ext/quotes.json?symbols={ticker}'
response = requests.get(url, auth=auth).json()
print(response['response']['quotes']['quote']['ask'])
print()
sleep(0.5)
except KeyError:
pass
except simplejson.errors.JSONDecodeError:
pass
@charlesalexanderlee
Copy link
Author

Here's a regex instead:

pattern = r"(?P<ticker>[a-z]+)\s+(?P<strike>\d+)\s?(?P<type>c|p)\s+(?P<date>\d{1,2}\/\d{1,2}\/\d{1,2})"

https://rubular.com/r/mjGWlpU5gMoNzI

Thanks so much man! I really appreciate it :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment