def portfolio_holding_lifo_test(): """ test of 'lifo' queuing by adding and removing a position. """ p1 = position.Position(symbol="AAPL", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", qty=1500, price=184.00, multiplier=1., fee=7.0, total_amt=276000., trans_date=1054202245.63) p3 = position.Position(symbol="AAPL", qty=-1500, price=186.00, multiplier=1., fee=7.0, total_amt=279000., trans_date=1055902486.22) h = portfolio.Holding() h.add_to(p1) h.add_to(p2) h.remove_from(p3, order='lifo') print "Holding: ", h print "Positions length: ", len(h.positions) print "Positions price: ", h.positions[0].price, h.positions[0].fee assert h.qty==1000 assert len(h.positions)==1 # simple check to make sure the position that we expect is left over... p = h.positions[0] assert p.price==185.25
def portfolio_holding_fifo2_test(): """ test of 'fifo' and 'lifo' queuing by adding and removing a position. Note: there are no protections on 'removing' a position which is in the wrong direction. ## TODO: decide when to make determination of add or remove -- in the class or when deciding to call the appropriate method? """ p1 = position.Position(symbol="AAPL", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", qty=1500, price=184.00, multiplier=1., fee=7.0, total_amt=276000., trans_date=1054202245.63) p3 = position.Position(symbol="AAPL", qty=-500, price=186.00, multiplier=1., fee=7.0, total_amt=279000., trans_date=1055902486.22) h = portfolio.Holding() h.add_to(p1) h.add_to(p2) h.remove_from(p3, order='fifo') assert h.qty==2000 assert len(h.positions)==2 # simple check to make sure the positions that we expect are left over... p = h.positions[0] assert p.price==185.25 p = h.positions[1] assert p.price==184.00
def portfolio_port_test(): """ test of 'lifo' queuing by adding and removing a position. """ p1 = position.Position(symbol="AAPL", id="1110", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", id="1111", qty=1500, price=184.00, multiplier=1., fee=7.0, total_amt=276000., trans_date=1054202245.63) p3 = position.Position(symbol="GOOG", id="1112", qty=2000, price=286.00, multiplier=1., fee=7.0, total_amt=572000., trans_date=1055902486.22) h = portfolio.Holding() h.add_to(p1) h.add_to(p2) h2 = portfolio.Holding() h2.add_to(p3) portf = portfolio.Portfolio(name="Test Portfolio", holdings=[h, h2]) print dir(portf) print portf.holdings print portf.pprint assert len(portf.holdings) == 2 assert portf.holdings["GOOG"].symbol == "GOOG" return
def portfolio_holding_test(): """ Simple test to add position to holdings. """ p = position.Position(symbol="AAPL", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) h = portfolio.Holding() h.add_to(p) assert h.qty==1000 return
def main(): df = pd.read_csv(open('./test/transtest.csv'), comment="***") bought_filter = lambda x: str(x).startswith("Bought") sold_filter = lambda x: str(x).startswith("Sold") boughts = df['DESCRIPTION'].map(bought_filter) solds = df['DESCRIPTION'].map(sold_filter) # Allow the fees to be summed by replacing nans with 0.0s fee_cols = ["COMMISSION", "REG FEE"] for col in fee_cols: df[col].replace(np.nan, 0.0) dff = df[(boughts) | (solds)] port = portfolio.Portfolio(name="TD Ameritrade - BMP") # Loop through each Symbol as a holding symbs = np.asarray(dff['SYMBOL'].unique(), dtype='S50') for symb in symbs: # Filter for current symbol dff_symb = dff[dff['SYMBOL'] == symb] hld = portfolio.Holding() # Populate a holding with all positions for tran in dff_symb.iterrows(): tran_data = tran[1] if tran_data['DESCRIPTION'].startswith("Bought"): side = "BUY" elif tran_data['DESCRIPTION'].startswith("Sold"): side = "SELL" else: side = "UNKNOWN" p = position.Position(symbol=tran_data['SYMBOL'], id=tran_data['TRANSACTION ID'], description=tran_data['DESCRIPTION'], trans_date=tran_data['DATE'], qty=tran_data['QUANTITY'], price=tran_data['PRICE'], fee=tran_data['COMMISSION'] + tran_data['REG FEE'], total_amt=tran_data['AMOUNT'], side=side) if side == "BUY": print "Adding %s to Portfolio" % p hld.add_to(p) elif side == "SELL" and hasattr(hld, "positions"): print "Removing %s from Portfolio" % p hld.remove_from(p) else: print "Adding short position %s to Portfolio" % p hld.add_to(p) port.add_holding(hld) port.pprint() #if tran['DESCRIPTION'].startswith("Bought") # port.add_to(Position(transaction[''])) return port