# Stock Investors Financial Math # https://www.fmlabs.com/reference/default.htm?url=SimpleMA.htm # Alpha Vantage # https://www.alphavantage.co/documentation/ # Pandas # https://pandas.pydata.org/pandas-docs/stable/index.html # Knowledge # https://www.investopedia.com/articles/technical/02/050602.asp # https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1 import matplotlib.pyplot as plt import matplotlib as mpl mpl.rc('figure', max_open_warning = 0) from stock import Stock import argparse import json params = { 'analyze_range_days' : 5, 'plot_range_days' : 0, 'btfd' : {'thresh_max' : -7, 'thresh_min' : 1, 'cand_window' : 5}, 'k_euro' : 1 / 1.11, 'q_range_days' : 10, 'fetch_on_outdated' : True } show_symbols = [] parser = argparse.ArgumentParser() parser.add_argument('--symbol', help='Symbol to fetch') parser.add_argument('--analyze-range', type=int, help='Analyze window [days]') parser.add_argument('--plot-range', type=int, help='Plot range [days]') parser.add_argument('--q-range', type=int, help='Q range [days]') args = parser.parse_args() f = open('stocks.json', 'r') s = f.read() symbols = json.loads(s) f.close() if args.symbol is not None: sfc = args.symbol.split('|') symbol = sfc[0] show_symbols = [symbol] if symbol not in symbols: try: fullname = sfc[1] except: fullname = 'Enter full name here' try: currency = sfc[2] except: currency = 'USD' symbols[symbol] = {'name' : fullname, 'currency' : currency} s = json.dumps(symbols, sort_keys=True, indent=4) f = open('stocks.json', 'w') f.write(s) f.close() else: print ("{} is in database".format(symbol)) if args.analyze_range is not None: params['analyze_range_days'] = args.analyze_range if args.plot_range is not None: params['plot_range_days'] = args.plot_range if args.q_range is not None: params['q_range_days'] = args.q_range do_plot = params['plot_range_days'] > 0 buy_list = {} def buy_callback(data): name = data['name'] item = data['item'] if name not in buy_list: buy_list[name] = {'items': [item]} else: buy_list[name]['items'].append(item) print(data) figNum = 1 if len(show_symbols) > 0: for symbol in show_symbols: if symbol in symbols: title = Stock(params, symbol, symbols[symbol]) title.fetch() title.statistics() try: ind = title.analyze(buy_callback, range_days=params['analyze_range_days']) if do_plot: title.show(ind, figNum=figNum) figNum += 1 except: print ("Exception occurred for {}".format(symbol)) else: for symbol in symbols: title = Stock(params, symbol, symbols[symbol]) title.fetch() title.statistics() try: ind = title.analyze(buy_callback, range_days=params['analyze_range_days']) if Stock.has_candidate(ind, 'BTFD'): print('-----------------------------------------------') if do_plot: title.show(ind, figNum=figNum) figNum += 1 except: print ("Exception occurred for {}".format(symbol)) gain_accum = 0 num_stocks = 0 for symbol in buy_list: items = buy_list[symbol]['items'] title = Stock(params, symbol, symbols[symbol]) title.fetch() title.statistics() gain = title.get_latest('close_n') - items[0]['value'] gain_accum += gain num_stocks += 1 print ('{}: Gain = {} %'.format(symbol, gain)) if num_stocks > 0: print ('Gain total = {} %'.format(gain_accum/num_stocks)) if do_plot: plt.show()