def _add_stock(self, lines): """ get lines's information and create new Stock adding in stock_collection :param lines: list :return: Stock """ try: stock_code = lines[0][0] for line in lines: # get every line's data if line[0] != stock_code: raise RuntimeError("invalid format!") if line[1] == 'DA': date = line[2] elif line[1] == 'OP': opening_price = line[2] elif line[1] == 'HI': high_price = line[2] elif line[1] == 'LO': low_price = line[2] elif line[1] == 'CL': closing_price = line[2] elif line[1] == 'VO': volume = line[2] trading_data = stocks.TradingData(date, float(opening_price), float(high_price), float(low_price), float(closing_price), int(volume)) except Exception as e: raise RuntimeError("invalid format!") stock = self._stocks.get_stock(stock_code) stock.add_day_data(trading_data)
def _process(self, file): """ Process the loaded CSV file, get the trading data and add it into appropriate stocks object and an instance of StockCollection Class. Parameter (file): Read the file line-by-line and extract the data. Assumptions: Dates are valid if they contain exactly 8 numerical characters. Prices need only be valid non-negative floats, and volume must be a positive integer. """ for line in file: line = line.strip() try: stock_code, date, opening_price, high_price, low_price, closing_price, volume = line.split( ",") stock = self._stocks.get_stock(stock_code) # Trading data Object day_data = stocks.TradingData(date, float(opening_price), float(high_price), float(low_price), float(closing_price), int(volume)) except Exception: raise RuntimeError stock.add_day_data( day_data) # Add trading data into appropriate stock object
def add_stock(self, data_list): """ Get data list information and create new Stock adding in stock_collection Parameters: data_list: Load and parse the stock market data from function _process. """ try: stock_code = data_list[0][0] for line in data_list: # get every list's data if line[0] != stock_code: raise RuntimeError("invalid format!") if line[1] == 'DA': date = line[2] elif line[1] == 'OP': opening_price = line[2] elif line[1] == 'HI': high_price = line[2] elif line[1] == 'LO': low_price = line[2] elif line[1] == 'CL': closing_price = line[2] elif line[1] == 'VO': volume = line[2] trading_data = stocks.TradingData(date, float(opening_price), float(high_price), float(low_price), float(closing_price), int(volume)) except Exception as e: raise RuntimeError("invalid format!") stock = self._stocks.get_stock(stock_code) stock.add_day_data(trading_data)
def _process(self, file): """Iterate through the file, extracting the data from a line""" stocks_list = [] try: for line in file: line = line.strip() stocks_list = line.split(":") code = stocks_list[0] if stocks_list[1] == "DA": DA = stocks_list[2] elif stocks_list[1] == "OP": OP = stocks_list[2] elif stocks_list[1] == "HI": HI = stocks_list[2] elif stocks_list[1] == "LO": LO = stocks_list[2] elif stocks_list[1] == "CL": CL = stocks_list[2] elif stocks_list[1] == "VO": VO = stocks_list[2] trading_data = stocks.TradingData(DA, float(OP), float(HI), float(LO), float(CL), int(VO)) single_stock = self._stocks.get_stock(code) single_stock.add_day_data(trading_data) except IndexError: raise RuntimeError
def __init__(self, delta): self._delta = delta self._date = '' self._flag = 0 self._closing_price = 0 self._opening_price = 0 self._stock = stocks.TradingData("", 0, 0, 0, 0, 0)
def __init__(self, filename, load_stocks): if ".trp" not in filename: raise RuntimeError("Contain a none-trp file") self._flag = 0 self._trading_data = stocks.TradingData("", -1, -1, -1, -1, -1) super().__init__(filename, load_stocks)
def _process(self, file): for line in file.readlines(): line = line.strip('\r\n') items = line.split(',') print(items) code = items.pop(0) data = stocks.TradingData(items[0], float(items[1]), float(items[2]), float(items[3]), float(items[4]), int(items[5])) stock = stocks.Stock(code) stock.add_day_data(data) self._stocks.set_stock(code, stock)
def _process(self, file): try: for line in file: row = line.strip().split(',') # row[0 : 6]: stock_code,date,opening_price,high_price,low_price,closing_price,volume trading_data = stocks.TradingData(row[1], float(row[2]), float(row[3]), float(row[4]), float(row[5]), int(row[6])) stock = self._stocks.get_stock(row[0]) stock.add_day_data(trading_data) except Exception as e: raise RuntimeError("invalid format!")
def _process(self, file): """Load and parse the stock market data from 'file'.""" try: for line in file: line = line.strip() items = line.split(',') stock_code = items[0] stock = self._stocks.get_stock(stock_code) data = stocks.TradingData(items[1], float(items[2]), float(items[3]), float(items[4]), float(items[5]), int(items[6])) stock.add_day_data(data) except Exception as e: raise RuntimeError()
def _process(self, file): """Parses through the triplet file, pulling the data from each line to form a new TradingData object. Next, attaches this TradingData to a Stock object, which is found by retrieving information from StockCollection. Parameters: file (str): The file as identified in __init__ method parameters (after being opened using __init__ method) """ count = 0 stock_code = 0 date = "" day_open = 0 day_high = 0 day_low = 0 day_close = 0 volume = 0 for line in file: #Aligns data with the correct parameter for a TradingData object #and checks for ValueErrors at the same time try: stock_code, key, data = line.split(":") if key == "DA": date = data elif key == "OP": day_open = float(data) elif key == "HI": day_high = float(data) elif key == "LO": day_low = float(data) elif key == "CL": day_close = float(data) elif key == "VO": volume = int(data) count += 1 except ValueError: raise RuntimeError("Error: Invalid data in file. Could not process.") #Every six lines form a complete TradingData object, even if the #data is out of order, so the program clears that data as a new #object and restarts the counter if count == 6: new_trading_object = stocks.TradingData(date, day_open, day_high, day_low, day_close, volume) collection_entry = self._stocks.get_stock(stock_code) collection_entry.add_day_data(new_trading_object) count = 0
def _process(self, file): try: for line in file: line = line.strip() item = line.split(",") stock_code = item[0] stock = self._stocks.get_stock(stock_code) trading_data = stocks.TradingData(item[1], float(item[2]), float(item[3]), float(item[4]), float(item[5]), float(item[6])) stock.add_day_data(trading_data) except IndexError: raise RuntimeError("invalid format")
def _process(self, file): """ Process the loaded Triplet file, get the trading data and add it into appropriate stocks object and an instance of StockCollection Class. Parameter (file): Read the file line-by-line and extract the data. Assumption: There must be exactly 6 parameters to a company's particular day's stock data. """ keyword_dict = { 'DA': 'date', 'OP': 'opening_price', 'HI': 'high_price', 'LO': 'low_price', 'CL': 'closing_price', 'VO': 'volume' } counter = 0 for line in file: line = line.strip() try: code, parameter, value = line.split(":") if parameter in keyword_dict.keys(): keyword_dict[ parameter] = value # Map the parameter values in keyword dict. if counter == 5: stock = self._stocks.get_stock(code) # Trading data Object day_data = stocks.TradingData( keyword_dict.get('DA'), float(keyword_dict.get('OP')), float(keyword_dict.get('HI')), float(keyword_dict.get('LO')), float(keyword_dict.get('CL')), int(keyword_dict.get('VO'))) stock.add_day_data(day_data) counter = 0 else: counter += 1 except Exception: raise RuntimeError
def _process(self, file): data = file.readlines() group = len(data) // 6 # sub the list about data read for x in range(group): open = x * 6 end = open + 6 temp = [] code = '-1' for line in data[open:end]: line = line.strip('\r\n') items = line.split(':') code = items[0] temp.append(items[2]) tradingData = stocks.TradingData(temp[0], float(temp[1]), float(temp[2]), float(temp[3]), float(temp[4]), int(temp[5])) stock = stocks.Stock(code) stock.add_day_data(tradingData) self._stocks.set_stock(code, stock)
def _process(self, file): """Load and parse the stock market data from 'file'.""" for line in file: data = line.strip("\n").split(",") if len(data) != 7: raise ValueError("This is not a stock csv file") try: trading_data = stocks.TradingData(data[1], float(data[2]), float(data[3]), float(data[4]), float(data[5]), int(data[6])) stock = self._stocks.get_stock(data[0]) stock.add_day_data(trading_data) except ValueError: print("Data {0} in csv is wrong.".format(data[0]))
def _process(self, file): """Parse the CSV format stock market data and format into TradingData class object. Next, attaches this TradingData to a Stock object, which is found by retrieving information from StockCollection. Parameters: file (str): The file as identified in __init__ method parameters (after being opened using __init__ method) """ for line in file: all_data = tuple(line.split(',')) #Errors identify individual problems with different parts of file if len(all_data) != 7: raise RuntimeError("Error: not enough parameters in the file.") stock_code = all_data[0] date, day_open, day_high, day_low, day_close, volume = all_data[1:] try: day_open = float(day_open) except ValueError: raise RuntimeError("Error:'day_open' not a float.") try: day_high = float(day_high) except ValueError: raise RuntimeError("Error: 'day_high' not a float.") try: day_low = float(day_low) except ValueError: raise RuntimeError("Error: 'day_low' not a float.") try: day_close = float(day_close) except ValueError: raise RuntimeError("Error: 'day_close' not a float.") try: volume = int(volume.strip("\n")) except ValueError: raise RuntimeError("Error: 'volume' not an integer.") new_trading_object = stocks.TradingData(date, day_open, day_high, day_low, day_close, volume) collection_entry = self._stocks.get_stock(stock_code) collection_entry.add_day_data(new_trading_object)
def reset(self): """Reset the analysis process in order to perform a new analysis.""" self._flag = 0 self._trading_data = stocks.TradingData("", -1, -1, -1, -1, -1)
import stocks some_stocks = stocks.StockCollection() stock = some_stocks.get_stock("1AD") day_data = stocks.TradingData("20170227", 0.225, 0.225, 0.225, 0.225, 19478) stock.add_day_data(day_data) day_data = stocks.TradingData("20170306", 0.22, 0.225, 0.22, 0.225, 24259) stock.add_day_data(day_data) stock = some_stocks.get_stock("ABU") day_data = stocks.TradingData("20170227", 0.11, 0.115, 0.11, 0.115, 262306) stock.add_day_data(day_data) day_data = stocks.TradingData("20170306", 0.115, 0.115, 0.115, 0.115, 86782) stock.add_day_data(day_data) day_data = stocks.TradingData("20170313", 0.105, 0.105, 0.105, 0.105, 75025) stock.add_day_data(day_data) stock = some_stocks.get_stock("AGL") day_data = stocks.TradingData("20170227", 23.6, 23.92, 23.6, 23.85, 1433574) stock.add_day_data(day_data) day_data = stocks.TradingData("20170306", 24.7, 24.88, 24.4, 24.86, 1156183) stock.add_day_data(day_data) day_data = stocks.TradingData("20170313", 25.48, 25.53, 25.32, 25.45, 1892685) stock.add_day_data(day_data)