def getHistoricalAdjustedPriceData(self, symbol, start_date, end_date, time_interval): d1 = str(start_date.getDay()) m1 = str(start_date.getMonth() - 1) y1 = str(start_date.getYear()) d2 = str(end_date.getDay()) m2 = str(end_date.getMonth() - 1) y2 = str(end_date.getYear()) if time_interval == "daily": time_interval = "d" elif time_interval == "weekly": time_interval = "w" elif time_interval == "monthly": time_interval = "m" URL = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol + "&b=" + d1 URL += "&a=" + m1 + "&c=" + y1 + "&e=" + d2 + "&d=" + m2 + "&f=" + y2 + "&g=" URL += time_interval + "&ignore=.cvs" csv_dict = self.getCSVDictForApiCall(URL) price_obj_list = [] for index in csv_dict: if (str(index) == "0"): continue hist_price_obj = YAFIObjects.YAFIObjectHistoricalPrice(csv_dict[index]) price_obj_list.append(hist_price_obj) return price_obj_list
def save(self): info_filename = Util.getDepotInfoFile(self.name) fileh = open(info_filename, "w") self.info_obj = YAFIObjects.YAFIObjectDepotInfo([self.starting_cash]) fileh.write(self.info_obj.getAsString()) for portfolio in self.portfolios: portfolio.save()
def __init__(self, sim_env, api_wrapper, name, cash): self.sim_env = sim_env self.api_wrapper = api_wrapper self.name = name self.portfolios = [] self.info_obj = YAFIObjects.YAFIObjectDepotInfo([0]) self.starting_cash = cash self.loadStuff()
def loadDepotInfo(depot_name): filename = getDepotInfoFile(depot_name) if os.path.exists(filename): fileh = open(filename, "r") csv_dict = getDictFromCsvString(fileh.read()) info_obj = YAFIObjects.YAFIObjectDepotInfo(csv_dict[0]) return info_obj return None
def getAdjustedPriceDataRangeStack(self, symbol, start_date, end_date): if symbol not in self.price_history_cache: successful = self.cachePriceData(symbol) if not successful: return None stack = YAFIObjects.HistoricalAdjustedPriceRangeStack() date_prices_map = self.symbol_date_price_hashmap[symbol] date_counter = end_date while True: date_string = date_counter.getAsString() if date_counter < start_date: break if date_string in date_prices_map: hist_price_obj = YAFIObjects.YAFIObjectHistoricalPrice(date_prices_map[date_string]) stack.addPriceObject(hist_price_obj) date_counter = date_counter.getBeforeDayDate() return stack
def getAdjustedPriceDataForDate(self, symbol, date): if symbol not in self.price_history_cache: successful = self.cachePriceData(symbol) if not successful: return None date_price_dict = self.symbol_date_price_hashmap[symbol] date_string = date.getAsString() if date_string not in date_price_dict: return None return YAFIObjects.YAFIObjectHistoricalPrice(date_price_dict[date_string])
def getAdjustedPriceDataRangeStackForAmountOfDays(self, symbol, end_date, days): if symbol not in self.price_history_cache: return None fail_counter = 0 stack = YAFIObjects.HistoricalAdjustedPriceRangeStack() date_prices_map = self.symbol_date_price_hashmap[symbol] date_counter = end_date while True: date_string = date_counter.getAsString() if days == 0 or fail_counter >= 7: break if date_string in date_prices_map: hist_price_obj = YAFIObjects.YAFIObjectHistoricalPrice( date_prices_map[date_string]) stack.addPriceObject(hist_price_obj) days -= 1 fail_counter = 0 date_counter = date_counter.getBeforeDayDate() fail_counter += 1 return stack
def loadPositionHistory(depot_name, portfolio_name, symbol): filename = getPositionHistoryFilename(depot_name, portfolio_name, symbol) if os.path.exists(filename): list_of_actions = [] fileh = open(filename, "r") csv_dict = getDictFromCsvString(fileh.read()) for key in csv_dict: api_obj = YAFIObjects.YAFIObjectPositionHistoryAction( csv_dict[key]) date = parseDateString(api_obj.getData("date")) action_obj = SimEnv.PositionHistoryAction( date, api_obj.getData("action_string"), api_obj.getData("amount"), api_obj.getData("price"), api_obj.getData("reason")) list_of_actions.append(action_obj) return SimEnv.PositionHistory(symbol, list_of_actions) else: return SimEnv.PositionHistory(symbol, [])
def getCurrentStockPrice(self, symbol): URL1 = "http://finance.yahoo.com/d/quotes.csv?s=" URL2 = "&f=snat1" csv_dict = self.getCSVDictForApiCall(URL1 + symbol + URL2) price_obj = YAFIObjects.YAFIObjectCurrentStockPrice(csv_dict[0]) return price_obj