Stock monitoring and alarm system

 
 
 
 
# Stock Monitoring Alarm System-for Tencent
# The stock pool can be set to monitor multiple stocks, and it can automatically alarm according to the range of increase

import requests
from bs4 import BeautifulSoup
import win32api,win32con
import re
import time

class stock:
    def stock(self,id):
        # Infinite loop monitoring until the alarm is raised
        while True:
            message = []
            time_list= []
            # time
            now_time = time.strftime('[%Y-%m-%d %H:%M:%S]', time.localtime(time.time()))
            message.append(now_time[1:-2])
            time_list.append(now_time[1:-2])

            for i in range(len(id)):
                stock_id1 = id[i]
                url = 'http://gu.qq.com/' + stock_id1 + '/gp'
                soup=self.request(url)

                # stock name
                stock_name=soup.find('h1', class_='col-1-1').get_text()

                print('------------------------\n', now_time[1:-2])
                print('Stock:',stock_name)
                # Stock symbol
                stock_id = soup.find('h1', class_='col-1-2').get_text()
                print('Stock code:', stock_id)
                # print('----------------')

                # real-time price
                realtime_price = soup.find('span',class_='data').get_text()
                zhangdie=soup.find('div',class_='item-2').find_all('span')
                # Up and down prices
                zhangdie_price=zhangdie[0].get_text()
                # Change %
                zhangdie_percent=zhangdie[1].get_text()
                print('Current price:', realtime_price)
                print('Rise and fall price:',zhangdie_price)
                print('Rise and fall:',zhangdie_percent)


                # All information (closed yesterday, opened today, ,, amplitude)
                all=soup.find('div',class_='col-2 fr').find_all('li')
                for a in range(12):
                    a=all[a].get_text()
                    print(a)

                # Execute the message box according to the percentage change
                zhangdie_percent_re = ''.join(re.findall('(.*?)%', zhangdie_percent))

                if float(zhangdie_percent_re)>zhangdie_percent_range[0] :
                    message.append('\n%s:\n最新价:%s  涨跌额:%s  涨跌幅:%s  %s  %s  %s  %s  %s  %s\n' % (stock_name,realtime_price, zhangdie_price,zhangdie_percent,all[0].get_text(),all[1].get_text(),all[2].get_text(),all[3].get_text(),all[8].get_text(),all[9].get_text()))
                elif float(zhangdie_percent_re)<=zhangdie_percent_range[1]:
                    message.append('\n%s:\n最新价:%s  涨跌额:%s  涨跌幅:%s  %s  %s  %s  %s  %s  %s\n' % (stock_name,realtime_price, zhangdie_price,zhangdie_percent,all[0].get_text(),all[1].get_text(),all[2].get_text(),all[3].get_text(),all[8].get_text(),all[9].get_text()))

            if message !=time_list:
                print('*******************************')
                message=''.join(message)
                win32api.MessageBox(0,message,'Stock price alert system - by range'+str(zhangdie_percent_range[1])+'%'+'<x<'+str(zhangdie_percent_range[0])+'% Query: ' , win32con.MB_OK)
                break
            else:
                print('\nNo alarms meet the conditions! This round is finished!\n******************************** ***')

    def request(self,url):
        req=requests.get(url)
        req.encoding='utf-8'
        soup=BeautifulSoup(req.text,'lxml')
        return soup


stock=stock()

# Stock Pool: Guofang Group, Lanxiao Technology, Sinnet
id=['sh601086','sz300487','sz300383']

# Set the range for generating alerts: range[0]~range[1]
zhangdie_percent_range=[2,-2]

stock.stock(id)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218297&siteId=291194637