-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha8_sec gov api.py
250 lines (192 loc) · 7.97 KB
/
a8_sec gov api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#%%
from datetime import datetime,timedelta
import sys,os,copy,ast,socket,random,math,webbrowser,getpass,time,shutil,requests
import numpy as np
import pandas as pd
from pytz import timezone
import matplotlib.pyplot as plt
from pathlib import Path
from sec_edgar_downloader import Downloader
import re
import openai,getpass,os
from openai import OpenAI
path1=f'{path_to_openai_api}'
with open(path1,'r') as f:
key=f.readline()
os.environ["OPENAI_API_KEY"] = key
openai.api_key = key
openai_client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key=key,
)
from twilio.rest import Client
import yagmail
from datetime import datetime,timedelta
import pandas as pd
# Your Account SID from twilio.com/console
account_sid = "{account_sid_twilio}"
auth_token = "{auth_token_twilio}"
client = Client(account_sid, auth_token)
sender='{id_yagmail}'
#receiver='[email protected]'
email1=sender
passw1='{password_yagmail}'
passw1='{password_yagmail}'
yag = yagmail.SMTP(user=sender,password=passw1)
content1='1'
subject1='1'
def send_email(to_email,title,content):
yag.send(to=to_email,subject=title,contents=content)
def gpt_answer(define,content):
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": define},
{"role": "user", "content": content}
]
)
return response.choices[0].message.content
# %%
# %%
#%%
dl = Downloader("eontech", "[email protected]")
dl
# %%
# dl.get("8-K", "GNE")
# dl.get("10-K", 'METC', limit=1)
# %%\
list1=[]
path = f"/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news"
os.chdir(path)
#%%
tickers=input('TSLA,AAPL,MSFT')
# tickers.split(',')
# tickers='CTAS,TT,RSG,CAH,FICO,TW,MANH,FCN,PSN,OBDC,CWCO'
ticker_list = [ticker.strip() for ticker in tickers.split(',')]
ticker='BRBR'
for ticker in ticker_list:
print("ticker: ",ticker,'generate 10k...')
dl.get("10-K", ticker, limit=1)
time.sleep(1)
time.sleep(5)
#%%
def generate_10k_summary(ticker):
print("ticker: ",ticker)
folder=f'/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/sec-edgar-filings/{ticker}'
dl.get("10-K", ticker, limit=1)
time.sleep(5)
if os.path.isdir(folder):
dl.get("10-K", ticker, limit=1)
else:
dl.get("10-K", ticker, limit=1)
time.sleep(5)
sub2=os.listdir(folder)[0]
sub3=os.listdir(os.path.join(folder,sub2))[0]
txt1=os.listdir(os.path.join(folder,sub2,sub3))[0]
path1=os.path.join(folder,sub2,sub3,txt1)
while not os.path.isfile(path1):
print("Waiting for download to complete...",path1)
time.sleep(1) # Check every 5 seconds
# path1='/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/sec-edgar-filings/TSLA/10-K/0000950170-23-001409/full-submission.txt'
# path1="/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/sec-edgar-filings/MSFT/10-K/0000950170-23-035122/full-submission.txt"
with open(path1,'r') as file:
data=file.read()
# data = re.sub('<[^>]+>', '', data)
# TARGET_NAME='ITEM 1.'.lower()
TARGET_NAME='>business<'.lower()
# TARGET_NAME='ITEM 7.'
split_list=data.lower().split(TARGET_NAME)
if len(split_list)>3:
splited_list=data.lower().split(TARGET_NAME)[1].split('\n')[:20]
else:
splited_list=data.lower().split(TARGET_NAME)[-1].split('\n')[:20]
print("\n>> len(splited_list)= ", len(splited_list))
text1="".join(splited_list)
cleaned_text = re.sub('<[^>]+>', '', text1)[:4000]
print("\n>> len(cleaned_text)= ", len(cleaned_text))
url=f'https://api.polygon.io/v3/reference/tickers/{ticker}?apiKey={polygon_api_key}'
response=requests.get(url)
json1=response.json()
stock_name=json1['results']['name']
stock_name
cleaned_text="the company is "+stock_name+". "+cleaned_text
# define="summarize based which products/services this company make. why is it better than competitors. what kind of customers will buy and why"
define='Does it include business description of the company, what products this company sells, and why customers buy from them?'
answer1=gpt_answer(define,cleaned_text)
answer1
# TARGET_NAME='>risk factors<'
TARGET_NAME='item 1a.'
TARGET_NAME=TARGET_NAME.lower()
split_list=data.lower().split(TARGET_NAME)
cleaned_text=split_list[-1]
cleaned_text = re.sub('<[^>]+>', '', cleaned_text)[:4000]
cleaned_text="the company is "+stock_name+". "+cleaned_text
# define="summarize based on what are important events that happened for the company. If it has any financial numbers or other numbers, give me summary with the numbers."
define='Does it include risk factors for this company?'
answer2=gpt_answer(define,cleaned_text)
answer2
TARGET_NAME='DISCUSSION AND ANALYSIS OF FINANCIAL CONDITION AND RESULTS OF OPERA'
TARGET_NAME=TARGET_NAME.lower()
split_list=data.lower().split(TARGET_NAME)
cleaned_text=split_list[-1].split('overview')[-1]
cleaned_text = re.sub('<[^>]+>', '', cleaned_text)[:4000]
cleaned_text="the company is "+stock_name+". "+cleaned_text
# define="summarize based on what are important events that happened for the company. If it has any financial numbers or other numbers, give me summary with the numbers."
define='Does it include good news or bad news for the company?'
answer3=gpt_answer(define,cleaned_text)
answer3
list1.append(ticker)
list1.append(f"#1 - description: {answer1}")
list1.append('\n\n')
list1.append(f"#2 - risk factors: {answer2}")
list1.append('\n\n')
list1.append(f"#3 - financial: {answer3}")
list1.append('\n\n')
list1.append('-----------------------------------')
list1.append('\n\n')
# os.remove(folder)
folder_10k = Path(folder+"/10-K")
if folder_10k.exists():
# Recursively delete the folder and all its contents
shutil.rmtree(folder_10k)
for ticker in ticker_list:
try:
generate_10k_summary(ticker)
except Exception as e:
print( 0 ,' = >>> some error = ',e)
list1.append('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')
send_email('{id_yagmail}',"10k info: "+str(", ".join(ticker_list))," ".join([item for item in list1]))
# conda activate feature1
# cd "/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/"
# python3 "a8_sec gov api.py"
#%%
dl.get("10-K", "PSN", limit=1)
# %%
path3=f'/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/data/full-submission.txt'
path3=f'/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/sec-edgar-filings/CTAS/10-K/0000723254-23-000025/full-submission.txt'
with open(path3,'r') as file:
data=file.read()
data = re.sub('<[^>]+>', '', data)
#%%
TARGET_NAME='DISCUSSION AND ANALYSIS OF FINANCIAL CONDITION AND RESULTS OF OPERA'
TARGET_NAME=TARGET_NAME.lower()
split_list=data.lower().split(TARGET_NAME)
cleaned_text=split_list[-1].split('overview')[1][:4000]
# %%
path4=f'/Users/{getpass.getuser()}/Library/CloudStorage/[email protected]/My Drive/t1_code/t6_trading_news/sec-edgar-filings/CTAS/10-K/0000723254-23-000025/full-submission.txt'
with open(path4,'r') as file:
data=file.read()
data = re.sub('<[^>]+>', '', data)
TARGET_NAME='DISCUSSION AND ANALYSIS OF FINANCIAL CONDITION AND RESULTS OF OPERA'
TARGET_NAME=TARGET_NAME.lower()
split_list=data.lower().split(TARGET_NAME)
print("\n>> len(split_list)= ", len(split_list))
#%%
new1=split_list[-1].split('overview')
print("\n>> len(new1)= ", len(new1))
#%%
split_list[-1][:1000]
#%%
cleaned_text=split_list[-1].split('overview')[1][:4000]
cleaned_text="the company is "+'stock_name'+". "+cleaned_text
# %%