def insider(l_args, s_ticker): parser = argparse.ArgumentParser( prog='insider', description= """Prints information about inside traders. The following fields are expected: Date, Relationship, Transaction, #Shares, Cost, Value ($), #Shares Total, Insider Trading, SEC Form 4. [Source: Finviz]""") parser.add_argument('-n', "--num", action="store", dest="n_num", type=check_positive, default=10, help='number of latest inside traders.') (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) if l_unknown_args: print( f"The following args couldn't be interpreted: {l_unknown_args}\n") return d_finviz_insider = finviz.get_insider(s_ticker) df_fa = pd.DataFrame.from_dict(d_finviz_insider) df_fa.set_index("Date", inplace=True) df_fa = df_fa[[ 'Relationship', 'Transaction', '#Shares', 'Cost', 'Value ($)', '#Shares Total', 'Insider Trading', 'SEC Form 4' ]] print(df_fa.head(n=ns_parser.n_num)) print("")
def get_insider(ticker): try: insider = finviz.get_insider(ticker) insider = pd.DataFrame(insider) insider = insider.set_index('Date') return insider except Exception as e: return e
def insider(other_args: List[str], ticker: str): """Display insider activity for a given stock ticker Parameters ---------- other_args : List[str] argparse other args - ["-n", "10"] ticker : str Stock ticker """ parser = argparse.ArgumentParser( add_help=False, prog="insider", description=""" Prints information about inside traders. The following fields are expected: Date, Relationship, Transaction, #Shares, Cost, Value ($), #Shares Total, Insider Trading, SEC Form 4. [Source: Finviz] """, ) parser.add_argument( "-n", "--num", action="store", dest="n_num", type=check_positive, default=10, help="number of latest inside traders.", ) try: ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return d_finviz_insider = finviz.get_insider(ticker) df_fa = pd.DataFrame.from_dict(d_finviz_insider) df_fa.set_index("Date", inplace=True) df_fa = df_fa[[ "Relationship", "Transaction", "#Shares", "Cost", "Value ($)", "#Shares Total", "Insider Trading", "SEC Form 4", ]] print(df_fa.head(n=ns_parser.n_num)) print("") except Exception as e: print(e) print("") return
def get_last_insider_activity(ticker: str) -> Dict: """Get last insider activity for a given stock ticker. [Source: Finviz] Parameters ---------- ticker : str Stock ticker Dict Latest insider trading activity """ return finviz.get_insider(ticker)
def insider(l_args, s_ticker): parser = argparse.ArgumentParser( prog="insider", description=""" Prints information about inside traders. The following fields are expected: Date, Relationship, Transaction, #Shares, Cost, Value ($), #Shares Total, Insider Trading, SEC Form 4. [Source: Finviz] """, ) parser.add_argument( "-n", "--num", action="store", dest="n_num", type=check_positive, default=10, help="number of latest inside traders.", ) try: ns_parser = parse_known_args_and_warn(parser, l_args) d_finviz_insider = finviz.get_insider(s_ticker) df_fa = pd.DataFrame.from_dict(d_finviz_insider) df_fa.set_index("Date", inplace=True) df_fa = df_fa[[ "Relationship", "Transaction", "#Shares", "Cost", "Value ($)", "#Shares Total", "Insider Trading", "SEC Form 4", ]] print(df_fa.head(n=ns_parser.n_num)) print("") except Exception as e: print(e) print("") return
def data_layer(sp_100_file, data_path, perc_space, perc_time): sp_100_df = pd.read_csv(sp_100_file) simbols = sp_100_df.Symbol.values[:-1] map_month2month_number = dict( (v, k) for k, v in enumerate(calendar.month_abbr)) stock = {} cont = 0 for simbol in simbols: try: insider_info = fz.get_insider(simbol) insider_data = pd.DataFrame.from_dict(insider_info) insider_data[["month_name", "day"]] = insider_data.Date.str.split(" ", expand=True) insider_data["month"] = insider_data["month_name"].map( map_month2month_number) insider_data["year"] = insider_data["month"].apply( infer_year(datetime.datetime.now().month, datetime.datetime.now().year)) insider_data["date"] = pd.to_datetime( insider_data[["year", "month", "day"]], format="%y%m%d") cont = cont + 1 except Exception as inst: print(simbol, end=" ") print(type(inst), end=" ") # the exception instance print(inst) continue curr_ticker = yf.Ticker(simbol) curr_hist = curr_ticker.history(period="1y") curr_table = pd.pivot_table(insider_data, index=['date'], columns=['Transaction'], aggfunc={"Transaction": len}) curr_hist["volatility"] = (curr_hist.High - curr_hist.Low) / ( curr_hist.Open + np.finfo(float).eps) curr_hist["change_price_beg_end"] = curr_hist.Open - curr_hist.Close curr_hist["change_price"] = curr_hist.High - curr_hist.Low curr_hist[ "Volume_rel_price"] = curr_hist.Volume * curr_hist["change_price"] curr_hist[ "Vol_rel_volatility"] = curr_hist.Volume * curr_hist["volatility"] curr_table = ~curr_table.isnull() curr_table.columns = [col[1] for col in curr_table.columns] curr_hist = curr_hist.merge(curr_table, how="left", left_index=True, right_index=True) curr_hist = curr_hist.fillna(False) if "Buy" not in curr_hist.columns: curr_hist["Buy"] = False if "Sale" not in curr_hist.columns: curr_hist["Sale"] = False if "Option Exercise" not in curr_hist.columns: curr_hist["Option Exercise"] = False stock[simbol] = curr_hist n_train_space = int(len(stock.keys()) * perc_space) n_train_out_space = len(stock.keys()) - n_train_space train_space = np.random.choice(list(stock.keys()), size=n_train_space, replace=False).tolist() test_space = list(set(stock.keys()) - set(train_space)) return stock