File size: 2,619 Bytes
a49888f
 
 
 
 
 
671c2d8
5a0b17f
a49888f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d724757
a49888f
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
"""
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