def updateVanguardPrices(fundId):
    fname = "./fundData/fund{:02d}.csv".format(fundId)

    if os.path.isfile(fname):
        with open(fname, "r") as fin:
            for line in fin: pass
        lastDate = datetime.datetime.strptime(line.split(",", 1)[0], "%Y-%m-%d")
        if lastDate.date() < datetime.date.today():
            startDate = lastDate + datetime.timedelta(days=1)
            try:
                Date, Purchase, NAV, Withdrawal = \
                    getVanguardPrices(fundId, startDate, None)
                with open(fname, "a") as out:
                    for i in zip(Date, Purchase, NAV, Withdrawal):
                        out.write(",".join(i) + "\n")
            except:
                pass
    else:
        startDate = datetime.datetime.strptime("2013-01-01", "%Y-%m-%d")
        Date, Purchase, NAV, Withdrawal = \
            getVanguardPrices(fundId, startDate , None)
        with open(fname, "w") as out:
            out.write("Date,Purchase,NAV,Withdrawal")
            for i in zip(Date, Purchase, NAV, Withdrawal):
                out.write(",".join(i) + "\n")
Exemple #2
0
def main():
    parser = argparse.ArgumentParser(
            formatter_class=argparse.RawDescriptionHelpFormatter,
            description=textwrap.dedent('''\
                Retrives the historical prices of Vanguard funds from
                https://www.vanguardinvestments.com.au/retail/ret/investments/price-history-rtl.jsp
    
                If no arguments supplied, returns prices for the past month
                of 'Vanguard Index Australian Shares Fund' (fundId=1).
                '''))
    parser.add_argument("-f", "--fundId", default="1", metavar="fundId",
            choices=[str(j) for j in [i for i in range(9)] + [28, 29]])
    parser.add_argument("-s", "--startDate", type=strToDate, metavar="yyyy-mm-dd")
    parser.add_argument("-e", "--endDate", type=strToDate, metavar="yyyy-mm-dd")
    args = parser.parse_args()
    
    if args.endDate:
        if args.startDate:
            if args.endDate < args.startDate:
                msg = "End date not after start date"
                raise argparse.ArgumentTypeError(msg)
        else:
            msg = "Start date needed"
            raise argparse.ArgumentTypeError(msg)
    
    Date, Purchase, NAV, Withdrawal = \
        getVanguardPrices(args.fundId, args.startDate, args.endDate)
    
    print("Date,Purchase,NAV,Withdrawal")
    for i in zip(Date, Purchase, NAV, Withdrawal):
        print(",".join(i))