stock

version: 1
disable_existing_loggers: False
formatters:
        simple:
            format: "%(asctime)s - %(name)s - %(levelname)s - %(threadName)s - %(lineno)d - %(message)s"
handlers:
    console:
            class: logging.StreamHandler
            level: DEBUG
            formatter: simple
            stream: ext://sys.stdout
    info_file_handler:
            class: logging.handlers.RotatingFileHandler
            level: INFO
            formatter: simple
            filename: info.log
            maxBytes: 10485760
            backupCount: 20
            encoding: utf8
    error_file_handler:
            class: logging.handlers.RotatingFileHandler
            level: ERROR
            formatter: simple
            filename: errors.log
            maxBytes: 10485760
            backupCount: 20
            encoding: utf8
loggers:
    my_module:
            level: ERROR
            handlers: [info_file_handler]
            propagate: no
root:
    level: INFO
    handlers: [console,info_file_handler,error_file_handler]
log_config.yaml
 
# encoding: utf-8
import requests
import logging
import logging.config
import random
import os
import yaml
import time
import threading

class Observer(object):
    open_price_last_1 = ''
    close_price_last_1 = ''
    highest_price_last_1 = ''
    minimum_price_last_1 = ''
    ding_fen_xing = []
    di_fen_xing = []
    not_ding_di = []
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}

    def __init__(self):
        self.stop_flag = False

    def get_stock_data(self,stock_code,scale):
        """
        获取K 线数据
        :param stock_code: 代码
        :param scale: 级别
        :return: k线数据
        """
        #url = 'http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sh600030,m30,,320&_var=m30_today&r=0.1700474168906776'
        url = 'http://ifzq.gtimg.cn/appstock/app/kline/mkline'
        params = {
            'param':  '{},m{},,320'.format(stock_code,scale),
            '_var': 'm{}_today'.format(scale),
            'r': '0.1700474{}'.format("".join(random.choice("0123456789") for i in range(10)))
        }
        logging.info('url:%s \t params:%s',url,params)
        res = requests.get(url,params=params,headers=self.headers)
        logging.info('response:%s:',res.content.decode('unicode_escape').rstrip())

    def get_stock_code(self,a_type):
        """
        获取两市股票代码
        :param a_type: sh or sz
        :return: stock_code
        """
        #url = 'http://stock.gtimg.cn/data/index.php?appn=rank&t=rankash/chr&p=1&o=0&l=40&v=list_data'
        url = 'http://stock.gtimg.cn/data/index.php'
        params = {
            'appn': 'rank',
            't': 'ranka{}/chr'.format(a_type),
            'p': 1,
            'o': 0,
            'l': 3000,
            'v': 'list_data'
        }
        logging.info('url:%s \t params:%s', url, params)
        res = requests.get(url, params=params, headers=self.headers)
        logging.info('response:%s:',res.content.decode('unicode_escape').rstrip())

    def get_plate_data(self,stock_code):
        """
        获取盘中数据,如果出现大于80万的买单弹框提示
        :param stock_code:
        :return:
        """
        #url = 'http://web.sqt.gtimg.cn/q=sh601208?r=0.9884275211413494'
        url = 'http://web.sqt.gtimg.cn'
        params = {
            'q': stock_code,
             'r': '0.1700474{}'.format("".join(random.choice("0123456789") for i in range(10)))
        }
        logging.info('url:%s \t params:%s', url, params)
        res = requests.get(url, params=params, headers=self.headers)
        res_text = res.content.decode('GBK')
        logging.info('response:%s:',res_text.rstrip())
        plate_data = res_text.split('~')
        stock_name_this = plate_data[1]
        stock_code_this = plate_data[2]
        for list_data in plate_data:
            if '|' in list_data:
                plate_date_strike  = list_data.split('|')
                #logging.info(plate_date_strike )
                buy_list = []
                for plate_date_strike_detail in plate_date_strike:
                    if 'B' in plate_date_strike_detail :
                        buy_list.append(int(plate_date_strike_detail.split('/')[4]))
                logging.info(buy_list)
                if buy_list and max(buy_list) > 800000:
                    logging.info('%s %s 出现了80万以上的买单' % (stock_name_this, stock_code_this))
                    self.alert_msg('%s %s 出现了80万以上的买单' % (stock_name_this, stock_code_this))
                    self.stop_flag = True
                else:
                    logging.info('%s %s 没有出现80万以上的买单' % (stock_name_this, stock_code_this))


    def alert_msg(self,msg):
        commond = 'msg * %s'%msg
        os.system(commond)

class MonitorThread(threading.Thread):
    def __init__(self,stock_code):
        threading.Thread.__init__(self)
        self.name = stock_code
        self.stock_code = stock_code

    def run(self):
        logging.info(self.name + '监控开始...')
        o = Observer()
        while 1:
            o.get_plate_data(self.stock_code)
            if o.stop_flag:
                break
            time.sleep(5)
        logging.info(self.name + '监控结束!')

if __name__ == '__main__':
    path = 'logging.yaml'
    value = os.getenv('LOG_CFG', None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, "r") as f:
            config = yaml.load(f)
            logging.config.dictConfig(config)
    else:
        print('log config file not found!')

    monitor_list = ['sz002464','sh603008']
    for stock_code in monitor_list:
        thread_a = MonitorThread(stock_code)
        thread_a.start()
View Code

 

# encoding: utf-8
import requests
import logging
import logging.config
import random
import os
import yaml
import time
import threading

class Observer(object):
    open_price_last_1 = ''
    close_price_last_1 = ''
    highest_price_last_1 = ''
    minimum_price_last_1 = ''
    ding_fen_xing = []
    di_fen_xing = []
    not_ding_di = []
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}

    def __init__(self):
        self.stop_flag = False

    def get_stock_data(self,stock_code,scale):
        """
        获取K 线数据
        :param stock_code: 代码
        :param scale: 级别
        :return: k线数据
        """
        #url = 'http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sh600030,m30,,320&_var=m30_today&r=0.1700474168906776'
        url = 'http://ifzq.gtimg.cn/appstock/app/kline/mkline'
        params = {
            'param':  '{},m{},,320'.format(stock_code,scale),
            '_var': 'm{}_today'.format(scale),
            'r': '0.1700474{}'.format("".join(random.choice("0123456789") for i in range(10)))
        }
        logging.info('url:%s \t params:%s',url,params)
        res = requests.get(url,params=params,headers=self.headers)
        res_str = res.content.decode('unicode_escape').rstrip()
        #logging.info('response:%s:',res_str)
        res_dict = eval(res_str[res_str.index("={")+1:res_str.index("}}}")+3])
        scale_data = res_dict['data'][stock_code]['m'+scale]
        #logging.info(scale_data)
        return scale_data


    def get_stock_code(self,a_type):
        """
        获取两市股票代码
        :param a_type: sh or sz
        :return: stock_code
        """
        #url = 'http://stock.gtimg.cn/data/index.php?appn=rank&t=rankash/chr&p=1&o=0&l=40&v=list_data'
        url = 'http://stock.gtimg.cn/data/index.php'
        params = {
            'appn': 'rank',
            't': 'ranka{}/chr'.format(a_type),
            'p': 1,
            'o': 0,
            'l': 3000,
            'v': 'list_data'
        }
        logging.info('url:%s \t params:%s', url, params)
        res = requests.get(url, params=params, headers=self.headers)
        res_str = res.content.decode('unicode_escape').rstrip()
        #logging.info('response:%s:',res_str)
        res_str_list = res_str[res_str.index("data:'") + 6:res_str.index("'}")].split(',')
        logging.info(res_str_list)
        return res_str_list


    def get_plate_data(self,stock_code):
        """
        获取盘中数据,如果出现大于80万的买单弹框提示
        :param stock_code:
        :return:
        """
        #url = 'http://web.sqt.gtimg.cn/q=sh601208?r=0.9884275211413494'
        url = 'http://web.sqt.gtimg.cn'
        params = {
            'q': stock_code,
             'r': '0.1700474{}'.format("".join(random.choice("0123456789") for i in range(10)))
        }
        logging.info('url:%s \t params:%s', url, params)
        res = requests.get(url, params=params, headers=self.headers)
        res_text = res.content.decode('GBK')
        logging.info('response:%s:',res_text.rstrip())
        plate_data = res_text.split('~')
        stock_name_this = plate_data[1]
        stock_code_this = plate_data[2]
        for list_data in plate_data:
            if '|' in list_data:
                plate_date_strike  = list_data.split('|')
                #logging.info(plate_date_strike )
                buy_list = []
                for plate_date_strike_detail in plate_date_strike:
                    if 'B' in plate_date_strike_detail :
                        buy_list.append(int(plate_date_strike_detail.split('/')[4]))
                logging.info(buy_list)
                if buy_list and max(buy_list) > 800000:
                    logging.info('%s %s 出现了80万以上的买单' % (stock_name_this, stock_code_this))
                    self.alert_msg('%s %s 出现了80万以上的买单' % (stock_name_this, stock_code_this))
                    self.stop_flag = True
                else:
                    logging.info('%s %s 没有出现80万以上的买单' % (stock_name_this, stock_code_this))


    def alert_msg(self,msg):
        commond = 'msg * %s'%msg
        os.system(commond)

class MonitorThread(threading.Thread):
    def __init__(self,stock_code):
        threading.Thread.__init__(self)
        self.name = stock_code
        self.stock_code = stock_code

    def run(self):
        logging.info(self.name + '监控开始...')
        o = Observer()
        while 1:
            o.get_plate_data(self.stock_code)
            if o.stop_flag:
                break
            time.sleep(5)
        logging.info(self.name + '监控结束!')

if __name__ == '__main__':
    path = 'logging.yaml'
    value = os.getenv('LOG_CFG', None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, "r") as f:
            config = yaml.load(f)
            logging.config.dictConfig(config)
    else:
        print('log config file not found!')

    # monitor_list = ['sz002464','sh603008']
    # for stock_code in monitor_list:
    #     thread_a = MonitorThread(stock_code)
    #     thread_a.start()
    o = Observer()
    code_list_sh = o.get_stock_code('sh')
    code_list_sz = o.get_stock_code('sz')
    code_list = code_list_sh + code_list_sz
    code_list_keguanzhu = []
    for stock_code in code_list:
        m30_data = o.get_stock_data(stock_code, '30')
        #倒数第二个k线最低点是最后三根k线最低点中最低且最后一根k线收盘价高于中间一个k线的最高价
        if min(float(m30_data[-1][4]), float(m30_data[-2][4]), float(m30_data[-3][4])) == float(m30_data[-2][4]) and float(m30_data[-1][2]) > float(m30_data[-2][3]):
            logging.info('%s可以关注'%stock_code)
            code_list_keguanzhu.append(stock_code)
    logging.info('可关注的%d个票:%s'%(len(code_list_keguanzhu),str(code_list_keguanzhu)))
选股

 

转载于:https://www.cnblogs.com/xiaodebing/p/9857303.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值