def dingdang(data, stock, stockname): strategy = '叮当三号' bars = {stock: PytdxToAwpBar(data)} # print(bars[stock]) O = get_k_line_column(bars[stock], ohlc='open') H = get_k_line_column(bars[stock], ohlc='high') L = get_k_line_column(bars[stock], ohlc='low') C = get_k_line_column(bars[stock], ohlc='close') uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) try: knum = len(H) signalall = buysigdetail.count(True) * 2 avsigp = int(knum / signalall) if buysigdetail[-1]: print(stockname, '发出买入信号。。。') ttsengine.say(strategy + stockname + '发出买入信号。') ttsengine.runAndWait() # playsound('2.wav') print(stock, '最近的买入信号:', avsigp, buysigdetail[(knum - avsigp):]) # sleep(1) sleep(2) elif sellsigdetail[-1]: print(stockname, '发出卖出信号。。。') ttsengine.say(strategy + stockname + '发出卖出信号。') ttsengine.runAndWait() # playsound('Alarm04.wav') print(stock, '最近的卖出信号:', avsigp, sellsigdetail[(knum - avsigp):]) # sleep(1) sleep(2) print('-' * 50) except: pass
def dingdangNo6(): st = time.time() lastsig = '' initFund = 500000 fundNow = initFund fundTemp = fundNow pp = 0 fundList = list() initFundReal = 500000 fundNowReal = initFundReal fundTempReal = fundNowReal sigAllReal = list() PPReal = 0 fundListReal = list() skprofitReal = 0 bkprofitReal = 0 profitlist = [] losslist = [] Signals = list() skprofit = 0 bkprofit = 0 bkloss = 0 skloss = 0 feeRatio = 0.0065 instMultifier = 10 signalslist = list() while True: if not q_depth_market_data.empty(): tick = q_depth_market_data.get() tickToBar(tick, granularity) # dayopen = tick.OpenPrice # dayhigh = tick.HighestPrice # daylow = tick.LowestPrice # dayavp = int(tick.AveragePrice / 10) # BB = (tick.LastPrice - daylow) / (dayhigh - daylow) if dayhigh != daylow else 0.5 # siglast = min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) # quote_info = "合约:{0},当前价格:{price},时间:{ttime},日内振幅:{1},价格位置:{2} 做多信号:{3},做空信号:{4}" # # print quote_info.format(tick.InstrumentID,dayhigh - daylow,BB, buysigdetail.count(True),sellsigdetail.count(True), price=tick.LastPrice, ttime = tick.UpdateTime) # quote_info1 = "合约:{instid},日内振幅:{zhenfu},价格位置:{BB:.3f} 做多信号:{buynum},做空信号:{sellnum},信号距离:{siglast}" # info= {'instid': tick.InstrumentID, # 'zhenfu' : dayhigh - daylow, # 'buynum' : buysigdetail.count(True), # 'sellnum' : sellsigdetail.count(True), # 'BB':BB, # 'siglast': siglast} # # print quote_info1.format(**info) # knum = len(H) # signalall = buysigdetail.count(True) # avsigp = knum / signalall # buysigprice = C[len(C) - be_apart_from(buysigdetail)] # sellsigprice = C[len(C) - be_apart_from(sellsigdetail)] # sellsigprice1 = C[-1] if sellsigdetail[-1] else 0 # 另外一种方法。 # # lastsig = 'SK' if be_apart_from(buysigdetail) > be_apart_from(sellsigdetail) else 'BK' if not q_signals.empty(): lastsigdict = q_signals.get() allSignal.append(lastsigdict) print(lastsigdict) # O = get_k_line_column(bars, inst, granularity, ohlc='open') # H = get_k_line_column(bars, inst, granularity, ohlc='high') # L = get_k_line_column(bars, inst, granularity, ohlc='low') # C = get_k_line_column(bars, inst, granularity, ohlc='close') # # lastk = get_last_k_line(bars, 'rb1905', 780) # print lastk # # uperband = HHV(H, 23) # lowerband = LLV(L, 23) # dingdangline = MID(uperband, lowerband) # signalList = cross(C, dingdangline) # buysigdetail = CROSS(C, dingdangline) # sellsigdetail = CROSS(dingdangline, C) # # knum = len(H) # # signalall = buysigdetail.count(True) # # avsigp = knum / signalall if signalall > 0 else knum # # # print(buysigdetail.count(True)) # # print(sellsigdetail.count(True)) # print u'叮 当: ', dingdangline[(knum - avsigp):] # print u'收盘价: ', C[(knum - avsigp):] # # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):]) # # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):]) if not q_bar.empty(): bar = q_bar.get() bars[barinterval].append(bar) # with open(bardatafile, 'a') as f: # f.writelines(json.dumps(bar, ensure_ascii=False) + '\n') # print len(bars[barinterval]) ed = time.time() lastk = get_last_k_line(bars, inst, granularity) print(lastk, len(bars[barinterval]), ed-st) O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') dt = lastk['date_time'] HH = H[-50:] LL = L[-50:] CC = C[-50:] uperband = HHV(HH, 23) lowerband = LLV(LL, 23) dingdangline = MID(uperband, lowerband) if cross2(C[-2:], dingdangline[-2:]): signalslist.append('bk') elif cross2(dingdangline[-2:], C[-2:], ): signalslist.append('sk') else: signalslist.append(None) signalList = cross(CC, dingdangline) buysigdetail = CROSS(CC, dingdangline) sellsigdetail = CROSS(dingdangline, CC) knum = len(H) signalall = signalslist.count('bk') + signalslist.count('sk') + signalslist.count('bp') + signalslist.count( 'sp') # signalall1 = buysigdetail.count(True) + sellsigdetail.count(True) # # avsigp = knum / signalall if signalall > 0 else knum # print('buysig all:',buysigdetail.count(True)) # print('sellsig all:', sellsigdetail.count(True)) # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):]) # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):]) # print min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) # print be_apart_from(sellsigdetail) # pp= getPositionRatio(buysigdetail) if buysigdetail[-1]: fundTemp += skprofit * pp * instMultifier - (pp * instMultifier * C[-1] * 1.11 / 10000) * 2 fundratio = getPositionRatio(sellsigdetail) fundToUse = fundratio * fundTemp unitPrice = C[-1]*10*0.15 pp = int(fundToUse/unitPrice) if skprofit >0: profitlist.append(skprofit) else: losslist.append(skprofit) bkprofits = [] signal = {'date_time': dt, 'signaltype': 'BK', 'signalprice': C[-1], 'pos': pp} q_signals.put(signal) Signals.append(signal) # with open('allsignal.json', 'a') as f: # f.writelines(json.dumps(signal, ensure_ascii=False) + '\n') order_price = C[-1] # td.PrepareOrder(tick.InstrumentID, b, k, pp, order_price) # log.info(log.printfNow() + u'下多单:' + str(pp) + ',orderprice:'+str(order_price)) print('buy order issued.') lastsig = 'BK' buysigprice = C[-1] print('buysigprice', buysigprice) if sellsigdetail[-1]: fundTemp += bkprofit * pp * instMultifier - (pp * instMultifier * C[-1] * 1.11 / 10000) * 2 fundratio = getPositionRatio(buysigdetail) fundToUse = fundTemp * fundratio unitPrice = C[-1] * 10 * 0.15 pp = int(fundToUse / unitPrice) if bkprofit >0: profitlist.append(bkprofit) else: losslist.append(bkprofit) skprofits = [] signal = {'date_time': dt, 'signaltype': 'SK', 'signalprice': C[-1], 'pos': pp} q_signals.put(signal) Signals.append(signal) # with open('allsignal.json', 'a') as f: # f.writelines(json.dumps(signal, ensure_ascii=False) + '\n') # order_price = tick.LastPrice # # # TODO 暂时没有处理平昨仓和今仓,待处理... 应该有个完整的策略持仓管理模块 # # td.PrepareOrder(tick.InstrumentID, s, p, pp, order_price) # # log.info(log.printfNow() + u'平多单:' + str(pp) + ',orderprice:'+str(order_price)) # print('close order issued...') lastsig = 'SK' sellsigprice = C[-1] print('sellsigprice', sellsigprice) if lastsig == 'BK': # print tick.LastPrice, buysigprice bkprofit = C[-1] - buysigprice print('多单盈利价差', bkprofit) bkprofits.append(bkprofit) bkpmax = max(bkprofits) if bkprofits else 0 bkpmin = min(bkprofits) if bkprofits else 0 print('当前信号:', lastsig, '信号价:', buysigprice, '当前价: ', C[ -1], '仓位', pp, '盈利价差:', bkprofit, '最大:', bkpmax, '最小', bkpmin, '持续周期:', be_apart_from( buysigdetail), '理论持仓', pp) sigprofit_info.append([lastsig, bkpmax, bkpmin]) # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):], pp) if len(bkprofits) >= 2: fundNow = fundTemp + (bkprofits[-1]-bkprofits[-2]) * pp * instMultifier elif lastsig == 'SK': skprofit = sellsigprice - C[-1] skprofits.append(skprofit) skpmax = max(skprofits) if skprofits else 0 skpmin = min(skprofits) if skprofits else 0 # print u'当前信号:', lastsig, u'信号价:', sellsigprice, u'当前价: ', C[ # -1], u'盈利价差:', skprofit, u'最大:', skpmax, u'最小', skpmin, u'持续周期:', be_apart_from(sellsigdetail) print('当前信号:', lastsig, '信号价:', sellsigprice, '当前价: ', C[ -1], '仓位', pp, '盈利价差:', skprofit, '最大:', skpmax, '最小', skpmin, '持续周期:', be_apart_from( buysigdetail), '理论持仓', pp) sigprofit_info.append([lastsig, max(skprofits), min(skprofits)]) # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):], pp) if len(skprofits) >= 2: fundNow = fundTemp + (skprofits[-1]-skprofits[-2]) * pp * instMultifier fundList.append(fundNow) fundListMa = MA(fundList, 23) getinlist = CROSS(fundList, fundListMa) getoutlist = CROSS(fundListMa, fundList) # print getinlist.count(True) if getinlist[-1]: sigReal = {'date_time':dt, 'signaltype':signal['signaltype'], 'signalprice': C[-1], 'pos':signal['pos']} # if len(sigAllReal)>1 and sigAllReal[-1]['signaltype']==sigReal['signalprice']: # continue # else: sigAllReal.append(sigReal) elif getoutlist[-1]: sigReal = {'date_time': dt, 'signaltype': signal['signaltype'], 'signalprice': C[-1], 'pos': signal['pos']} # if len(sigAllReal)>1 and sigAllReal[-1]['signaltype']==sigReal['signalprice']: # continue # else: sigAllReal.append(sigReal) sigNow = sigAllReal[-1] sigNow1 = sigReal if sigNow['signaltype'] == 'BK': profitReal = C[-1] - sigNow['signalprice'] if sigNow['signaltype'] == 'SK': profitReal = sigNow['signalprice'] - C[-1] fundTempReal += profitReal * sigNow['pos'] * instMultifier - (pp * instMultifier * C[-1] * 1.11 / 10000) * 2 print(max(fundList), min(fundList)) print('初始权益:', initFund, '策略当前权益: ', fundNow, '账号1权益', fundTempReal)
# bars[barinterval] = barss # O30 = get_k_line_column(bars, inst, 300, ohlc='open') O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') lastk = get_last_k_line(bars, inst, granularity) print(lastk) uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) # print len(O) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) # print be_apart_from(buysigdetail) # print be_apart_from(sellsigdetail)
def dingdangno6bksp(md, td, instid, instrumentid): while True: time.sleep(0.3) if not md.q_depth_market_data.empty(): tick = q_depth_market_data.get() tickToBar(tick, granularity) dayopen = tick.OpenPrice dayhigh = tick.HighestPrice daylow = tick.LowestPrice dayavp = int(tick.AveragePrice / 10) BB = (tick.LastPrice - daylow) / (dayhigh - daylow) if dayhigh != daylow else 0.5 siglast = min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) quote_info = "合约:{0},当前价格:{price},时间:{ttime},日内振幅:{1},价格位置:{2} 做多信号:{3},做空信号:{4}" print( quote_info.format(tick.InstrumentID, dayhigh - daylow, BB, buysigdetail.count(True), sellsigdetail.count(True), price=tick.LastPrice, ttime=tick.UpdateTime)) quote_info1 = "合约:{instid},日内振幅:{zhenfu},价格位置:{BB:.3f} 做多信号:{buynum},做空信号:{sellnum},信号距离:{siglast}" info = { 'instid': tick.InstrumentID, 'zhenfu': dayhigh - daylow, 'buynum': buysigdetail.count(True), 'sellnum': sellsigdetail.count(True), 'BB': BB, 'siglast': siglast } print(quote_info1.format(**info)) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall # sigsum2 = buysigdetail[(knum - avsigp):-2].count(True) + sellsigdetail[(knum - avsigp):-2].count(True) # sigsum4 = sigsum2 * 2 # sigsum5 = sigsum4 if sellsigdetail[-1] or buysigdetail[-1] else 0 # # pp = min((sigsum5 + 1), 8) buysigprice = C[len(C) - be_apart_from(buysigdetail)] sellsigprice = C[len(C) - be_apart_from(sellsigdetail)] sellsigprice1 = C[-1] if sellsigdetail[-1] else 0 # 另外一种方法。 lastsig = 'SK' if be_apart_from(buysigdetail) > be_apart_from( sellsigdetail) else 'BK' # if buysigdetail[-1] or sellsigdetail[-1]: # knum = len(H) # signalall = buysigdetail.count(True) # avsigp = knum / signalall # sigsum2 = buysigdetail[(knum - avsigp):-2].count(True) + sellsigdetail[(knum - avsigp):-2].count(True) # sigsum3 = sigsum2 * 2 # # pp = min((sigsum3 + 1), 8) # posratio.append(pp) if lastsig == 'BK': bkprofit = tick.LastPrice - buysigprice bkprofits.append(bkprofit) bkpmax = max(bkprofits) if bkprofits else 0 bkpmin = min(bkprofits) if bkprofits else 0 print(INVESTOR_ID, '当前信号:', lastsig, '信号价:', buysigprice, tick.LastPrice, '盈利价差:', bkprofit, '最大:', bkpmax, '最小', bkpmin, '持续周期:', be_apart_from(buysigdetail), '理论持仓', pp) sigprofit_info.append([lastsig, bkpmax, bkpmin]) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):], pp)) elif lastsig == 'SK': skprofit = sellsigprice - tick.LastPrice skprofits.append(skprofit) skpmax = max(skprofits) if skprofits else 0 skpmin = min(skprofits) if skprofits else 0 print('当前信号:', lastsig, '信号价:', sellsigprice, sellsigprice1, tick.LastPrice, '盈利价差:', skprofit, '最大:', skpmax, '最小', skpmin, '持续周期:', be_apart_from(sellsigdetail)) sigprofit_info.append( [lastsig, max(skprofits), min(skprofits)]) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):], pp)) # print tick.InstrumentID, dayhigh - daylow, BB, buysigdetail.count(True), sellsigdetail.count(True), min( # be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) if not q_signals.empty(): lastsig = q_signals.get() print(lastsig) O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') lastk = get_last_k_line(bars, 'rb1905', 780) print(lastk) uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) print('叮 当: ', dingdangline[(knum - avsigp):]) print('收盘价: ', C[(knum - avsigp):]) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) if not q_bar.empty(): bar = q_bar.get() bars[barinterval].append(bar) with open(bardatafile, 'a') as f: f.writelines(json.dumps(bar, ensure_ascii=False) + '\n') print(len(bars[barinterval])) # bar_s = dict() # barss = load_data_from_server(server_base=serverport, instruments_id=inst, granularity=granularity) # barinterval = inst + '_' + str(granularity) # bar_s[barinterval] = barss # print len(bar_s[barinterval]) lastk = get_last_k_line(bars, 'rb1905', 780) print(lastk) # O30 = get_k_line_column(bars, inst, 300, ohlc='open') O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) print( min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail))) # print be_apart_from(sellsigdetail) # if buysigdetail[-1] or sellsigdetail[-1]: # 发出交易信号并确认,计算下单仓位 # knum = len(H) # signalall = buysigdetail.count(True) # avsigp = knum / signalall # sigsum2 = buysigdetail[(knum - avsigp):-2].count(True) + sellsigdetail[(knum - avsigp):-2].count(True) # sigsum3 = sigsum2 * 2 # # pp = min((sigsum3 + 1), 8) # posratio.append(pp) if buysigdetail[-1]: bkprofits = [] signal = { 'date_time': time.time(), 'signaltype': 'BK', 'signalprice': tick.LastPrice } q_signals.put(signal) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall sigsum2 = buysigdetail[(knum - avsigp):-2].count( True) + sellsigdetail[(knum - avsigp):-2].count(True) sigsum3 = sigsum2 * 2 pp = min((sigsum3 + 1), 8) order_price = tick.LastPrice td.PrepareOrder(tick.InstrumentID, b, k, pp, order_price) if sellsigdetail[-1]: skprofits = [] signal = { 'date_time': time.time(), 'signaltype': 'SK', 'signalprice': tick.LastPrice } q_signals.put(signal) order_price = tick.LastPrice # TODO 暂时没有处理平昨仓和今仓,待处理... 应该有个完整的策略持仓管理模块 if bkprofit > 0: td.PrepareOrder(tick.InstrumentID, s, p, pp, order_price) # spk sellcount = 0 down_trading_flag = False
# bars[barinterval] = barss # O30 = get_k_line_column(bars, inst, 300, ohlc='open') O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') lastk = get_last_k_line(bars, inst, granularity) print(lastk) uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) # print len(O) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) # print be_apart_from(buysigdetail) # print be_apart_from(sellsigdetail)
def initStrategy(q_initdata): bkprofits = [] skprofits = [] sigprofit_info = [] posratio = [] bkprofit = 0 skprofit = 0 initFund = 20000 instMultifier = 10 bars[barinterval] = list() while not q_initdata.empty(): bar = q_initdata.get() bars[barinterval].append(bar) O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') lastk = get_last_k_line(bars, inst, granularity) print(lastk) uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) pp = getPositionRatio(buysigdetail) if buysigdetail[-1]: initFund += skprofit * pp * instMultifier bkprofits = [] # signal = {'date_time': time.time(), 'signaltype': 'BK', 'signalprice': tick.LastPrice} # q_signals.put(signal) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall sigsum2 = buysigdetail[(knum - avsigp):-2].count( True) + sellsigdetail[(knum - avsigp):-2].count(True) sigsum3 = sigsum2 * 2 pp = min((sigsum3 + 1), 8) # order_price = tick.LastPrice # td.PrepareOrder(tick.InstrumentID, b, k, pp, order_price) # log.info(log.printfNow() + u'下多单:' + str(pp) + ',orderprice:' + str(order_price)) print('buy order issued.') if sellsigdetail[-1]: initFund += bkprofit * pp * instMultifier skprofits = [] signal = { 'date_time': time.time(), 'signaltype': 'SK', 'signalprice': tick.LastPrice } q_signals.put(signal) order_price = tick.LastPrice # TODO 暂时没有处理平昨仓和今仓,待处理... 应该有个完整的策略持仓管理模块 td.PrepareOrder(tick.InstrumentID, s, p, pp, order_price) log.info(log.printfNow() + '平多单:' + str(pp) + ',orderprice:' + str(order_price)) print('close order issued...') # spk sellcount = 0 down_trading_flag = False knum = len(H) signalall = buysigdetail.count(True) if signalall > 0: avsigp = knum / signalall # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) # print len(O) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) # print be_apart_from(buysigdetail) # print be_apart_from(sellsigdetail) # 计算发出信号时刻,最近平均信号距离内的信号数量 sigsum2 = buysigdetail[(knum - avsigp):-2].count( True) + sellsigdetail[(knum - avsigp):-2].count(True) sigsum4 = sigsum2 * 2 sigsum5 = sigsum4 if sellsigdetail[-1] or buysigdetail[-1] else 0 # pp = min((sigsum5+1)*10, 80) # 计算下单仓位数量 pp = min((sigsum5 + 1), 8)
def dingdangNo6(): lastsig = '' initFund = 20000 fundNow = 0 skprofit = 0 bkprofit = 0 initFund = 20000 feeRatio = 0.0065 instMultifier = 10 signalslist = list() while True: if not q_depth_market_data.empty(): tick = q_depth_market_data.get() tickToBar(tick, granularity) # dayopen = tick.OpenPrice # dayhigh = tick.HighestPrice # daylow = tick.LowestPrice # dayavp = int(tick.AveragePrice / 10) # BB = (tick.LastPrice - daylow) / (dayhigh - daylow) if dayhigh != daylow else 0.5 # siglast = min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) # quote_info = "合约:{0},当前价格:{price},时间:{ttime},日内振幅:{1},价格位置:{2} 做多信号:{3},做空信号:{4}" # # print quote_info.format(tick.InstrumentID,dayhigh - daylow,BB, buysigdetail.count(True),sellsigdetail.count(True), price=tick.LastPrice, ttime = tick.UpdateTime) # quote_info1 = "合约:{instid},日内振幅:{zhenfu},价格位置:{BB:.3f} 做多信号:{buynum},做空信号:{sellnum},信号距离:{siglast}" # info= {'instid': tick.InstrumentID, # 'zhenfu' : dayhigh - daylow, # 'buynum' : buysigdetail.count(True), # 'sellnum' : sellsigdetail.count(True), # 'BB':BB, # 'siglast': siglast} # # print quote_info1.format(**info) # knum = len(H) # signalall = buysigdetail.count(True) # # avsigp = knum / signalall # # # buysigprice = C[len(C) - be_apart_from(buysigdetail)] # sellsigprice = C[len(C) - be_apart_from(sellsigdetail)] # # sellsigprice1 = C[-1] if sellsigdetail[-1] else 0 # 另外一种方法。 # # lastsig = 'SK' if be_apart_from(buysigdetail) > be_apart_from(sellsigdetail) else 'BK' # # if not q_signals.empty(): lastsigdict = q_signals.get() print(lastsigdict) # O = get_k_line_column(bars, inst, granularity, ohlc='open') # H = get_k_line_column(bars, inst, granularity, ohlc='high') # L = get_k_line_column(bars, inst, granularity, ohlc='low') # C = get_k_line_column(bars, inst, granularity, ohlc='close') # # lastk = get_last_k_line(bars, 'rb1905', 780) # print lastk # # uperband = HHV(H, 23) # lowerband = LLV(L, 23) # dingdangline = MID(uperband, lowerband) # signalList = cross(C, dingdangline) # buysigdetail = CROSS(C, dingdangline) # sellsigdetail = CROSS(dingdangline, C) # # knum = len(H) # # signalall = buysigdetail.count(True) # # avsigp = knum / signalall if signalall > 0 else knum # # # print(buysigdetail.count(True)) # # print(sellsigdetail.count(True)) # print u'叮 当: ', dingdangline[(knum - avsigp):] # print u'收盘价: ', C[(knum - avsigp):] # # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):]) # # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):]) if not q_bar.empty(): bar = q_bar.get() bars[barinterval].append(bar) # with open(bardatafile, 'a') as f: # f.writelines(json.dumps(bar, ensure_ascii=False) + '\n') print(len(bars[barinterval])) lastk = get_last_k_line(bars, 'rb1905', 780) print(lastk) # O30 = get_k_line_column(bars, inst, 300, ohlc='open') O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) if cross2(C[-2:], dingdangline[-2:]): signalslist.append('bk') elif cross2( dingdangline[-2:], C[-2:], ): signalslist.append('sk') else: signalslist.append(None) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) knum = len(H) signalall = signalslist.count('bk') + signalslist.count( 'sk') + signalslist.count('bp') + signalslist.count('sp') signalall1 = buysigdetail.count(True) + sellsigdetail.count(True) avsigp = knum / signalall if signalall > 0 else knum print(('buysig all:', buysigdetail.count(True))) print(('sellsig all:', sellsigdetail.count(True))) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) # print min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) # print be_apart_from(sellsigdetail) # pp= getPositionRatio(buysigdetail) if buysigdetail[-1]: pp = getPositionRatio(sellsigdetail) initFund += skprofit * pp * instMultifier - ( pp * instMultifier * tick.LastPrice / 10000) * 2 bkprofits = [] signal = { 'date_time': time.time(), 'signaltype': 'BK', 'signalprice': tick.LastPrice } q_signals.put(signal) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall sigsum2 = buysigdetail[(knum - avsigp):-2].count( True) + sellsigdetail[(knum - avsigp):-2].count(True) sigsum3 = sigsum2 * 2 pp = min((sigsum3 + 1), 8) order_price = tick.LastPrice # td.PrepareOrder(tick.InstrumentID, b, k, pp, order_price) # log.info(log.printfNow() + u'下多单:' + str(pp) + ',orderprice:'+str(order_price)) print('buy order issued.') lastsig = 'BK' buysigprice = tick.LastPrice print('buysigprice', buysigprice) if sellsigdetail[-1]: pp = getPositionRatio(buysigdetail) initFund += bkprofit * pp * instMultifier - ( pp * instMultifier * tick.LastPrice / 10000) * 2 skprofits = [] signal = { 'date_time': time.time(), 'signaltype': 'SK', 'signalprice': tick.LastPrice } q_signals.put(signal) # order_price = tick.LastPrice # # # TODO 暂时没有处理平昨仓和今仓,待处理... 应该有个完整的策略持仓管理模块 # # td.PrepareOrder(tick.InstrumentID, s, p, pp, order_price) # # log.info(log.printfNow() + u'平多单:' + str(pp) + ',orderprice:'+str(order_price)) # print('close order issued...') lastsig = 'SK' sellsigprice = tick.LastPrice print('sellsigprice', sellsigprice) if lastsig == 'BK': print(tick.LastPrice, buysigprice) bkprofit = tick.LastPrice - buysigprice print(bkprofit) bkprofits.append(bkprofit) bkpmax = max(bkprofits) if bkprofits else 0 bkpmin = min(bkprofits) if bkprofits else 0 print('当前信号:', lastsig, '信号价:', buysigprice, tick.LastPrice, '盈利价差:', bkprofit, '最大:', bkpmax, '最小', bkpmin, '持续周期:', be_apart_from(buysigdetail), '理论持仓', pp) sigprofit_info.append([lastsig, bkpmax, bkpmin]) # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):], pp) fundNow = initFund + bkprofit * pp * instMultifier elif lastsig == 'SK': skprofit = sellsigprice - tick.LastPrice skprofits.append(skprofit) skpmax = max(skprofits) if skprofits else 0 skpmin = min(skprofits) if skprofits else 0 print('当前信号:', lastsig, '信号价:', sellsigprice, sellsigprice, tick.LastPrice, '盈利价差:', skprofit, '最大:', skpmax, '最小', skpmin, '持续周期:', be_apart_from(sellsigdetail)) sigprofit_info.append( [lastsig, max(skprofits), min(skprofits)]) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):], pp)) fundNow = initFund + skprofit * pp * instMultifier print('初始权益:', initFund, '策略当前权益: ', fundNow)
def dingdangNo6(): while True: if not q_depth_market_data.empty(): tick = q_depth_market_data.get() q_depth_market_data.task_done() tickToBar(tick, granularity) # dayopen = tick.OpenPrice # dayhigh = tick.HighestPrice # daylow = tick.LowestPrice # dayavp = int(tick.AveragePrice / 10) # BB = (tick.LastPrice - daylow) / (dayhigh - daylow) if dayhigh != daylow else 0.5 # siglast = min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) # quote_info = "合约:{0},当前价格:{price},时间:{ttime},日内振幅:{1},价格位置:{2} 做多信号:{3},做空信号:{4}" # print quote_info.format(tick.InstrumentID,dayhigh - daylow,BB, buysigdetail.count(True),sellsigdetail.count(True), price=tick.LastPrice, ttime = tick.UpdateTime) # quote_info1 = "合约:{instid},日内振幅:{zhenfu},价格位置:{BB:.3f} 做多信号:{buynum},做空信号:{sellnum},信号距离:{siglast}" # # info= {'instid': tick.InstrumentID, # 'zhenfu' : dayhigh - daylow, # 'buynum' : buysigdetail.count(True), # 'sellnum' : sellsigdetail.count(True), # 'BB':BB, # 'siglast': siglast} # print quote_info1.format(**info) # knum = len(H) # signalall = buysigdetail.count(True) # # avsigp = knum / signalall # # sigsum2 = buysigdetail[(knum - avsigp):-2].count(True) + sellsigdetail[(knum - avsigp):-2].count(True) # sigsum4 = sigsum2 * 2 # sigsum5 = sigsum4 if sellsigdetail[-1] or buysigdetail[-1] else 0 # # pp = min((sigsum5 + 1), 8) # buysigprice = C[len(C) - be_apart_from(buysigdetail)] # sellsigprice = C[len(C) - be_apart_from(sellsigdetail)] # # sellsigprice1 = C[-1] if sellsigdetail[-1] else 0 # 另外一种方法。 # # lastsig = 'SK' if be_apart_from(buysigdetail) > be_apart_from(sellsigdetail) else 'BK' # # if lastsig == 'BK': # # bkprofit = tick.LastPrice - buysigprice # bkprofits.append(bkprofit) # # bkpmax = max(bkprofits) if bkprofits else 0 # bkpmin = min(bkprofits) if bkprofits else 0 # # print u'当前信号:', lastsig, u'信号价:', buysigprice, tick.LastPrice, u'盈利价差:', bkprofit, u'最大:', bkpmax, u'最小', bkpmin, u'持续周期:', be_apart_from(buysigdetail) # sigprofit_info.append([lastsig, bkpmax, bkpmin]) # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):], pp) # elif lastsig == 'SK': # skprofit = sellsigprice - tick.LastPrice # skprofits.append(skprofit) # # skpmax = max(skprofits) if skprofits else 0 # skpmin = min(skprofits) if skprofits else 0 # print u'当前信号:', lastsig, u'信号价:', sellsigprice, sellsigprice1, tick.LastPrice, u'盈利价差:', skprofit, u'最大:', skpmax, u'最小', skpmin, u'持续周期:',be_apart_from(sellsigdetail) # sigprofit_info.append([lastsig, max(skprofits), min(skprofits)]) # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):], pp) # print tick.InstrumentID, dayhigh - daylow, BB, buysigdetail.count(True), sellsigdetail.count(True), min( # be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) if not q_signals.empty(): lastsig = q_signals.get() print(lastsig) # O = get_k_line_column(bars, inst, granularity, ohlc='open') # H = get_k_line_column(bars, inst, granularity, ohlc='high') # L = get_k_line_column(bars, inst, granularity, ohlc='low') # C = get_k_line_column(bars, inst, granularity, ohlc='close') # # lastk = get_last_k_line(bars, 'rb1905', 780) # print lastk # # uperband = HHV(H, 23) # lowerband = LLV(L, 23) # dingdangline = MID(uperband, lowerband) # signalList = cross(C, dingdangline) # buysigdetail = CROSS(C, dingdangline) # sellsigdetail = CROSS(dingdangline, C) # # knum = len(H) # signalall = buysigdetail.count(True) # # avsigp = knum / signalall # # # print(buysigdetail.count(True)) # # print(sellsigdetail.count(True)) # print dingdangline[(knum - avsigp):] # print C[(knum - avsigp):] # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):]) # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):]) if not q_bar.empty(): bar = q_bar.get() bars[barinterval].append(bar) # with open(bardatafile, 'a') as f: # f.writelines(json.dumps(bar, ensure_ascii=False) + '\n') # print len(bars[barinterval]) # bar_s = dict() # barss = load_data_from_server(server_base=serverport, instruments_id=inst, granularity=granularity) # barinterval = inst + '_' + str(granularity) # bar_s[barinterval] = barss # print len(bar_s[barinterval]) # lastk = get_last_k_line(bars, 'rb1905', 780) # print lastk # O30 = get_k_line_column(bars, inst, 300, ohlc='open') O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) # knum = len(H) # signalall = buysigdetail.count(True) # if signalall >0: # avsigp = knum / signalall # # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) # print('buy signal:', avsigp, buysigdetail[(knum - avsigp):]) # print('sell signal:', avsigp, sellsigdetail[(knum - avsigp):]) # print min(be_apart_from(buysigdetail), be_apart_from(sellsigdetail)) # print be_apart_from(sellsigdetail) # sigsum2 = buysigdetail[(knum - avsigp):-2].count(True) + sellsigdetail[(knum - avsigp):-2].count(True) # sigsum4 = sigsum2 * 2 # sigsum5 = sigsum4 if sellsigdetail[-1] or buysigdetail[-1] else 0 # pp = min((sigsum5 + 1), 8) # # print 'pp:', pp if sellsigdetail[-1]: skprofits = [] pp = getPositionRatio(sellsigdetail) sigdt = ' '.join([str(tick.ActionDay), str(tick.UpdateTime)]) signal = { 'date_time': sigdt, 'signaltype': 'SK', 'signalprice': tick.LastPrice, 'pos': pp } q_signals.put(signal) order_price = tick.LastPrice # td.PrepareOrder(tick.InstrumentID, s, k, pp, order_price) # spk sellcount = 0 down_trading_flag = False # if not down_trading_flag and tick.LastPrice < get_last_k_line(bars,'rb1905',780)['low']: # down_trading_flag = True # sellcount +=1 # sk(pp) # # # pass elif buysigdetail[-1]: bkprofits = [] pp = getPositionRatio(buysigdetail) sigdt = ' '.join([str(tick.ActionDay), str(tick.UpdateTime)]) signal = { 'date_time': sigdt, 'signaltype': 'BK', 'signalprice': tick.LastPrice, 'pos': pp } q_signals.put(signal) order_price = tick.LastPrice # td.PrepareOrder(tick.InstrumentID, b, k, pp, order_price) # bpk pass
# bars[barinterval] = barss # O30 = get_k_line_column(bars, inst, 300, ohlc='open') O = get_k_line_column(bars, inst, granularity, ohlc='open') H = get_k_line_column(bars, inst, granularity, ohlc='high') L = get_k_line_column(bars, inst, granularity, ohlc='low') C = get_k_line_column(bars, inst, granularity, ohlc='close') lastk = get_last_k_line(bars, inst, granularity) print(lastk) uperband = HHV(H, 23) lowerband = LLV(L, 23) dingdangline = MID(uperband, lowerband) signalList = cross(C, dingdangline) buysigdetail = CROSS(C, dingdangline) sellsigdetail = CROSS(dingdangline, C) knum = len(H) signalall = buysigdetail.count(True) avsigp = knum / signalall # print(buysigdetail.count(True)) # print(sellsigdetail.count(True)) # print len(O) print(('buy signal:', avsigp, buysigdetail[(knum - avsigp):])) print(('sell signal:', avsigp, sellsigdetail[(knum - avsigp):])) # print be_apart_from(buysigdetail) # print be_apart_from(sellsigdetail)