def check(stocks, strategy, strategy_func): end = None m_filter = check_enter(end_date=end, strategy_fun=strategy_func) results = list(filter(m_filter, stocks)) push.strategy( '**************"{0}"**************\n{1}\n**************"{0}"**************\n' .format(strategy, results))
def check_exit(): t_shelve = db.ShelvePersistence() file = t_shelve.open() for key in file: code_name = file[key]['code_name'] data = utils.read_data(code_name) if turtle_trade.check_exit(code_name, data): push.strategy("{0} 达到退出条件".format(code_name)) del file[key] elif turtle_trade.check_stop(code_name, data, file[key]): push.strategy("{0} 达到止损条件".format(code_name)) del file[key] file.close()
def check(code_name, data, end_date=None, threshold=60): origin_data = data if len(data) < threshold: logging.debug("{0}:样本小于{1}天...\n".format(code_name, threshold)) return data['ma60'] = pd.Series(tl.MA(data['close'].values, 60), index=data.index.values) begin_date = data.iloc[0].date if end_date is not None: if end_date < begin_date: # 该股票在end_date时还未上市 logging.debug("{}在{}时还未上市".format(code_name, end_date)) return False if end_date is not None: mask = (data['date'] <= end_date) data = data.loc[mask] data = data.tail(n=threshold) breakthrough_row = None for index, row in data.iterrows(): if row['open'] < row['ma60'] <= row['close']: if enter.check_volume(code_name, origin_data, row['date'], threshold): breakthrough_row = row if breakthrough_row is None: return False data_front = data.loc[(data['date'] < breakthrough_row['date'])] data_end = data.loc[(data['date'] >= breakthrough_row['date'])] for index, row in data_front.iterrows(): if not (-0.05 < (row['ma60'] - row['close']) / row['ma60'] < 0.2): return False push.strategy("股票{0} 突破日期:{1}".format(code_name, breakthrough_row['date'])) return True
def check(code_name, data, end_date=None, threshold=60): if len(data) < 250: logging.debug("{0}:样本小于250天...\n".format(code_name)) return data['ma250'] = pd.Series(tl.MA(data['close'].values, 250), index=data.index.values) begin_date = data.iloc[0].date if end_date is not None: if end_date < begin_date: # 该股票在end_date时还未上市 logging.debug("{}在{}时还未上市".format(code_name, end_date)) return False if end_date is not None: mask = (data['date'] <= end_date) data = data.loc[mask] data = data.tail(n=threshold) last_close = data.iloc[-1]['close'] # 区间最低点 lowest_row = data.iloc[-1] # 区间最高点 highest_row = data.iloc[-1] # 近期低点 recent_lowest_row = data.iloc[-1] # 计算区间最高、最低价格 for index, row in data.iterrows(): if row['close'] > highest_row['close']: highest_row = row elif row['close'] < lowest_row['close']: lowest_row = row if lowest_row['volume'] == 0 or highest_row['volume'] == 0: return False data_front = data.loc[(data['date'] < highest_row['date'])] data_end = data.loc[(data['date'] >= highest_row['date'])] if data_front.empty: return False # 前半段由年线以下向上突破 if not (data_front.iloc[0]['close'] < data_front.iloc[0]['ma250'] and data_front.iloc[-1]['close'] > data_front.iloc[-1]['ma250']): return False if not data_end.empty: # 后半段必须在年线以上运行(回踩年线) for index, row in data_end.iterrows(): if row['close'] < row['ma250']: return False if row['close'] < recent_lowest_row['close']: recent_lowest_row = row date_diff = datetime.date(datetime.strptime(recent_lowest_row['date'], '%Y-%m-%d')) - \ datetime.date(datetime.strptime(highest_row['date'], '%Y-%m-%d')) if not (timedelta(days=10) <= date_diff <= timedelta(days=50)): return False # 回踩伴随缩量 vol_ratio = highest_row['volume'] / recent_lowest_row['volume'] back_ratio = recent_lowest_row['close'] / highest_row['close'] if not (vol_ratio > 2 and back_ratio < 0.8): return False push.strategy("{0} 回撤幅度: {1}".format(code_name, 1 - back_ratio)) return True
def test_strategy(): settings.init() strategy("") strategy("1")