def cmd_macd(self, res, out_func=print): try: s = self.stocks[res['s']] except KeyError: print('Wrong stock name or the stock has not been added') raise CommandNotExecuted('') k_list = list(res.keys()) ws = 12 wb = 26 p = 9 if '-ws' in k_list: ws = res['-ws'] if '-wb' in k_list: wb = res['-wb'] if '-ap' in k_list: p = res['-ap'] if ws > wb: out_func("Illegal value of ws and wb") raise CommandNotExecuted('macd') k, style = s.get_macd_indicator(wins=ws, winb=wb, p=p) if '-p' in k_list: p = mp.Process(target=Stock.plot_macd, args=(k, )) p.start() if '-e' in k_list: with open(res['-e'], 'w') as f: f.write(k.to_csv(line_terminator='\n'))
def cmd_add_stock(self, res, out_func=print): k_list = list(res.keys()) if '-sn' in k_list: stock_name = res['-sn'] else: stock_name = input(self.input_string + "Enter the stock name: ") if '-tr' in k_list: stock_tracker = res['-tr'] else: stock_tracker = input(self.input_string + "Enter the stock tracker: ") if '-sec' in k_list: stock_sector = res['-sec'] else: stock_sector = "Null" try: out_func("Fetching stock details") stock = Stock(stock_name, stock_tracker, sector=stock_sector) except IndexError: out_func("Tracker name is wrong. Exiting") raise CommandNotExecuted('add') except (gaierror, NewConnectionError, MaxRetryError, ConnectionError): out_func("Connection error. Exiting") raise CommandNotExecuted('add') stock.fill_hist_data(self.start, self.end) self.stocks[stock_name] = stock out_func(stock) out_func("Stock added")
def cmd_sar(self, res, out_func=print): try: s = self.stocks[res['s']] except KeyError: print('Wrong stock name or the stock has not been added') raise CommandNotExecuted('') k_list = list(res.keys()) afi = 0.02 afl = 0.2 if '-afi' in k_list: afi = res['-afi'] if '-afl' in k_list: afl = res['-afl'] if afi > afl: out_func("Illegal value of afi and afl") raise CommandNotExecuted('sar') k, style = s.get_parabolic_sar_indicator(af_const=afi, af_max_const=afl) if '-p' in k_list: p = mp.Process(target=Stock.plot_sar, args=(k, style)) p.start() if '-e' in k_list: with open(res['-e'], 'w') as f: f.write(k.to_csv(line_terminator='\n'))
def cmd_ichimoku(self, res): try: s = self.stocks[res['s']] except KeyError: print('Wrong stock name or the stock has not been added') raise CommandNotExecuted('') k_list = list(res.keys()) kp = 26 tp = 9 cp = -26 sp = 26 if '-kp' in k_list: kp = res['-kp'] if '-tp' in k_list: tp = res['-tp'] if '-cp' in k_list: cp = -1 * cp if '-sp' in k_list: sp = res['-sp'] k, style = s.get_ichimoku_kinko_hyo_indicator(kp=kp, tp=tp, cp=cp, sp=sp) if '-p' in k_list: p = mp.Process(target=Stock.plot_ichimoku_kinko_hyo, args=(k, style)) p.start() if '-e' in k_list: with open(res['-e'], 'w') as f: f.write(k.to_csv(line_terminator='\n'))
def cmd_update_stocks(self, res, out_func=print): try: if len(res) == 0: self.update_stock_details(force=True, out_func=out_func) else: for v in res.values(): out_func("Updating: " + self.stocks[v].get_name()) self.stocks[v].fill_hist_data(self.start, self.end) except (NewConnectionError, gaierror, MaxRetryError, ConnectionError, AttributeError): print('Connection cannot be established') raise CommandNotExecuted('update_stocks')
def cmd_start_script(self, res, out_func=print): try: with open(res['-fn'], 'r') as f: for line in f: line = line.strip(' ') line = line.strip('\t') line = line.strip('\n') line = line.replace('\t', ' ') if line != '': if '-v' in res: out_func(self.input_string + line) try: self.cmd_line_cont = self.execute_command(line) except CommandNotExecuted as e: print(e) break if not self.cmd_line_cont: break except FileNotFoundError: out_func('The file not found') raise CommandNotExecuted('start_script') except UnicodeDecodeError: out_func('The data in the file is corrupted') raise CommandNotExecuted('start_script')
def cmd_import_data(self, res): try: with open(res['-fn'], 'rb') as f: ex = dict(pk.load(f)) for k, v in ex['metadata'].items(): self.__setattr__(k, v) self.stocks = dict() for sts in ex['data'].values(): tmp = Stock(sts['name'], sts['tracker']) tmp.hist_data = sts['hist_data'] self.stocks[tmp.get_name()] = tmp self.load_dependent_values() except FileNotFoundError: print("The data file not found") raise CommandNotExecuted('import_data')
def cmd_rsi(self, res): try: s = self.stocks[res['s']] except KeyError: print('Wrong stock name or the stock has not been added') raise CommandNotExecuted('') k_list = list(res.keys()) win = 13 if '-w' in k_list: win = res['-w'] k, style = s.get_rsi_indicator(win=win) if '-p' in k_list: p = mp.Process(target=Stock.plot_rsi, args=(k, )) p.start() if '-e' in k_list: with open(res['-e'], 'w') as f: f.write(k.to_csv(line_terminator='\n'))
def cmd_stochastic(self, res): try: s = self.stocks[res['s']] except KeyError: print('Wrong stock name or the stock has not been added') raise CommandNotExecuted('') k_list = list(res.keys()) kp = 14 dp = 3 if '-kp' in k_list: kp = res['-kp'] if '-dp' in k_list: dp = res['-dp'] k, style = s.get_stochastic_indicator(kp=kp, dp=dp) if '-p' in k_list: p = mp.Process(target=Stock.plot_stochastic, args=(k, )) p.start() if '-e' in k_list: with open(res['-e'], 'w') as f: f.write(k.to_csv(line_terminator='\n'))
def cmd_moving_average(self, res): try: s = self.stocks[res['s']] except KeyError: print('Wrong stock name or the stock has not been added') raise CommandNotExecuted('') k_list = list(res.keys()) win1 = 20 win2 = 100 if '-w1' in k_list: win1 = res['-w1'] if '-w2' in k_list: win2 = res['-w2'] k, style = s.get_moving_average_indicator(win1, win2) if '-p' in k_list: p = mp.Process(target=Stock.plot_moving_average, args=(k, win1, win2)) p.start() if '-e' in k_list: with open(res['-e'], 'w') as f: f.write(k.to_csv(line_terminator='\n'))