def Run(self): """ """ #self._log('Strategy_Boll_Pre') #以下为交易测试 code = self.data.get_code() #当前策略处理的股票 self.code = code if not self.is_backtesting and not self.AllowCode(code): return df_hisdat = self.data.get_hisdat(code) #日k线 df_five_hisdat = self.data.get_hisdat(code, dtype='5min') #5分钟k线 df_fenshi = self.data.get_fenshi(code) #日分时 if len(df_fenshi) == 0: self.data.log(code + u"未取到分时数据") return account = self._getAccount() #获取交易账户 price = float(df_fenshi.tail(1)['p']) #当前股价 closes = df_hisdat['c'] yestoday_close = closes[-2] #昨日收盘价 zhangfu = stock.ZhangFu(price, yestoday_close) self._log( 'price=%.2f %s %s' % (price, str(df_fenshi.index[-1]), str(df_five_hisdat.iloc[-1]))) account_mgr = ac.AccountMgr(account, price, code) trade_num = ac.ShouShu(account_mgr.init_money() * self.trade_num_use_money_percent / price) trade_num = max(100, trade_num) # 信号计算 four = stock.FOUR(closes) four = four[-1] upper, middle, lower = stock.TDX_BOLL(df_five_hisdat['c']) highs, lows, closes = df_five_hisdat['h'], df_five_hisdat[ 'l'], df_five_hisdat['c'] adx = stock.TDX_ADX(highs, lows, closes) self._log('boll : %.2f,%.2f,%.2f' % (upper[-1], middle[-1], lower[-1])) boll_w = abs(upper[-1] - lower[-1]) / middle[-1] * 100 #50个周期内最高值 is_high = abs(price - max(df_five_hisdat[-1000:]['h'])) / price < 0.005 boll_poss = [ upper[-1], (upper[-1] - middle[-1]) / 2 + middle[-1], middle[-1], (middle[-1] - lower[-1]) / 2 + lower[-1], lower[-1], ] self._log('boll_poss: %.2f, %.2f boll_w=%.2f adx=%.2f' % (boll_poss[0], boll_poss[1], boll_w, adx[-1])) #上一个成交的价位 pre_price = account_mgr.last_chengjiao_price() pre_pre_price = account_mgr.last_chengjiao_price(index=-2) sell_count = account_mgr.queryTradeCount(1) buy_count = account_mgr.queryTradeCount(0) #买入均价 adx = adx[-1] boll_up_ratio = 0.02 #信号判断 num = 0 if so.assemble( price > boll_poss[1], price > pre_price * (1 + self.trade_ratio), #price > boll_poss[2], #price > self.max_buy_price*(1+self.trade_ratio), #boll_w > 3.5, #adx > 60, #sell_count < 2, #pr.horizontal(df_five_hisdat), 0, ): num = -trade_num self.trade_status = self.enum.boll_up_mid #if self.order(1, code, price, num): #self._log(agl.utf8_to_ascii('一档卖出%s, %.2f, %d'%(code, price, num))) if so.assemble( price > boll_poss[0], price > pre_price * (1 + self.trade_ratio), #price > self.max_buy_price*(1+self.trade_ratio), #boll_w > 3, adx > 60, is_high, #four > self.trade_four[1], #sell_count < 2, #self.trade_status == self.enum.nothing, #0, ): num = -trade_num * 3 self.trade_status = self.enum.boll_up #if self.order(1, code, price, num): #self._log(agl.utf8_to_ascii('二档卖出%s, %.2f, %d'%(code, price, num))) if so.assemble( price < boll_poss[-2] * (1 + boll_up_ratio), price < pre_price * (1 - self.trade_ratio), #price < boll_poss[2], #price < self.min_sell_price*(1-0.03), #boll_w > 3.5, #adx>60, #buy_count < 2, #pr.horizontal(df_five_hisdat), 0, ): num = trade_num self.trade_status = self.enum.boll_down_mid #if boll_w > 6: #num *= self.trade_num_ratio #if self.order(0, code, price, num): #self._log(agl.utf8_to_ascii('一档买入%s, %.2f, %d'%(code, price, num))) if so.assemble( price < boll_poss[-1], price < pre_price * (1 - self.trade_ratio), #price < self.min_sell_price*(1-0.03), #boll_w > 3, #buy_count < 2, #self.trade_status == self.enum.nothing, #adx>70, #four < self.trade_four[0], #0, ): num = trade_num * 3 #num = account_mgr.last_chengjiao_num() self.trade_status = self.enum.boll_down #if self.order(0, code, price, num): #self._log(agl.utf8_to_ascii('二档买入%s, %.2f, %d'%(code, price, num))) #成本区间 if so.assemble( price < pre_price * (1 - 0.05), four < -0.1, self.trade_status == self.enum.boll_up, 0, ): num = trade_num * self.trade_num_ratio self.trade_status = self.enum.nothing if so.assemble( price > pre_price * (1 + 0.05), four > 0.1, self.trade_status == self.enum.boll_down, 0, ): num = -trade_num * self.trade_num_ratio self.trade_status = self.enum.nothing #zz顶抛出后回补 if so.assemble( price < pre_price * (1 - 0.02), #sell_count >= 2, self.trade_status == self.enum.zz_up, 0, ): #上次zz卖出的数量 num = account_mgr.last_chengjiao_num() self.trade_status = self.enum.zz_hui_bu if so.assemble( price > pre_price * (1 + 0.02), #sell_count >= 2, self.trade_status == self.enum.zz_down, 0, ): #上次zz卖出的数量 num = account_mgr.last_chengjiao_num() self.trade_status = self.enum.zz_hui_bu #计算分时zz zz_sign = 0 closes = df_five_hisdat['c'][-200:].values zz = stock.ZigZag(closes) if len(zz) > 2: zz_result = stock.analyzeZZ(zz) zz_line_ratio = zz_result[1] / zz_result[0] #线段比率 #扑捉大涨回头的信号 if abs(zz_result[0]) > 0.05 and abs(zz_line_ratio) > 0.05 and abs( zz_line_ratio) < 0.2 and abs(zz_result[0]) > 0.04: zz_sign = agl.where(zz_result[1] > 0, 1, -1) if num != 0: bSell = agl.where(num > 0, 0, 1) num = abs(num) #if bSell: #num = self._compensate(num, bSell, code) #基本上每天的振幅都在1个点以上 if abs(stock.ZhangFu(price, yestoday_close)) > 0.01: self.order(bSell, code, price, num) zz_pre_price = myredis.createRedisVal('policy_basesign_zz_pre_price', price) if so.assemble( zz_sign != 0, 0, ): #print self.price, getZZPrePrice(self.price),abs(self.price-getZZPrePrice(self.price))/self.price num = trade_num * 12 bSell = agl.where(zz_sign > 0, 0, 1) num = self._compensate(num, bSell, code) bCanOrder = False if so.assemble( bSell, price > zz_pre_price.get() * (1 + 0.03), #price > pre_price*(1+self.trade_ratio), ): bCanOrder = True self.trade_status = self.enum.zz_up if so.assemble( (not bSell), price < zz_pre_price.get() * (1 - 0.03), #price < pre_price*(1-self.trade_ratio) ): bCanOrder = True self.trade_status = self.enum.zz_down if bCanOrder: self._getAccount().Order(bSell, code, price, num) zz_pre_price.set(price) #信号发生时语音播报, 并通知界面回显 if not self.is_backtesting and (price > boll_poss[1] or price < boll_poss[-2]): codename = stock.GetCodeName(code) s = '%s, %.2f' % (codename, price) self.data.show(codename) #通知界面显示 self.data.speak2(s) #语音播报 #tick report if self.is_backtesting and self.is_tick_report: self._getAccount().TickReport(df_five_hisdat, 'win') return
def recorg(pl, df_boll): sign = 0 df = df_boll if 0: df = pd.DataFrame #计算参数 #输出原始图片 #closes, boll_up, boll_mid, boll_low = df[['c', 'upper','middle', colname.boll_lower]].to_list() closes = df['c'].values boll_up = df[colname.boll_upper].values boll_mid = pd.Series(df[colname.boll_middle]).values boll_low = pd.Series(df[colname.boll_lower]).values #转zz zz_boll_up = stock.ZigZag(boll_up) zz_boll_mid = stock.ZigZag(boll_mid) zz_boll_low = stock.ZigZag(boll_low) zz_close = stock.ZigZag(closes, percent=5) zz_close_short = stock.ZigZag(closes, percent=.5) adx = df[colname.adx].values[-1] bollw = stock.BOLLW(boll_up, boll_low, closes) #数值判断 obj = boll_params() obj.h1 = closes[-1] - boll_mid[-1] obj.h2 = closes[-1] - boll_low[-1] boll_y = get_y_boll(closes[-1], boll_low, boll_mid) techs = calc_property() y1, y2 = stock.analyzeZZ(zz_close) techs.close_zz_0 = "%.3f" % y1 techs.close_zz_1 = "%.3f" % y2 y1, y2 = stock.analyzeZZ(zz_boll_low) techs.boll_low_zz_0 = "%.3f" % y1 techs.boll_low_zz_1 = "%.3f" % y2 techs.boll_w = "%.3f" % bollw[-1] techs.adx = "%.3f" % adx techs.boll_x = get_x_boll(closes[-1], boll_low) techs.boll_y = boll_y techs.boll_y = "%.2f%%" % (techs.boll_y * 100) #集中度计算, 使用黄金分割或者是三分之一 techs.jzd = kurtosis.calc_kurtosis( stock.GuiYiHua(closes[-int(len(closes) * (1 - 0.618)):])) sign = False n = 1 # 如果是日线,n=10 #第二次机会 if sign_observation.assemble(0, obj.h1 < 0, adx > 25, bollw[-1] > 0.02 * n, boll_y < 0.02 * n, techs.boll_x > 10): techs.choice = 2 sign = True if sign_observation.assemble( 1, obj.h1 < 0, adx > 10, bollw[-1] > 0.03 * n, boll_y < 0.5 * n, techs.boll_x > 10, techs.jzd[1] * 1000 < 2, #标准差与集中度相关, 偏度与峰度与集中度没有发现关联 #abs(techs.jzd[-2]) < 1, #abs(techs.jzd[-1]) < 0.5, 1): techs.choice = 3 sign = True #第一次机会 if sign_observation.assemble(0, obj.h1 < 0, adx > 25, bollw[-1] > 0.02 * n, float(techs.boll_low_zz_1) < -0.01 * n, techs.boll_x > 1, boll_y > 0.8, 1): sign = True techs.choice = 1 #输出判断 if sign: pl: publish.Publish pl.insertHtml("<tr><td>") ui.drawBoll(pl, closes, boll_up, boll_mid, boll_low) pl.insertHtml("</td><td>") ui.DrawZZ(pl, zz_boll_up, is_append=ui.draw_style.head) ui.DrawZZ(pl, zz_boll_mid, is_append=ui.draw_style.mid) ui.DrawZZ(pl, zz_close, c='b', is_append=ui.draw_style.mid) ui.DrawZZ(pl, zz_close_short, c='g', is_append=ui.draw_style.mid) ui.DrawZZ(pl, zz_boll_low, is_append=ui.draw_style.end) pl.insertHtml("</td><td>") vals = agl.get_print_object(techs) pl.insertHtml(vals) pl.insertHtml("<br>%.2f,%.3f,%.2f,%.2f" % techs.jzd) pl.insertHtml("</td>") #pl.insertHtml("<td>") #pl.insertHtml("</td>") pl.insertHtml("</tr>") return sign
def _test_DrawZZ(self): code = '300033' df_five_hisdat = stock.getFiveHisdatDf(code) closes = df_five_hisdat['c'][-500:] zz = stock.ZigZag(closes,percent=1) DrawDvsAndZZ(pl, closes, zz)