# print  zip(buysequence)

starterports = []
firstcsv = True
for csvfile in inputcsvfiles:
   starterports.append(rebalance.read_cmc_pnl_to_portfdict(csvfile,desiredport))
   if not firstcsv:
      starterports.append(rebalance.add_portfdict(starterports[-2],starterports[-1]))
   else:
      firstcsv = False

portfolios = [starterports[-1]]
starterport = starterports[-1]

print "starter portfolio"
rebalance.printport(starterport)
starterrating = rebalance.isrebalancegood(starterport)
print "starter rating", starterrating

totalspend = 0
fees = 0
for buycode,buyamount in buysequence:
   print  "buy ",buyamount," of ",buycode
   portfolios.append(rebalance.rebalance(float(buyamount),portfolios[-1],buycode))
   totalspend = totalspend + float(buyamount)
   fees = fees + fee
   rebalance.printport(portfolios[-1])
   print "rating of ",rebalance.isrebalancegood(portfolios[-1])

print "totalspend is: ",totalspend," (inc fees is: ",totalspend + fees," )"
print "fees are: ",fees," which is ",fees/(totalspend + fees) * 100," per cent"
if __name__ == '__main__':
   try:
      fee, desiredport, singleproc = rebalance.readconfig(sys.argv[1])
      cmcpnlcsvfilename = sys.argv[2]
      addedcash = float(sys.argv[3])
      numtries = int(sys.argv[4])
   except:
      usage()

   starterport = rebalance.read_cmc_pnl_to_portfdict(cmcpnlcsvfilename,desiredport)

   buychoices = {}

   print "starter portfolio, loaded from the file ",cmcpnlcsvfilename
   rebalance.printport(starterport)
   starterrating = rebalance.isrebalancegood(starterport)
   print "starter balance rating (i.e. how close to the desired balance):", starterrating
   print

   # first gather all the single buy choices.
   for asxcode in starterport.keys():
      newport = rebalance.rebalance(addedcash - fee,starterport,asxcode)
      newrating = rebalance.isrebalancegood(newport)
      # print asxcode,newrating
      # if newrating > starterrating or asxcode == 'TOTALS:':
      if asxcode == rebalance.TOTALS:
	 # print "dont bother with", asxcode
	 pass
      else:
	 buychoices[newrating] = [addedcash,asxcode]