def modelCombine(models, f, conf=basicConf, arg=None): bank = bankManage.Bank() curve = loss.Loss(conf.StartValue) sizes = [] for model, weit in models: sizes.extend(model.conf.getSizes()) phase = wave.Phase(sizes) tt = 0 vs = [] N = 5000 restBank = 0 #余量窗口 bw1 = 0.6 bw2 = 0.4 lastLen = 0 for line in f: vs.append(line) if len(vs) >= N: phase1 = copy.deepcopy(phase) curve1 = loss.Loss(curve.now) bank1 = copy.deepcopy(bank) tt1 = tt (phase, curve, bank, tt, restBank) = modelCombinePhase(models, vs[lastLen:], phase, curve, bank, tt, False, restBank, conf, arg) #models=selectModelCombine(models,vs,phase1,curve1,bank1,tt1,restBank,conf,arg,N) #print "model",models[0][1],models[1][1] #del vs[0:30000] vs = [] lastLen = len(vs) if len(vs) > 0: (phase, curve, bank, tt, restBank) = modelCombinePhase(models, vs[lastLen:], phase, curve, bank, tt, True, restBank, conf, arg) print curve
def duralTrust(f, conf=basicConf, arg=None): bank = bankManage.Bank() detail = (arg == "detail") start = conf.StartValue curve = loss.Loss(start) tt = 0 phase = wave.Phase(conf.getSizes()) blist = Stack.Queue(conf.MidSize / conf.TSize + 1, [0, 0, 0, 0]) restBank = [0, 0] #余量窗口 minFlow = curve.now flow = curve.now for line in f: tt += 1 lastD, nowD = data.readLine(line, conf) if nowD == None: continue v0 = nowD.lastP v1 = nowD.p phase.addV(v0)
def m2(f, conf1=MultiConf(1), conf2=MultiConf(2), arg=None): detail = (arg == "detail") bank1 = bankManage.Bank() bank2 = bankManage.Bank() start = conf1.StartValue curve = loss.Loss(start) tt = 0 phase1 = wave.Phase(conf1.getSizes()) blist1 = Stack.Queue(conf1.MidSize / conf1.TSize + 1, [0, 0, 0, 0]) phase2 = wave.Phase(conf2.getSizes()) blist2 = Stack.Queue(conf2.MidSize / conf2.TSize + 1, [0, 0, 0, 0]) restBank1 = [0, 0] #余量窗口 restBank2 = [0, 0] minFlow = curve.now flow = curve.now vm1 = 0 vm2 = 0 lastD1 = data.Data() lastD2 = data.Data() nowD1 = data.Data() nowD2 = data.Data() for line in f: tt += 1 if arg == "Curve": print "%s%.4f" % (conf1.CurveFix, curve.now / start * 30.0) if tt < 3: #忽略第一行 continue lastD1, nowD1 = data.readLine(line, conf1, nowD1, lastD1) lastD2, nowD2 = data.readLine(line, conf2, nowD2, lastD2) if nowD1 == None or nowD2 == None: continue v01 = nowD1.lastP v02 = nowD2.lastP vm1 = v01 * (1.0 - conf1.SlideRate) + conf1.SlideRate * nowD1.p vm2 = v02 * (1.0 - conf2.SlideRate) + conf2.SlideRate * nowD2.p phase1.add(v01) phase2.add(v02) curve.addTime() if nowD1.settle: e, f = bank1.destroy(vm1) curve.addE(e) flow += f restBank1 = [0, vm1] blist1 = Stack.Queue(conf1.MidSize / conf1.TSize + 1, [0, 0, 0, 0]) if nowD2.settle: e, f = bank2.destroy(vm2) curve.addE(e) flow += f restBank2 = [0, vm2] blist2 = Stack.Queue(conf2.MidSize / conf2.TSize + 1, [0, 0, 0, 0]) continue tn1 = clear(tt, blist1, v01, vm1, conf1, curve, phase1, detail) tn2 = clear(tt, blist2, v02, vm2, conf2, curve, phase2, detail) tn1 -= restBank1[0] #与整数操作的差额操作 tn2 -= restBank2[0] if tt % conf1.TSize == 7 % conf1.TSize: d1 = direction(phase1, conf1, conf1.Size, detail) d2 = direction(phase2, conf2, conf2.Size, detail) br1 = conf1.BankRate br2 = conf2.BankRate #if abs(d2) >= abs(d1): # br2+=br1 # br1=0 if abs(d1) > 0: n1 = (d1 * curve.now / conf1.MidSize * conf1.TSize * br1 / v01 * conf1.GangRate / conf1.HandSize) tn1 += n1 blist1.add([n1, vm1, vm1, tt]) if abs(d2) > 0: n2 = (d2 * curve.now / conf2.MidSize * conf2.TSize * br2 / v02 * conf2.GangRate / conf2.HandSize) tn2 += n2 blist2.add([n2, vm2, vm2, tt]) (tni1, tnv1) = near(tn1) (tni2, tnv2) = near(tn2) if abs(tni1) > 0: (e, f) = bank1.addBank(tni1, vm1, conf1) curve.addE(e) flow += f if abs(tni2) > 0: e, f = bank2.addBank(tni2, vm2, conf2) curve.addE(e) flow += f if flow < minFlow: minFlow = flow restBank1 = [tnv1, vm1] restBank2 = [tnv2, vm2] (e, f) = bank1.destroy(vm1) curve.addE(e) flow += f (e, f) = bank2.destroy(vm2) curve.addE(e) flow += f if flow < minFlow: minFlow = flow if arg == "Curve": print "%s%.4f" % (conf1.CurveFix, curve.now / start * 30.0) print curve print minFlow
def phase(f, conf=basicConf, arg=None): bank = bankManage.Bank() detail = (arg == "detail") start = conf.StartValue curve = loss.Loss(start) tt = 0 tn = 0 phase = wave.Phase(conf.getSizes()) volSize = 30 smallVolSize = 10 volPhase = wave.Phase([smallVolSize, volSize], [10, 100]) #vol2=wave.Phase([smallVolSize,volSize],[10,100]) sum = 0 blist = Stack.Queue(conf.MidSize + 1, [0, 0, 0, 0]) restBank = [0, 0] #余量窗口 minFlow = curve.now flow = curve.now lastDay = "" day = "" vm = 0 lastV = -1 maxBuyRate = -1 divs = [] divSum = 0 divN = 0 drs = [] drSum = 0 afterWardFac = 0 #滞后因子 myBank = 0 vbn = 0 for line in f: tt += 1 lastD, nowD = data.readLine(line, conf) if nowD == None: continue v0 = nowD.lastP if v0 <= 1e-12: sys.stderr.write(line) break v1 = nowD.p lastV = v0 vol = lastD.vol if lastD.vol > 0 and nowD.vol - lastD.vol < 5 and nowD.vol - lastD.vol >= 0 and nowD.vol < 3000: vbn += 1 if vbn > 10: break if arg == "Curve": print "%s%.4f" % (conf.CurveFix, curve.now / start * 30.0) #phase.add(v1) divs.append(v1) if len(divs) >= conf.DivSize: vopen = divs[-conf.DivSize] max = divs[-1] divRate = abs(max - vopen) / vopen drs.append(divRate) divSum += abs(max - vopen) / vopen divN += 1 #print "",conf.MaxLoss,"divSum",divSum,"divN",divN,divSum/divN,"max-vopen",max-vopen,(max-vopen)/vopen,"max",max,"vopen",vopen divs = [] drSum += divRate if len(drs) > 100: drSum -= drs[0] del drs[0] #conf.MaxLoss=divSum/divN #conf.MaxLoss=0.99*divSum/divN+0.01*divRate #conf.MaxLoss=0.94*divSum/divN+0.05*drSum/len(drs)+0.01*divRate volPhase.simpleAdd(vol) phase.add(v1) #vol2.add(vol) curve.addTime() vm = v1 #v0*(1.0-conf.SlideRate)+v1*conf.SlideRate #print "vm",vm if nowD.settle: if detail: print "destroy: vm", vm, "bank", bank.bank, "price", bank.price, "now", curve.now myBank = 0 e, f = bank.destroy(vm, conf) curve.addE(e) flow += f if detail: print "after destroy:bank", bank.bank, "e", e, "flow", flow, "now", curve.now if abs(e) > 0: curve.addWin(e / conf.HandSize, conf) restBank = [0, vm] blist = Stack.Queue(conf.MidSize / conf.TSize + 1, [0, 0, 0, 0]) continue bankDiff = myBank - bank.bank if abs(bankDiff) > 1e-6: if True: print "tt", tt, "tn", tn, "myBank", myBank, "bank", bank.bank, "rest", restBank[ 0], "bankDiff", bankDiff #myBank-=checkBank(blist,bankDiff,conf,detail) clearN = clear(tt, blist, v0, vm, conf, curve, phase, detail) tn = restBank[0] #-restBank[0] #与整数操作的差额操作 tn += bankDiff myBank -= bankDiff tn += clearN createN = 0 if data.afterInDay(nowD, conf.DayEnd) <= 0 and data.afterInDay( nowD, conf.DayStart) >= 0: createN = create(tt, conf, phase, vm, curve, bank, detail) if createN > 0 and createN < conf.unitLowRate * conf.UnitLimit and bank.bank <= 0: createN = conf.UnitLimit if createN < 0 and createN > -conf.unitLowRate * conf.UnitLimit and bank.bank >= 0: createN = -conf.UnitLimit #afterWardFac=afterWardFac*(conf.MidSize-1.0)/conf.MidSize+createN/conf.MidSize #if abs(afterWardFac) > 0.8*abs(createN): # createN+=0.02*afterWardFac tryBank = bank.bank + tn ceilBank = (curve.now * conf.GangRate * conf.BankRate) / (v0 * conf.HandSize) if abs(createN) > 0 and abs(createN + tryBank) > ceilBank: #if random.random() > 0.9: # print "createN",createN,"tryBank",tryBank,"ceilBank",ceilBank createN *= (ceilBank - abs(tryBank)) / abs(createN) tn += createN if abs(createN) > 0: blist.add([createN, vm, vm, tt]) #phase.add(v1) #(tni,tnv)=near(tn) #tni=int(tn)+clearN tni = int(tn) tnv = tn - int(tn) #int(tn)-tn if tnv > conf.StopLossNearInt and bank.bank + tni < 0: tni += 1 tnv -= 1.0 if tnv < -conf.StopLossNearInt and bank.bank + tni > 0: tni -= 1 tnv += 1.0 myBank += tni #if abs(createN) > 0: # blist.add([createN,vm,vm,tt]) #if abs(int(tn)) > 0: # blist.add([int(tn),vm,vm,tt]) nfold = abs(tni * v1 * conf.HandSize) / (curve.now * conf.GangRate) if maxBuyRate < nfold: #print "nfold",nfold,"maxBuyRate",maxBuyRate,"curve.now",curve.now maxBuyRate = nfold #if nfold > 0.3: #print "nfold",nfold,"maxBuyRate",maxBuyRate,"curve.now",curve.now if conf.Stock and (tni + bank.bank) < 0: tni = -bank.bank tnv = tn - tni #tni-tn if abs(tni) > 0: if tni > 0: vm = vm * (1.0 + conf.SlideRate / 2500.0) else: vm = vm * (1.0 - conf.SlideRate / 2500.0) bb = bank.bank bp = bank.price bf = flow if random.random() < 1.999: #0.1%失败交易 (e, f) = bank.addBank(tni, vm, conf) curve.addE(e) flow += f if detail: print "tt:", tt, "bb:", bb, "bp:", bp, "tni:", tni, "vm:", vm, "now:", curve.now, "e:", e, "f:", f, "flow:", flow else: print "trade fail: tni", tni dflow = curve.now - bank.value(conf) / conf.GangRate - flow if abs(dflow) > 1.0: print line print dflow, curve.now, tni, "vm:", vm, "bb:", bb, "bp:", bp, "after:", bank, e, bf, f, flow break if flow < minFlow: minFlow = flow #浮动 #pass curve.addTrackE((vm - bank.price) * bank.bank * conf.HandSize) if arg == "Bank": #print "%s%.4f"%(conf.CurveFix,tni) print "%s%.4f" % (conf.CurveFix, flow / curve.now) if conf.Stock and bank.bank < 0: print "bank<0", curve.now, bank, tnv restBank = [tnv, vm] if detail: print "destroy: vm", vm, "bank", bank.bank, "price", bank.price, "now", curve.now if bank.bank > 0: vm = vm * (1.0 - conf.SlideRate / 2500.0) else: vm = vm * (1.0 + conf.SlideRate / 2500.0) (e, f) = bank.destroy(vm, conf) curve.addE(e) flow += f if detail: print "after destroy:bank", bank.bank, "e", e, "flow", flow, "now", curve.now if abs(e) > 0: curve.addWin(e / conf.HandSize, conf) return curve
def back(f, conf, guohu=1.0): phase = wave.Phase([100, conf.LargeSize, conf.Size, conf.MidSize]) li = 0 lsmall = 0 bi = -1 bn = 0 bprice = 0 lastV = -1 lastLow = -1 smallQueue = Stack.Queue(3, init=0) smallV = 0 navg = 0 flow = 10000.0 state = State() lastClose5 = False lastIsBull = False for line in f: li += 1 lastD, nowD = data.readLine(line, conf) if nowD == None: if bn > 0: bi = -1 bn = 0 lastV = -1 lastLow = -1 continue if nowD.open <= 0 or nowD.close <= 0: if bn > 0: bi = -1 bn = 0 lastV = -1 lastLow = -1 continue v = nowD.close high = nowD.high if lastV < 0: lastV = nowD.open add = (v - lastV) / float(lastV) if add > 0.11 or add < -0.11: bn = 0 bi = -1 lastV = -1 continue rate = (v - nowD.open) / float(nowD.open) hrate = (high - nowD.open) / float(nowD.open) hadd = (high - lastV) / float(lastV) oadd = (nowD.open - lastV) / float(lastV) avg = phase.avg(conf.MidSize) mavg = phase.avg(conf.LargeSize) avg100 = phase.avg(100) SA = 0.07 #B12 if bn > 0: #B1,B2,B3,B4 p = nowD.close #B11,B12,B13,B14,B15 if nowD.open > (1.0 + SA) * lastV: p = nowD.open #B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15 elif nowD.high > (1.0 + SA) * lastV: p = (1.0 + SA) * lastV e = bn * 100 * (p - 0.0013 * p - guohu * 0.6 / 1000 - bprice) bn = 0 print "%s\t%f\t%s" % (nowD.day, e / flow, str(state)) #curve.addE(e) bi = -1 #elif bi > 0: # if nowD.open < 1.06*lastV: # bprice=nowD.open*(1.0+0.0003)+guohu*0.6/1000 # bn=int(curve.now/(100*bprice)) if high > avg: navg += 1 else: navg = 0 #B1,2,3,4,5 #if (hrate > 0.085 or (hadd > 0.085 and hrate > 0.05) )and lsmall >= 1 and nowD.low <= avg and nowD.close > avg: #B6 增长率:0.790062 最大下降:0.228 @683 增长下降比:3.4653 0.6760年 #if (hrate > 0.07 or (hadd > 0.07 and hrate > 0.05) )and lsmall >= 1 and nowD.low <= avg and nowD.close > avg: #B7 HR = 0.08 #加oadd < (0.095-HR)防止买不进 #if hrate > HR and oadd < (0.095-HR) and lsmall >= 1 and nowD.low <= avg and nowD.close > avg: #B8,B9,B10,B11,B12,B13,B14,B15 #HR=0.04 HA = -0.02 #B8 #if hrate > HR and oadd < (0.095-HR) and lsmall >= 1 and lastV <= avg*1.02 and nowD.high > (1+HA)*avg: #B9 #if hrate > HR and oadd < (0.095-HR) and lsmall >= 1 and lastV <= avg*1.02 and nowD.high > (1+HA)*avg: state.reset() #B10,B11,B12,B13,B15 if hrate > HR and oadd < (0.095 - HR) and lsmall >= 1 and lastV <= avg * 1.02: #B14 #if hrate > HR and oadd < (0.095-HR) and lsmall >= 1: state.LastClose5 = lastClose5 state.IsBull = lastIsBull bi = li #B1,2,3,4,5,6 p = nowD.close #B7.1 #HA=0.0 #if hadd > HA: #两个条件要同时满足 # if nowD.open*(1+HR) <= (1.0+HA)*lastV: # p=(1.0+HA)*lastV # else: #sys.stderr.write("high open%.3f!\n"%(nowD.open)) #B7,B10,B11,B12,B13,B14,B15,B16 p = nowD.open * (1 + HR) #B8,B9 #if p < (1+HA)*avg: # p=(1+HA)*avg bprice = p * (1.0 + 0.0003) + guohu * 0.6 / 1000 bn = int(flow / (100 * bprice)) #if add < 0.099 or rate > 0.5: # bprice=nowD.close*(1.0+0.0003)+guohu*0.6/1000 # bn=int(curve.now/(100*bprice)) phase.simpleAdd(nowD.close) smallV -= smallQueue.pop() #B12,B13,B14,B15 lastClose5 = False lastIsBull = False if mavg > avg100: lastIsBull = True #B1 0.48 0.25 #B1#if add < 0.04 and rate < 0.04 and rate > -0.06 and nowD.low <= avg and nowD.high >= avg: #B2# 增长率:0.55 最大下降:0.217 mrate:0.6321 #if add < 0.04 and rate < 0.04 and nowD.low <= avg and nowD.high >= avg: #B3# 0.53 最大下降:0.157 0.6680年 mrate:0.6491 #B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14 if add < 0.04 and rate < 0.02 and nowD.low <= avg * 1.02 and nowD.high >= avg * 0.98: #B15 #if add < 0.04 and rate < 0.02 and nowD.low <= avg*1.03 and nowD.high >= avg*0.97: #B12,14 #if nowD.open >= avg*0.98 and nowD.close <= avg*1.02 and nowD.low<= avg and nowD.high >= avg: #B13,B15 if nowD.low <= avg and nowD.high >= avg: lastClose5 = True lsmall += 1 smallV += 1 smallQueue.add(1) else: lsmall = 0 smallQueue.add(0) lastV = v
def slideTrade(f, conf=basicConf, arg=None): conf.dir = 0 d = data.Data() lastD = data.Data() tt = 0 askDiv = 0 bidDiv = 0 bank = bankManage.Bank() #仓位管理 detail = (arg == "detail") start = conf.StartValue #初始资产 curve = loss.Loss(start) minFlow = curve.now flow = curve.now buyPos = -1 #建仓位置 lastLossPos = 0 LSize = conf.LSize v0 = 0 tni = 0 lastP = -1 lastTT = 0 td = None conf.LargeSize = 10 * conf.Size phase = wave.Phase([conf.LargeSize, conf.Size]) for line in f: tt += 1 if tt < 2: continue try: lastTd, td = data.readTick(line, d, lastD, conf) except Exception, e: sys.stderr.write("readTick Fail!\n") sys.stderr.write(str(e) + "\n") continue if td == None: continue v0 = td.p curve.addTime() phase.simpleAdd(v0) lavg = phase.avg(conf.LargeSize) avg = phase.avg(conf.Size) #if tt > conf.Size+2: # print avg,lavg KD = (avg > lavg and askDiv >= conf.base * 4 and askDiv <= conf.base * 16 and bidDiv <= conf.base) KK = (lavg > avg and askDiv <= conf.base and bidDiv >= conf.base * 4 and bidDiv <= conf.base * 16) #print tt,lastLossPos,LSize if abs(bank.bank) < 1e-6: #信号开仓 askDiv = td.ask - td.p bidDiv = td.p - td.bid unit = int((curve.now * conf.GangRate * conf.BankRate) / (v0 * conf.HandSize)) #按v0计算买入手数 if tni <= 0 and KD and tt - lastLossPos > LSize: tni += unit buyPos = tt #记录开仓位置 if tni >= 0 and KK and tt - lastLossPos > LSize: tni = -unit buyPos = tt elif abs(tni) < 1e-6: e1 = 0 if bank.bank > 0: e1 = v0 - bank.price else: e1 = bank.price - v0 if e1 < -(conf.MaxLoss * bank.price) or (bank.bank > 0 and KK) or ( bank.bank < 0 and KD): #止损平仓 tni = -bank.bank lastLossPos = tt elif tt - buyPos > conf.MidSize: #到指定周期结束平仓 tni = -bank.bank if abs(tni) > 0 and lastP < 0: lastP = v0 lastTT = tt if lastP > 0: if tni > 0: if tt - lastTT < conf.PTIME and (td.ask <= lastP or v0 < lastP): (e, f) = bank.addBank(tni, lastP, conf) curve.addWin(e / conf.HandSize, conf) #计算胜率 curve.addE(e) tni = 0 lastP = -1 elif tt - lastTT >= conf.PTIME: #if bank.bank < -0.1: (e, f) = bank.addBank(tni, td.ask, conf) curve.addWin(e / conf.HandSize, conf) #计算胜率 curve.addE(e) tni = 0 lastP = -1 elif tni < 0: if tt - lastTT < conf.PTIME and (td.bid >= lastP or v0 > lastP): (e, f) = bank.addBank(tni, lastP, conf) curve.addWin(e / conf.HandSize, conf) #计算胜率 curve.addE(e) tni = 0 lastP = -1 elif tt - lastTT >= conf.PTIME: #if bank.bank > 0.1: (e, f) = bank.addBank(tni, td.bid, conf) curve.addWin(e / conf.HandSize, conf) #计算胜率 curve.addE(e) tni = 0 lastP = -1 else: curve.addTrackE( (v0 - bank.price) * bank.bank * conf.HandSize) #更新资产净值
(e, f) = bank.addBank(tni, vm, conf) curve.addE(e) flow += f if flow < minFlow: minFlow = flow restBank = tnv tt += 1 if destroy: bank.destroy(vm) #print curve #print minFlow return (phase, curve, bank, tt, restBank) WK = 5 m2 = [(1.0, 0.0, wave.Phase([WK])), (0.0, 1.0, wave.Phase([WK])), (0.5, 0.5, wave.Phase([WK])), [0.6, 0.4, wave.Phase([WK])]] def selectModelCombine(models, vs, phase, curve, bank, tt, restBank, conf, arg, N): if len(models) == 2: MR = N / (100000.0 + N) bw1 = models[0][1] bw2 = models[1][1] m2[-1][0] = bw1 m2[-1][1] = bw2 bmax = -1000000 #(curve.now-baseRate)/(curve.maxTrack+baseRate*0.1) for w1, w2, wphase in m2: models[0][1] = w1