Rename pycatchs/st.py to pycatchs/stdb.py
Browse files- pycatchs/st.py +0 -0
- pycatchs/stdb.py +87 -0
pycatchs/st.py
DELETED
File without changes
|
pycatchs/stdb.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This module provides functionality for performing Automatic data updating tasks.
|
3 |
+
"""
|
4 |
+
import threading
|
5 |
+
import time
|
6 |
+
import pandas as pd
|
7 |
+
from stocklist import Stocks
|
8 |
+
from utils import eqt
|
9 |
+
from datetime import datetime
|
10 |
+
from pymongo import MongoClient
|
11 |
+
from datetime import datetime, timedelta
|
12 |
+
import pytz
|
13 |
+
import os
|
14 |
+
|
15 |
+
tz = pytz.timezone('Asia/Kolkata')
|
16 |
+
df_logo = pd.read_csv('https://raw.githubusercontent.com/jarvisx17/nifty500/main/Nifty500.csv')[['Symbol', 'logo']]
|
17 |
+
mongo_url = os.environ['MongoURL']
|
18 |
+
|
19 |
+
def UpdatedCollectionName():
|
20 |
+
current_time = datetime.now(tz)
|
21 |
+
collection_name = current_time.strftime('%Y-%m-%d')
|
22 |
+
if current_time.time() >= datetime.strptime('15:30', '%H:%M').time():
|
23 |
+
collection_name = (current_time + timedelta(days=1)).strftime('%Y-%m-%d')
|
24 |
+
return collection_name
|
25 |
+
else:
|
26 |
+
return collection_name
|
27 |
+
|
28 |
+
import concurrent.futures
|
29 |
+
import yfinance as yf
|
30 |
+
|
31 |
+
def get_live_price(symbol):
|
32 |
+
return yf.Ticker(symbol).history(period="1d").iloc[-1][['High','Close']].round(2)
|
33 |
+
|
34 |
+
def status(row):
|
35 |
+
if row['LTP'] > row['High'] or row['High_T'] > row['High']:
|
36 |
+
return "Active"
|
37 |
+
else:
|
38 |
+
return "Pending"
|
39 |
+
|
40 |
+
def get_live_prices(df):
|
41 |
+
print("it's live")
|
42 |
+
symbols = df['Symbol'].tolist()
|
43 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
44 |
+
prices = list(executor.map(get_live_price, symbols))
|
45 |
+
df[['High_T', 'LTP']] = prices
|
46 |
+
df['Status'] = df.apply(status, axis=1)
|
47 |
+
return df
|
48 |
+
|
49 |
+
class DataManager:
|
50 |
+
"""
|
51 |
+
This is a DataManager class that demonstrates its functionality.
|
52 |
+
"""
|
53 |
+
def __init__(self):
|
54 |
+
self.stocks = None
|
55 |
+
self.equity = None
|
56 |
+
self.data_thread = threading.Thread(target=self.update_data)
|
57 |
+
self.data_thread.daemon = True
|
58 |
+
self.data_thread.start()
|
59 |
+
|
60 |
+
def update_data(self):
|
61 |
+
while True:
|
62 |
+
client = MongoClient(mongo_url)
|
63 |
+
db = client['mydatabase']
|
64 |
+
collection_name = UpdatedCollectionName()
|
65 |
+
if collection_name in db.list_collection_names():
|
66 |
+
collection = db[collection_name]
|
67 |
+
cursor = collection.find({})
|
68 |
+
stocks = pd.DataFrame(list(cursor))
|
69 |
+
stocks.drop('_id', axis=1, inplace=True)
|
70 |
+
self.stocks = stocks
|
71 |
+
else:
|
72 |
+
stocks = Stocks()
|
73 |
+
collection = db[collection_name]
|
74 |
+
cursor = collection.find({})
|
75 |
+
stocks = pd.DataFrame(list(cursor))
|
76 |
+
stocks.drop('_id', axis=1, inplace=True)
|
77 |
+
self.stocks = stocks
|
78 |
+
time.sleep(120)
|
79 |
+
|
80 |
+
def get_stocks_data(self):
|
81 |
+
stocks = get_live_prices(self.stocks)
|
82 |
+
self.results = stocks.to_dict(orient="records")
|
83 |
+
return self.results
|
84 |
+
|
85 |
+
def get_equity_data(self, ticker, startdate, share_qty):
|
86 |
+
self.equity = eqt(ticker, startdate, share_qty)
|
87 |
+
return self.equity
|