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 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
class TestPositionService(unittest.TestCase): """ Test parsing of positions """ @classmethod def setUpClass(self): self.pyTradeMonster = PyTradeMonster('../cred.dat') self.positionService = PositionServices(self.pyTradeMonster) self.accountsService = AccountServices(self.pyTradeMonster) self.accounts = self.accountsService.getParsedAccountObjects() def testGetSinglePosition(self): positions = self.positionService.getParsedPositionsDetail(self.accounts[ACCOUNT_NUMBER].accountId) if len(positions) > 0: print 'Found positions' else : self.fail('No positions found or unable to parse, please have a live position in the account first!')
class TestPositionService(unittest.TestCase): """ Test parsing of positions """ @classmethod def setUpClass(self): self.pyTradeMonster = PyTradeMonster('../cred.dat') self.positionService = PositionServices(self.pyTradeMonster) self.accountsService = AccountServices(self.pyTradeMonster) self.accounts = self.accountsService.getParsedAccountObjects() def testGetSinglePosition(self): positions = self.positionService.getParsedPositionsDetail( self.accounts[ACCOUNT_NUMBER].accountId) if len(positions) > 0: print 'Found positions' else: self.fail( 'No positions found or unable to parse, please have a live position in the account first!' )
def setUpClass(self): self.pyTradeMonster = PyTradeMonster('../cred.dat') self.positionService = PositionServices(self.pyTradeMonster) self.accountsService = AccountServices(self.pyTradeMonster) self.accounts = self.accountsService.getParsedAccountObjects()
class TestOrderService(unittest.TestCase): """ Test most functionality surrounding submission, execution, and retrieval of orders """ @classmethod def setUpClass(self): self.pyTradeMonster = PyTradeMonster('../cred.dat') self.orderService = OrderServices(self.pyTradeMonster) self.accountsService = AccountServices(self.pyTradeMonster) self.accounts = self.accountsService.getParsedAccountObjects() @classmethod def tearDownClass(self): ''' Cancel all outstanding orders :return: ''' print 'Going to cancel all outstanding orders from unit testing...' self.orderService.sendCancelAllOrders( self.accounts[ACCOUNT_NUMBER].accountNumber) def createSimpleLimitOrder(self): order = LimitOrder() orderLeg = OrderLeg() orderLeg.instrumentType = TradeMonsterConstants.INSTRUMENTS.EQUITY orderLeg.symbol = 'SPY' orderLeg.orderSide = OrderLeg.side.BUY order.price = 0.01 order.quantity = 1 order.orderLegs = [orderLeg] order.instrumentType = TradeMonsterConstants.INSTRUMENTS.EQUITY order.timeInForce = LimitOrder.timeInForceEnum.DAY order.marketSession = LimitOrder.marketSessionEnum.REG return order def testSingleLimitOrder(self): self.orderService.sendCancelAllOrders( self.accounts[ACCOUNT_NUMBER].accountNumber ) #cancel everything first just in case order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) status = orderResponse.status print 'Status of order is {0}'.format(status) self.assertTrue(status in OrderStatus.status.__dict__.keys()) def testGetOrderConfirmation(self): self.orderService.sendCancelAllOrders( self.accounts[ACCOUNT_NUMBER].accountNumber ) #cancel everything first just in case order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) print 'Trying to confirm order...' result = self.orderService.getOrderConfirmation( self.accounts[ACCOUNT_NUMBER], order, orderResponse) confirm = result[TradeMonsterConstants.ResponseRoots. RETRIEVE_ORDER_CONFIRMATION_ROOT] self.assertTrue(confirm['orderDescription'] != None) def testSpreadOrder(self): ''' Test a simple buy spread (debit) :return: ''' self.orderService.sendCancelAllOrders( self.accounts[ACCOUNT_NUMBER].accountNumber ) #cancel everything first just in case order = LimitOrder() shortLeg = OrderLeg() longLeg = OrderLeg() shortLeg.instrumentType = TradeMonsterConstants.INSTRUMENTS.OPTION shortLeg.symbol = 'SPYX1517C300000' shortLeg.orderSide = OrderLeg.side.SELL shortLeg.quantityRatio = 1 longLeg.instrumentType = TradeMonsterConstants.INSTRUMENTS.OPTION longLeg.symbol = 'SPYX1517C310000' longLeg.orderSide = OrderLeg.side.BUY longLeg.quantityRatio = 1 order.price = 0.01 order.quantity = 1 order.instrumentType = TradeMonsterConstants.INSTRUMENTS.OPTION order.timeInForce = LimitOrder.timeInForceEnum.DAY order.marketSession = LimitOrder.marketSessionEnum.REG order.orderLegs = [] order.orderLegs.append(shortLeg) order.orderLegs.append(longLeg) order.spreadName = TradeMonsterConstants.OrderRequests.ORDER_SPREAD_TYPES.PUT_VERTICAL #send a live order with a silly price result = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) status = result.status print 'Status of order is {0}'.format(status) self.assertTrue(status in OrderStatus.status.__dict__.keys()) def testCancelSingleOrder(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) print 'Going to cancel order', orderResponse.orderId, '...' time.sleep(1) result = self.orderService.sendCancelOrder(orderResponse.orderId) self.assertTrue(TradeMonsterConstants.ResponseRoots. RETRIEVE_ORDER_CANCELLED_ROOT in result) def testCancelAllOrders(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) print 'Going to cancel all orders', orderResponse.orderId, '...' time.sleep(1) result = self.orderService.sendCancelAllOrders( self.accounts[ACCOUNT_NUMBER].accountNumber) self.assertTrue(TradeMonsterConstants.ResponseRoots. RETRIEVE_ALL_CANCELLED_ROOT in result) def testCancelDayOrder(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) print 'Going to cancel day order', orderResponse.orderId, '...' time.sleep(1) result = self.orderService.sendCancelDayOrders( self.accounts[ACCOUNT_NUMBER].accountNumber) self.assertTrue(TradeMonsterConstants.ResponseRoots. RETRIEVE_DAY_CANCELLED_ROOT in result) def testCountAllOrders(self): order = self.createSimpleLimitOrder() self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) order = self.createSimpleLimitOrder() self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) print 'Going to count all orders...' time.sleep(1) result = self.orderService.sendCountAllOpenOrders( self.accounts[ACCOUNT_NUMBER].accountNumber) print 'Counted', result, 'orders total' self.assertEquals(result, 2) def testCountDayOrders(self): self.orderService.sendCancelAllOrders( self.accounts[ACCOUNT_NUMBER].accountNumber ) #cancel everything first just in case order = self.createSimpleLimitOrder() self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) print 'Going to count day orders...' time.sleep(1) result = self.orderService.sendCountDayOrders( self.accounts[ACCOUNT_NUMBER].accountNumber) print 'Counted', result, 'day orders' self.assertEquals(result, 1) def testGetOrderHistory(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) result = self.orderService.sendGetOrderHistory(orderResponse.orderId) self.fail( 'TradeMonster call getOrderHistory not yet working - followup with them...' ) def testGetOrderDetails(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse( self.accounts[ACCOUNT_NUMBER], order) result = self.orderService.sendGetOrderDetail(orderResponse.orderId) self.assertTrue(TradeMonsterConstants.ResponseRoots. RETRIEVE_ORDER_DETAILS_ROOT in result)
class TestOrderService(unittest.TestCase): """ Test most functionality surrounding submission, execution, and retrieval of orders """ @classmethod def setUpClass(self): self.pyTradeMonster = PyTradeMonster('../cred.dat') self.orderService = OrderServices(self.pyTradeMonster) self.accountsService = AccountServices(self.pyTradeMonster) self.accounts = self.accountsService.getParsedAccountObjects() @classmethod def tearDownClass(self): ''' Cancel all outstanding orders :return: ''' print 'Going to cancel all outstanding orders from unit testing...' self.orderService.sendCancelAllOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) def createSimpleLimitOrder(self): order = LimitOrder() orderLeg = OrderLeg() orderLeg.instrumentType = TradeMonsterConstants.INSTRUMENTS.EQUITY orderLeg.symbol = 'SPY' orderLeg.orderSide = OrderLeg.side.BUY order.price = 0.01 order.quantity = 1 order.orderLegs = [orderLeg] order.instrumentType = TradeMonsterConstants.INSTRUMENTS.EQUITY order.timeInForce = LimitOrder.timeInForceEnum.DAY order.marketSession = LimitOrder.marketSessionEnum.REG return order def testSingleLimitOrder(self): self.orderService.sendCancelAllOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) #cancel everything first just in case order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) status = orderResponse.status print 'Status of order is {0}'.format(status) self.assertTrue(status in OrderStatus.status.__dict__.keys() ) def testGetOrderConfirmation(self): self.orderService.sendCancelAllOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) #cancel everything first just in case order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) print 'Trying to confirm order...' result = self.orderService.getOrderConfirmation(self.accounts[ACCOUNT_NUMBER], order, orderResponse) confirm = result[TradeMonsterConstants.ResponseRoots.RETRIEVE_ORDER_CONFIRMATION_ROOT] self.assertTrue(confirm['orderDescription'] != None) def testSpreadOrder(self): ''' Test a simple buy spread (debit) :return: ''' self.orderService.sendCancelAllOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) #cancel everything first just in case order = LimitOrder() shortLeg = OrderLeg() longLeg = OrderLeg() shortLeg.instrumentType = TradeMonsterConstants.INSTRUMENTS.OPTION shortLeg.symbol = 'SPYX1517C300000' shortLeg.orderSide = OrderLeg.side.SELL shortLeg.quantityRatio = 1 longLeg.instrumentType = TradeMonsterConstants.INSTRUMENTS.OPTION longLeg.symbol = 'SPYX1517C310000' longLeg.orderSide = OrderLeg.side.BUY longLeg.quantityRatio = 1 order.price = 0.01 order.quantity = 1 order.instrumentType = TradeMonsterConstants.INSTRUMENTS.OPTION order.timeInForce = LimitOrder.timeInForceEnum.DAY order.marketSession = LimitOrder.marketSessionEnum.REG order.orderLegs = [] order.orderLegs.append(shortLeg) order.orderLegs.append(longLeg) order.spreadName = TradeMonsterConstants.OrderRequests.ORDER_SPREAD_TYPES.PUT_VERTICAL #send a live order with a silly price result = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) status = result.status print 'Status of order is {0}'.format(status) self.assertTrue(status in OrderStatus.status.__dict__.keys()) def testCancelSingleOrder(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) print 'Going to cancel order',orderResponse.orderId,'...' time.sleep(1) result = self.orderService.sendCancelOrder(orderResponse.orderId) self.assertTrue(TradeMonsterConstants.ResponseRoots.RETRIEVE_ORDER_CANCELLED_ROOT in result) def testCancelAllOrders(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) print 'Going to cancel all orders',orderResponse.orderId,'...' time.sleep(1) result = self.orderService.sendCancelAllOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) self.assertTrue(TradeMonsterConstants.ResponseRoots.RETRIEVE_ALL_CANCELLED_ROOT in result) def testCancelDayOrder(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) print 'Going to cancel day order',orderResponse.orderId,'...' time.sleep(1) result = self.orderService.sendCancelDayOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) self.assertTrue(TradeMonsterConstants.ResponseRoots.RETRIEVE_DAY_CANCELLED_ROOT in result) def testCountAllOrders(self): order = self.createSimpleLimitOrder() self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) order = self.createSimpleLimitOrder() self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) print 'Going to count all orders...' time.sleep(1) result = self.orderService.sendCountAllOpenOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) print 'Counted', result, 'orders total' self.assertEquals(result,2) def testCountDayOrders(self): self.orderService.sendCancelAllOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) #cancel everything first just in case order = self.createSimpleLimitOrder() self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) print 'Going to count day orders...' time.sleep(1) result = self.orderService.sendCountDayOrders(self.accounts[ACCOUNT_NUMBER].accountNumber) print 'Counted', result, 'day orders' self.assertEquals(result,1) def testGetOrderHistory(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) result = self.orderService.sendGetOrderHistory(orderResponse.orderId) self.fail('TradeMonster call getOrderHistory not yet working - followup with them...') def testGetOrderDetails(self): order = self.createSimpleLimitOrder() orderResponse = self.orderService.sendOrderAndGetParsedResponse(self.accounts[ACCOUNT_NUMBER], order) result = self.orderService.sendGetOrderDetail(orderResponse.orderId) self.assertTrue(TradeMonsterConstants.ResponseRoots.RETRIEVE_ORDER_DETAILS_ROOT in result)