def run_csv_gen(sec_dict, invs_res, output_dir, verbose=False): ctr = 0 assert output_dir != None output_dir = invs_utils.rp(output_dir) if not os.path.isdir(output_dir): print("{} doesn't exist. Creating it now !!".format(output_dir)) os.makedirs(output_dir) # endif print('Output dir = {}'.format(output_dir)) # Iterate over all security dict for sec_code in sec_dict.keys(): # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], invs_res, verbose=verbose) sec_name = sec_dict[sec_code]['name'] # If dataframe is empty, continue if d_this.empty: continue # endif # Set time axis as index d_this = d_this.set_index('t') # save to CSV file csv_file_this = output_dir + '/{}_{}.csv'.format(sec_name.replace(' ', '_'), invs_res) print('{}. CSV Report for {}'.format(ctr, coloritf(sec_name, 'magenta'))) d_this.to_csv(csv_file_this) ctr = ctr + 1 # endfor # Write to csv file print(coloritf('--------------------- REPORT END --------------------------------', 'green'))
def run_csv_gen(sec_dict, res, plots_dir): ctr2 = 0 csv_rep_list = [] strategy_name = 'csvgen' if plots_dir: # Other things plots_dir = os.path.expanduser(plots_dir) if not os.path.isdir(plots_dir): print("{} doesn't exist. Creating it now !!".format(plots_dir)) os.makedirs(plots_dir) # endif print('Plots dir = {}'.format(plots_dir)) else: print('plots_dir cannot be None') sys.exit(-1) # endif print('Running {} strategy.'.format(strategy_name)) print( Fore.GREEN + '--------------------- GENERATING REPORT --------------------------------' + Fore.RESET) # Iterate over all security dict for sec_code in sec_dict.keys(): logging.debug("{} : Running {} strategy over {}".format( ctr2, strategy_name, sec_code)) # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) sec_name = sec_dict[sec_code]['name'] # If dataframe is empty, continue if d_this.empty: continue # endif # Set time axis as index d_this = d_this.set_index('t') # save to CSV file csv_file_this = plots_dir + '/{}_{}.csv'.format( sec_name.replace(' ', '_'), res) print('{}. CSV Report for {}'.format( ctr2, Fore.MAGENTA + sec_name + Fore.RESET)) d_this.to_csv(csv_file_this) ctr2 = ctr2 + 1 # endfor # Write to csv file print(Fore.GREEN + '--------------------- REPORT END --------------------------------' + Fore.RESET)
def run_scanner_sec_supp_res(sec_dict, res='1m', rep_file=None, disp_levels=True, ema_period=9, n_samples=200): ctr2 = 0 csv_rep_list = [] strategy_name = 'suppresgen' header_l = ['Name', 'supp_res_v'] csv_report_file = '~/csv_report_stats_{}.csv'.format(datetime.datetime.now( ).date().isoformat()) if rep_file == None else rep_file print('Running {} strategy.'.format(strategy_name)) print('ema_period = {}'.format(ema_period)) print('n_samples = {}'.format(n_samples)) print( Fore.GREEN + '--------------------- GENERATING REPORT --------------------------------' + Fore.RESET) # Add header csv_rep_list.append(header_l) # Iterate over all security dict for sec_code in sec_dict.keys(): logging.debug("{} : Running {} strategy over {}".format( ctr2, strategy_name, sec_code)) # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) sec_name = sec_dict[sec_code]['name'] # Calculate stats sr_list = invs_tools.supp_res(d_this, ema_period=ema_period, n_samples=n_samples) srd_list = sr_list.diff() sr_mean = srd_list.mean() sr_med = srd_list.median() # Print stats sec_name_c = Fore.MAGENTA + sec_name + Fore.RESET print('{}. {:<50}, suppres={:.2f}-{:.2f}'.format( ctr2, sec_name_c, sr_mean, sr_med)) if disp_levels: print('** Levels : {}'.format( Fore.YELLOW + ','.join(['{0:.2f}'.format(x) for x in sr_list.tolist()]) + Fore.RESET)) # endif csv_rep_list.append([sec_name, sr_mean]) ctr2 = ctr2 + 1 # endfor # Write to csv file print(Fore.GREEN + '--------------------- REPORT END --------------------------------' + Fore.RESET) print('Writing report file to {}'.format(csv_report_file)) with open(os.path.expanduser(csv_report_file), 'w') as f_out: csv_writer = csv.writer(f_out, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for item_this in csv_rep_list: csv_writer.writerow(item_this) # endfor # endwith return csv_report_file
def run_scanner_sec_stats(sec_dict, res='1m', rep_file=None): ctr2 = 0 csv_rep_list = [] strategy_name = 'calc_stats' header_l = [ 'Name', 'price_spike(nearness)', 'price_spike2(fluctuation)', 'vol_spike' ] csv_report_file = '~/csv_report_stats_{}.csv'.format(datetime.datetime.now( ).date().isoformat()) if rep_file == None else rep_file print('Running {} strategy.'.format(strategy_name)) print( Fore.GREEN + '--------------------- GENERATING REPORT --------------------------------' + Fore.RESET) # Add header csv_rep_list.append(header_l) # Iterate over all security dict for sec_code in sec_dict.keys(): logging.debug("{} : Running {} strategy over {}".format( ctr2, strategy_name, sec_code)) # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) sec_name = sec_dict[sec_code]['name'] # Calculate stats # Av d dhlc_a = (d_this['h'] + d_this['l'] + d_this['c']) / 3 dhlc_ra = dhlc_a.ewm(span=9).mean() dhlc_rad = (dhlc_a - dhlc_ra) / dhlc_ra dhlc_m = dhlc_rad.max() # Spike 2 dhlc_raa = dhlc_ra.ewm(span=9).mean() dhlc_raad = (dhlc_ra - dhlc_raa) / dhlc_raa dhlc_m2 = dhlc_raad.mean() # Vol spike dhlc_r00 = dhlc_ra / dhlc_ra.shift(5) dhlc_r01 = dhlc_r00[::5] dhlc_r02 = abs(np.log(dhlc_r01.mean())) # Print stats sec_name_c = Fore.GREEN + sec_name + Fore.RESET print( '{}. {:<50}, price_spike={}, price_spike2={}, vol_spike={}'.format( ctr2, sec_name_c, dhlc_m, dhlc_m2, dhlc_r02)) csv_rep_list.append([sec_name, dhlc_m, dhlc_m2, dhlc_r02]) ctr2 = ctr2 + 1 # endfor # Write to csv file print(Fore.GREEN + '--------------------- REPORT END --------------------------------' + Fore.RESET) print('Writing report file to {}'.format(csv_report_file)) with open(os.path.expanduser(csv_report_file), 'w') as f_out: csv_writer = csv.writer(f_out, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for item_this in csv_rep_list: csv_writer.writerow(item_this) # endfor # endwith return csv_report_file
def graph_generator(sec_dict, res='1W', plots_dir=None, plot_period=None, fig_ratio=1.0): g_graphs_dir = '/graphs/' ctr = 0 if plots_dir: # Other things plots_dir = os.path.expanduser(plots_dir) if not os.path.isdir(plots_dir): print("{} doesn't exist. Creating it now !!".format(plots_dir)) os.makedirs(plots_dir) os.makedirs(plots_dir + g_graphs_dir) # endif print('Plots dir = {}'.format(plots_dir)) # endif period_list = [] period_list = period_list + [ opt_args['ema_period0'] ] if 'ema_period0' in opt_args else period_list period_list = period_list + [ opt_args['ema_period1'] ] if 'ema_period1' in opt_args else period_list period_list = period_list + [ opt_args['ema_period2'] ] if 'ema_period2' in opt_args else period_list print('Running graph_generator strategy') print(Fore.GREEN + '-----------------------------------------------------' + Fore.RESET) # Iterate over all security dict for sec_code in sec_dict.keys(): # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) # Print sec_name = Fore.GREEN + sec_dict[sec_code]['name'] + Fore.RESET sys.stdout.write('{}. graph_generator : {}\n'.format(ctr, sec_name)) sys.stdout.flush() ctr = ctr + 1 # If dataframe is empty, continue if d_this.empty: continue # endif # Save plot if plots_dir: pic_name = plots_dir + g_graphs_dir + sec_dict[sec_code][ 'name'].replace(' ', '_') + '.png' invs_plot.gen_candlestick(d_this, period_list=period_list, title=sec_dict[sec_code]['name'], plot_period=plot_period, file_name=pic_name, fig_ratio=fig_ratio, plot_volume=False) # endif # endfor print(Fore.GREEN + '-----------------------------------------------------' + Fore.RESET)
def run_stretegy_over_all_securities(sec_dict, lag=30, res='1W', strategy_name="ema_crossover", plots_dir=None, rep_file=None, plot_monthly=False, plot_period=100, opt_args={}): csv_report_file = '~/csv_report_security_list_{}.csv'.format( datetime.datetime.now().date().isoformat( )) if rep_file == None else rep_file csv_rep_list = [] g_graphs_dir = '/graphs/' # Headers header_l = [ 'Name', 'Switch Direction', 'Time Delta', 'Peek to Trough %', 'Price', 'Volume Up' ] # set default value of plot_period plot_period = plot_period if plot_period else 100 if plots_dir: # Add header header_l = header_l + ['Graph'] # Other things plots_dir = os.path.expanduser(plots_dir) csv_report_file = plots_dir + '/' + os.path.basename(csv_report_file) # Make dirs os.makedirs(plots_dir, exist_ok=True) os.makedirs(plots_dir + g_graphs_dir, exist_ok=True) print('Plots dir = {}'.format(plots_dir)) # endif # Start scan if strategy_name == "ema_crossover": sec_list = [] ctr = 0 ctr2 = 0 # Add csv headers csv_rep_list.append(header_l) # Hyper parameters fast_period = get_arg(opt_args, 'ema_fast_period', 14) slow_period = get_arg(opt_args, 'ema_slow_period', 21) volume_lag = get_arg(opt_args, 'volume_lag', 10) opt_period = None if 'ema_opt_period' not in opt_args else opt_args[ 'ema_opt_period'] period_list = [fast_period, slow_period] period_list = period_list if opt_period == None else period_list + [ opt_period ] print('Running {} strategy using lag={} & period_list={}'.format( strategy_name, lag, period_list)) print( Fore.MAGENTA + 'Peak to trough percentage has meaning only when trend is down to up !!' + Fore.RESET) print( Fore.GREEN + '--------------------- GENERATING REPORT --------------------------------' + Fore.RESET) # Iterate over all security dict for sec_code in sec_dict.keys(): ctr2 = ctr2 + 1 logging.debug("{} : Running {} strategy over {}".format( ctr2, strategy_name, sec_code)) # NOTE: Don't know what the hell I am calculating using these. # They need to be reviewed def _c_up(d): return (d['c'].max() - dindx(d, 'c', -1)) / dindx(d, 'c', -1) # enddef def _c_dwn(d): return (dindx(d, 'c', -1) - d['c'].min()) / dindx(d, 'c', -1) # enddef def _vol_up(d, vol_lag=10): try: return (dindx(d, 'v_ma', -1) - dindx(d, 'v_ma', -1 - vol_lag)) / dindx( d, 'v_ma', -1 - vol_lag) except: return 0 # endif # enddef # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) # Run strategy logging.debug("{} : Running ema crossover function over {}".format( ctr2, sec_code)) status, tdelta, trend_switch, d_new = invs_scanners.run_ema2( d_this, fast_period=fast_period, slow_period=slow_period, lag=lag) # Add volume ma d_v_this = invs_scanners.add_vol_ma(d_this, period_list=period_list) # Analyse data p2t_up = _c_up(d_new) p2t_down = _c_dwn(d_new) vol_up = _vol_up(d_v_this, vol_lag=volume_lag) # Print reports if (status == True): row_this = [] # This row if trend_switch: t_swt = "Down2Up" t_switch = Fore.GREEN + "Down to Up" + Fore.RESET p2t = int(p2t_up * 100) else: t_swt = "Up2Down" t_switch = Fore.RED + "Up to Down" + Fore.RESET p2t = int(p2t_down * 100) # endif # Add rep list entry row_this = row_this + [ sec_dict[sec_code]['name'], t_swt, str(tdelta), str(p2t), dindx(d_this, 'c', -1), vol_up ] # Print sec_name = Fore.GREEN + sec_dict[sec_code]['name'] + Fore.RESET sys.stdout.write( '{}. {} switched trend from {}, {} days ago. Peak to trough % = {}%\n' .format(ctr, sec_name, t_switch, tdelta, p2t)) sys.stdout.flush() ctr = ctr + 1 # Save plot if plots_dir: pic_name = plots_dir + g_graphs_dir + sec_dict[sec_code][ 'name'].replace(' ', '_') + '_{}p.png'.format(plot_period) invs_plot.gen_candlestick(d_this, period_list=period_list, title=sec_dict[sec_code]['name'], plot_period=plot_period, file_name=pic_name) # Plot monthly chart if required if plot_monthly: d_mon = invs_core.fetch_data( sec_dict[sec_code]['ticker'], '1M') pic_name_mon = plots_dir + g_graphs_dir + sec_dict[ sec_code]['name'].replace( ' ', '_') + '_monthly_{}p.png'.format(plot_period) invs_plot.gen_candlestick( d_mon, title=sec_dict[sec_code]['name'], plot_period=plot_period, file_name=pic_name_mon) # endif # Add to csv row_this = row_this + [ '=HYPERLINK("file:///{}")'.format(pic_name) ] # endif # Append final row to list csv_rep_list.append(row_this) # endif # endfor elif strategy_name == "supertrend_rsi_long": ctr2 = 0 ctr2_f = 0 # Hyper parameters str_params = (get_arg(opt_args, 'supertrend_period', 10), get_arg(opt_args, 'supertrend_multiplier', 3)) # (Period, Multiplier) rsi_period = get_arg(opt_args, 'rsi_period', 14) sup_lperiod = get_arg(opt_args, 'supertrend_lookback_period', 100) print('Running {} strategy using lag={}'.format(strategy_name, lag)) print( Fore.GREEN + '--------------------- GENERATING REPORT --------------------------------' + Fore.RESET) # Iterate over all security dict for sec_code in sec_dict.keys(): logging.debug("{} : Running {} strategy over {}".format( ctr2, strategy_name, sec_code)) # NOTE: Don't know what the hell I am calculating using these. # They need to be reviewed # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) if len(d_this) > sup_lperiod: d_this = copy.copy(d_this[-sup_lperiod:]) else: d_this = d_this # endif # Run strategy d_new = invs_indicators.SuperTrend(d_this, str_params[0], str_params[1]) # This indicator behaves stragely sometimes. Add a check if d_new.empty: print( '** Returned empty dataframe for {} after applying Supertrend indicator. Skipping !!' .format(sec_code)) ctr2_f = ctr2_f + 1 continue # endif d_new['RSI'] = invs_indicators.talib.RSI(d_new['c'], rsi_period) d_new['RSI_l'] = [40.0] * len(d_new) d_new['RSI_h'] = [60.0] * len(d_new) logic = (dindx(d_new, 'SuperTrend', -1) < dindx( d_new, 'c', -1)) and (dindx(d_new, 'RSI', -1) > 60) if logic: print('[{:<3}/{:<3}]. {}'.format(ctr2, ctr2_f, sec_code)) ctr2 = ctr2 + 1 else: ctr2_f = ctr2_f + 1 continue # endif # Save plot if plots_dir: pic_name = plots_dir + g_graphs_dir + sec_dict[sec_code][ 'name'].replace(' ', '_') + '_{}p.png'.format(plot_period) invs_plot.gen_candlestick( d_new, period_list=[], title=sec_dict[sec_code]['name'], plot_period=plot_period, file_name=pic_name, plot_columns=['SuperTrend'], plot_columns_subplot=['RSI', 'RSI_l', 'RSI_h']) # endif # endfor elif strategy_name == "supertrend_rsi_short": ctr2 = 0 ctr2_f = 0 # Hyper parameters str_params = (get_arg(opt_args, 'supertrend_period', 10), get_arg(opt_args, 'supertrend_multiplier', 3)) # (Period, Multiplier) rsi_period = get_arg(opt_args, 'rsi_period', 14) sup_lperiod = get_arg(opt_args, 'supertrend_lookback_period', 100) print('Running {} strategy using lag={}'.format(strategy_name, lag)) print( Fore.GREEN + '--------------------- GENERATING REPORT --------------------------------' + Fore.RESET) # Iterate over all security dict for sec_code in sec_dict.keys(): logging.debug("{} : Running {} strategy over {}".format( ctr2, strategy_name, sec_code)) # NOTE: Don't know what the hell I am calculating using these. # They need to be reviewed # Fetch data d_this = invs_core.fetch_data(sec_dict[sec_code]['ticker'], res) if len(d_this) > sup_lperiod: d_this = copy.copy(d_this[-sup_lperiod:]) else: d_this = d_this # endif # Run strategy d_new = invs_indicators.SuperTrend(d_this, str_params[0], str_params[1]) # This indicator behaves stragely sometimes. Add a check if d_new.empty: print( '** Returned empty dataframe for {} after applying Supertrend indicator. Skipping !!' .format(sec_code)) ctr2_f = ctr2_f + 1 continue # endif d_new['RSI'] = invs_indicators.talib.RSI(d_new['c'], rsi_period) d_new['RSI_l'] = [40.0] * len(d_new) d_new['RSI_h'] = [60.0] * len(d_new) logic = (dindx(d_new, 'SuperTrend', -1) > dindx( d_new, 'c', -1)) and (dindx(d_new, 'RSI', -1) < 40) if logic: print('[{:<3}/{:<3}]. {}'.format(ctr2, ctr2_f, sec_code)) ctr2 = ctr2 + 1 else: ctr2_f = ctr2_f + 1 continue # endif # Save plot if plots_dir: pic_name = plots_dir + g_graphs_dir + sec_dict[sec_code][ 'name'].replace(' ', '_') + '_{}p.png'.format(plot_period) invs_plot.gen_candlestick( d_new, period_list=[], title=sec_dict[sec_code]['name'], plot_period=plot_period, file_name=pic_name, plot_columns=['SuperTrend'], plot_columns_subplot=['RSI', 'RSI_l', 'RSI_h']) # endif # endfor else: print("Strategy : {}, not implemented yet !!".format(strategy_name)) return # endif # Write to csv file print(Fore.GREEN + '--------------------- REPORT END --------------------------------' + Fore.RESET) print('Writing report file to {}'.format(csv_report_file)) with open(os.path.expanduser(csv_report_file), 'w') as f_out: csv_writer = csv.writer(f_out, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for item_this in csv_rep_list: csv_writer.writerow(item_this) # endfor # endwith return csv_report_file