Beispiel #1
0
 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()
Beispiel #2
0
 def test_init(self):
     '''
     test the initialization of investment class, ie. the assignment of position and num_trials
     '''
     self.assertEqual(
         Investment.investment([1, 10, 100, 1000], 100).positions,
         [1, 10, 100, 1000])
     self.assertEqual(
         Investment.investment([1, 10, 100, 1000], 100).num_trials, 100)
 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)
Beispiel #4
0
 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)
Beispiel #5
0
 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)
Beispiel #6
0
 def test_investment_return(self):
     '''
     test the investment return (daily) calculation function
     '''
     investment=Investment.investment(10,1000)
     daily_return=investment.investment_return(10)
     self.assertEqual(len(daily_return),1000)
     self.assertGreaterEqual(daily_return.all(), -1)
     self.assertLessEqual(daily_return.all(), 1)
Beispiel #7
0
 def test_investment_return(self):
     '''
     test the investment return (daily) calculation function
     '''
     investment = Investment.investment(10, 1000)
     daily_return = investment.investment_return(10)
     self.assertEqual(len(daily_return), 1000)
     self.assertGreaterEqual(daily_return.all(), -1)
     self.assertLessEqual(daily_return.all(), 1)
Beispiel #8
0
class TestInvestments(unittest.TestCase):
    def setUp(self):
        date = "2020-10-03"
        inv_name = "Test Investment"
        self.new_investment = Investment(date, inv_name)

    def test_base_investment_constructor(self):
        date = "2020-10-03"
        inv_name = "Test Investment"

        self.assertEqual(self.new_investment.purchase_date, date)

    def test_get_name(self):
        name = "Test Investment"

        self.assertEqual(self.new_investment.get_name(), name)
Beispiel #9
0
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()
Beispiel #11
0
 def test_init(self):
     '''
     test the initialization of investment class, ie. the assignment of position and num_trials
     '''
     self.assertEqual(Investment.investment([1,10,100,1000],100).positions,[1,10,100,1000])
     self.assertEqual(Investment.investment([1,10,100,1000],100).num_trials,100)
Beispiel #12
0
def main():
    
    '''This function will take the output from the user, test whether it's valid,
    and output the required histograms and statistical information'''    
    done = False
    while done == False:
        ValidInt = False
        ValidList = False
        
        while ValidList == False:
            '''Will run until user inputs a valid list. Note this program considers
            a valid list to be a list of numbers separated by commas, and enclosed 
            by brackets'''    
            try:
                input = raw_input("Enter a list of positions: ")
                if input in ['quit', 'quit()']:    
                    exit()
            except (KeyboardInterrupt, EOFError):
                continue
            else:      
                try:
                    position_list = ast.literal_eval(input)
                    instance = Investment(position_list, 1)
                    for i in position_list:
                        if 1000%i != 0:
                            raise ValueError
                        elif i <= 0:
                            raise ValueError 
                except ValueError:
                    print "Please enter a list of positive numbers that are factors of 1000."        
                except:
                    print "Please enter a valid list of numbers."
                else:                 
                    if len(position_list) < 2:
                        print "Please enter a list of more than one number."
                    else:
                        ValidList = True 

        while ValidInt == False and ValidList == True: 
            '''will run until the user inputs a valid number of simulations'''             
            try:
                num_trials = raw_input("Enter the number of trials you want to simulate: ")
                if num_trials in ['quit', 'quit()']:
                    exit()
            except (KeyboardInterrupt, EOFError):
                exit()
            else:     
                try:
                    instance = Investment(position_list, num_trials) 
                except ValueError:
                    print "Please enter an integer number greater than 0."         
                except TypeError:
                    print "Please enter an integer." 
                else:
                    ValidInt = True    
        if ValidInt == True and ValidList == True:  
                 
            for position in position_list:
                daily_ret = instance.repeat_investment(position)
                plt.hist(daily_ret, 100, range = [-1,1])
                plt.savefig('histogram_' + str(position)+ '.pdf')
                plt.close("all")
                
            #change the output to write to results.txt
            orig_stdout = sys.stdout
            f = file('results.txt', 'w')
            sys.stdout = f
            
            #iterate through position_list and output the mean and standard deviation for each
            for position in position_list:
                daily_ret = instance.repeat_investment(position)
                print "Position: ", position
                print "Mean: ", np.mean(daily_ret)
                print "Standard Deviation" , np.std(daily_ret)
                print "\n"
                done = True
            f.close()    
Beispiel #13
0
    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),
Beispiel #14
0
class Fond:
    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 __eq__(self, other):
        return isinstance(other, Fond) and self.ticker == other.ticker

    def to_json(self):
        return {
            "name": self.name,
            "ticker": self.ticker,
            "deposits": self.deposits,
        }

    @property
    def quotes(self):
        quotes = self.fond_quotes.get_quotes()
        if self.deposits:
            start_idx = self.find_quote_entry_by_date(quotes,
                                                      self.deposits[0]["date"])
        else:
            start_idx = -10

        return quotes[start_idx:]

    def _string_to_date(self, date):
        if isinstance(date, str) or isinstance(date, unicode):
            return datetime.datetime.strptime(date, "%Y-%m-%d").date()
        elif isinstance(date, datetime.date):
            return date

        raise InvalidDate("Unknown type '%s' for date" % type(date).__name__)

    def deposit(self, amount, date):
        date = self._string_to_date(date)
        if self.get_deposit_by_date(date):
            raise InvalidUsage("A deposit for that date is already registered")

        updated_deposits = self.deposits + [{"amount": amount, "date": date}]
        self.deposits = sorted(updated_deposits,
                               key=lambda deposit: deposit["date"])

    def delete_deposit(self, date):
        date = self._string_to_date(date)
        num_deposits_before = len(self.deposits)
        self.deposits = filter(lambda x: x["date"] != date, self.deposits)
        num_deposits_after = len(self.deposits)

        num_deleted = num_deposits_before - num_deposits_after
        if num_deleted is not 1:
            raise InvalidUsage("failed to delete deposit (%s)" % num_deleted)

    def find_quote_entry_by_date(self, quotes, date):
        for i in range(len(quotes) - 1, -1, -1):
            if quotes[i]["quote_date"] == date:
                return i
        return None

    def get_deposit_by_date(self, date):
        date = self._string_to_date(date)
        return sum([
            deposit["amount"] for deposit in self.deposits
            if deposit["date"] == date
        ])

    def _price_developement_percent(self, before, after):
        return float(after["close"]) / float(before["close"])

    def get_developement(self):
        rows = []
        cash = 0

        for i in range(0, len(self.quotes)):
            curr_date = self.quotes[i]["quote_date"]
            deposit = self.get_deposit_by_date(curr_date)

            if i == 0:
                percent_development = 1
            else:
                percent_development = self._price_developement_percent(
                    self.quotes[i - 1], self.quotes[i])

            cash = cash * percent_development + deposit
            rows.append({
                "date": curr_date,
                "value": cash,
                "deposit": deposit,
                "quote": self.quotes[i]
            })

        return rows

    def get_summary(self):
        development = self.get_developement()
        return {
            "ticker": self.ticker,
            "name": self.name,
            "development": development,
            "total_deposited": sum(map(lambda x: x["deposit"], development))
        }
Beispiel #15
0
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
Beispiel #16
0
                    'Invalid Input: all numbers of shares need to be positive')
            break
        except ValueError as msg:
            print(msg)
        except KeyboardInterrupt:
            print('Keyboard Interruption')
            sys.exit()
        except EOFError:
            print('EOFError')
            sys.exit()

    while True:
        try:
            inputstr2 = input(
                'Please enter the number of times you want to repeat the simulation'
            )
            if (inputstr2 == 'quit'):
                print("User Directed Quit.")
                sys.exit()
            num_trials = int(inputstr2)
            Investment.investment(positions, num_trials).simulation()
            break
        except ValueError as msg:
            print(msg)
        except KeyboardInterrupt:
            print('Keyboard Interruption')
            sys.exit()
        except EOFError:
            print('EOFError')
            sys.exit()
Beispiel #17
0
 def setUp(self):
     date = "2020-10-03"
     inv_name = "Test Investment"
     self.new_investment = Investment(date, inv_name)
Beispiel #18
0
class ManagedLandWithFarmerBehaviour(LandCover):
    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 initial(self):
        self.lc_parameters_module.initial()
        self.initial_condition_module.initial()
        self.root_zone_water_module.initial()
        self.snow_frost_module.initial()

        # self.drainage_module.initial()
        
        # self.evapotranspiration_module.initial()
        self.potential_et_module.initial()
        
        self.interception_module.initial()
        self.irrigation_demand_module.initial()
        self.irrigation_supply_module.initial()
        self.infiltration_module.initial()
        self.capillary_rise_module.initial()
        self.drainage_module.initial()
        
        self.actual_et_module.initial()
        
        self.crop_yield_module.initial()
        self.income_module.initial()
        self.investment_module.initial()
        self.accounting_module.initial()
        self.grid_cell_mean_module.initial()
        self.reporting_module = Reporting(
            self,
            self._configuration.outNCDir,
            self._configuration.NETCDF_ATTRIBUTES,
            self._configuration.irrNonPaddy,
            variable_list_crop,
            'irrNonPaddy')
        
    def dynamic(self):
        self.lc_parameters_module.dynamic()
        self.initial_condition_module.dynamic()
        
        self.root_zone_water_module.dynamic()
        self.snow_frost_module.dynamic()
        # self.drainage_module.dynamic()
        # self.evapotranspiration_module.dynamic()
        self.potential_et_module.dynamic()
        self.interception_module.dynamic()
        self.root_zone_water_module.dynamic()
        self.infiltration_module.compute_infiltration_capacity()        
        self.irrigation_demand_module.dynamic()        
        self.irrigation_supply_module.dynamic()

        # the order here (infiltration/cap rise/drainage)
        # is the same as in CWATM
        self.infiltration_module.dynamic()
        self.root_zone_water_module.dynamic()
        self.capillary_rise_module.dynamic()
        self.drainage_module.dynamic()
        
        self.root_zone_water_module.dynamic()
        self.actual_et_module.dynamic()
        self.crop_yield_module.dynamic()
        self.income_module.dynamic()
        self.investment_module.dynamic()
        self.accounting_module.dynamic()
        self.grid_cell_mean_module.dynamic()
        self.reporting_module.report()
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
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)))