def get_neighbor(self, i, j): temp_portfolio = self.portfolio.copy() temp_portfolio[i] = Investment( self.portfolio[i].symbol, self.portfolio[i].start_amount - (self.portfolio[i].start_amount * .1), self.portfolio[i].percent_change) temp_portfolio[j] = Investment( self.portfolio[j].symbol, self.portfolio[j].start_amount + (self.portfolio[i].start_amount * .1), self.portfolio[j].percent_change) return Portfolio(temp_portfolio)
def random_restart(self): remaining = self.total_investment temp_portfolio = [] for i, invest in enumerate(self.start_portfolio.portfolio): if i == 9: temp_portfolio.append( Investment(invest.symbol, remaining, invest.percent_change)) break invest_amt = rand.randint(0, remaining) temp_portfolio.append( Investment(invest.symbol, invest_amt, invest.percent_change)) remaining -= invest_amt return Portfolio(temp_portfolio)
def __init__(self, var, config_section_name): super(ManagedLandWithFarmerBehaviour, self).__init__( var, config_section_name) self.lc_parameters_module = ManagedLandWithFarmerBehaviourParameters(self, config_section_name) self.initial_condition_module = InitialConditionManagedLand(self) self.root_zone_water_module = RootZoneWaterIrrigatedLand(self) self.snow_frost_module = SnowFrost(self) # self.drainage_module = Drainage(self) self.potential_et_module = PotentialEvapotranspiration(self) # self.evapotranspiration_module = Evapotranspiration(self) self.interception_module = Interception(self) self.irrigation_demand_module = IrrigationMultipleCrops(self) self.irrigation_supply_module = IrrigationSupply(self) self.infiltration_module = Infiltration(self) self.capillary_rise_module = CapillaryRise(self) self.drainage_module = Drainage(self) self.actual_et_module = ActualEvapotranspiration(self) self.crop_yield_module = CropYield(self) self.income_module = Income(self) self.investment_module = Investment(self) self.accounting_module = Accounting(self) self.grid_cell_mean_module = GridCellMean(self) self.add_dimensions()
def __init__(self, ticker=None, name=None, deposits=[]): if not ticker: raise InvalidUsage( "Ticker must contain a valid ticker (was {})".format(ticker)) self.ticker = ticker self.name = name self.fond_quotes = Investment("%s.FOND" % self.ticker) self.deposits = map( lambda x: { "date": datetime.datetime.strptime(x["date"], "%Y-%m-%d").date( ), "amount": int(x["amount"]) }, deposits)
def build_investment_array(position_value: [int, float], position: int) -> np.ndarray: """Build numpy array of Investment positions. :param position_value: ratio of initial holdings over number of investments. :param position: number of investments to be made. :return: investments: array of Investment positions. """ # Define initial array containing a single investment's position. base = np.array([Investment(position_value).position]) # Utilize numpy's repeat method to create an array of size :position # containing copies of our :base array. investments = np.repeat(base, position) # Return our array of Investment positions. return investments
def __init__(self, symbols=[], proportions=[]): """Ctor Constructs a Portfolio object Args: symbols (:list<string>, optional): List of string symbol proportions (:list<float>, optional) """ self.__symbols = symbols # sanity check if proportions and symbols: # make sure the sum of the proportions sums up to almost 1.00 assert (len(symbols) == len(proportions)) # for each proportion, zip it with a symbol into a dictionary for i in range(len(symbols)): self.__proportions[symbols[i]] = proportions[i] # initialize every symbol as an investment object self.__investments = [Investment(symbol=s) for s in symbols] self.__computeState()
print "{end} ({days:3} days) ${revenue:.2f} ${refProfit:.2f} ${totalRevenue:.2f} ${profit:.2f} {profitPercent:.2f}% {minimumBuy}".format( end=projection.endDate, days=days, revenue=revenue, totalRevenue=totalRevenue, profit=profit, profitPercent=profitPercent, refProfit=refProfit, minimumBuy=minimumBuy ), # for miner in projection.miners: # print "{hash}GHS ${revenue:.2f}/day {start} - {end}".format( # hash=int(miner.hashRate), revenue=miner.dailyRevenue, start=miner.startDate, end=miner.endDate # ) printTitle = True includeReferralBonus = True investments = [ Investment(1800, '5/30/2018'), Investment(300, '5/30/2018'), Investment(300, '5/30/2018'), Investment(10, '6/4/2018'), Investment(10, '6/8/2018') ] totalInvested = 0 for investiment in investments: totalInvested = totalInvested + investiment.amount miners = [ Miner(hashRate=40600, startDate='5/31/2018', days=90), Miner(hashRate=3450, startDate='6/4/2018', days=90), Miner(hashRate=1000, startDate='6/5/2018', days=90), Miner(hashRate=1270, startDate='6/7/2018', days=90),
class Main: portfolio = [] print("======================================================================================================") print("Welcome to the Intelligent Investor(tm). You provide 10 stocks for a portfolio you'd like to optimize.") print("I'll tell you what the best mix of investment in those 10 stocks would be based on 30-day history.") print("Results will be calculated based on either hill-climbing or simulated annealing algorithms.") print("======================================================================================================") invest_amount = round(float(input("Enter dollar amount to invest: $")), 2) # We're going to query the AlphaVantage API to get realtime 30-day changes # Only valid NASDAQ stock symbols will work for the API but errors are handled gracefully # and the user is reprompted to enter a new symbol until 10 stocks have been chosen successfully while len(portfolio) < 10: yesterday = date.today() - timedelta(days=1) date_string = yesterday.strftime("%Y-%m-%d") thirty_days_ago = yesterday - timedelta(days=30) thirty_string = thirty_days_ago.strftime("%Y-%m-%d") potential_symbol = input("Input a valid NASDAQ stock symbol: ") if potential_symbol in portfolio: print("You already selected that symbol, let's diversify the portfolio! (Please enter unique symbols only)") continue # building the query according to API format query = "https://www.alphavantage.co/query?function={}&symbol={}&apikey={}".format( API_FUNCTION, potential_symbol, ALPHAVANTAGE_API_KEY) ssl_cert = ssl.SSLContext() # required to use https thirty_day_history = requests.urlopen(query, context=ssl_cert) json_data = json.loads(thirty_day_history.read()) # load the json as a dict # invalid stock symbol if 'Error Message' in json_data: print("\tInvalid stock symbol, only valid NASDAQ stock symbols allowed!") continue # search for 30-day change, not all days have an entry (weekends are absent) so # some adjustments must be done to perform well across all days used closed = False while not closed: try: last_close = json_data["Time Series (Daily)"][date_string]["4. close"] closed = True except KeyError: yesterday = yesterday - timedelta(days=1) date_string = yesterday.strftime("%Y-%m-%d") thirty_days_ago = yesterday - timedelta(days=30) thirty_string = thirty_days_ago.strftime("%Y-%m-%d") closed = False # make sure the 30 day change is appropriate length while not closed: try: thirty_close = json_data[FUNC_STRING][thirty_string]["4. close"] closed = True except KeyError: thirty_days_ago = thirty_days_ago - timedelta(days=1) thirty_string = thirty_days_ago.strftime("%Y-%m-%d") thirty_day_change = (float(last_close) - float(thirty_close)) / float(thirty_close) portfolio.append(Investment(potential_symbol, invest_amount / 10, thirty_day_change)) print("Successfully loaded data for {} and added to portfolio.\n".format(potential_symbol)) # here we go...output the portfolio with Trust Fund Baby approach start_portfolio = Portfolio(portfolio) print("\nHere is the portfolio I will optimize an investment mix in:") print(start_portfolio) # now do Hill Climbing with random restarts and print the corresponding optimized Portfolio start = time() print("Calculating optimal investment mix with hill-climbing (random restarts)...") climber = HillClimber(start_portfolio, 10, invest_amount) hill_best = climber.hill_climb() end = time() print("Done in {} seconds.\n".format(end-start)) print(hill_best) # finally do Simulated Annealing and print corresponding optimized Portfolio start = time() print("Calculating optimal investment mix with simulated annealing...") annealer = Annealer(start_portfolio, 1000000) anneal_best = annealer.anneal() print(anneal_best) end = time() print("Done in {} seconds.\n".format(end-start)) print("\n\n") print("Strategy $ Profit") print("-------- --------") print(" {:>3} {:>7}".format("TFB", round(start_portfolio.worth - invest_amount, 2))) print(" {:>3} {:>7}".format("HC", round(hill_best.worth - invest_amount, 2))) print(" {:>3} {:>7}".format("SA", round(anneal_best.worth - invest_amount, 2)))
def convert_Vanguard_webpage_to_Investment(inputfile): output_list = [] outputfile = inputfile + '.csv' output_fd = open(outputfile, 'w') filelines = open(inputfile, "r").read().splitlines() list_size = len(filelines) # print "list_size = " + str(list_size) found_section = False fileline_index = -1 section_title = '' new_section_start = 'Registration details|YAHOO' section_index = -1 symbol_start = False quantity_list = [] funds = Investments() Yahoo_entries_start = False # print "<Comments>" while True: # print "fileline_index = " + str(fileline_index) + " . list_size = " + str(list_size) # print "line = " + filelines[fileline_index] fileline_index += 1 if fileline_index >= list_size: break if re.search('Add another Vanguard account', filelines[fileline_index]): break if re.search('^\s*$', filelines[fileline_index]): continue # print "line: " + str(fileline_index) + ", list_size = " + str(list_size) # print "line: " + str(fileline_index) + ": " + filelines[fileline_index] # print "found_section = " + str(found_section) if found_section == False: if re.search(new_section_start, filelines[fileline_index]): found_section = True section_index += 1 if "YAHOO" in filelines[fileline_index]: section_title = filelines[fileline_index] # print "Section:" + section_title # print else: section_title = filelines[fileline_index] continue if re.search('^Total', filelines[fileline_index]): found_section = False section_index = -1 quantity_list.append("") output_fd.write('\n') Yahoo_entries_start = False continue if "YAHOO" in section_title: if not Yahoo_entries_start: if not re.search('Current balance', filelines[fileline_index]): continue fileline_index += 1 Yahoo_entries_start = True found = re.search('^([^ ]+) ([^ ]+) ([^ ]+)', filelines[fileline_index]) if not found: print("ERROR: input file line " + str(fileline_index) + ": " + filelines[fileline_index] + ". Expecting fund name, quantity, share price.") sys.exit(1) section_index += 1 symbol = "TBD_symbol" name = found.group(1) account_ID = "TBD_account_ID" quantity = found.group(2).strip().replace(',','') share_price = found.group(3).strip()[1:] owner = "Mireille" retirement_class = "401K" else: # print "line2: " + str(fileline_index) + ": " + filelines[fileline_index] # if not re.search('Change\s+Current balance|Buy\s+Sell', filelines[fileline_index]): # print filelines[fileline_index] if not re.search('Current balance|Buy ', filelines[fileline_index]): continue if 'Buy ' in filelines[fileline_index]: if 'Total' in filelines[fileline_index+1]: continue fileline_index += 1 found = re.search('^(\S+)\s+(.+)$', filelines[fileline_index]) if not found: print("ERROR: input file line " + str(fileline_index) + ": " + filelines[fileline_index] + ". Expecting stock symbol and name.") sys.exit(1) section_index += 1 symbol = found.group(1) name = found.group(2).strip() fileline_index += 1 # found = re.search('^\s+\S+\s+\S+\s+(\S+)\s+', filelines[fileline_index]) # found = re.search('%\s+\S+\s+(\S+)\s+', filelines[fileline_index]) # print "line3: " + filelines[fileline_index] found = re.search('^\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+', filelines[fileline_index]) if not found: print("ERROR: input file quantity line " + str(fileline_index) + ": " + filelines[fileline_index] + ". Expecting stock quantity.") sys.exit(1) account_ID = found.group(1) # details_file = "Vanguard_details/" + symbol + "," + name + "," + account_ID + ".txt" # if not os.path.isfile(details_file): # print "WARNING: File does not exist: " + details_file quantity = re.sub(',', '', found.group(2)) # remove embedded commas # quantity_list.append(quantity) share_price = found.group(3)[1:] if "Mark T." in section_title and "Mireille" in section_title: owner = "Joint" elif "Mark T." in section_title: owner = "Mark" elif "Mireille" in section_title: owner = "Mireille" else: owner = "" if owner == "Joint": retirement_class = "Non_Retirement" else: if '—' in section_title: retirement_class = section_title.split('—')[1].strip() if '-' in retirement_class: retirement_class = retirement_class.split('-')[0].strip() if retirement_class == "SEP": retirement_class = "SEP IRA" investment_total = Money().__set_by_string__(share_price).__mult__(quantity).__str__() # output_line = str(section_index) + "," + symbol + "," + name + "," + quantity output_line = symbol + "," + name + "," + account_ID + "," + quantity + "," + share_price + "," + investment_total # output_list.append(output_line) # print "output_line: " + output_line output_fd.write(output_line + '\n') funds.__add__(Investment(symbol=symbol, name=name, account_ID=account_ID, quantity=quantity, share_price=share_price, owner=owner, retirement_class=retirement_class)) output_fd.close() # print "</Comments>" # for row in quantity_list: # print row return 0, funds
def setUp(self): date = "2020-10-03" inv_name = "Test Investment" self.new_investment = Investment(date, inv_name)
def convert_investment_spreadsheet_to_Investment(inputfile): output_list = [] outputfile = inputfile + '.csv' output_fd = open(outputfile, 'w') # filelines = open(inputfile, "r").readlines() filelines = open(inputfile, "r").read().splitlines() list_size = len(filelines) # print "list_size = " + str(list_size) # Get price per share start_capturing = False share_prices = {} for line in filelines: if 'Current Last Price' in line: start_capturing = True continue if start_capturing == True: key, price = line.split(',')[:2] # print key, price if key == '': break share_prices[key] = price.replace('$', '') ''' found_section = False section_title = '' new_section_start = 'Registration details|YAHOO' section_index = -1 symbol_start = False quantity_list = [] Yahoo_entries_start = False # print "<Comments>" ''' funds = Investments() fileline_index = -1 retirement_class = "TBD_retirement_class" owner = "TBD_owner" broken_line = '' ignore_lines = False one_time_retirement_class = '' while True: # print "fileline_index = " + str(fileline_index) + " . list_size = " + str(list_size) # print "line = " + filelines[fileline_index] fileline_index += 1 if fileline_index >= list_size: break # filelines[fileline_index] = re.sub('^"', '', filelines[fileline_index]) if re.search('^Non-Retirement Assets', filelines[fileline_index]): retirement_class = "Non_Retirement" continue if re.search('^Retirement Assets', filelines[fileline_index]): retirement_class = "Retirement" ignore_lines = True continue if re.search('^Name of Investment', filelines[fileline_index]): ignore_lines = False continue if ignore_lines: continue if retirement_class == "TBD_retirement_class": continue ''' # if not re.search(",,,,,,,,,,,,", filelines[fileline_index]): # if not re.search("\ $", filelines[fileline_index]): # print "temp = " + temp # print "line = " + filelines[fileline_index] # if not re.search("\ $", filelines[fileline_index]): # if not re.search("\r", filelines[fileline_index]): temp = filelines[fileline_index] temp = temp.rstrip() if temp == filelines[fileline_index]: # line missing end ^M ''' if not re.search(",,,,,,,$", filelines[fileline_index]): broken_line += filelines[fileline_index] # print "broken_line = " + broken_line continue else: # line ends with ^M if broken_line != '': broken_line += filelines[fileline_index] filelines[fileline_index] = broken_line # print "broken_line = " + broken_line broken_line = '' # else: # regular single line ending with ^M if re.search('^,,,,,,,,,,,,', filelines[fileline_index]): continue found = re.search("^([^ ]+?[- ]IRA),,,,,", filelines[fileline_index]) if found: retirement_class = found.group(1) one_time_retirement_class = '' continue found = re.search("^.+?(Traditional[- ]IRA)[^,]*,", filelines[fileline_index], re.IGNORECASE) if found: one_time_retirement_class = found.group(1) found = re.search("^.+?(Traditional[- ]401K)[^,]*,", filelines[fileline_index], re.IGNORECASE) if found: one_time_retirement_class = found.group(1) found = re.search("^.+?(Roth[- ]401K)[^,]*,", filelines[fileline_index], re.IGNORECASE) if found: one_time_retirement_class = found.group(1) found = re.search("^.+?(Roth[- ]IRA)[^,]*,", filelines[fileline_index], re.IGNORECASE) if found: one_time_retirement_class = found.group(1) if re.search('^Vanguard joint account', filelines[fileline_index]): owner = "Joint" continue if re.search("^Mark's Vanguard retirement account", filelines[fileline_index]): owner = "Mark" continue if re.search("^Mireille's Vanguard retirement account", filelines[fileline_index]): owner = "Mireille" continue if re.search("^Mireille's Yahoo 401K", filelines[fileline_index]): owner = "Mireille" retirement_class = "401K" continue if re.search("^Mark misc. retirement accounts", filelines[fileline_index]): owner = "Mark" continue if re.search(',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,', filelines[fileline_index]): continue if re.search("^Totals for all above", filelines[fileline_index]): break if re.search("^Total", filelines[fileline_index]): continue # print str(fileline_index-1) + " = " + filelines[fileline_index] # line_list = filelines[fileline_index].split(',') rc, line_list, max_num_columns = get_csv( csv_input_source="string", csv_string=filelines[fileline_index]) # print rc # print len(line_list) # print line_list[0] ''' if len(line_list[1]) < 2: # ignore labels continue if line_list[1] == '': # ignore labels continue ''' if retirement_class == "Non_Retirement": name = line_list[0] history = name description = line_list[0] symbol = line_list[1] account_ID = "TBD_account_ID" quantity = line_list[2].replace(',', '') header = [ "Name of Investment", "Symbol", "Number of shares", "Cost Basis", "Cost Basis date", "difference", "Cost Basis percent change", "", "", "", "Small Growth", "Mid Growth", "Large Growth", "Small Blend", "Mid Blend Aggressive", "Large Blend", "Small Value", "Mid Value", "Large Value", "Bonds", "Energy", "Global", "Healthcare", "REIT", "Individual Stocks" ] ''' Fidelity Contrafund (FCNTX),FCNTX,831.612,"$45,877 ",,"$38,748 ",84%,,,,,,"$84,625",,,,,,,,,,,,,,,,,,, ''' asset_class = "TBD_asset_class" for index in range(10, len(header)): if line_list[index] != '': asset_class = header[index] total_value = line_list[index] break else: # print filelines[fileline_index] name = line_list[0] history = name description = line_list[0] symbol = line_list[1] account_ID = "TBD_account_ID" quantity = line_list[3].replace(',', '') header = [ "Name of Investment", "Symbol", "Fund & Account", "Number of shares", "Cost Basis", "Cost Basis date", "Difference", "Cost Basis Percent change", "", "Tradtional IRA - pre-tax", "Traditional IRA - post-tax", "Roth IRA", "Traditional 401K", "Roth 401K", "Small Growth", "Mid Growth", "Large Growth", "Small Blend", "Mid Blend", "Large Blend", "Small Value", "Mid Value", "Large Value", "Bonds", "Energy", "Global", "Healthcare", "REIT", "", "", "" ] asset_class = "TBD_asset_class" for index in range(14, len(header)): if line_list[index] != '': asset_class = header[index] total_value = line_list[index] break if symbol in share_prices: share_price = share_prices[symbol] else: msg = "ERROR: Share price for symbol " + symbol + " missing from lookup table in spreadsheet." print(msg) error_list.append(msg) share_price = "TBD_share_price_missing_from_spreadsheet" save_retirement_class = retirement_class if one_time_retirement_class != '': retirement_class = one_time_retirement_class funds.add( Investment(symbol=symbol, name=name, description=description, quantity=quantity, account_ID=account_ID, owner=owner, asset_class=asset_class, retirement_class=retirement_class, history=history, total_value=total_value, share_price=share_price, filename=inputfile, file_line_num=str(fileline_index))) one_time_retirement_class = '' retirement_class = save_retirement_class ''' # print "line: " + str(fileline_index) + ", list_size = " + str(list_size) # print "line: " + str(fileline_index) + ": " + filelines[fileline_index] # print "found_section = " + str(found_section) if found_section == False: if re.search(new_section_start, filelines[fileline_index]): found_section = True section_index += 1 if "YAHOO" in filelines[fileline_index]: section_title = filelines[fileline_index] # print "Section:" + section_title # print else: section_title = filelines[fileline_index] continue if re.search('^Total', filelines[fileline_index]): found_section = False section_index = -1 quantity_list.append("") output_fd.write('\n') Yahoo_entries_start = False continue if "YAHOO" in section_title: if not Yahoo_entries_start: if not re.search('Current balance', filelines[fileline_index]): continue fileline_index += 1 Yahoo_entries_start = True found = re.search('^([^ ]+) ([^ ]+) ([^ ]+)', filelines[fileline_index]) if not found: print "ERROR: input file line " + str(fileline_index) + ": " + filelines[fileline_index] + ". Expecting fund name, quantity, share price." sys.exit(1) section_index += 1 symbol = "TBD_symbol" name = found.group(1) account_ID = "TBD_account_ID" quantity = found.group(2).strip() share_price = found.group(3).strip()[1:] owner = "Mireille" retirement_class = "401K" else: # print "line2: " + str(fileline_index) + ": " + filelines[fileline_index] # if not re.search('Change\s+Current balance|Buy\s+Sell', filelines[fileline_index]): # print filelines[fileline_index] if not re.search('Current balance|Buy ', filelines[fileline_index]): continue if 'Buy ' in filelines[fileline_index]: if 'Total' in filelines[fileline_index+1]: continue fileline_index += 1 found = re.search('^(\S+)\s+(.+)$', filelines[fileline_index]) if not found: print "ERROR: input file line " + str(fileline_index) + ": " + filelines[fileline_index] + ". Expecting stock symbol and name." sys.exit(1) section_index += 1 symbol = found.group(1) name = found.group(2).strip() fileline_index += 1 # found = re.search('^\s+\S+\s+\S+\s+(\S+)\s+', filelines[fileline_index]) # found = re.search('%\s+\S+\s+(\S+)\s+', filelines[fileline_index]) # print "line3: " + filelines[fileline_index] found = re.search('^\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+', filelines[fileline_index]) if not found: print "ERROR: input file quantity line " + str(fileline_index) + ": " + filelines[fileline_index] + ". Expecting stock quantity." sys.exit(1) account_ID = found.group(1) # details_file = "Vanguard_details/" + symbol + "," + name + "," + account_ID + ".txt" # if not os.path.isfile(details_file): # print "WARNING: File does not exist: " + details_file quantity = re.sub(',', '', found.group(2)) # remove embedded commas # quantity_list.append(quantity) share_price = found.group(3)[1:] if "Mark T." in section_title and "Mireille" in section_title: owner = "Joint" elif "Mark T." in section_title: owner = "Mark" elif "Mireille" in section_title: owner = "Mireille" else: owner = "" if owner == "Joint": retirement_class = "Non_Retirement" else: if '—' in section_title: retirement_class = section_title.split('—')[1].strip() if '-' in retirement_class: retirement_class = retirement_class.split('-')[0].strip() if retirement_class == "SEP": retirement_class = "SEP IRA" investment_total = Money().__set_by_string__(share_price).__mult__(quantity).__str__() # output_line = str(section_index) + "," + symbol + "," + name + "," + quantity output_line = symbol + "," + name + "," + account_ID + "," + quantity + "," + share_price + "," + investment_total # output_list.append(output_line) # print "output_line: " + output_line output_fd.write(output_line + '\n') funds.add(Investment(symbol=symbol, name=name, account_ID=account_ID, quantity=quantity, share_price=share_price, owner=owner, retirement_class=retirement_class)) output_fd.close() # print "</Comments>" # for row in quantity_list: # print row ''' return 0, funds