Skip to content

Instantly share code, notes, and snippets.

@ms747
Created March 29, 2021 13:29
Show Gist options
  • Save ms747/d4dff175bfe55ac28b1e89cacaf38de8 to your computer and use it in GitHub Desktop.
Save ms747/d4dff175bfe55ac28b1e89cacaf38de8 to your computer and use it in GitHub Desktop.
Stocks
import threading
import time
from nsetools import Nse
from pprint import pprint
nse = Nse()
allStocks = nse.get_stock_codes()
def get_stock_info(stock):
try:
data = nse.get_quote(stock)
if data != None and "companyName" in data:
print(f"{data['symbol']} Low: {data['low52']} High: {data['high52']} Current: {data['lastPrice']}")
except:
pass
threads = []
counter = 0
for stock in allStocks:
# To avoid rate limit
# if counter == 5:
# break
threads.append(threading.Thread(target=get_stock_info, args=(stock,)))
# counter += 1
for thread in threads:
thread.start()
# To avoid rate limit
time.sleep(1)
for thread in threads:
thread.join()
import operator
class stock:
def __init__(self, name: str, low: float, high: float, price: float):
self.name: str = name
self.low: float = float(low)
self.high: float = float(high)
self.price: float = float(price)
self.predict: float = round(100.00 - ((self.price / self.high) * 100), 2)
def __repr__(self):
return f"Stock(Name={self.name}, Low={self.low}, High={self.high}, Current={self.price}, FromPeak={self.predict}%)"
stocks: [stock] = []
with open("./stocks.txt", "r") as myfile:
while True:
line = myfile.readline().strip()
if not line:
break
temp = line.split()
s = stock(temp[0], temp[2], temp[4], temp[6])
stocks.append(s)
stocks.sort(key=operator.attrgetter('predict'), reverse=True)
for stock in stocks:
print(stock)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment