使用baoStock+talib画一个股票筹码图
筹码类
class Chip:
index=0
open=0
close=0
max=0
min=0
avc_price=0
chouMa=0
def __init__(self,index,open,close,max,min,avc_price,chouMa):
self.index=index
self.open=open
self.close=close
self.max=max
self.min=min
self.avc_price=avc_price
self.chouMa=chouMa
def getChouMa(self):
return self.chouMa
def getOpen(self):
return self.open
def getClose(self):
return self.close
def getAvcPrice(self):
return self.avc_price
def getMax(self):
return self.max
def getMin(self):
return self.min
def getIndex(self):
return self.index
def print(self):
print("日期:"+str(self.getIndex())+" 筹码:"+str(self.getChouMa())+" 最高:"+str(self.getMax())+" 最低:"+str(self.getMin())+" 平均:"+str(self.getAvcPrice()))
2、筹码计算
# 筹码量的计算
from src.NewTun.Chip import Chip
class ChipCalculate:
isProd=False
shuanJian = 1
# 1.价格和筹码量的分布
price_vol = {}
# 2.每个交易日的筹码量
DayChouMaList = []
# 倒叙计算每日的筹码量
# 传入的数据id,open,high,low,close,volume,typePrice,trun
# 0, 1, 2, 3, 4, 5, 6, 7
def getDataByShowLine(self, data,prod):
True =
result = []
dataLength = len(data)
csdnAvcPrice = []
TtodayChouma = []
TTprice = 0
TTmax = 0
shuanjian = 0
# 保障显示120天的筹码线
# 倒叙计算每日的筹码量,当日的筹码分布和最近的120天有关系
:
self.DayChouMaList.clear()
# 倒数第k日的基本数据
Baseline = data[dataLength - 1 - k]
if Baseline[8] < 1:
continue
# 拿到id
baseIndex = int(Baseline[0])
currentPrice = float(Baseline[4])
for i in range(120):
if i < 1:
continue
line = data[dataLength - k - i]
if line[8] < 1:
continue
if line[0] == '':
index = 0
else:
index = int(line[0])
open = float(line[1])
close = float(line[4])
max = float(line[2])
min = float(line[3])
vol = 0
if line[5] == '':
vol = 0
else:
vol = float(line[5])
avc_price = float(line[6])
if line[7] == '':
0 =
if data[dataLength - i][8] == '' or data[dataLength - i][8] == None or float(
- i][8]) < 1:
continue
if i == 1:
currentChouMa = vol
chip = Chip(index, open, close, max, min, avc_price, currentChouMa)
shuanjian = (1 - float(data[dataLength - i][7]) / 100)
self.DayChouMaList.append(chip)
else:
value = 0
if data[dataLength - i][5] != '':
value = float(data[dataLength - i][5])
chouma = shuanjian * value
shuanjian = shuanjian * (1 - float(data[dataLength - i][7]) / 100)
chip = Chip(index, open, close, max, min, avc_price, chouma)
self.DayChouMaList.append(chip)
# 倒序计算完每日的筹码量,然后将筹码平均分布到当日的价格上
tmax, csdn ,maxVolprice,myUp= self.adviseChouMa2Price()
if k == 0:
TtodayChouma = todayChouma
TTmax = tmax
csdnTemp = []
csdnTemp.append(baseIndex)
csdnTemp.append(csdn)
csdnTemp.append(TtodayChouma)
csdnTemp.append("")
csdnTemp.append(TTmax)
t = 0
if currentPrice * 100 > maxVolprice:
# 当前的价格大于筹码的平均价格
t = 1
else:
t = 0
csdnTemp.append(t)
csdnTemp.append(myUp)
if csdn * 100 >= maxVolprice and t == 1:
csdnTemp.append(1)
else:
csdnTemp.append(0)
result.append(csdnTemp)
return result
# 将每日的筹码分布到价格上
def adviseChouMa2Price(self):
length = len(self.DayChouMaList)
self.price_vol.clear()
for i in range(length):
# 当日的筹码
chouma = self.DayChouMaList[i].getChouMa()
index = self.DayChouMaList[i].getIndex()
open = self.DayChouMaList[i].getOpen()
close = self.DayChouMaList[i].getClose()
max = self.DayChouMaList[i].getMax()
min = self.DayChouMaList[i].getMin()
avcPrice = self.DayChouMaList[i].getAvcPrice()
# 移入地时候,矩形和三角形的面积比为3比7,其中矩形的筹码分布占0.3,三角形占0.7
# 1.先算矩形部分的筹码迁移,将每股价格精确到分。
maoshu = round((max - min), 2) * 100
if maoshu <= 0:
continue
# 得到矩形部分筹码量
everyMao = chouma * 0.3 / maoshu
for j in range(int(maoshu)):
# 从最小价格向最大价格进行逐个筹码分布
key = j + round(min * 100)
# 如果已经包含了当前价格,那么就进行累加
if self.price_vol.__contains__(key):
volTemp = self.price_vol.get(key)
volTemp = volTemp + everyMao
volTemp =
else:
# 当前价格上的筹码量
everyMao =
# 三角形一半的筹码量
totalChouma = chouma * 0.35
# 将三角形的筹码分布到价格上
for j in range(int(maoshu / 2)):
# 从下往上
if min * 100 + j < avcPrice * 100:
key = int(j + min * 100)
# -- max
# -----
# ----------- avrageprice
# -------
# ------ min
# 看是递增还是递减的,k大于零表示递增,k小于零表示递减
k = (avcPrice - min) / ((max - min) / 2)
# 当前价格上应该分配的筹码在三角形半上所占据的比列多少
ditVol = (j * k) / (((avcPrice - min) * (max - min) / 2) / 2)
ditChouma = totalChouma * ditVol
# 下三角筹码分配
if self.price_vol.__contains__(key):
volTemp = self.price_vol[key]
volTemp = volTemp + ditChouma
volTemp =
else:
ditChouma =
# 上三角筹码分布
if self.price_vol.__contains__(int(max * 100 - j)):
volTemp = self.price_vol[int(max * 100 - j)]
volTemp = volTemp + ditChouma
* 100 - j)] = volTemp
else:
* 100 - j)] = ditChouma
choumaList = []
isFirst = 1
totalVol = 0
totalPrice = 0
tmax = 0
#筹码峰价格
maxVolprice=0
#weight价格
weightPrice=0
for i in sorted(self.price_vol):
# 这里的i就表示价格
if isFirst == 1:
isFirst = 0
cm = []
# 寻找最大的筹码量
if self.price_vol[i] > tmax:
tmax = self.price_vol[i]
maxVolprice = i
# 计算当日的各个价格上的筹码量之和
totalVol = totalVol + self.price_vol[i]
# 计算当前价格上筹码的累计大小
totalPrice = totalPrice + i * self.price_vol[i]
# 封装当前价格和价格上的筹码量
cm.append(i)
cm.append(self.price_vol[i])
choumaList.append(cm)
if totalVol == 0:
csdn = 0
return choumaList, 0, tmax, csdn,0,0
else:
csdn = round((totalPrice / totalVol) / 100, 2)
myUp=1
if self.isProd==True:
close = self.DayChouMaList[0].getClose()
downPower=0
upPower=0
for i in sorted(self.price_vol):
# 这里的i就表示价格
#战斗力对比
tP=close-round(i/100,2)
if tP<0:
downPower=downPower+(tP*self.price_vol[i])
if tP>0:
upPower=upPower+(tP*self.price_vol[i])
result=upPower+downPower
if result>=0:
myUp=1
else:
myUp=0
return choumaList, tmax, csdn,maxVolprice,myUp
3、筹码作图
# 筹码计算
import time
import numpy as np
import pandas as pd
import talib
from matplotlib import pyplot as plt
from src.NewTun.ChipCalculate import ChipCalculate
import baostock as bs
class ParseChouMa:
#画160的窗口期筹码
window = 160
def doParseByCode(self,code):
endTime = time.strftime('%Y-%m-%d', time.localtime(time.time()))
lg = bs.login()
rs = bs.query_history_k_data_plus(code,
"date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
start_date="2018-01-01", end_date=endTime,
frequency="d", adjustflag="2")
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
start = len(result) - self.window
result = result.loc[:, ['date', 'open', 'high', 'low', 'close', 'volume', 'turn']]
if code == 'sh.000001':
result['temp'] = 100
result['open'] = talib.DIV(result['open'], result['temp'])
result['high'] = talib.DIV(result['high'], result['temp'])
result['low'] = talib.DIV(result['low'], result['temp'])
result['close'] = talib.DIV(result['close'], result['temp'])
result = result[-self.window:]
chipCalculateList = []
for index, row in result.iterrows():
if index>=start:
temp = []
temp.append(index-start)
print(index-start)
temp.append(row['open'])
temp.append(row['high'])
temp.append(row['low'])
temp.append(row['close'])
temp.append(row['volume'])
temp.append(row['close'])
temp.append(row['turn'])
temp.append(1)
chipCalculateList.append(temp)
index=[]
closePrice=[]
for i in range(len(chipCalculateList)):
index.append(chipCalculateList[i][0])
close=chipCalculateList[i][4]
close=round(float(close), 2)
closePrice.append(close)
return chipCalculateList,index,closePrice
# K线的展示
def show(self,choumaList,TavcPrice,tmax,index,closePrice):
fig=plt.figure()
ax=fig.add_axes([0.1,0.35,0.6,0.6])
# 画平均成本
ax.plot(index, closePrice, color="green")
cmx=fig.add_axes([0.7,0.35,0.2,0.6])
chouMalit=np.array(choumaList)
for item in chouMalit:
item[0]=item[0]*1.0/100
cmx.barh(chouMalit[:,0],chouMalit[:,1],color="Turquoise",align="center",height=0.01)
cmx.barh(TavcPrice,tmax,color="red",height=0.02)
fig.show()
pass
4、demo演示
parseFile=ParseChouMa()
chipCalculateList,index,closePrice=parseFile.doParseByCode("sh.000001")
calcualate = ChipCalculate()
resultEnd = calcualate.getDataByShowLine(chipCalculateList, True)
parseFile.show(resultEnd[0][2],resultEnd[0][1],resultEnd[0][4],index,closePrice)
5、与通达信、腾讯自选股筹码图的对比
上证指数的筹码分布:
通达信
对比发现跟通达信的筹码差距还挺大。我们按真实的价格去重新算一下。发现还是那样,所以到底哪个比较靠谱呐?我们再看看其他的股票。
金证股份
看看通达信的筹码
手机app
发现差别还挺大的,我们再对比双汇发展。
手机app
我们再对比一下冀中能源
通达信
手机app
西麦食品
通达信
手机app
6、总结:
评论