def __init__(self, name, quids): self.name=name self.quids=quids self.balanceHistory=RingList(90) #history of daily balances over last 90 days self.debitsHistory=RingList(90) #history of daily debits over last 90 days self.dailyDebits=[] #daily debits list self.velocity=0.0
def __init__(self, variables): self.variables = variables self.transactionCount = 0 self.failedCount = 0 self.turnover = 0.0 self.dailyTurnover = [] self.balanceHistory = RingList(90)
class Person(object): def __init__(self, name, quids): self.name=name self.quids=quids self.balanceHistory=RingList(90) #history of daily balances over last 90 days self.debitsHistory=RingList(90) #history of daily debits over last 90 days self.dailyDebits=[] #daily debits list self.velocity=0.0 def startDayTrading(self): self.dailyDebits=[] #on day trading start, the daily debits list gets reset def appendDailyDebits(self): self.debitsHistory.append(self.dailyDebits) #individual bought something def closeDayTrading(self): #day ends self.appendDailyDebits() self.balanceHistory.append(self.quids) def checkLimits(self): #not needed yet print "Check Limits" def removeQuids(self): #velocity below limits --> quids removed print "removeQuids" toremove = self.quids*(5.0/100) self.quids=self.quids -toremove self.newBalance() return toremove def newBalance(self): #just print balance if DEBUGLEVEL >= 7: print "Member %s new balance: %s " %(self.name, self.quids) def addQuids(self): #velocity above higher limit --> quids added print "addQuids" toadd = self.quids*(5.0/100) self.quids=self.quids + toadd self.newBalance() return toadd def check(self, buy): #does individual have funds to buy the good? if DEBUGLEVEL >= 8: print "%s would like to buy %s for %s" %(self.name, buy.good, buy.quid) if (self.quids < buy.quid): if DEBUGLEVEL >= 8: print "%s does not have sufficient funds to buy %s" %(self.name,buy.good) return 0 else: return 1 def buy(self, buy): #Yes, so do buying if DEBUGLEVEL >= 8: print "Transaction %s: %s buying %s for %s" %(buy.getID(), self.name, buy.good, buy.quid) self.quids=self.quids - buy.quid buy.setSuccessful(1) self.dailyDebits.append(buy.quid) self.newBalance() def sell(self,sell): #actually crediting an account (a business) if DEBUGLEVEL >= 8: print "Selling %s for %s" %(sell.good, sell.quid) self.quids=self.quids + sell.quid self.newBalance() def computeDailyVelocity(self): # dailyvelocity = 0.0 sum = 0.0 dailytransactions = len(self.dailyDebits) if dailytransactions == 0: if DEBUGLEVEL >= 7: print "No activity on account %s yet" %self.name else: for i in self.dailyDebits: sum = sum + i dailyvelocity = sum / dailytransactions if DEBUGLEVEL >= 5: print "%s's daily velocity: %s" %dailyvelocity def computeVelocity(self): #at week termination, the velocity gets calculated balances = self.balanceHistory.get() days = len(balances) if days == 0: if DEBUGLEVEL >= 7: print "No activity on account %s yet" %self.name return velocity = 0.0 totaldebits = self.calcTotalDebitsInPeriod() totaltransactionsinperiod = self.__getTotalTransactionsInPeriod__() if (totaltransactionsinperiod > 0): velocity = totaldebits / totaltransactionsinperiod #divide by average balance or total number of transactions? #averagebalance = self.calcAverageBalance() #if averagebalance >0: # velocity = totaldebits / averagebalance if DEBUGLEVEL >= 3: print "%s's velocity over the last %s days: %s" %(self.name, days, velocity) return velocity def __getTotalTransactionsInPeriod__(self): debits = self.debitsHistory.get() totalTransactions = 0 for dailyDebits in debits: transactions = len(dailyDebits) totalTransactions = totalTransactions + transactions return totalTransactions def getVelocity(self): return self.velocity def calcAverageBalance(self): totalbalance = 0.0 averagebalance = 0.0 balances = self.balanceHistory.get() days = len(balances) for i in balances: totalbalance = totalbalance + i if days > 0: averagebalance = totalbalance / days return averagebalance def calcTotalDebitsInPeriod(self): totaldebits = 0.0 debits = self.debitsHistory.get() for day in debits: for transaction in day: totaldebits = totaldebits + transaction if DEBUGLEVEL >= 6: print "%s's total debits over the last 90 days: %s" %(self.name,totaldebits) return totaldebits
class NetworkManager(object): def __init__(self, variables): self.variables = variables self.transactionCount = 0 self.failedCount = 0 self.turnover = 0.0 self.dailyTurnover = [] self.balanceHistory = RingList(90) def getTurnover(self): return self.turnover #on every transaction def transactionEvent(self, transaction): self.transactionCount = self.transactionCount + 1 if (not transaction.isSuccessful()): self.failedCount = self.failedCount + 1 else: self.turnover = self.turnover + transaction.quid self.dailyTurnover.append(transaction.quid) #on every day def dayStartedEvent(self): self.dailyTurnover = [] #end of day def dayClosedEvent(self): self.balanceHistory.append(self.dailyTurnover) #returns how many transactions in total def getTransactionCount(self): return self.transactionCount #how many ta's rejected def getFailedCount(self): return self.failedCount #check if the threshold for failing/succeeding ta's should be changed def checkPercentage(self): tcount = self.getTransactionCount() vars = self.variables if (tcount == vars.higherAcceptanceThreshold) and (vars.percentSuccessfulTransactions <= vars.maxSuccessfulTransactions): vars.percentSuccessfulTransactions = vars.percentSuccessfulTransactions + 10 vars.higherAcceptanceThreshold = vars.higherAcceptanceThreshold + vars.higherAcceptanceIncreaseStep print "New successful transactions percentage: %s" %vars.percentSuccessfulTransactions #global velocity of the system def getGlobalVelocity(self): balances = self.balanceHistory.get() days = len(balances) balance = 0.0 globalvelocity = 0.0 transactions = 0 for dailyBalance in balances: for transaction in dailyBalance: balance = balance + transaction transactions = transactions + 1 if (transactions >0 ): if DEBUGLEVEL >= 3: print "Global balance: %s - Global transactions: %s" %(balance,transactions) globalvelocity = balance / transactions if DEBUGLEVEL >= 3: print "Global velocity over the last %s days: %s" %(days,globalvelocity) return globalvelocity #determines if a transaction will be successful or will fail (simulating acceptance, etc.) def evalShouldTransactionBeSuccessful(self): self.checkPercentage() rate = self.variables.percentSuccessfulTransactions/100.0 rnd = random.random() if rnd <= rate: successful = 1 #above success rate --> succeeds else: successful = 0 #below --> fails return successful #checks if the amount of ta's happening with existing businesses rather new ones should be adjusted def shouldIncreaseNewBusinessRate(self,existingBiz): if existingBiz == 0: return tmp = existingBiz % vars.newBizThreshold if tmp <= vars.newBizThreshold: if tmp==0 and vars.percentNewBusiness != 0: vars.percentNewBusiness = vars.percentNewBusiness - vars.percentNewBusinessAdjustment vars.newBizThreshold = vars.newBizThreshold + vars.newBizThresholdStep return