""" This module provides functionality for performing Automatic data updating tasks. """ import threading import time import pandas as pd from stls import Stocks from levels import eqt from datetime import datetime from pymongo import MongoClient from datetime import datetime, timedelta import pytz import os tz = pytz.timezone('Asia/Kolkata') mongo_url = os.environ['MongoURL'] def UpdatedCollectionName(): current_time = datetime.now(tz) collection_name = current_time.strftime('%Y-%m-%d') if current_time.time() >= datetime.strptime('15:30', '%H:%M').time(): collection_name = (current_time + timedelta(days=1)).strftime('%Y-%m-%d') return collection_name else: return collection_name import concurrent.futures import yfinance as yf def get_live_price(symbol): return yf.Ticker(symbol).history(period="1d").iloc[-1][['High','Close']].round(2) def status(row): if row['LTP'] > row['High'] or row['High_T'] > row['High']: return "Active" else: return "Pending" def get_live_prices(df): print("it's live") symbols = df['Symbol'].tolist() with concurrent.futures.ThreadPoolExecutor() as executor: prices = list(executor.map(get_live_price, symbols)) df[['High_T', 'LTP']] = prices df['Status'] = df.apply(status, axis=1) return df class DataManager: """ This is a DataManager class that demonstrates its functionality. """ def __init__(self): self.stocks = None self.equity = None self.data_thread = threading.Thread(target=self.update_data) self.data_thread.daemon = True self.data_thread.start() def update_data(self): while True: client = MongoClient(mongo_url) db = client['mydatabase'] collection_name = UpdatedCollectionName() if collection_name in db.list_collection_names(): collection = db[collection_name] cursor = collection.find({}) stocks = pd.DataFrame(list(cursor)) stocks.drop('_id', axis=1, inplace=True) self.stocks = stocks else: stocks = Stocks() collection = db[collection_name] cursor = collection.find({}) stocks = pd.DataFrame(list(cursor)) stocks.drop('_id', axis=1, inplace=True) self.stocks = stocks time.sleep(120) def get_stocks_data(self): stocks = get_live_prices(self.stocks) self.results = stocks.to_dict(orient="records") return self.results def get_equity_data(self, ticker, startdate, share_qty): self.equity = eqt(ticker, startdate, share_qty) return self.equity