Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_price_from_float(self):
parsed = PriceParser.parse(self.float)
self.assertEqual(parsed, 101234567)
self.assertIsInstance(parsed, int)
def test_unparsed_display(self):
displayed = PriceParser.display(self.float)
self.assertEqual(displayed, 10.12)
self.assertEqual(self.position.action, "BOT")
self.assertEqual(self.position.ticker, "XOM")
self.assertEqual(self.position.quantity, 0)
self.assertEqual(self.position.buys, 450)
self.assertEqual(self.position.sells, 450)
self.assertEqual(self.position.net, 0)
self.assertEqual(
PriceParser.display(self.position.avg_bot, 5), 74.65778
)
self.assertEqual(
PriceParser.display(self.position.avg_sld, 5), 74.95778
)
self.assertEqual(PriceParser.display(self.position.total_bot), 33596.00)
self.assertEqual(PriceParser.display(self.position.total_sld), 33731.00)
self.assertEqual(PriceParser.display(self.position.net_total), 135.00)
self.assertEqual(PriceParser.display(self.position.total_commission), 5.50)
self.assertEqual(PriceParser.display(self.position.net_incl_comm), 129.50)
self.assertEqual(
PriceParser.display(self.position.avg_price, 3), 74.665
)
self.assertEqual(PriceParser.display(self.position.cost_basis), 0.00)
self.assertEqual(PriceParser.display(self.position.market_value), 0.00)
self.assertEqual(PriceParser.display(self.position.unrealised_pnl), 0.00)
self.assertEqual(PriceParser.display(self.position.realised_pnl), 129.50)
)
# Finally, sell the remaining GOOG 100 shares
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(704.78), PriceParser.parse(1.00)
)
# The figures below are derived from Interactive Brokers
# demo account using the above trades with prices provided
# by their demo feed.
self.assertEqual(len(self.portfolio.positions), 0)
self.assertEqual(len(self.portfolio.closed_positions), 2)
self.assertEqual(PriceParser.display(self.portfolio.cur_cash), 499100.50)
self.assertEqual(PriceParser.display(self.portfolio.equity), 499100.50)
self.assertEqual(PriceParser.display(self.portfolio.unrealised_pnl), 0.00)
self.assertEqual(PriceParser.display(self.portfolio.realised_pnl), -899.50)
def setUp(self):
"""
Set up the Position object that will store the PnL.
"""
self.position = Position(
"BOT", "XOM", 100,
PriceParser.parse(74.78), PriceParser.parse(1.00),
PriceParser.parse(74.78), PriceParser.parse(74.80)
)
def x(p):
return PriceParser.display(p)
def calculate_signals(self, event):
if event.type in [EventType.TICK, EventType.BAR]:
# Format the event for human display
if event.type == EventType.BAR:
event.open_price = PriceParser.display(event.open_price)
event.high_price = PriceParser.display(event.high_price)
event.low_price = PriceParser.display(event.low_price)
event.close_price = PriceParser.display(event.close_price)
event.adj_close_price = PriceParser.display(event.adj_close_price)
else: # event.type == EventType.TICK
event.bid = PriceParser.display(event.bid)
event.ask = PriceParser.display(event.ask)
if self.i % self.n == 0 and self.i != 0:
print(s_speed(event, self.i, self.t0))
print("")
if self.i % self.n in range(self.n_window):
print("%d %s" % (self.i, event))
self.i += 1
def display(x): # flake8: noqa
return round(x / PriceParser.PRICE_MULTIPLIER, 2)
def _create_event(self, index, period, ticker, row):
"""
Obtain all elements of the bar from a row of dataframe
and return a BarEvent
"""
open_price = PriceParser.parse(row["Open"])
high_price = PriceParser.parse(row["High"])
low_price = PriceParser.parse(row["Low"])
close_price = PriceParser.parse(row["Close"])
adj_close_price = PriceParser.parse(row["Adj Close"])
volume = int(row["Volume"])
bev = BarEvent(
ticker, index, period, open_price,
high_price, low_price, close_price,
volume, adj_close_price
)
return bev
def calculate_signals(self, event):
if event.type in [EventType.TICK, EventType.BAR]:
# Format the event for human display
if event.type == EventType.BAR:
event.open_price = PriceParser.display(event.open_price)
event.high_price = PriceParser.display(event.high_price)
event.low_price = PriceParser.display(event.low_price)
event.close_price = PriceParser.display(event.close_price)
event.adj_close_price = PriceParser.display(event.adj_close_price)
else: # event.type == EventType.TICK
event.bid = PriceParser.display(event.bid)
event.ask = PriceParser.display(event.ask)
if self.i % self.n == 0 and self.i != 0:
print(s_speed(event, self.i, self.t0))
print("")
if self.i % self.n in range(self.n_window):
print("%d %s" % (self.i, event))
self.i += 1