#!/usr/bin/python3.6 import pandas as pd import pandas_datareader.data import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib.ticker as ticker from mpl_finance import candlestick_ohlc from mpl_finance import candlestick2_ohlc from pandas.plotting import register_matplotlib_converters register_matplotlib_converters() import datetime import cgi import os, random, re form = cgi.FieldStorage() select_day = "" if form.getvalue("select-day"): select_day = form.getvalue("select-day") m_select_day = re.match("([0-9]{4})-([0-9]{2})-([0-9]{2})", select_day) if m_select_day: year = m_select_day.groups()[0] month = m_select_day.groups()[1] day = m_select_day.groups()[2] chart_type = form.getvalue("chart_type") if chart_type == "line": today_s = datetime.date.today().strftime('%Y%m%d') #df = pd.read_csv('futures.' + today_s + '.txt', names=['Time', 'HSIF', 'Volume'], delim_whitespace=True) df = pd.read_csv('/root/futures/' + year + '-' + month + '/futures.' + year + month + day + '.txt', names=['Time', 'HSIF', 'Volume'], delim_whitespace=True) #df = pd.read_csv('/root/futures/2019-05/futures-sample.txt', names=['Time', 'HSIF', 'Volume'], delim_whitespace=True) df['Time'] = pd.to_datetime(df['Time']) # RSI window_length = 14 df['Delta'] = df['HSIF'].diff().shift(0) df['DeltaUp'] = df['Delta'].apply(lambda x: x if x > 0 else 0) df['DeltaDown'] = df['Delta'].apply(lambda x: -x if x < 0 else 0) df['RollUp'] = df['DeltaUp'].rolling(window_length).mean() df['RollDown'] = df['DeltaDown'].rolling(window_length).mean() df['RS'] = df['RollUp'] / df['RollDown'] df['RSI'] = 100.0 - (100.0 / (1.0 + df['RS'])) pd.set_option('display.max_rows', 10000) #df = df.set_index('Time') #print(df) df = df[df['Volume']>0] df = df.reset_index(drop=True) N = len(df) def format_date(x, pos=None): thisind = np.clip(int(x+0.5), 0, N-1) return df.Time[thisind].strftime('%H:%M') fig , ax1 = plt.subplots() ax1.set_title(year + month + day + ' HSIF Chart - by Patrick Chan') ax1.set_xlim([0,N+3]) ax1.set_xlabel('Time') ax1.set_ylabel('HSIF', color='blue') ax1.plot(np.arange(len(df)),df['HSIF'], color='blue') ax1.tick_params(axis='y', labelcolor='blue') ax1.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) ax1.xaxis.set_major_locator(ticker.FixedLocator([15,45,75,105,135,166,197,227,257,287,317,347,377])) ax1.grid(True) #ax2= ax1.twinx() #ax2.set_ylabel('RSI', color='orchid') #ax2.plot(np.arange(len(df)),df['RSI'], color='orchid') #ax2.tick_params(axis='y', labelcolor='orchid') fig = ax1.get_figure() fig.set_size_inches(8, 5) fig.savefig('/var/www/html/chart.png', dpi=100) elif chart_type == "candlestick": pd.set_option('display.max_rows', 10000) df = pd.read_csv('/root/futures/' + year + '-' + month + '/futures.' + year + month + day + '.txt', names=['Time', 'HSIF', 'Volume'], delim_whitespace=True) df['Time'] = pd.to_datetime(df['Time'], format='%Y/%m/%d-%H:%M') df = df[df['Volume']>0] df_ohlc = df.set_index('Time')['HSIF'].resample('5T').ohlc() df_ohlc = df_ohlc[pd.notnull(df_ohlc['open'])] df_ohlc.reset_index(inplace=True) #print(df_ohlc) xtime = [ i.strftime('%H:%M') for i in df_ohlc['Time'] ] #print(xtime) df_ohlc['Time'] = df_ohlc['Time'].apply(mdates.date2num) #print(df_ohlc) def mydate(x, pos): return xtime[pos*6] N = len(df_ohlc) fig, ax = plt.subplots() #candlestick_ohlc(ax, df_ohlc.values, width=5/(24*60), colorup='g', colordown='r') candlestick2_ohlc(ax, df_ohlc['open'], df_ohlc['high'], df_ohlc['low'], df_ohlc['close'], width=.6, colorup='g', colordown='r') ax.set_title(year + month + day + ' HSIF Chart - by Patrick Chan') ax.set_xlim([0,N+3]) ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate)) ax.xaxis.set_major_locator(ticker.FixedLocator(np.arange(0,len(df_ohlc),6))) #ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) #ax.xaxis_date() ax.grid(True) fig.set_size_inches(8,5) fig.savefig('/var/www/html/chart.png', dpi=100) # OS Walk path = '/root/futures' available_days = [] for root,dirs,files in os.walk(path): for name in files: m = re.match(r"futures\.([0-9]{8})\.txt", name) if m: available_day = m.groups()[0] available_day_formatted = available_day[0:4] + '-' + available_day[4:6] + '-' + available_day[6:8] available_days.append(available_day_formatted) available_days = sorted(available_days, reverse=True) # Print HTML print("Content-type:text/html\r\n\r\n") print('''
''') print('') print('''
   CandleStick Line

''')