From 76731eecebf3d9af4f77327eea28ca52331fbc91 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Tue, 7 Jan 2020 20:00:46 +0000 Subject: [PATCH] - store stocks in JSON file - use convenient command line params git-svn-id: http://moon:8086/svn/projects/Stock@348 fda53097-d464-4ada-af97-ba876c37ca34 --- robot.py | 146 +++++++++++++++---------------------------------------- stock.py | 6 +-- 2 files changed, 41 insertions(+), 111 deletions(-) diff --git a/robot.py b/robot.py index fc18877..3cae37d 100644 --- a/robot.py +++ b/robot.py @@ -11,17 +11,17 @@ # https://www.investopedia.com/articles/technical/02/050602.asp # https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1 -do_plot = 1 - -if do_plot: - import matplotlib.pyplot as plt - import matplotlib as mpl - mpl.rc('figure', max_open_warning = 0) +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 = { - 'show_range_days' : 10, + 'analyze_range_days' : 5, + 'plot_range_days' : 0, 'btfd' : {'thresh_max' : -7, 'thresh_min' : 1, 'cand_window' : 5}, 'k_euro' : 1 / 1.11, 'ema_alpha' : 0.75, @@ -30,108 +30,38 @@ params = { 'fetch_on_outdated' : True } -show_symbols = ['OHB.DE', 'ITMPF', 'PLUG', 'MOR.DE', 'CSCO', 'ERCA.DE', 'AVGO', 'DIS', 'UBSFF', 'DHER.DE', 'AIR'] -show_symbols = ['CSCO'] -#show_symbols = ['PCELF'] -#show_symbols = ['OHB.DE'] -#show_symbols = ['UBSFF'] -#show_symbols = ['DHER.DE'] -#show_symbols = ['WDI.DE'] -#show_symbols = ['EVT.DE'] show_symbols = [] -symbols = { - 'KEP' : {'name' : 'Korea Electric Power Corporation', 'currency' : '$'}, - 'MRK.DE' : {'name' : 'Merck', 'currency' : '€'}, - 'TOM.F' : {'name' : 'Toyota Motor Corporation', 'currency' : '€'}, - 'SSHPF' : {'name' : 'Scanship Holding ASA', 'currency' : '$'}, - 'RIG' : {'name' : 'Transocean Ltd.', 'currency' : '$'}, - 'BBIO' : {'name' : 'BridgeBio Pharma, Inc.', 'currency' : '$'}, - 'APA' : {'name' : 'Apache Corporation', 'currency' : '$'}, - 'CBB-PB' : {'name' : 'Cincinnati Bell Inc.', 'currency' : '$'}, - 'NMHLY' : {'name' : 'NMC Health Plc', 'currency' : '$'}, - 'PINS' : {'name' : 'Pinterest', 'currency' : '$'}, - 'TSLA' : {'name' : 'Tesla Inc.', 'currency' : '$'}, - 'ACB' : {'name' : 'Aurora Cannabis Inc.', 'currency' : '$'}, - 'ITCI' : {'name' : 'Intra-Cellular Therapies', 'currency' : '$'}, - 'WDI.DE' : {'name' : 'WireCard', 'currency' : '€'}, - 'AAPL' : {'name' : 'Apple', 'currency' : '$'}, - 'XLNX' : {'name' : 'Xilinx', 'currency' : '$'}, - 'QCOM' : {'name' : 'Qualcomm', 'currency' : '$'}, - 'DPW.DE' : {'name' : 'Deutsche Post', 'currency' : '€'}, - 'CSCO' : {'name' : 'Cisco', 'currency' : '$'}, - 'AIR' : {'name' : 'Airbus', 'currency' : '$'}, - 'BA' : {'name' : 'Boeing', 'currency' : '$'}, - 'NVDA' : {'name' : 'Nvidia', 'currency' : '$'}, - 'MSFT' : {'name' : 'Microsoft', 'currency' : '$'}, - 'DIS' : {'name' : 'Disney', 'currency' : '$'}, - 'NFLX' : {'name' : 'Netflix', 'currency' : '$'}, - 'OHB.DE' : {'name' : 'OHB', 'currency' : '€'}, - 'ERCA.DE' : {'name' : 'Ericsson', 'currency' : '€'}, - 'VAR1.DE' : {'name' : 'Varta', 'currency' : '€'}, - 'HD' : {'name' : 'Home Depot', 'currency' : '$'}, - 'AMZN' : {'name' : 'Amazon', 'currency' : '$'}, - 'GOOGL' : {'name' : 'Google', 'currency' : '$'}, - 'FB2A.DE' : {'name': 'Facebook Inc.', 'currency' : '€'}, - 'ZIL2.DE' : {'name': 'ElringKlinger', 'currency' : '€'}, - 'SIS.DE' : {'name': 'First Sensor', 'currency' : '€'}, - 'SIE.DE' : {'name': 'Siemens', 'currency' : '€'}, - 'GFT.DE' : {'name': 'GFT Technologies', 'currency' : '€'}, - 'AMD.DE' : {'name': 'Advanced Micro Devices', 'currency' : '€'}, - 'CAP.DE' : {'name': 'Encavis', 'currency' : '€'}, - 'ADBE' : {'name': 'Adobe', 'currency' : '$'}, - 'PANW' : {'name': 'Palo Alto Networks', 'currency' : '$'}, - 'AMAT' : {'name': 'Applied Materials', 'currency' : '$'}, - 'RIB.DE' : {'name': 'RIB Software', 'currency' : '€'}, - 'WAF.DE' : {'name': 'Siltronic', 'currency' : '€'}, - 'EVT.DE' : {'name': 'Evotec', 'currency' : '€'}, - 'VOW.DE' : {'name': 'Volkswagen', 'currency' : '€'}, - 'BMW.DE' : {'name': 'BMW', 'currency' : '€'}, - 'NSU.DE' : {'name': 'Audi', 'currency' : '€'}, - 'DAI.DE' : {'name': 'Daimler', 'currency' : '€'}, - 'SHA.DE' : {'name': 'Schaeffler', 'currency' : '€'}, - 'CON.DE' : {'name': 'Continental', 'currency' : '€'}, - 'DHER.DE' : {'name': 'Delivery Hero', 'currency' : '€'}, - 'BSL.DE' : {'name': 'Basler', 'currency' : '€'}, - 'D6H.DE' : {'name': 'DATAGROUP', 'currency' : '€'}, - 'TC1.DE' : {'name': 'Tele Columbus', 'currency' : '€'}, - 'VODI.DE' : {'name': 'Vodaphone Group', 'currency' : '€'}, - 'ATVI' : {'name': 'Activision Blizzard', 'currency' : '$'}, - 'GME' : {'name': 'GameStop', 'currency' : '$'}, - 'NTO.F' : {'name': 'Nintendo', 'currency' : '$'}, - 'UBSFF' : {'name': 'UBI Soft', 'currency' : '$'}, - 'NXPRF' : {'name': 'Nexans', 'currency' : '$'}, -# 'IMPUF' : {'name': 'Impala Platinum Holdings', 'currency' : '$'}, - 'NMPNF' : {'name': 'Northam Platinum', 'currency' : '$'}, - 'ALSMY' : {'name': 'Alstom', 'currency' : '$'}, - 'ARRD.F' : {'name': 'Arcelor Mittal', 'currency' : '$'}, - 'PHG' : {'name': 'Philips', 'currency' : '$'}, - 'KEYS' : {'name': 'KeySight', 'currency' : '$'}, - 'ORCL' : {'name': 'Oracle', 'currency' : '$'}, - 'UBER' : {'name': 'Uber', 'currency' : '$'}, - 'VMW' : {'name': 'VMWare', 'currency' : '$'}, - 'AVGO' : {'name' : 'Avago', 'currency' : '$'}, - 'CY' : {'name': 'Cypress Semiconductor', 'currency' : '$'}, - 'QABSY' : {'name': 'Qanta Airways', 'currency' : '$'}, - 'BLDP' : {'name': 'Ballard Power', 'currency' : '$'}, - 'D7G.F' : {'name': 'Nel ASA', 'currency' : '$'}, - 'ITMPF' : {'name': 'ITM Power', 'currency' : '$'}, - 'PLUG' : {'name': 'PlugPower', 'currency' : '$'}, - 'PCELF' : {'name': 'PowerCell', 'currency' : '$'}, - 'SU.PA' : {'name': 'Schneider Electric', 'currency' : '$'}, -# 'SON1.DE' : {'name': '', 'currency' : '€'}, - 'STM.DE' : {'name': 'STM Micro', 'currency' : '€'}, - 'BC8.DE' : {'name': 'Bechtle', 'currency' : '€'}, - 'MOR.DE' : {'name' : 'Morphosys', 'currency' : '€'}, - 'BAYN.DE' : {'name' : 'Bayer', 'currency' : '€'}, - 'BEI.DE' : {'name' : 'Beiersdorf', 'currency' : '€'}, - 'EUZ.DE' : {'name': 'Eckert & Ziegler', 'currency' : '€'}, - 'SLAB' : {'name': 'Silicon Laboratories', 'currency' : '$'}, - 'SPLK' : {'name' : 'Splunk', 'currency' : '$'}, - 'IAG' : {'name' : 'IAG', 'currency' : '$'}, - 'INTC' : {'name' : 'Intel Corporation', 'currency' : '$'}, - 'I' : {'name': 'IntelSat', 'currency' : '$'}} +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]') +args = parser.parse_args() + +f = open('stocks.json', 'r') +s = f.read() +symbols = json.loads(s) +f.close() + +if args.symbol is not None: + show_symbols = [args.symbol] + if args.symbol not in symbols: + symbols[args.symbol] = {'name' : 'Full name here', 'currency' : '$'} + s = json.dumps(symbols, sort_keys=True, indent=4) + f = open('stocks.json', 'w') + f.write(s) + f.close() + else: + print ("{} is already in database".format(args.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 + +do_plot = params['plot_range_days'] > 0 buy_list = {} def buy_callback(data): name = data['name'] @@ -151,7 +81,7 @@ if len(show_symbols) > 0: title.fetch() title.statistics() try: - ind = title.analyze(buy_callback, range_days=params['show_range_days']) + ind = title.analyze(buy_callback, range_days=params['analyze_range_days']) if do_plot: title.show(ind, figNum=figNum) figNum += 1 @@ -164,7 +94,7 @@ else: title.fetch() title.statistics() try: - ind = title.analyze(buy_callback, range_days=params['show_range_days']) + ind = title.analyze(buy_callback, range_days=params['analyze_range_days']) if Stock.has_candidate(ind, 'BTFD'): print('-----------------------------------------------') if do_plot: diff --git a/stock.py b/stock.py index 2313b06..43b5f36 100644 --- a/stock.py +++ b/stock.py @@ -24,7 +24,7 @@ class Stock(object): self.fig = None self.currency = symbol_params['currency'] self.currency_corr = 1 - if '$' in self.currency: + if 'USD' in self.currency: self.currency_corr = self.params['k_euro'] self.data = {} @@ -135,7 +135,7 @@ class Stock(object): def __plot(self, id, data, keys): ax = plt.subplot(id) N = len(data['index']) - start = max(0, N - self.params['show_range_days']) + start = max(0, N - self.params['plot_range_days']) stop = N xr = list(range(start, stop)) sliced = Stock.slice(data, start, stop) @@ -153,7 +153,7 @@ class Stock(object): def __ind(self, data, keys): N = len(data['index']) - start = max(0, N - self.params['show_range_days']) + start = max(0, N - self.params['plot_range_days']) stop = N xr = list(range(start, stop)) sliced = Stock.slice(data, start, stop)