【練習】python指令碼看股票即時盈虧

來源:互聯網
上載者:User

標籤:

#!/usr/bin/pythonimport urllib2import sysdef stock_data(stock_id, stock_num, purchase_price):    url = ‘http://hq.sinajs.cn/list=%s%06d‘ % ((stock_id == 1 or stock_id > 600000) and ‘sh‘ or ‘sz‘, stock_id)    page = urllib2.urlopen(url)    stock_data = page.read().split(‘"‘)[1]    name = stock_data.split(‘,‘)[0]    opening_price = float(stock_data.split(‘,‘)[1])    closing_price = float(stock_data.split(‘,‘)[2])    price = float(stock_data.split(‘,‘)[3])    high = float(stock_data.split(‘,‘)[4])    low = float(stock_data.split(‘,‘)[5])    print ‘%-10s‘ % name,    print ‘\033[%dm‘ % (price > closing_price and 31 or 32),    print ‘%10.2f‘ % price,    print ‘%10.2f‘ % (price - closing_price),    print ‘%9.2f%%‘ % ((price - closing_price) * 100 / closing_price),    print ‘\033[0m‘,    print ‘%6.2f%%‘ % ((high - closing_price) * 100 /closing_price),    print ‘%6.2f%%‘ % ((low - closing_price) * 100 / closing_price),    print ‘%6.2f%%‘ % ((high - low) * 100 / closing_price),    if stock_num > 0:        #print ‘%10d‘ % stock_num,        #print ‘%10.3f‘ % purchase_price,        print ‘\033[%dm‘ % (price > purchase_price and 31 or 32),        print ‘%10.2f‘ % ((price - purchase_price) * stock_num),        print ‘%9.2f%%‘ % ((price - purchase_price) * 100 / purchase_price),    print ‘\033[0m‘    return (price - purchase_price) * stock_numif len(sys.argv) != 2:    print "Usage : sys.argv[0] <stock_list_file>"    sys.exit(1)total = 0f = file(sys.argv[1], ‘r‘)while True:    line = f.readline()    if len(line) == 0:        break    line = line.split(‘\n‘)[0]    line = line.split(‘\r‘)[0]    line = line.split(‘#‘)[0]    if len(line) > 0:        args = line.split(‘ ‘)        if len(args) == 3:            total += stock_data(int(args[0]), int(args[1]), float(args[2]))        if len(args) == 1:            stock_data(int(args[0]), 0, 0)f.close()print "\033[1;37;4%dm%.2f\033[0m" % (total > 0 and 1 or 2, total)

用法

【練習】python指令碼看股票即時盈虧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.