def stat_feat_15(security_symbol, start_date, end_date, thrd=-0.001, interval=None, conn=None): conn = connect_to_db() if conn is None else conn df = download_data_daily(conn, security_symbol, start_date, end_date) features = feature_extraction_fallraise(df) pos_cnt = 0 neg_cnt = 0 pos = 0 neg = 0 expp_list = list() for index, row in features.iterrows(): if row['lr02'] < row['lr01'] < thrd: if row['lr0'] > 0.000: pos_cnt += 1 pos += row['lr0'] elif row['lr0'] < -0.000: neg_cnt += 1 neg += row['lr0'] if interval is not None and pos_cnt + neg_cnt >= interval: exp_p = (pos + neg) / (pos_cnt + neg_cnt) expp_list.append(round(100*exp_p, 2)) pos_cnt = 0 neg_cnt = 0 pos = 0 neg = 0 if interval is None: exp_p = (pos + neg)/(pos_cnt + neg_cnt) print(f"corr: {pos_cnt} with expected return {round(100*pos/pos_cnt, 2)}%, wrong: {neg_cnt} with expected return " f"{round(100*neg/neg_cnt, 2)}%, Strategy Total Expected Return {round(100*exp_p, 2)}%") else: print(expp_list)
def dist_feat_15(security_symbol, start_date, end_date, thrd=-0.001, conn=None): conn = connect_to_db() if conn is None else conn df = download_data_daily(conn, security_symbol, start_date, end_date) features = feature_extraction_fallraise(df) features_ss = features.loc[(features['lr01'] < thrd) & (features['lr02'] < features['lr01'])] print(f"Mean: {features_ss['lr0'].mean()}, Sigma: {features_ss['lr0'].std()}") # print(features_ss) features_ss.hist(column='lr0', bins=500) plt.show()
def stat_feat_14(security_symbol, start_date, end_date, thrd=-0.001, interval=None, conn=None): conn = connect_to_db() if conn is None else conn df = download_data_daily(conn, security_symbol, start_date, end_date) features = feature_extraction_fallraise(df) pos_cnt = 0 neg_cnt = 0 pos = 0 neg = 0 expp_list = list() triggers = list() for index, row in features.iterrows(): if row['lr01'] < row['lr0'] < thrd and row['close_price'] < row[ 'vwap_ma_3']: if row['lr5-5'] > 0.000: pos_cnt += 1 pos += row['lr5-5'] ann = { 'x': row['date_key_int'], 'y': row['high_price'], 'text': round(row['lr5-5'], 2) } triggers.append(ann) elif row['lr5-5'] < -0.000: neg_cnt += 1 neg += row['lr5-5'] ann = { 'x': row['date_key_int'], 'y': row['high_price'], 'text': round(row['lr5-5'], 2) } triggers.append(ann) if interval is not None and pos_cnt + neg_cnt >= interval: exp_p = (pos + neg) / (pos_cnt + neg_cnt) expp_list.append(round(100 * exp_p, 2)) pos_cnt = 0 neg_cnt = 0 pos = 0 neg = 0 if interval is None and (pos_cnt + neg_cnt) > 0: exp_p = (pos + neg) / (pos_cnt + neg_cnt) pos_rate = round(100 * pos / pos_cnt, 2) if pos_cnt > 0 else "N/A" neg_rate = round(100 * neg / neg_cnt, 2) if neg_cnt > 0 else "N/A" print( f"corr: {pos_cnt} with expected return {pos_rate}%, wrong: {neg_cnt} with expected return " f"{neg_rate}%, Strategy Total Expected Return {round(100 * exp_p, 2)}%" ) return exp_p, triggers else: print(expp_list) print(features.iloc[-1]['date_key_int']) return expp_list, triggers
def candle_stick_daily(security_symbol, start_date, end_date, conn=None, df=None, annotations=None): """ :param security_symbol: :param start_date: :param end_date: :param conn: :param df: :param annotations: [{'x': date_key_int, 'y': high_price, 'text': 'falling?'}] :return: """ def __update_annotations(fig, anns): if anns is not None: for ann in anns: fig.add_annotation(x=pd.to_datetime(str(ann['x']), format='%Y%m%d'), y=ann['y'], text=ann['text']) fig.update_annotations( dict(xref="x", yref="y", showarrow=True, arrowhead=7, ax=0, ay=-40)) return True if df is None: conn = connect_to_db() if conn is None else conn df = download_data_daily(conn, security_symbol, start_date, end_date) df['date_key'] = df['date_key_int'].apply( lambda x: pd.to_datetime(str(x), format='%Y%m%d')) print(f"plotting chart for {security_symbol} ...") fig = go.Figure(data=[ go.Candlestick(x=df['date_key'], open=df['open_price'], high=df['high_price'], low=df['low_price'], close=df['close_price'], name="Candle Stick") ]) __update_annotations(fig, annotations) fig.update_layout(title=f'Daily Price Visualization', yaxis_title=f'{security_symbol} Stock') fig.show()
def show_all_fallraise(security_symbol, start_date, end_date, f_thd=-0.01, r_thd=0.02, conn=None): conn = connect_to_db() if conn is None else conn df = download_data_daily(conn, security_symbol, start_date, end_date) df = log_return(df) signals = get_falls_raises(df, falls_thd=f_thd, raises_thd=r_thd) signals['raises'].extend(signals['falls']) print(len(signals['raises']), len(signals['falls'])) candle_stick_daily(security_symbol, start_date, end_date, conn, annotations=signals['raises'])
def rank_performances(file_name, start_date, end_date): """ :param file_name: :param start_date: :param end_date: :return: """ # take second element for sort def __take_second(elem): return elem[1] secs = read_sec_list(file_name) conn = connect_to_db() results = list() for sec in secs: df = download_data_daily(conn, sec, start_date, end_date) perf = get_performance_log_return(df) results.append([sec, perf['mean'], perf['std']]) results.sort(key=__take_second) for each in results: print(each)
from analytics import stat_feat_14, dist_feat_14, stat_feat_15, dist_feat_15, stat_m21, dist_m21, compare_hl_oc_dists from utils.db_util import connect_to_db, download_data_daily from utils.helpers import read_sec_list from utils.visualization import candle_stick_daily if __name__ == '__main__': conn = connect_to_db() securities = read_sec_list("sec_list.csv") for sec in securities: print(f"evaluating {sec}") df = download_data_daily(conn, sec, 20170101, 20200601) mean_hl, mean_oc, sigma_hl, sigma_oc = compare_hl_oc_dists( df_candle=df) print( f"mean_hl: {round(100*mean_hl, 2)}%, mean_oc: {round(100*mean_oc, 2)}%," f" sigma_hl: {round(100*sigma_hl, 2)}%, sigma_oc: {round(100*sigma_oc, 2)}%" ) input() # stat_m21(sec, 20170101, 20200520, thrd=-0.01, interval=None, conn=conn) # dist_feat_14("USMV", 20130101, 20200520, conn=conn) # stat, ann = stat_feat_14("SDOW", 20130601, 20200601, thrd=-0.001, interval=None, conn=conn) # stat_m21("TQQQ", 20130601, 20200601, thrd=-0.001, interval=None, conn=conn) # candle_stick_daily("SDOW", 20130610, 20200601, annotations=ann)