forked from cybermanish2287/algo_trading
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp3.py
65 lines (57 loc) · 1.98 KB
/
app3.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
import os
import pandas as pd
from datetime import date, timedelta
from datetime import datetime
from GoogleNews import GoogleNews
import time
import random
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
cwd = os.chdir("C:\Desktop\Learning\Courses\zerodha")
sym = pd.read_csv("NSE_Symbols_Pink.csv", dtype={'Instrument': int})[['Symbol','Instrument','Name']]
dic1 = sym.set_index('Name').to_dict()
tokens = sym['Name'].tolist()[:]
end = datetime.now()
start = datetime.now() - timedelta(1)
end = end.strftime("%m-%d-%Y")
start = start.strftime("%m-%d-%Y")
cwd = os.chdir("C:/Desktop/Learning/Courses/zerodha/exp")
analyser = SentimentIntensityAnalyzer()
def comp_score(text):
return analyser.polarity_scores(text)["compound"]
def main():
news = {}
for ticker in tokens:
try:
time.sleep(random.randint(5,12))
googlenews=GoogleNews(start=start,end=end)
googlenews.search(ticker)
articles=googlenews.result(sort=True)
body = []
for article in articles[:min(3,len(articles))]:
body.append(article['title'])
cont = ". ".join(body)
news[dic1['Symbol'][ticker]] = cont
except:
continue
final = pd.DataFrame(news.items())
final.columns = ['Symbol','News']
final['time'] = str(t1.strftime('%H_%M'))
try:
df1 = pd.read_csv("News.csv")
except:
df1= pd.DataFrame()
df2 = final.append(df1)
df2 = df2.sort_values(by = ['time'], ascending = [True])
df2 = df2.drop_duplicates(['Symbol'], keep='last')
df2["sentiment"] = df2["News"].apply(comp_score)
df2.to_csv("News.csv", index = False)
return df2
i = 0
while True:
t1 = datetime.now()
final = main()
i = i +1
t2 = datetime.now()
print(f"Loop Number------>{i}")
#time.sleep(max(1,2*60 - (t2-t1).seconds))
time.sleep(random.randint(25*60,30*60))