def plot(self): import matplotlib.pyplot as plt from common_stock.stock_plotter import StockAxisPlot from stock_data_manager.ddr_file_cache import read_ddr_fast df = read_ddr_fast(self.code).df df = df[df.index <= self.date_range[1]] df = df[df.index >= self.date_range[0]] fig, ax = plt.subplots() stock_plotter = StockAxisPlot((fig, ax), df, code=self.code) from common.scipy_helper import pdSr buy_series = pdSr(data=[item.buy_price for item in self.hold_periods], index=[item.buy_ts for item in self.hold_periods]) sell_series = pdSr(data=[item.sell_price for item in self.hold_periods], index=[item.sell_ts for item in self.hold_periods]) # print(buy_series) # print(sell_series) stock_plotter.add_scatter_point('buy', buy_series, color='k', marker='^') stock_plotter.add_scatter_point('sell', sell_series, color='k', marker='v') hold_days = [(item.buy_ts, item.sell_ts) for item in self.hold_periods] stock_plotter.add_hold_info(hold_days) plt.title(self.code) stock_plotter.plot() plt.show()
def main(): ddr = read_ddr_fast('510050.XSHG') df = ddr.df.tail(6) print(KlineFeatures.volume_percentage(df)) return val = extract_kline_pren_feature(df, window=5) print(val)
def analyser1(cls): accs = SpecificRunner.emu_hs300_p2() for trim_len in [0, 3, 5, 7]: for acc in accs: acc = acc.trim_hold_periods(trim_len, read_ddr_fast(acc.code).df.open) print(acc) acc.plot() print() print_line_item(cls.gain_from_acc(accs))
def test_HoldPeriod_left_trim(): hp = HoldPeriod('510050.XSHG', 20161024, 20161027, 1, 2) assert hp sr = read_ddr_fast('510050.XSHG').df.open hp.left_trim(2, sr) assert hp.buy_price == (sr.at[20161026]) assert hp hp = hp.left_trim(1, sr) assert not hp hp = hp.left_trim(1, sr) assert not hp
def main(): for trim_len in [0, 3, 5]: import datetime s_time = datetime.datetime.now() accs = SpecificRunner.emu_hs_all_sv() # accs = [item for item in accs if item.hold_yyield < 0.6] # numpy.random.shuffle(accs) # accs = [item for item in accs if item.code == '300099.XSHE'] # for acc in accs: # acc = acc.trim_hold_periods(trim_len, read_ddr_fast(acc.code).df.open) accs = [item.trim_hold_periods(trim_len, read_ddr_fast(item.code).df.open) for item in accs] # hold_len = [] # for val in accs: # hold_len.extend([item.hold_len for item in val.hold_periods]) # plot_histogram(hold_len) # print('Time: ', datetime.datetime.now() - s_time) print('Gain:', Analyser.gain_from_acc(accs)) pass
def bs_open_open(code, time_period, day_len) -> List[SingleEmuAccount]: df = read_ddr_fast(code).df.tail(day_len) assert len(df) > day_len / 2, 'data len is too small' days = df.index.values o, c = df.open.values, df.close.values skip_days = time_period sma = talib.EMA(df.close.values, time_period) index_day = list(enumerate(days))[skip_days:] sday, eday = index_day[0], index_day[-1] yield_ = c[eday[0]] / c[sday[0]] acc = SingleEmuAccount(code, (sday[1], eday[1]), yield_) for index, day in index_day: if index <= skip_days: continue if sma[index - 1] < c[index - 1]: acc.buy(day, o[index]) elif sma[index - 1] > c[index - 1]: acc.sell(day, o[index]) acc.calc_addition_infos() return acc
def bs_open_open(code, macd_param, day_len) -> List[SingleEmuAccount]: fast, slow, signal = macd_param df = read_ddr_fast(code).df.tail(day_len) if len(df) < day_len / 2: return days = df.index.values o, c = df.open.values, df.close.values skip_days = slow + signal + 1 macd_vals = talib.MACD(df.close.values, fast, slow, signal)[2] index_day = list(enumerate(days))[skip_days:] sday, eday = index_day[0], index_day[-1] yield_ = c[eday[0]] / c[sday[0]] acc = SingleEmuAccount(code, (sday[1], eday[1]), yield_) for index, day in index_day: if index <= skip_days: continue if macd_vals[index - 1] > 0: acc.buy(day, o[index]) elif macd_vals[index - 1] < 0: acc.sell(day, o[index]) acc.calc_addition_infos() return acc
def main(): ddr = read_ddr_fast('510050.XSHG') ddr = ddr.tail(100) plot_df_with_min_max(ddr.df)