import csv

import time
import gspread
import urllib3
import requests
from bs4 import BeautifulSoup
from telegram import Bot
import socket
import time
import datetime

bot_token = '6240833652:AAFUVI7UmZfYHK_UR3_tWYeS8hjsqYvEQ-U'
chat_id = 1234702524
bot = Bot(token=bot_token)


# Open the CSV file
sa = gspread.service_account(filename="/Users/jitendersingh/Documents/sheetsapi.json")
sh = sa.open('only74kotc')
sheet = sh.worksheet('stocks')

headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}

def check_internet_connection():
while True:
current_time = datetime.datetime.now().time()
current_day = datetime.datetime.now().weekday()
if current_day < 5 and current_time >= datetime.time(7, 30) and current_time <= datetime.time(17, 0):
bot.send_message(chat_id=chat_id, text="Share Market Bot running")
try:
# Check if internet connection is available
socket.create_connection(("www.google.com", 80))
print("Internet connection is working")
#Use below codes to find data.
for i in range(2, sheet.row_count + 1):
try:
# 1st
url = sheet.cell(i, 7).value
stockname = sheet.cell(i, 1).value
invested_amount = sheet.cell(i, 5).value
source = requests.get(url, allow_redirects=False, headers=headers)
soup = BeautifulSoup(source.text, 'html.parser')
price_element = soup.find('div', class_="market-bse").find_all('p')[1]
price = price_element.contents[0].strip()
print(price)

#Tracker before buy, checking if the share price is going down and down. if going up than last track then 10 msg alert.
def lastcode():
shareqt = sheet.cell(i, 2).value
modifyprice = sheet.cell(i, 4).value
currentprice = float(price) * int(shareqt)
sheet.update_cell(i, 3, currentprice)
percentage = 0.05 # 5% expressed as a decimal
threshold_down = int(modifyprice) * (1 - percentage) # calculate the threshold for price decrease
threshold_up = int(modifyprice) * (1 + percentage) # calculate the threshold for price increase

if currentprice < threshold_down:
sheet.update_cell(i, 4, currentprice)
alertmsg1 = "Invested " + str(invested_amount) + " in " + str(stockname) + ".\nModified was: " + str(modifyprice) + "\nCurrent: " + str(currentprice) + ""
bot.send_message(chat_id=chat_id, text=alertmsg1)

elif currentprice > threshold_up:
sheet.update_cell(i, 4, currentprice)
alertmsg2 = "SELL\nInvested " + str(invested_amount) + " in " + str(stockname) + ".\nModified: " + str(modifyprice) + "\nCurrent: " + str(currentprice) + ""
for _ in range(10):
bot.send_message(chat_id=chat_id, text=alertmsg2)

lastcode()
except:
bot.send_message(chat_id=chat_id, text="Error in 1st Source of stock finder")
try:
# 2nd
url = sheet.cell(i, 8).value
source = requests.get(url, allow_redirects=False, headers=headers)
soup = BeautifulSoup(source.text, 'html.parser')
price = soup.find('div', class_="midData").find('span', class_="ltp_block").text
print(price)

lastcode()


except:
bot.send_message(chat_id=chat_id, text="Error in 2nd Source of stock finder")
try:
# 3rd
url = sheet.cell(i, 9).value
source = requests.get(url, allow_redirects=False, headers=headers)
soup = BeautifulSoup(source.text, 'html.parser')
price = soup.find('h2', class_="equity-price").text
print(price)
lastcode()

except:
bot.send_message(chat_id=chat_id, text="Error in 3rd Source of stock finder")
try:
# 4th
url = sheet.cell(i, 10).value
source = requests.get(url, allow_redirects=False, headers=headers)
data = {"exchangeSelected": "nse"}
response = requests.post(url, headers=headers, data=data)
soup = BeautifulSoup(source.text, 'html.parser')
price = soup.find_all('h2', class_="share_amt")[0].text
print(price)
lastcode()

except:
bot.send_message(chat_id=chat_id, text="Error in All Source of stock finder")
# Play beep sound

break # Connection successful, exit the loop
except OSError:
print("Internet connection is not working")
# Play beep sound

time.sleep(300) # Wait for 5 minutes then again check

check_internet_connection()



#Tracker after buy, checking if the share price is going up and up. if going down than last track then 10 msg alert.

# percentage = 0.05 # 5% expressed as a decimal
# threshold_up = int(modifyprice) * (1 + percentage) # calculate the threshold for price increase
# threshold_down = int(modifyprice) * (1 - percentage) # calculate the threshold for price decrease

# if currentprice < threshold_down:
# sheet.update_cell(i, 4, currentprice)
# alertmsg1 = "SELL\nInvested " + str(invested_amount) + " in " + str(stockname) + ".\nModified: " + str(modifyprice) + "\nCurrent: " + str(currentprice) + ""
# for _ in range(10):
# bot.send_message(chat_id=chat_id, text=alertmsg1)

# elif currentprice > threshold_up:
# sheet.update_cell(i, 4, currentprice)
# alertmsg2 = "Invested " + str(invested_amount) + " in " + str(stockname) + ".\nModified: " + str(modifyprice) + "\nCurrent: " + str(currentprice) + ""
# bot.send_message(chat_id=chat_id, text=alertmsg2)

0 comments:

Post a Comment

 
Top