def pause(self, today): today_volume = self.kl_pd.volume[int(today.key)] upper, middle, lower = calc_boll(self.kl_pd.close, self.time_period, self.nb_dev) if pd.isnull(middle[int(today.key)]): return None dif = today.pre_close - today.close ma_volume = calc_ma(self.kl_pd.volume, 20) if pd.isnull(ma_volume[int(today.key)]): return if today_volume / ma_volume[int(today.key)] > 2.5 and dif < 0 and dif / today.pre_close < -0.1: self.lock = True self.status = 0 if today_volume / ma_volume[int(today.key)] > 2.5 and dif / today.pre_close > -0.05: self.status = 1 if self.status == 1 and today.close >= upper[int(today.key)] * 1.05: self.lock = False self.status = 0 if today.close >= upper[int(today.key)]: self.lock = False self.status = 0
def fit_day(self, today): if self.lock: # 如果封锁策略进行交易的情况下,策略不进行择时 return None long_kl = self.past_today_kl(today, self.past_factor * self.xd) tl_long = AbuTLine(long_kl.close, 'long') # 判断长周期是否属于上涨趋势 # if not tl_long.is_up_trend(up_deg_threshold=self.up_deg_threshold, show=False): # return None upper, middle, lower = calc_boll(self.kl_pd.close, self.time_period, self.nb_dev) if pd.isnull(middle[int(today.key) - 1]): return None if today.pre_close <= middle[int(today.key) - 1] and today.close >= middle[int( today.key)]: print(u"穿过布林带中间线, 买入", "pre_close=", str(today.pre_close), "middle=", str(middle[int(today.key) - 1]), "close=", str(today.close), "middle=", str(middle[int(today.key)]), 'date=', str(today.date)) return self.buy_tomorrow()
def fit_day(self, today, orders): """ 寻找向下突破作为策略卖出驱动event :param today: 当前驱动的交易日金融时间序列数据 :param orders: 买入择时策略中生成的订单序列 """ upper, middle, lower = calc_boll(self.kl_pd.close, self.time_period, self.nb_dev) dif, dea, bar = calc_macd(self.kl_pd.close) if pd.isnull(upper[int(today.key)]): return None # if self.is_wave_period(today): # if today.close < middle[int(today.key)]: # print(u"震荡卖出") # print("close=", str(today.close), "upper=", str(lower[int(today.key)]), 'date=', str(today.date)) # for order in orders: # self.sell_tomorrow(order) '''快速上涨macd策略卖出''' if self._is_fast_up_period(today) and int(today.key) != 0: pre_i = int(today.key) - 1 now_i = int(today.key) if dif[pre_i] > dea[pre_i] and dif[now_i] <= dea[now_i]: # print(u"快速上涨macd卖出", "close=", str(today.close), "middle=", str(middle[int(today.key)]), 'date=', str(today.date)) for order in orders: self.sell_tomorrow(order) '''原本是达到布林线上端时卖出,但回测结果并不好''' '''跌破布林线下端卖出''' if today.close < lower[int(today.key)]: # print(u"跌穿布林带底线卖出", "close=", str(today.close), "lower=", str(lower[int(today.key)]), 'date=', str(today.date)) for order in orders: self.sell_tomorrow(order)
def fit_day(self, today): # logging.info("enter fit_day, %s %d %f" % (self.kl_pd.name, today.date, today.close)) if self.lock: # 如果封锁策略进行交易的情况下,策略不进行择时 return None short_kl = self.past_today_kl(today, 21) sl_short = AbuTLine(short_kl.close, 'short') # 判断长周期是否属于上涨趋势 # if not tl_long.is_up_trend(up_deg_threshold=self.up_deg_threshold, show=False): # return None upper, middle, lower = calc_boll(self.kl_pd.close, self.time_period, self.nb_dev) dif, dea, bar = calc_macd(self.kl_pd.close) # if pd.isnull(middle[int(today.key) - 1]): if pd.isnull(middle[int(today.key)]): return None # long_kl = self.past_today_kl(today, 21) # long_line = AbuTLine(long_kl.close, 'long') # if long_line.is_down_trend(down_deg_threshold=-3, show=False): # return None '''处于快速上涨区, 跌到布林带中间线买入效果不好''' # if tl_short.is_up_trend(6, show=False): # if middle[int(today.key) - 1] < today.pre_close < middle[int(today.key) - 1] * 1.05 and today.close >= today.pre_close: # print(u"快速上涨区,跌到布林带中间线,未跌穿, 买入", "pre_close=", str(today.pre_close), "middle=", str(middle[int(today.key) - 1]), # "close=", str(today.close), "middle=", str(middle[int(today.key)]), 'date=', str(today.date)) # return self.buy_tomorrow() if int(today.key) != 0: pre_i = int(today.key) - 1 now_i = int(today.key) '''0值之上不操作买入, 增加这个逻辑后整体收益不好,去掉''' # if not sl_short.is_up_trend(up_deg_threshold=2, show=False): # return None if dif[pre_i] < dea[pre_i] and dif[now_i] >= dea[now_i]: # print(u"macd金叉, 买入", "pre_close=", str(today.pre_close), "middle=", str(middle[int(today.key) - 1]), # "close=", str(today.close), "middle=", str(middle[int(today.key)]), 'date=', str(today.date)) # order = # if order is not None: # today_record = FuTodayCanBuyRecord() # today_record.record_today_can_buy_stock(today, self.kl_pd.name, 1) return self.buy_today() if today.pre_close <= middle[int(today.key) - 1] and today.close >= middle[int( today.key)]: # print(u"穿过布林带中间线, 买入", "pre_close=", str(today.pre_close), "middle=", str(middle[int(today.key) - 1]), # "close=", str(today.close), "middle=", str(middle[int(today.key)]), 'date=', str(today.date)) # order = self.buy_today() return self.buy_today()
def fit_day(self, today): # logging.info("enter fit_day, %s %d %f" % (self.kl_pd.name, today.date, today.close)) self.pause(today) if self.lock: # 如果封锁策略进行交易的情况下,策略不进行择时 return None upper, middle, lower = calc_boll(self.kl_pd.close, self.time_period, self.nb_dev) if pd.isnull(middle[int(today.key)]): return None if today.close <= lower[int(today.key)]: return self.buy_today()
def fit_day(self, today, orders): """ 寻找向下突破作为策略卖出驱动event :param today: 当前驱动的交易日金融时间序列数据 :param orders: 买入择时策略中生成的订单序列 """ upper, middle, lower = calc_boll(self.kl_pd.close, self.time_period, self.nb_dev) if pd.isnull(upper[int(today.key)]): return None '''达到布林线上端时卖出''' if today.close >= upper[int(today.key)]: # print(u"跌穿布林带底线卖出", "close=", str(today.close), "lower=", str(lower[int(today.key)]), 'date=', str(today.date)) for order in orders: if order.buy_price < today.close: self.sell_today(order)
def sample_13(): kl_pd = ABuSymbolPd.make_kl_df( '601398', data_mode=ABuEnv.EMarketDataSplitMode.E_DATA_SPLIT_UNDO, start='2012-04-20', end='2018-04-20', parallel=False) print(kl_pd) # kl_pd.to_csv("/Users/juchen/abu/601398.csv") upper, middle, lower = calc_boll(kl_pd.close, 20, 2) print(middle) print(lower) rsi = calc_rsi(kl_pd.close) print(rsi) abupy.nd.boll.plot_boll_from_klpd(kl_pd) abupy.nd.rsi.plot_rsi_from_klpd(kl_pd)
def fit_pick(self, kl_pd, target_symbol): upper, middle, lower = calc_boll(kl_pd.close, 20, 2) ma_volume = calc_ma(kl_pd.volume, 20) status = 0 lock = False if len(kl_pd) < self.back_count: return False for index in np.arange(len(kl_pd) - self.back_count, len(kl_pd)): today_volume = kl_pd.volume[index] if pd.isnull(middle[index]): continue dif = kl_pd.close[index] - kl_pd.close[index - 1] if pd.isnull(ma_volume[index]): return if today_volume / ma_volume[ index] > 2 and dif < 0 and dif / kl_pd.close[index - 1] < -0.1: lock = True status = 0 '放量且不是大跌的情况,预解除锁定' if today_volume / ma_volume[index] > 2.5 and dif / kl_pd.close[ index - 1] > -0.05: status = 1 '放量后涨到boll带顶端,解除锁定' if status == 1 and kl_pd.close[index] >= upper[index] * 1.05: lock = False status = 0 # if kl_pd.close[index] >= upper[index]: # self.lock = False # self.status = 0 if ~lock: # return True if kl_pd.close[len(kl_pd.volume) - 1] < lower[len(kl_pd.volume) - 1] * 1.05: return True return False
def fit_day(self, today): upper, middle, lower = calc_boll(self.kl_pd.close) if pd.isnull(middle[int(today.key) - 1]): return None '''判断是否处于快速上涨区''' long_kl = self.past_today_kl(today, 120) tl_long = AbuTLine(long_kl.close, 'long') # 判断长周期是否属于上涨趋势 if tl_long.is_up_trend(up_deg_threshold=self.up_deg_threshold, show=False): if today.close == self.xd_kl.close.min() and \ AbuTLine(self.xd_kl.close, 'short').is_down_trend( down_deg_threshold=-self.down_deg_threshold, show=False): # if middle[int(today.key) - 1] < today.pre_close < middle[int(today.key) - 1] * 1.1 \ # and today.close >= today.pre_close: # print(u"补仓,快速上涨区,跌到布林带中间线,未跌穿, 买入", "pre_close=", str(today.pre_close), "middle=", # str(middle[int(today.key) - 1]), # "close=", str(today.close), "middle=", str(middle[int(today.key)]), 'date=', str(today.date)) # print("补仓") return self.buy_today()
def fit_pick(self, kl_pd, target_symbol): upper, middle, lower = calc_boll(kl_pd.close, 20, 3) ma_volume = calc_ma(kl_pd.volume, 20) status = 0 for i in np.arange(1, 20): index = len(kl_pd.volume) - i today_volume = kl_pd.volume[index] if pd.isnull(middle[index]): return False if index - 1 < 0: return False dif = kl_pd.close[index] - kl_pd.close[index - 1] if pd.isnull(ma_volume[index]): return if today_volume / ma_volume[ index] > 2.5 and dif < 0 and dif / kl_pd.close[index - 1] < -0.1: return False # if status == 1 and today_volume / ma_volume[index] > 2.5 and kl_pd.close[index - 1] > -0.05: if status == 1 and today_volume / ma_volume[ index] > 2.5 and kl_pd.close[index - 1] > -0.05: status = 2 break if kl_pd.close[index] >= upper[index] * 0.95: status = 1 if status == 2: # return True if kl_pd.close[len(kl_pd.volume) - 1] < lower[len(kl_pd.volume) - 1] * 1.05: return True return False
def parse_one_stock(his_df, symbol, symbol_complete): profit_sum, profit_cgs = parse_actions(symbol_complete) if profit_sum <= 0: logging.info('profit_sum < 0, symbol={}, profit_cgs={}'.format( symbol_complete, profit_cgs)) return global can_buy_df all_close = his_df.close.copy() # stock_now = now_df[now_df.code == int(symbol)] # if stock_now is None: # return # now_price_s = stock_now.trade.astype(float) # all_close.add(now_price_s) # if len(stock_now["trade"].values) == 0: # logging.error("today price null, {}".format(symbol)) # return # # now_price = stock_now["trade"].values.tolist()[0] now_price = all_close[len(all_close) - 1] upper, middle, lower = calc_boll(all_close, 20, 2) dif, dea, bar = calc_macd(all_close) # print(now_price) if his_df.close[len(his_df.close) - 1] < middle[len(middle) - 2] \ and now_price > middle[len(middle) - 1]: df_temp = pd.DataFrame( [[str(symbol), now_price, 'boll', profit_sum, profit_cgs]], columns=['symbol', 'price', 'type', 'profit_sum', 'profit_cgs']) can_buy_df = can_buy_df.append(df_temp) if dif[len(dif) - 2] < dea[len(dea) - 2] \ and dif[len(dif) - 1] >= dea[len(dea) - 1]: df_temp = pd.DataFrame( [[str(symbol), now_price, 'macd', profit_sum, profit_cgs]], columns=['symbol', 'price', 'type', 'profit_sum', 'profit_cgs']) can_buy_df = can_buy_df.append(df_temp)