def login(): # 登录行情服务器 user = MyMdApi(instruments=inst, broker_id=BROKER_ID, investor_id=INVESTOR_ID, password=PASSWORD) user.Create("data") user.RegisterFront(ADDRESS_MD) user.Init() print('行情服务器登录成功') bars = load_data_from_server(server_base='http://106.14.119.122', instruments_id=inst[0], granularity=granularity) while True: if Utils.exit_flag: msg = 'Thread CTPDataCollectEngine say bye-bye' print(msg) logger.info(msg=msg) return try: payload = q_depth_market_data.get(timeout=1) q_depth_market_data.task_done() instrument_id = payload.InstrumentID action_day = payload.ActionDay update_time = payload.UpdateTime.replace(':', '') last_price = payload.LastPrice volume = payload.Volume if volume == 0: continue tickToBar(payload, 13) if not q_bar.empty(): bar = q_bar.get() bars.append(bar) high = get_k_line_column(data=bars, depth=20) low = get_k_line_column(data=bars, ohlc='low') ma_5 = ma(elements=high, step=5) ma_10 = ma(elements=high, step=10) print(high) print(ma_5) print(ma_10) cu = cross(ma_5, ma_10) print(cu) far = be_apart_from(cu) print(far) except queue.Empty as e: 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)
td.Init() time.sleep(60) 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,
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
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 login(): # 登录行情服务器 user = MyMdApi(instruments=inst, broker_id=BROKER_ID, investor_id=INVESTOR_ID, password=PASSWORD) user.Create("data") user.RegisterFront(ADDR_MD) user.Init() print('行情服务器登录成功') while True: if Utils.exit_flag: msg = 'Thread CTPDataCollectEngine say bye-bye' print(msg) logger.info(msg=msg) return try: payload = q_depth_market_data.get(timeout=1) print(payload) q_depth_market_data.task_done() instrument_id = payload.InstrumentID action_day = payload.ActionDay update_time = payload.UpdateTime.replace(':', '') last_price = payload.LastPrice volume = payload.Volume if volume == 0: continue if update_time.find('.') != -1: dt = datetime.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S.%f") timestamp = time.mktime(dt.timetuple()) + (dt.microsecond / 1e6) else: timestamp = int(time.mktime(time.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S"))) date_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) ohlc_key = generate_ohlc_key(instrument_id=instrument_id, granularity=granularity, timestamp=timestamp) if ohlc_key not in nest: nest[ohlc_key] = { 'date_time': date_time, 'last_timestamp': timestamp, 'high': last_price, 'low': last_price, 'close': last_price, 'open': last_price } nest[ohlc_key]['last_timestamp'] = timestamp nest[ohlc_key]['date_time'] = date_time nest[ohlc_key]['close'] = last_price if last_price > decimal.Decimal(nest[ohlc_key]['high']): nest[ohlc_key]['high'] = last_price elif last_price < decimal.Decimal(nest[ohlc_key]['low']): nest[ohlc_key]['low'] = last_price if nest.__len__() > 1: for k, v in list(nest.items()): if k == ohlc_key: continue data.append(nest[k]) # del nest[k] high = get_k_line_column(data=data, depth=20) ma_5 = ma(elements=high, step=5) ma_10 = ma(elements=high, step=10) # print high # print ma_5 # print ma_10 cu = cross(ma_5, ma_10) print(cu) far = be_apart_from(cu) print(far) print(nest) except queue.Empty as e: pass
td.Init() time.sleep(60) 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)