-
Notifications
You must be signed in to change notification settings - Fork 56
/
aclass.py
89 lines (83 loc) · 2.96 KB
/
aclass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
import re
from termcolor import colored
asciiPattern = re.compile(r'[A-Z0-9* ]')
# 实时数据Class
class Stock:
def __init__(self, name, todayStart, yesterdayEnd, current, highest = '0', lowest = '0'):
self.name = name
self.todayStart = float(todayStart)
self.yesterdayEnd = float(yesterdayEnd)
self.current = float(current)
self.highest = float(highest)
self.lowest = float(lowest)
self.buyPercent = 0.0 # 买卖盘五档委比
# 计算买卖委比
def calcBuyPercent(self, volumes):
if len(volumes) < 10:
return
buyVolume = 0.0
for index in range(0, 5):
buyVolume += int(volumes[index])
sellVolume = 0.0
for index in range(5, 10):
sellVolume += int(volumes[index])
if buyVolume == 0 and sellVolume == 0:
self.buyPercent = 0.0
else:
self.buyPercent = 2.0 * buyVolume / (buyVolume + sellVolume) - 1.0
# 是否停牌的判断
def isStop(self):
return self.todayStart == 0 or self.yesterdayEnd == 0
# 处理出对齐的名称(字符宽度为8个字母宽)
def formattedName(self):
asciiCount = len(asciiPattern.findall(self.name))
nameWidth = (len(self.name) - asciiCount) * 2 + asciiCount
return ' ' * (8 - nameWidth) + self.name
# 打印股票数据
def printStockData(self):
# 停牌处理
if self.isStop():
print('{}: 停牌'.format(self.formattedName()))
return
# 计算现价显示的小数点位数
if self.current < 10:
priceStr = '{:6.3f}'.format(self.current)
elif self.current < 100:
priceStr = '{:6.2f}'.format(self.current)
elif self.current < 1000:
priceStr = '{:6.1f}'.format(self.current)
else:
priceStr = '{:6.0f}'.format(self.current)
# 计算今日的涨跌幅
if self.current == self.yesterdayEnd:
increaseStr = ' 0.00%'
else:
increaseStr = '{:+6.2f}%'.format((self.current - self.yesterdayEnd) / self.yesterdayEnd * 100)
if self.current < self.yesterdayEnd:
increaseStr = colored(increaseStr, 'green')
else:
increaseStr = colored(increaseStr, 'red')
# 计算振幅及现价在今日振幅中的百分比位置
swingRangeStr = ''
swingPercentStr = ''
if self.highest > 0 and self.lowest > 0:
if self.highest == self.lowest:
swingRangeStr = ' 0.00%'
if self.current < self.yesterdayEnd:
swingPercentStr = ' 0'
elif self.current > self.yesterdayEnd:
swingPercentStr = '100'
else:
swingPercentStr = ' 50'
else:
swingRangeStr = '{:5.2f}%'.format((self.highest - self.lowest) / self.yesterdayEnd * 100)
swingPercentStr = '{:3.0f}'.format((self.current - self.lowest) / (self.highest - self.lowest) * 100)
# 根据买卖盘委比给百分比数据加标记
buyPercentStr = ''
if self.buyPercent >= 0:
buyPercentStr = colored('+' * int(self.buyPercent * 5.99), 'red')
else:
buyPercentStr = colored('-' * int(self.buyPercent * -5.99), 'green')
# 打印结果
print('{}: {} {} {} {} {}'.format(self.formattedName(), priceStr, increaseStr, swingRangeStr, swingPercentStr, buyPercentStr))