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
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) # 计算发出信号时刻,最近平均信号距离内的信号数量 sigsum2 = buysigdetail[(knum - avsigp):-2].count(True) + sellsigdetail[ (knum - avsigp):-2].count(True) sigsum4 = sigsum2 * 2
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 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)
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) # 计算发出信号时刻,最近平均信号距离内的信号数量 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