python 涨跌价格幅度 大盘凑合

#! /usr/bin/python2
# coding=utf-8

import os
import threading
import urllib2
import time
from Tkinter import *
#from multiprocessing import Process
from multiprocessing import Process, Value, Array, Manager


def func():
    os._exit(0)



def creatfram(name):
    root = Tk()
    w = Label(root, text=name)
    w.pack()
    root.geometry('150x20+0+0')
    root.resizable(width=False, height=False)
    root.mainloop()



def get_price(code):
    url = 'http://hq.sinajs.cn/?list=%s' % code
    #print url
    req = urllib2.Request(url)
    #print req
    # 如果不需要设置代理,下面的set_proxy就不用调用了。由于公司网络要代理才能连接外网,所以这里有set_proxy…
    # req.set_proxy('proxy.XXX.com:911', 'http')
    content = urllib2.urlopen(req).read()
    str = content.decode('gbk', 'ignore')
    data = str.split('"')[1].split(',')
    #print data
    name = "%-6s" % data[0]
    price_current = "%-6s" % float(data[3])
    change_percent = (float(data[3]) - float(data[2])) * 100 / float(data[2])
    change_percent = "%-6s" % round(change_percent, 2)
    #print("股票名称:{0} 涨跌幅:{1} 最新价:{2}".format(name, change_percent, price_current))

    return change_percent, price_current, name




def p_stock(code_list):
    """
    A doubling function that can be used by a process
    """
    for code in code_list:
        var_code = code['code']
        price_high = code['price_high']
        flg_price_high = code['flg_price_high']
        percent_high = code['percent_high']
        flg_percent_high = code['flg_percent_high']
        price_low = code['price_low']
        flg_price_low = code['flg_price_low']
        percent_low = code['percent_low']
        flg_percent_low = code['flg_percent_low']

        percent, price, name = get_price(var_code)
        #print code, price
        info = var_code + " " + percent + " " + price
        flg_show = 0
        #print code['code'], float(price), float(percent), " alarm ",code['price_high'], code['percent_high']
        if (((float(price) > price_high and 0 != price_high) ) and (1 == flg_price_high)):
            flg_show = 1
            code['flg_price_high'] = 0
        if (((float(percent) > percent_high and 0 != percent_high)) and (1 == flg_percent_high)):
            flg_show = 1
            code['flg_percent_high'] = 0
        if (((float(price) < price_low and 0 != price_low) ) and (1 == flg_price_low)):
            flg_show = 1
            code['flg_price_low'] = 0
        if (((float(percent) < percent_low and 0 != percent_low)) and (1 == flg_percent_low)):
            flg_show = 1
            code['flg_percent_low'] = 0
        if 1 == flg_show:
            print var_code, name, float(price), float(percent), "alarm high", price_high, percent_high, " alarm low", price_low, percent_low
            timer = threading.Timer(5, func)
            timer.start()
            creatfram(info)

def stock_header(str):
    t1 = ('60', '900')
    t2 = ('000', '002', '300', '200')
    t3 = ('399001','399006')
    if str.startswith(t1):
        str2 = 'sh' + str
    elif str.startswith('000001'):
        str2 = 's_sh' + str
    elif str.startswith(t3):
        str2 = 's_sz' + str
    elif str.startswith(t2):
        str2 = 'sz' + str
    else:
        str2 = str
        print str

    return str2

def file_read(A, mgr):
    file = open("date.txt",'rU')
    text = file.read()
    lines = text.split('\n')

    for line in lines:
        # print line
        # 将每一行分隔开,提取姓名和联系方式
        i =0
        l = ['600127', '0', '0', '0', '0']
        for cell in line.split(','):
            if not cell:
                break
            l[i] = cell
            i += 1
        str = l[0]
        str2 = stock_header(str)
        #print i, "----------", str2, "----------", l[1], "----------", l[2]
        A.append(mgr.dict(code=str2, price_high=float(l[1]), percent_high=float(l[2]), price_low=float(l[3]), percent_low=float(l[4]), flg_price_high=1, flg_percent_high=1, flg_price_low=1, flg_percent_low=1))


        file.close()



if __name__ == '__main__':
    # print sys.getdefaultencoding()
    reload(sys)
    sys.setdefaultencoding('utf-8')
    # print sys.getdefaultencoding()
    mgr = Manager()
    A = Manager().list()
    file_read(A, mgr)
    #for s in A:
    #    print s

    time_interval = 60 * 10
    flg_time = 0
    old = 0
    while True:
        localtime = time.localtime(time.time())
        if 9 >= localtime.tm_hour or 15 <= localtime.tm_hour :
            print localtime.tm_hour
            break
        proc = Process(target=p_stock, args=(A, ))
        proc.start()
        proc.join()
        if 0 == flg_time:
            for code in A:
                if (0 == code['flg_price_high'] or 0 == code['flg_percent_high'] or 0 == code['flg_price_low'] or 0 == code['flg_percent_low']):
                    flg_time = 1
                    old = time.time()
                    print "---------------- ------------", old
        if ((1 == flg_time) and ((time.time() - old) > (time_interval))) :
            flg_time = 0
            print "++++++++++++++++++++++", time.time()
            for code in A:
                code['flg_price_high'] = 1
                code['flg_percent_high'] = 1

        #print A[0]['flg_price_high'], A[0]['flg_percent_high']



'''
date.txt 格式 普通股票 上涨到,涨幅,下跌到,跌幅
大盘 涨幅,0,跌幅,0
000001,1,0,-0.2,0
399001,1,0,-1,0
399006,1,0,-1,0
300127,24,5,18,-1
000639,25,5,15,-5
600718
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值