import math from util.tushare_dec import * from PyQt5.Qt import * import pyqtgraph as pg import math from util.tushare_dec import * #计算等比例线 def calGridlines(hightN,lowN,lines=7,bili=[0,0,0,0,0,0,0,0,0,0,0,]): #7条水平线位置 y = math.pow((lowN/hightN), 1/(lines - 1)) r = [] for i in range(lines): r.append(round(hightN*y**i, 3)) delta = r[0]*(1-y)*0.1 for i in range(lines): r[i] = r[i]+delta*bili[i] return r # #计算价格区间有哪些等比例线 # def calPriceReachedLines(gridlines=None,high=None,low=None): # if gridlines is None or high is None or low is None: # return None # highline, lowline = gridlines[0], gridlines[-1:][0] # y = math.pow((lowline / highline), 1 / (len(gridlines) - 1)) # x1 = math.log(high/highline)/math.log(y) # x2 = math.log(low/highline)/math.log(y) # # print(x1, x2) # x1 = math.ceil(x1) # x2 = math.floor(x2) # # return (x1, x2) # r = [] # x = x1 # while x <= x2: # r.append(x) # x += 1 # return r #区分哪些等比例线对应的买卖操作 def calsplitBSLines(gridlines,position,positiondict,open,high, low, profitLEV=1 ,Amountpeace=10000): r, Blist, Slist = {}, [], [] # profitLEV = 1 #N格利润 info = "" profit = 0 #持仓对应的股价 posprice = gridlines[position+1] # #计算open,high, low 相对lines的位置 # highline, lowline = gridlines[0], gridlines[-1:][0] # # print("H L O:", high, low, open," UPer Lower:", highline, lowline ,"pos:", position, "posprice:", posprice) # y = math.pow((lowline / highline), 1 / (len(gridlines) - 1)) # relativehigh= math.log(high / highline) / math.log(y) # relativelow = math.log(low / highline) / math.log(y) # relativehigh = math.ceil(relativehigh) # relativelow = math.floor(relativelow) #不规律 relativehigh2,relativelow2 = len(gridlines)-1, 0 for index, v in enumerate(gridlines): if high >= v: relativehigh2 = index break for index, v in enumerate(gridlines): if low <= v: relativelow2 = index # print("22222222222 {}={},{}={}".format(relativehigh2,relativehigh,relativelow2,relativelow,)) relativehigh, relativelow = relativehigh2,relativelow2 buyFirst = True if open < posprice else False for loop in range(2): if buyFirst: buyFirst = False # buy for i in range(position + 1, relativelow): # print("buy:", i, gridlines[i+1]) if position >= len(gridlines)-3: break position += 1 Blist.append(gridlines[i+1]) positiondict[i]=(gridlines[i+1],math.floor(Amountpeace/gridlines[i+1])) r["B"] = Blist else: buyFirst = True # sell for i in range(relativehigh + profitLEV -1 , position + 1): if i < 1: continue # print("sell:", i, gridlines[i+1], "->", gridlines[i]) # info += "sell:{} {}->{} ".format( i, gridlines[i+1], gridlines[i]) vt = gridlines[i-profitLEV+1] info += "S:{} ({}->{})*{} ".format(i, positiondict[i][0], vt, positiondict[i][1]) profit += (vt - positiondict[i][0])*positiondict[i][1] position -= 1 Slist.append(gridlines[i+1]) positiondict[i] = (0, 0) r["S"] = Slist r["I"] = info print("r:", r ) return r,position,positiondict,profit def calsplitBSLinesSimple(gridlines,position,open,high, low, profitLEV=1): ''' gridlines操作线 仓位 --- 0 --- 1 0 ... ... --- position+1 position ... ... --- len(gridlines)-1 --- len(gridlines) (不持仓) ''' r, Blist, Slist = {}, [], [] # profitLEV = 1 #N格利润 info = "" profit = 0 #持仓对应的股价 posprice = gridlines[position+1] #计算open,high, low 相对lines的位置 highline, lowline = gridlines[0], gridlines[-1:][0] y = math.pow((lowline / highline), 1 / (len(gridlines) - 1)) relativehigh= math.log(high / highline) / math.log(y) relativelow = math.log(low / highline) / math.log(y) relativehigh = math.ceil(relativehigh) relativelow = math.floor(relativelow) buyFirst = True if open < posprice else False for loop in range(2): if buyFirst: buyFirst = False # buy for i in range(position + 1, relativelow): if position >= len(gridlines)-2: break position += 1 Blist.append(gridlines[i+1]) positiondict[i]=gridlines[i+1] r["B"] = Blist else: buyFirst = True # sell for i in range(relativehigh + profitLEV -1 , position + 1): if i < 1: continue Slist.append(gridlines[i+1]) r["S"] = Slist return r #账户 class account(): positiondict = {} def __init__(self, position=0,Amountpeace=10000,lines=7): self.position = position # 分四份 self.Amountpeace=Amountpeace self.lines = lines def cal(self, high, low, open, hightN, lowN, profitLEV=1,Bili=[0,0,0,0,0,0,0,]): # print(high, low, open) gridlines = calGridlines(hightN, lowN, self.lines,bili=bili) print(gridlines) #调整gridlines最小限制 y = gridlines[0]/gridlines[1]-1 maxzf,minzf = 0.025,0.012 if y < minzf or y>maxzf: if y<minzf: y = minzf - y else: y = maxzf - y y1 = 1+y y2 = 1-y ll = [y1**3,y1**2,y1,1,y2,y2**2,y2**3] for i, v in enumerate(ll): gridlines[i] = gridlines[i]*v print("new GridLines:",gridlines,gridlines[0]/gridlines[1],y,y1,y2) # 计算在[h l]区间的Gridlines(等比例线) # p = calPriceReachedLines(gridlines, high, low) # print("lines in range :", p, "pos:",self.position) #需要加\减仓的等比例线 v, self.position,self.positiondict,profit = calsplitBSLines( gridlines, self.position,self.positiondict, open, high, low, profitLEV=profitLEV,Amountpeace=self.Amountpeace) print(self.positiondict,"profit:",profit) return self.positiondict,profit,gridlines pass # ac = account() vLine = None hLine = None def mouseMoved(evt): # print("mouseMoved") # print(evt) pos = evt[0] vLine is not None and vLine.setPos(pos.x()) hLine is not None and hLine.setPos(pos.y()) if __name__ == '__main__': symbol = ["603605","000156","688036","002242","300274", "601601","000333","601216","601318","600660", "000596","601857","000651","588000","600519", "000002","600660","000001"] clear = False symbolindex = 11 # 1 5 11 17 wast lines = 7 # 5 7:best 9 11 Amountpeace = 10000 profitLEV = 2 cycle,bei = 20, 0 #7, 0.0 best 抗跌 # cycle,bei = 2, 6 #2, 6 历史涨幅最大 bili = [3, 2, 1, 0, -1, -2, -2, 0, 0, 0, 0 ] # 61.806516298754296 % for i,v in enumerate(bili): bili[i] = v*bei df = get_symbol_close_volum(symbol[symbolindex], "2019-01-18", "2021-03-01", clear=clear) df['hightN'] = df['high'].rolling(window=cycle, center=False).max().shift(1) df['lowN'] = df['low'].rolling(window=cycle, center=False).min().shift(1) df['v10'] = df['volume'].rolling(window=7, center=False).mean() df['ma30'] = df['close'].rolling(window=30, center=False).mean() df['ma30R2'] = df['ma30'].shift(1) xl = df['ma30'] / df['ma30R2'] - 1 df["zxxs"] = (xl* 22 + 1) * df['ma30'] df["zxxs"] = df["zxxs"].shift(1) df['zxxs'] = df['zxxs'].rolling(window=10, center=False).mean() df['v20'] = df['volume'].rolling(window=20, center=False).mean() df['std1'] = df['volume'].rolling(window=10, center=False).std() df = df.dropna() hightN, lowN = 0, 0 ac = account(position=0,Amountpeace=Amountpeace,lines=lines) #statics close = df.iat[0, 6] profitsum,tradecnt,close2 = 0,0,0 positiondict = None #draw bars,retu,closes,zxxs = [],[],[],[] l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11 = [],[],[],[],[],[],[],[],[],[],[] ll = [l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11] for i, v in df.iterrows(): hightN, lowN = v['hightN'], v['lowN'], high, low, open = v['high'], v['low'], v['open'], print("H L O HN LN:",high, low, open, hightN, lowN ,v['date']) # hightN,lowN=v['zxxs']+(hightN-lowN)/4,v['zxxs']-(hightN-lowN)*3/4 hightN,lowN=v['zxxs']*1.05,v['zxxs']*0.9 close2 = open closes.append(close2) positiondict,profit,gridlines = ac.cal(high, low, open, hightN, lowN, profitLEV=profitLEV,Bili=bili) for i, v in enumerate(gridlines): ll[i].append(v) bars.append(profit) if profit!=0: tradecnt += 1 profitsum += profit retu.append(profitsum/40000) print("=" * 30) for k in positiondict.keys(): if positiondict[k][0]!=0: profitsum += (close2-positiondict[k][0])*positiondict[k][1] print("clear:{} ({}-{})*{}={}".format(k,close2,positiondict[k][0],positiondict[k][1],(close2-positiondict[k][0])*positiondict[k][1])) print("profit:{} 涨幅:{}".format(profitsum,profitsum*100/(lines-3)/Amountpeace), "%\n成交数{} 天数{} 比例{}:".format(tradecnt,len(df),tradecnt/len(df),)) #draw app = QApplication([]) pw = pg.GraphicsLayoutWidget() # l = [bars,retu,closes,df["volume"]] l = [bars,retu,closes,] p2 = None for i, v in enumerate(l): if i==0: bar = pg.BarGraphItem(x=range(len(v)),height=v,width=0.5) p1 = pw.addPlot(row=i, col=0, ) p1.addItem(bar) elif i==3: bar = pg.BarGraphItem(x=range(len(v)), height=v, width=0.5) p4 = pw.addPlot(row=i, col=0, ) p4.addItem(bar) p4.plot(df["v10"].tolist(),pen='r') p4.plot(df["v20"].tolist()) p4.plot(df["std1"].tolist(),pen='y') else: p2 = pw.addPlot(row=i, col=0, ) p2.plot(v) p2.setXLink(p1) colors=['y','c','g','m','r','l','d','s','w','b','k',] ll.append(df['ma30'].tolist()) ll.append(df["zxxs"].tolist()) for i, v in enumerate(ll): pen = 'y' if i>9: pen='y' else: pen = colors[i] p2.plot(v,pen=pen) vLine = pg.InfiniteLine(angle=90, movable=False, pen=pg.mkPen(width=1, color=QColor(255, 255, 255))) hLine = pg.InfiniteLine(angle=0, movable=False, pen=pg.mkPen(width=1, color=QColor(255, 255, 255))) pw.scene().addItem(vLine) pw.scene().addItem(hLine) move_slot = pg.SignalProxy(pw.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved) pw.show() app.exec_()