def visualize_peak_log(logfile): """ Create a histogram of the log entry timestamps and save it as png """ _, timestamps, _ = np.loadtxt(logfile, unpack=True, delimiter=' ', converters={ 0: mdates.bytespdate2num("%Y-%m-%d"), 1: mdates.bytespdate2num("%H:%M:%S") }) if timestamps.size > 1: first_stamp = mdates.num2date(timestamps[0]) last_stamp = mdates.num2date(timestamps[-1]) bin_range = range(first_stamp.hour, last_stamp.hour + 2) date_bins = [] for h in bin_range: date_bins.append(datetime.datetime(1900, 1, 1, h, 0, 0, 0)) num_bins = mdates.date2num(date_bins) else: num_bins = 1 _, ax = plt.subplots(1, 1) ax.hist(timestamps, num_bins, rwidth=1, ec='k') ax.xaxis.set_major_locator(mdates.HourLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%H')) plt.title('Number of entires per hour, in total ' + str(timestamps.size)) plt.savefig(datetime.datetime.now().strftime("%Y-%m-%d") + '-summary.png') plt.show()
def graph_stock(): stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: split_line = line.split(',') if len(split_line) == 7: if 'Volume' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, adjclosep, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, # %Y =full year 2015 # %y = partial year 15 # %m = number month # %H number hours # %M number minutes # %S number seconds converters={0: mdates.bytespdate2num('%Y-%m-%d')}) plt.plot_date(date, closep, '-', label='Price') plt.xlabel('Date') plt.ylabel('Price') plt.title('Lect 8+9') plt.legend() plt.show()
def drawStockGraph(stock, date): try: #stockFile = 'C:/Zerodha/Pi/Exported/'+ stock stockFile = cbook.get_sample_data('C:/Zerodha/Pi/Exported/' + stock, asfileobj=False) print('loading', stockFile) date, open, high, low, close, volume = np.loadtxt( stockFile, delimiter=',', converters={0: bytespdate2num(date)}, skiprows=2, unpack=True) #bytespdate2num('%d-%b-%y')},skiprows=1, usecols=(0, 2), unpack=True # mdates.strpdate2num('%d-%m-%Y %H:%M:%S')} fig = plt.figure() ax1 = plt.subplot(1, 1, 1) ax1.plot(date, close) plt.ylabel('Stock PRice') ax1.grid(True) plt.subplots_adjust(left=.17) plt.show() except Exception, e: print str(e), " error"
def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) # Unfortunately, Yahoo's API is no longer available # feel free to adapt the code to another source, or use this drop-in replacement. stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: split_line = line.split(',') if len(split_line) == 7: if 'values' not in line: stock_data.append(line) # 注意np.loadtxt需要把所有格式都转化成float的格式,对于bytes的日期时间我们需要用到mdates # 中间的bytepdate2num date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y-%m-%d')}) x = 0 y = len(date) ohlc = [] while x < y: append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x] ohlc.append(append_me) x += 1 # 注意蜡烛图的参数传入的原则,是list,且每一个元素是一个tuple,tuple俩面的值全部为float candlestick_ohlc(ax1, ohlc, width=0.4, colorup='#77d879', colordown='#db3f3f') for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax1.grid(True) plt.xlabel('Date') plt.ylabel('Price') plt.title(stock) plt.legend() plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0) plt.show()
def printError(my_dpi, data): """ All data contanining the stock price info are retrieved from the database given the stock name :param my_dpi: dpi screen :param data: data to be plot :param max_error: maximum error allowed """ try: stockFile = [] try: for eachLine in data: splitLine = eachLine.split(',') if len(splitLine) == 2: if 'values' not in eachLine: stockFile.append(eachLine) except Exception as e: print(str(e), 'failed to organize pulled data.') except Exception as e: print(str(e), 'failed to pull pricing data') try: date, closep_raw = np.loadtxt( stockFile, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y%m%d')}) closep = closep_raw[::-1] max_error = 0.1 while max_error < float(2 * max(closep)): segments1 = segment.slidingwindowsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) segments2 = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error) err1 = str(evaluate_MSE(closep, segments1)) err4 = str(evaluate_MSE(closep, segments2)) segments1 = segment.topdownsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) segments2 = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error) err2 = str(evaluate_MSE(closep, segments1)) err5 = str(evaluate_MSE(closep, segments2)) segments1 = segment.bottomupsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) segments2 = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error) err3 = str(evaluate_MSE(closep, segments1)) err6 = str(evaluate_MSE(closep, segments2)) print( str(max_error) + " " + err1 + " " + err2 + " " + err3 + " " + err4 + " " + err5 + " " + err6) max_error += 0.1 except e: print("Error")
def graph_raw_fx(): date, bid, ask = np.loadtxt( "GBPUSD1d.txt", unpack=True, delimiter=",", converters={0: mdates.bytespdate2num("%Y%m%d%H%M%S")}) # define figure fig = plt.figure(figsize=(10, 7)) ax1 = plt.subplot2grid(shape=(40, 40), loc=(0, 0), rowspan=40, colspan=40) # plot data #ax1.plot(date, bid) #ax1.plot(date, ask) # format x-axis so that it is in date format. ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M:%S")) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1_2 = ax1.twinx() ax1_2.fill_between(date, 0, (ask - bid), facecolor="g", alpha=.3) plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.subplots_adjust(bottom=.23) # add additional space below date ticks plt.grid(True)
def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) # Unfortunately, Yahoo's API is no longer available # feel free to adapt the code to another source, or use this drop-in replacement. stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: split_line = line.split(',') if len(split_line) == 7: if 'values' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y-%m-%d')}) x, y, ohlc = 0, len(date), [] while x < y: append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x] ohlc.append(append_me) x += 1 candlestick_ohlc(ax1, ohlc, width=0.4, colorup='g', colordown='r') for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax1.grid(True) # annotation example with arror ax1.annotate('You are here!', (date[9], highp[9]), xytext=(0.8, 0.9), textcoords='axes fraction', arrowprops=dict(facecolor='grey', color='grey')) # font dic example font_dict = {'family': 'sherif', 'color': 'darkred', 'size': 15} # hard coded text ax1.text(date[4410], closep[1], 'Text Example', fontdict=font_dict) plt.xlabel('Date') plt.ylabel('Price') plt.title(stock) # plt.legend() plt.subplots_adjust(left=0.09, bottom=0.2, right=0.94, top=0.9, wspace=0.2, hspace=0) plt.show()
def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=1, colspan=1) plt.title(stock) ax2 = plt.subplot2grid((6, 1), (1, 0), rowspan=4, colspan=1) plt.xlabel('Date') plt.ylabel('Price') ax3 = plt.subplot2grid((6, 1), (5, 0), rowspan=1, colspan=1) # Unfortunately, Yahoo's API is no longer available # feel free to adapt the code to another source, or use this drop-in replacement. stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: split_line = line.split(',') if len(split_line) == 7: if 'values' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y-%m-%d')}) x, y, ohlc = 0, len(date), [] while x < y: append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x] ohlc.append(append_me) x += 1 candlestick_ohlc(ax2, ohlc, width=0.4, colorup='g', colordown='r') for label in ax2.xaxis.get_ticklabels(): label.set_rotation(45) ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax2.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax2.grid(True) bbox_props = dict(boxstyle='larrow', fc='w', ec='k', lw=1) ax2.annotate(str(closep[-1]), (date[0], closep[-1]), xytext=(date[0] + 400, closep[-1]), bbox=bbox_props) # plt.legend() plt.subplots_adjust(left=0.11, bottom=0.24, right=0.86, top=0.9, wspace=0.2, hspace=0) plt.show()
def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) # Unfortunately, Yahoo's API is no longer available # feel free to adapt the code to another source, or use this drop-in replacement. stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: split_line = line.split(',') if len(split_line) == 7: if 'values' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y-%m-%d')}) ax1.plot_date(date, closep, '-', label='Price') ax1.plot([], [], lw=5, label='loss', color='r', alpha=0.5) ax1.plot([], [], lw=5, label='gain', color='g', alpha=0.5) ax1.fill_between(date, closep, closep[0], where=(closep > closep[0]), facecolor='g', alpha=0.5) ax1.fill_between(date, closep, closep[0], where=(closep < closep[0]), facecolor='r', alpha=0.5) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.grid(True) ax1.xaxis.label.set_color('c') ax1.yaxis.label.set_color('r') ax1.set_yticks([i for i in range(0, 800, 50)]) plt.xlabel('Date') plt.ylabel('Price') plt.title(stock) plt.legend() plt.subplots_adjust(left=0.09, bottom=0.2, right=0.94, top=0.9, wspace=0.2, hspace=0) plt.show()
def graph_data(): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) plt.ylabel('Price') plt.xlabel('Date') print('Currently viewing:') url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(url).read().decode() stock_data = [] split_source = source_code.split('\n') for each_line in split_source: split_line = each_line.split(',') if 'Date' not in each_line: stock_data.append(each_line) date, openp, highp, lowp, closep, adjusted_close, volume = np.loadtxt(stock_data, delimiter = ',', \ unpack = True, converters={0: mdates.bytespdate2num('%Y-%m-%d')}) x = 0 y = len(date) new_list = [] while x < y: append_line = date[x], openp[x], highp[x], lowp[x], closep[x], volume[ x] new_list.append(append_line) x += 1 candlestick_ohlc(ax1, new_list, width=.6, colorup='#41ad49', colordown='#ff1717') ax1.grid(True) #ax1.grid(True, color = 'g', linestyle='-', linewidth=3) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ##ax1.annotate('Oil Spill!!', (date[25], highp[25]), ## xytext=(0.8, 0.9), textcoords='axes fraction', ## arrowprops=dict(facecolor="#585858", color="#585858")) bbox_props = dict(boxstyle='round4, pad=0.3', fc="y", ec='k', lw=2) ax1.annotate(str(closep[-1]), (date[-1], closep[-1]), xytext=(date[-1] + 5, closep[-1]), bbox=bbox_props) plt.ylabel('Price') plt.subplots_adjust(left=.11, bottom=.26, right=.90, top=.95, wspace=.2, hspace=.2) plt.show()
def graph_stock(): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: split_line = line.split(',') if len(split_line) == 7: if 'Volume' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, adjclosep, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y-%m-%d')}) ax1.plot_date(date, closep, '-', label='Price') ax1.fill_between(date, closep, closep[0], where=(closep > closep[0]), facecolor='g', alpha=0.5) ax1.fill_between(date, closep, closep[0], where=(closep < closep[0]), facecolor='r', alpha=0.5) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.grid(True) #,color='g',linestyle='-') ax1.xaxis.label.set_color('c') ax1.yaxis.label.set_color('r') ax1.set_yticks([0, 25, 50, 75]) plt.xlabel('Date') plt.ylabel('Price') plt.title('Lect 10') plt.legend() plt.subplots_adjust(left=0.09, bottom=0.18, right=0.94, top=0.90, wspace=0.2, hspace=0) plt.show()
def ols(self): #stock, indice,stockDate,indiceDate try: indiceFile = cbook.get_sample_data( 'C:/Zerodha/Pi/Exported/NiftyIT.csv', asfileobj=False) print('loading', indiceFile) date_x, openp_x, highp_x, lowp_x, closep_x, volume_x = np.loadtxt( indiceFile, delimiter=',', skiprows=2, unpack=True, usecols=(0, 1, 2, 3, 4, 5), converters={0: bytespdate2num('%d-%m-%Y %H:%M:%S')}) stockFile_b = cbook.get_sample_data( 'C:/Zerodha/Pi/Exported/HCLTECH-EQ.csv', asfileobj=False) print('loading', stockFile_b) date_y, openp_y, highp_y, lowp_y, closep_y, volume_y = np.loadtxt( stockFile_b, delimiter=',', skiprows=2, unpack=True, usecols=(0, 1, 2, 3, 4, 5), converters={0: bytespdate2num('%d-%m-%Y %H:%M')}) alpha, beta = self.linreg(closep_x, closep_y) print 'alpha: ' + str(alpha) print 'beta: ' + str(beta) X2 = np.linspace(closep_x.min(), closep_x.max(), 20) Y_hat = X2 * beta + alpha plt.scatter(closep_x, closep_y, alpha=0.3) plt.ylabel("Stock PRice") plt.xlabel("Indice") # Add the regression line, colored in red plt.plot(X2, Y_hat, 'r', alpha=0.9) plt.show() except Exception, e: print str(e)
def load_data(path): if os.path.isdir(path) == False: print("Not dir") return # pattern_match = PatternMatch() pattern_match = {} for f in os.listdir(path): full_path = os.path.join(path, f) # if False == os.path.isfile(full_path): # continue if ('SH' != f[:2]) and ('SZ' != f[:2]): continue # print(f) input_file = open(full_path) lines = input_file.readlines() if (5 + StockPattern.PATTERN_LEN + StockPattern.OUTCOME_RANGE) > len(lines): print(f + " 's data is too small. " + repr(len(lines))) continue lines = lines[4:-1] input_file.close() # lines = lines[:-10] # back two weeks stock_code = f[3:-4] # print("Add " + repr(stock_code) + ". LEN: " + repr(len(lines))) date, op, hi, lo, cl, vol, trans = np.loadtxt( lines, unpack=True, # delimiter='\t', converters={0: mdates.bytespdate2num('%Y/%m/%d')}, ) stock_item_list = [] for i in range(len(date)): stock_item_list.append( StockItem(date[i], op[i], hi[i], lo[i], cl[i], vol[i], trans[i])) # stock_item_list.reverse() # print(stock_code + repr(stock_item_list[0])) # stock_pattern = StockPattern(stock_code, stock_item_list) # pattern_match.stock_list.append(stock_pattern) pattern_match[stock_code] = stock_item_list # print("Add "+stock_code) return pattern_match
def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1,1),(0,0)) stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: stock_data.append(line) date, openp, highp, lowp, closep, adjusted_close, volume = np.loadtxt(stock_data, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y-%m-%d')}) x= 0 y = 70 #len(date) ohlc = [] while x < y: append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x] ohlc.append(append_me) x += 1 candlestick_ohlc(ax1, ohlc, width=0.4, colorup='g', colordown='r') for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.xaxis.set_major_locator(mticker.MaxNLocator(5)) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax1.grid(True) ax1.annotate('Bad News!', (date[9], highp[9]), xytext=(0.8, 0.9), textcoords='axes fraction', arrowprops=dict(facecolor='grey', color='grey')) font_dict = {'family' : 'serif', 'color' : 'darkred', 'size' : 15} ax1.text(date[10], closep[1], 'Text Example', fontdict = font_dict) plt.xlabel('Date') plt.ylabel('Price') plt.title('Practicing Candle light') #plt.legend() plt.subplots_adjust(left=0.09, right=0.94, bottom=0.20, top=0.90, wspace=0.2, hspace=0) plt.show()
def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: stock_data.append(line) date, openp, highp, lowp, closep, adjusted_close, volume = np.loadtxt( stock_data, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y-%m-%d')}) x = 0 y = 25 #len(date) ohlc = [] while x < y: append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x] ohlc.append(append_me) x += 1 candlestick_ohlc(ax1, ohlc, width=0.4, colorup='g', colordown='r') for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.xaxis.set_major_locator(mticker.MaxNLocator(5)) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax1.grid(True) plt.xlabel('Date') plt.ylabel('Price') plt.title('Practicing Candle light') plt.legend() plt.subplots_adjust(left=0.09, right=0.94, bottom=0.20, top=0.90, wspace=0.2, hspace=0) plt.show()
def graphRawFx(): date, bid, ask = np.loadtxt( './GBPUSD/GBPUSD1d.txt', unpack=True, delimiter=',', converters={0: mdates.bytespdate2num('%Y%m%d%H%M%S')}) fig = plt.figure(figsize=(10, 7)) ax1 = plt.subplot2grid((40, 40), (0, 0), rowspan=40, colspan=40) ax1.plot(date, bid) ax1.plot(date, ask) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S')) plt.grid(True) plt.show()
def graph_data(stock): fig = plt.figure() stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source[1:]: stock_data.append(line) date, openp, highp, lowp, closep, adjusted_close, volume = np.loadtxt(stock_data, delimiter=',', unpack=True, converters={ 0: mdates.bytespdate2num('%Y-%m-%d')}) x = 0 y = 22 # len(date) ohlc = [] while x < y: append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x] ohlc.append(append_me) x += 1 ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=1, colspan=1) plt.title(stock) ax2 = plt.subplot2grid((6, 1), (1, 0), rowspan=4, colspan=1) plt.xlabel('Date') plt.ylabel('Price') ax3 = plt.subplot2grid((6, 1), (5, 0), rowspan=1, colspan=1) candlestick_ohlc(ax2, ohlc, width=0.4, colorup='#77d879', colordown='#db3f3f') for label in ax2.xaxis.get_ticklabels(): label.set_rotation(45) ax2.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax2.grid(True) # bbox_props = dict(boxstyle='round', fc='w', ec='k', lw=1) # ax1.annotate(str(closep[-1]), (date[-1], closep[-1]), # xytext=(date[-1] + 3, closep[-1]), bbox=bbox_props) plt.subplots_adjust(left=0.11, bottom=0.24, right=0.87, top=0.90, wspace=0.2, hspace=0) plt.show()
def read_weather(file_name): dtypes = np.dtype({ 'names': ('date', 'max temp', 'min temp'), 'formats': ['S9', np.float, np.float] }) dates, max_temp, min_temp = np.loadtxt( file_name, delimiter=',', skiprows=1, usecols=(0, 1, 2), dtype=dtypes, unpack=True, converters={0: dt.bytespdate2num('%Y-%m-%d')}) return dates, max_temp, min_temp
def graphRawFX(): (date, bid, ask) = np.loadtxt('GBPUSD1d.txt', unpack=True, delimiter=',', converters={0: mdates.bytespdate2num('%Y%m%d%H%M%S')}) fig = plt.figure(figsize=(10, 7)) ax1 = plt.subplot2grid((40, 40), (0, 0), rowspan=40, colspan=40) ax1.plot(date, bid) ax1.plot(date, ask) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) plt.subplots_adjust(bottom=.23) plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.grid(True) plt.show()
def graphRawFX(): c=str('GBPUSD1d.txt') date,bid,ask = np.loadtxt(c, unpack=True, delimiter=',',converters={0:mdates.bytespdate2num('%Y%m%d%H%M%S')})#,dtype={'names': ('gender', 'age', 'weight'),'formats': ('S1', 'i4', 'f4')})#converters = #{0:mdates.datestr2num('%w')}) #dtype=(float,int,int))#,converters={0:mdates.strpdate2num('%Y%m%d%H%M%S')}) print(date) fig=plt.figure(figsize=(10,7)) ax1 = plt.subplot2grid((40,40), (0,0), rowspan=40, colspan=40) ax1.plot(date,bid) ax1.plot(date,ask) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) plt.subplots_adjust(bottom=.23) plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.grid(True) plt.show()
def unpack(source): # For datetime: when charting more than 1d if 'values:Date' in source: print("Date found") return np.loadtxt(stock_data, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y%m%d')}) # For Unix time codes: when charting 1d elif 'values:Time' in source: print("Time found") date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data, delimiter=',', unpack=True) date_conv = np.vectorize(dt.datetime.fromtimestamp) date = date_conv(date) return date, closep, highp, lowp, openp, volume else: return "Date or Time not found"
def calAcf(stock, guiDate): stockFile = cbook.get_sample_data('C:/Zerodha/Pi/Exported/' + stock, asfileobj=False) print('loading', stockFile) date, openp, highp, lowp, closep, volume = np.loadtxt( stockFile, delimiter=',', skiprows=2, unpack=True, usecols=(0, 1, 2, 3, 4, 5), converters={0: bytespdate2num(guiDate)}) closep_acf = acf(closep, unbiased=True, nlags=40) closep_pacf = pacf(closep, nlags=40) closep_ar = AR(20) #draw plt.plot(closep_ar, 'ro') plt.xlabel('Lag') plt.ylabel('Close PRice ACF ') plt.show()
def calculateCT(self, stock, guiDate): try: stockFile = cbook.get_sample_data('C:/Zerodha/Pi/Exported/' + stock, asfileobj=False) print('loading', stockFile) date, openp, highp, lowp, closep, volume = np.loadtxt( stockFile, delimiter=',', skiprows=2, unpack=True, usecols=(0, 1, 2, 3, 4, 5), converters={0: bytespdate2num(guiDate)}) self.ols() print centralTendency.artmeticMean(closep) #candlestickChart(stock,date,openp,highp,lowp,closep,volume) except Exception, e: print str(e)
def animate(i): datetime, y = np.loadtxt( open(input_file, 'r'), delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y-%m-%d %H:%M:%S EST')}, usecols=[0, 3]) ax1.clear() ax1.plot_date(datetime, y, 'g-') ax1.xaxis_date() ax1.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d\n%H:%M:%S')) plt.subplots_adjust(bottom=.3) locs, labels = plt.xticks() plt.setp(labels, rotation=70) plt.title("Pedestrian Data") plt.ylabel("# of Pedestrians In Park") plt.xlabel("Date Time") plt.tight_layout() plt.grid(True)
def graph_data(stock): stock_price_url='https://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv' source_code=urllib.request.urlopen(stock_price_url).read() stock_data=[] split_source=source_code.split('\n') for line in split_source: split_line=line.split(',') if len(split_line)==6: if 'values' not in line and 'labels' not in line: stock_data.append(line) date,closep,highp,lowp,openp,volume=np.loadtxt(stock_data, delimiter=',', unpack=True, #%Y = full year. 2015 #%y = partial yr. 15 #%m = month number #%d = day number #%H = hours #%M = minutes #%S = seconds #%m-%d-%Y = 12-06-2014 converters={0:bytespdate2num('%Y%m%d')}) # matplot lib doesnt use unix time, it has its own conversion
""" ============== Load Converter ============== """ import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook import matplotlib.dates as mdates from matplotlib.dates import bytespdate2num datafile = cbook.get_sample_data('msft.csv', asfileobj=False) print('loading', datafile) dates, closes = np.loadtxt(datafile, delimiter=',', converters={0: bytespdate2num('%d-%b-%y')}, skiprows=1, usecols=(0, 2), unpack=True) fig, ax = plt.subplots() ax.plot_date(dates, closes, '-') fig.autofmt_xdate() plt.show()
""" ============== Load Converter ============== """ import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook from matplotlib.dates import bytespdate2num datafile = cbook.get_sample_data('msft.csv', asfileobj=False) print('loading', datafile) dates, closes = np.loadtxt(datafile, delimiter=',', converters={0: bytespdate2num('%d-%b-%y')}, skiprows=1, usecols=(0, 2), unpack=True) fig, ax = plt.subplots() ax.plot_date(dates, closes, '-') fig.autofmt_xdate() plt.show()
import numpy as np import matplotlib.dates as mdates from settings import * date, bid, ask = np.loadtxt( file_name, unpack=True, delimiter=',', converters={0: mdates.bytespdate2num('%Y%m%d%H%M%S')}) data_length = int(bid.shape[0]) # Creates an array with the average values between the bid and the ask all_data = ((bid + ask) / 2) accuracy_array = []
def draw_window_API(my_dpi, max_error, stockToFetch): """ All data contanining the stock price info are retrieved from the database given the stock name :param my_dpi: dpi screen :param data: data to be plot :param max_error: maximum error allowed """ fig = plt.figure(figsize=(1000 / my_dpi, 700 / my_dpi), dpi=96, edgecolor='k', facecolor='black') fig.suptitle("PIECEWISE SEGMENTATION INTERPOLATION", fontsize="15", color="white", fontweight='bold', bbox={ 'facecolor': 'red', 'alpha': 0.5, 'pad': 10 }) try: print('Currently Pulling', stockToFetch) urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + stockToFetch + '/chartdata;type=quote;range=5y/csv' stockFile = [] try: sourceCode = urllib.request.urlopen(urlToVisit).read().decode() splitSource = sourceCode.split('\n') for eachLine in splitSource: splitLine = eachLine.split(',') if len(splitLine) == 6: if 'values' not in eachLine: stockFile.append(eachLine) except Exception as e: print(str(e), 'failed to organize pulled data.') except Exception as e: print(str(e), 'failed to pull pricing data') try: date, closep, highp, lowp, openp, volume = np.loadtxt( stockFile, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y%m%d')}) SP = len(date) # First subplot ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) segments = segment.slidingwindowsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax1, "Sliding window with interpolation") draw_segments(segments, 'red') plt.ylabel('Stock Price') plt.title("Sliding window", color='w') # Second subplot ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3) segments = segment.topdownsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax2, "Sliding window with interpolation") draw_segments(segments, 'green') plt.ylabel('Stock Price') plt.title("Top down", color='w') # Third subplot ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3) segments = segment.bottomupsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax3, "Sliding window with interpolation") draw_segments(segments, 'blue') plt.ylabel('Stock Price') plt.title("Bottom up", color='w') plt.subplots_adjust(hspace=0.3) plt.show() except e: print("Error")
def draw_window(my_dpi, data, max_error): """ All data contanining the stock price info are retrieved from the database given the stock name :param my_dpi: dpi screen :param data: data to be plot :param max_error: maximum error allowed """ fig = plt.figure(figsize=(1000 / my_dpi, 700 / my_dpi), dpi=96, facecolor='black') fig.suptitle("PIECEWISE SEGMENTATION INTERPOLATION", fontsize="15", color="white", fontweight='bold', bbox={ 'facecolor': 'red', 'alpha': 0.5, 'pad': 10 }) try: stockFile = [] try: for eachLine in data: splitLine = eachLine.split(',') if len(splitLine) == 2: if 'values' not in eachLine: stockFile.append(eachLine) except Exception as e: print(str(e), 'failed to organize pulled data.') except Exception as e: print(str(e), 'failed to pull pricing data') try: date, closep_raw = np.loadtxt( stockFile, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y%m%d')}) closep = closep_raw[::-1] print(max(closep)) max_error = max(closep) * 2.5 # First subplot ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) segments = segment.slidingwindowsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax1, "Sliding window with interpolation") draw_segments(segments, 'red') plt.ylabel('Stock Price') plt.title("SLIDING WINDOW - ERROR " + str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold') # Second subplot ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3) segments = segment.topdownsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax2, "Sliding window with interpolation") draw_segments(segments, 'green') plt.ylabel('Stock Price') plt.title("TOP DOWN - ERROR " + str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold') # Third subplot ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3) segments = segment.bottomupsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) segments = segment.bottomupsegment(closep, fit.interpolate, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax3, "Sliding window with interpolation") draw_segments(segments, 'blue') plt.ylabel('Stock Price') plt.title("BOTTOM UP - ERROR " + str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold') plt.subplots_adjust(hspace=0.3) plt.show() except e: print("Error")
def graph_data(): fig = plt.figure() ax1 = plt.subplot2grid((1, 1), (0, 0)) plt.ylabel('Price') plt.xlabel('Date') print('Currently viewing:') url = 'https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(url).read().decode() stock_data = [] split_source = source_code.split('\n') for each_line in split_source: split_line = each_line.split(',') if 'Date' not in each_line: stock_data.append(each_line) date, openp, highp, lowp, closep, adjusted_close, volume = np.loadtxt(stock_data, delimiter = ',', \ unpack = True, converters={0: mdates.bytespdate2num('%Y-%m-%d')}) """ #date, openp, highp, lowp, closep, adjusted_close, volume = np.loadtxt(stock_data, delimiter = ',', \ # unpack = True) """ """ # date_conv = np.vectorize(dt.datetime.fromtimestamp) # date = date_conv(date) """ x = 0 y = len(date) new_list = [] while x < y: append_line = date[x], openp[x], highp[x], lowp[x], closep[x], volume[ x] new_list.append(append_line) x += 1 """ #ax1.plot(date, closep, '-') #ax1.fill_between(date, closep, 400, where=(closep >= 400), facecolor='g', alpha=0.5) #ax1.fill_between(date, closep, 200, where=(closep <= 200), facecolor='r', alpha=0.5) #ax1.axhline(200, color='r') #ax1.axhline(400, color='g') #ax1.fill_between(date, closep, 0, alpha=0.5, edgecolor='r') #ax1.fill_between(date, closep, 0, alpha=0.5) """ #candlestick_ohlc(ax1, new_list) #candlestick_ohlc(ax1, new_list, width=.6, colorup='g', colordown='r') candlestick_ohlc(ax1, new_list, width=.6, colorup='#41ad49', colordown='#ff1717') ax1.grid(True) #ax1.grid(True, color = 'g', linestyle='-', linewidth=3) """ ax1.yaxis.label.set_color('m') ax1.xaxis.label.set_color('c') ax1.set_yticks([0, 700, 1400]) ax1.spines['left'].set_color('c') ax1.spines['bottom'].set_color('c') ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) ax1.spines['left'].set_linewidth(8) ax1.spines['bottom'].set_linewidth(8) """ for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) plt.ylabel('Price') plt.subplots_adjust(left=.09, bottom=.26, right=.94, top=.95, wspace=.2, hspace=.2) plt.show()
def draw_window(my_dpi, data): """ All data contanining the stock price info are retrieved from the database given the stock name :param my_dpi: dpi screen :param data: data to be plot :param max_error: maximum error allowed """ fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, facecolor='black') fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10}) try: stockFile = [] try: for eachLine in data: splitLine = eachLine.split(',') if len(splitLine) == 2: if 'values' not in eachLine: stockFile.append(eachLine) except Exception as e: print(str(e), 'failed to organize pulled data.') except Exception as e: print(str(e), 'failed to pull pricing data') try: date, closep_raw = np.loadtxt(stockFile, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y%m%d')}) closep = closep_raw[::-1] max_closep = max(closep) if(max_closep > 2.0): max_error = max(closep)*2.7; else: max_error = max(closep)/2.5; print(max_error) # First subplot ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error) draw_plot(closep,plt,ax1,"Sliding window with regression") draw_segments(segments,'red') plt.ylabel('Stock Price') plt.title("SLIDING WINDOW - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold') # Second subplot ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3) segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax2, "Sliding window with regression") draw_segments(segments,'green') plt.ylabel('Stock Price') plt.title("TOP DOWN - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold') # Third subplot ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3) segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax3, "Sliding window with regression") draw_segments(segments,'blue') plt.ylabel('Stock Price') plt.title("BOTTOM UP - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold') plt.subplots_adjust(hspace=0.3) plt.show() except e: print("Error")
def draw_window_API(my_dpi, max_error, stockToFetch): """ All data contanining the stock price info are retrieved from the database given the stock name :param my_dpi: dpi screen :param data: data to be plot :param max_error: maximum error allowed """ fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, edgecolor='k', facecolor='black') fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10}) try: print('Currently Pulling',stockToFetch) urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stockToFetch+'/chartdata;type=quote;range=5y/csv' stockFile =[] try: sourceCode = urllib.request.urlopen(urlToVisit).read().decode() splitSource = sourceCode.split('\n') for eachLine in splitSource: splitLine = eachLine.split(',') if len(splitLine) == 6: if 'values' not in eachLine: stockFile.append(eachLine) except Exception as e: print(str(e), 'failed to organize pulled data.') except Exception as e: print(str(e), 'failed to pull pricing data') try: date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=',', unpack=True, converters={0: mdates.bytespdate2num('%Y%m%d')}) SP = len(date) # First subplot ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error) draw_plot(closep,plt,ax1,"Sliding window with regression") draw_segments(segments,'red') plt.ylabel('Stock Price') plt.title("Sliding window", color='w') # Second subplot ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3) segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax2, "Sliding window with regression") draw_segments(segments,'green') plt.ylabel('Stock Price') plt.title("Top down", color='w') # Third subplot ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3) segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error) draw_plot(closep, plt, ax3, "Sliding window with regression") draw_segments(segments,'blue') plt.ylabel('Stock Price') plt.title("Bottom up", color='w') plt.subplots_adjust(hspace=0.3) plt.show() except e: print("Error")
import pylab from datetime import datetime import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook import matplotlib.dates as mdates from matplotlib.dates import bytespdate2num datafile = cbook.get_sample_data("/home/polaris/PycharmProjects/WeatherData/tempData.dat", asfileobj=False) print("loading", datafile) #datefunc = lambda x:mdates.date2num(datetime.strptime(x,'%Y-%m-%d')) dates, temps = np.loadtxt(datafile, delimiter=',', dtype=float, converters={0: bytespdate2num('%Y-%m-%d %I:%M %p' )}, usecols=(0,1), unpack=True) fig = plt.figure() #1x1 grid ax = fig.add_subplot(111) ax.set_xticks(dates) ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M')) ax.set_title('Temperature in Porvoo') ax.set_ylabel('Temp') ax.set_xlabel('Date') ax.plot_date(dates,temps,'-', marker='o') ax.grid(True) fig.autofmt_xdate(rotation=45) plt.show()