def plotAccountPnl(pyTradeMonster, transactionType, accountNumber, start, end, filterTicker=None): ''' Retrieve all live accounts' activity and plot cumulative pnl for a given transaction type and account Filter ticker filters out PNL by an individual ticker, i.e. 'GOOG' ''' accountService = AccountServices(pyTradeMonster) accounts = accountService.getParsedAccountObjects() graphFrame = pd.DataFrame() for account in accounts.itervalues(): if account.accountNumber == accountNumber and account.accountType != TradeMonsterConstants.AccountRequests.ACCOUNT_TYPES.OPTION: log.info('Processing account: {0}'.format(account)) accountHistory = accountService.getParsedAccountHistory( account, MAX_TRANSACTIONS, transactionType, start, end) historyList = [{ key: value for key, value in x.__dict__.items() if not key.startswith('__') and not callable(key) } for x in accountHistory] historyFrame = pd.DataFrame(historyList) historyFrame = historyFrame.reindex( index=historyFrame.index[::-1] ) #make dataframe sorted in ascending chronological order historyFrame.transactionDate = historyFrame.transactionDate.str[: -6] if filterTicker: historyFrame = historyFrame[ historyFrame['symbol'].str.contains(filterTicker)] historyFrame['date'] = historyFrame.transactionDate.apply( lambda d: datetime.strptime( d, TradeMonsterConstants.TRANSACTION_TIME)) historyFrame['cumPnl'] = historyFrame.amount.astype(float).cumsum() graphFrame = graphFrame.append(historyFrame) plot = ggplot(aes(x='date', y='cumPnl'), data=graphFrame) + geom_line() print plot
def setUpClass(self): self.pyTradeMonster = PyTradeMonster('../cred.dat') self.positionService = PositionServices(self.pyTradeMonster) self.accountsService = AccountServices(self.pyTradeMonster) self.accounts = self.accountsService.getParsedAccountObjects()