def cal_plot_cci(ticker, period): print("Calculating CCI...") stock_data = stock.stock_preprocess_arr_list(ticker) cci = talib.CCI(stock_data[3], stock_data[4], stock_data[2], timeperiod=int(period)) ##### plotting fig, ax_list = plt.subplots(2, 1, figsize=(15,15)) name = stock.check_all_ticker(ticker)[0] plt.suptitle('Commodity Channel Index {}({})'.format(name, ticker), fontsize = 20, fontweight='bold') ax_list[0].plot(stock_data[0], stock_data[2], label='Price - '+name, color='black') ax_list[0].get_xaxis().set_visible(False) ax_list[1].plot(stock_data[0], cci, label='CCI - '+period+'-day period', color='#BB9F6D') ax_list[1].axhline(y=100, color='red') ax_list[1].axhline(y=-100, color='red') for i in range(2): ax_list[i].grid(True) ax_list[i].legend() ax_list[i].minorticks_on() ax_list[i].tick_params(axis='x',which='minor',bottom='off') fig.tight_layout() fig.subplots_adjust(hspace=0, top=0.95) #plt.show() stock.save_plot('CCI', ticker)
def cal_plot_psar(ticker, step=0.02, max_step=0.2): print("Calculating Parabolic SAR...") stock_data = stock.stock_preprocess_arr_list(ticker) psar = talib.SAR(stock_data[3], stock_data[4], acceleration=step, maximum=max_step) ##### plotting fig, ax = plt.subplots(1, 1, figsize=(15, 15)) name = stock.check_all_ticker(ticker)[0] plt.suptitle('Parabolic Stop and Reverse of {}({})'.format(name, ticker), fontsize=20, fontweight='bold') ax.plot(stock_data[0], stock_data[2], label='Price - ' + name, color='black') ax.plot(stock_data[0], psar, '.', label='Parabloic SAR', color='#708EB2') ax.grid(True) ax.legend() ax.minorticks_on() ax.tick_params(axis='x', which='minor', bottom='off') fig.tight_layout() fig.subplots_adjust(top=0.95) #plt.show() stock.save_plot('PSAR', ticker)
def get_client_tickers(cus_id): id = 'scb_' + cus_id customers_df = pd.read_csv(mp.DIR_DATA_CUSTOMERS + 'customer_trade_book.csv') customers_df = customers_df[['ID', 'Symbol', 'Portfolio_Value']] customers_df = customers_df[customers_df.ID == id] customers_df = customers_df[customers_df.Portfolio_Value > 0] ticker_list = list(set(customers_df['Symbol'])) if len(ticker_list) > 0: ticker_list = [(4 - len(str(tk))) * '0' + str(tk) + '.HK' for tk in ticker_list] remove_tk_list = [] print("Customer {} has the following equities in his/her portfolio:". format(id)) for i, tk in enumerate(sorted(ticker_list)): found = stock.check_all_ticker(tk) if found[1] == True: print(str(i + 1) + ')', tk + ' - ' + found[0]) else: print(str(i + 1) + ')', found[0]) remove_tk_list.append(tk) for tk in remove_tk_list: ticker_list.remove(tk) print("\n") return ticker_list else: return None
def cal_plot_rsi(ticker): print("Calculating RSI...") stock_data = stock.stock_preprocess_arr_list(ticker) name = stock.check_all_ticker(ticker)[0] rsi = talib.RSI(stock_data[2], timeperiod=14) ##### plotting fig, ax_list = plt.subplots(2, 1, figsize=(15,15)) plt.suptitle('Relative Strength Index of {}({})'.format(name, ticker), fontsize = 20, fontweight='bold') ax_list[0].plot(stock_data[0], stock_data[2], label='Price - '+name, color='black') ax_list[1].plot(stock_data[0], rsi, label='RSI', color='#A139B3') ax_list[1].fill_between(stock_data[0], 70, 30, color='#DED3E5') ax_list[0].get_xaxis().set_visible(False) for i in range(2): ax_list[i].legend() ax_list[i].grid(True) ax_list[i].minorticks_on() ax_list[i].tick_params(axis='x',which='minor',bottom='off') ax_list[1].xaxis.set_major_locator(mdates.MonthLocator()) ax_list[1].xaxis.set_major_formatter(mdates.DateFormatter('%y-%b')) fig.tight_layout() fig.subplots_adjust(hspace=0, top=0.95) #plt.show() stock.save_plot('RSI', ticker)
def cal_plot_obv(ticker): print("Calculating OBV...") stock_data = stock.stock_preprocess_arr_list(ticker) obv = talib.OBV(stock_data[2], stock_data[5].astype(float)) ##### plotting fig, ax_list = plt.subplots(2, 1, figsize=(15, 15)) name = stock.check_all_ticker(ticker)[0] plt.suptitle('On-Balance Volume {}({})'.format(name, ticker), fontsize=20, fontweight='bold') ax_list[0].plot(stock_data[0], stock_data[2], label='Price - ' + name, color='black') ax_list[0].get_xaxis().set_visible(False) ax_list[1].plot(stock_data[0], obv, label='OBV', color='#BB9F6D') for i in range(2): ax_list[i].grid(True) ax_list[i].legend() ax_list[i].minorticks_on() ax_list[i].tick_params(axis='x', which='minor', bottom='off') fig.tight_layout() fig.subplots_adjust(hspace=0, top=0.95) #plt.show() stock.save_plot('OBV', ticker)
def cal_plot_bband(ticker): print("Calculating B-Bands...") stock_data = stock.stock_preprocess_candlestick(ticker) upper, middle, lower = talib.BBANDS(stock_data[2], matype=talib.MA_Type.SMA) ##### plotting fig, ax_list = plt.subplots(2, 1, figsize=(15, 15)) plt.suptitle('Bollinger Bands of {}({})'.format( stock.check_all_ticker(ticker)[0], ticker), fontsize=20, fontweight='bold') candlestick_ochl(ax_list[0], stock_data[0], width=0.8, colorup='#53B987', colordown='#EB4D5C') ax_list[0].xaxis.set_major_locator(mdates.MonthLocator()) ax_list[0].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b-%d')) ax_list[0].get_xaxis().set_visible(False) volume_overlay(ax_list[1], *stock_data[1:4], width=0.8, colorup='#53B987', colordown='#EB4D5C') #ax_list[1].xaxis.set_major_locator(mdates.MonthLocator()) #ax_list[1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b-%d')) ax_list[1].set_xticks(range(0, len(stock_data[4]), 30)) ax_list[1].set_xticklabels(stock_data[4][::30]) ax_list[0].plot(stock_data[4], upper, color='#85C0C0') ax_list[0].plot(stock_data[4], middle, label='20-SMA', color='#AC7878') ax_list[0].plot(stock_data[4], lower, color='#85C0C0') ax_list[0].fill_between(stock_data[4], upper, lower, color='#F3F9F9') for i in range(2): ax_list[i].minorticks_on() ax_list[i].tick_params(axis='x', which='minor', bottom='off') ax_list[0].legend() plt.setp(plt.gca().get_xticklabels()) fig.tight_layout() fig.subplots_adjust(hspace=0, top=0.95) #plt.show() stock.save_plot('BBANDS', ticker)
def plot_HSI_MACD(arr_date, close_prices, avg_fast, avg_slow, macd, macd_signal, histogram, ticker, fast=12, slow=26, signal=9, type='EMA'): fig, ax_list = plt.subplots(2, 2, figsize=(15,10)) if ticker == 'HSI': plt.suptitle('MACD-related plots of {}'.format(ticker), fontsize = 20, fontweight='bold') else: plt.suptitle('MACD-related plots of {}({})'.format(stock.check_all_ticker(ticker)[0], ticker), fontsize = 20, fontweight='bold') ax_list[0][0].set_title('{}-days and {}-days {}'.format(fast, slow, type), fontstyle='italic') ax_list[0][0].plot(arr_date, close_prices, label='^HSI', color='black') ax_list[0][0].plot(arr_date, avg_fast, label=str(fast)+'-days(fast)') ax_list[0][0].plot(arr_date, avg_slow, label=str(slow)+'-days(slow)') ax_list[0][1].set_title('MACD/DIF (EMA)', fontstyle='italic') ax_list[0][1].plot(arr_date, macd, label='{}-days - {}-days'.format(fast, slow), color='xkcd:purple') ax_list[0][1].axhline(y=0, color='red') ax_list[1][0].set_title('MACD/DIF & {}day-Signal'.format(signal), fontstyle='italic') ax_list[1][0].plot(arr_date, macd, label='MACD/DIF', color='xkcd:purple') ax_list[1][0].plot(arr_date, macd_signal, label='Signal', color='xkcd:orange') ax_list[1][0].fill_between(arr_date, histogram, label='MACD/DIF - Signal', color='gray') ax_list[1][0].axhline(y=0, color='red') ax_list[1][1].set_title('MACD Histogram', fontstyle='italic') ax_list[1][1].fill_between(arr_date, histogram, label='MACD/DIF - Signal', color='C9') ax_list[1][0].axhline(y=0, color='red') for row in range(len(ax_list)): for col in range(len(ax_list[0])): ax_list[row][col].legend() ax_list[row][col].grid(True) ax_list[row][col].minorticks_on() ax_list[row][col].tick_params(axis='x',which='minor',bottom='off') ax_list[row][col].xaxis.set_major_locator(mdates.MonthLocator()) ax_list[row][col].xaxis.set_major_formatter(mdates.DateFormatter('%y-%b')) for major_tick in ax_list[row][col].xaxis.get_ticklabels(): major_tick.set_rotation(40) plt.tight_layout() # prevent overlapping fig.subplots_adjust(top=0.9) #plt.show() stock.save_plot('MACD', ticker)
def cal_plot_kdj(ticker): print("Calculating KDJ...") stock_data = stock.stock_preprocess_arr_list(ticker) k, d = talib.STOCH(stock_data[3], stock_data[4], stock_data[2], fastk_period=14, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) j = 3 * k - 2 * d ##### plotting fig, ax_list = plt.subplots(2, 1, figsize=(15, 15)) name = stock.check_all_ticker(ticker)[0] plt.suptitle('Stochastic with J Line of {}({})'.format(name, ticker), fontsize=20, fontweight='bold') ax_list[0].plot(stock_data[0], stock_data[2], label='Price - ' + name, color='black') ax_list[0].get_xaxis().set_visible(False) ax_list[1].plot(stock_data[0], k, label='%K', color='#DC9F86') ax_list[1].plot(stock_data[0], d, label='%D', color='#13F90D') ax_list[1].plot(stock_data[0], j, label='%J', color='#E06FC4') for i in range(2): ax_list[i].grid(True) ax_list[i].legend() ax_list[i].minorticks_on() ax_list[i].tick_params(axis='x', which='minor', bottom='off') fig.tight_layout() fig.subplots_adjust(hspace=0, top=0.95) #plt.show() stock.save_plot('KDJ', ticker)