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_path_order_runs_though_broker():
wallets = [
Wallet(exchange, 10000 * USD),
Wallet(exchange, 0 * BTC)
]
portfolio = Portfolio(base_instrument=USD, wallets=wallets)
exchange.reset()
portfolio.reset()
broker.reset()
base_wallet = portfolio.get_wallet(exchange.id, USD)
quantity = (1 / 10) * base_wallet.balance
order = Order(side=TradeSide.BUY,
trade_type=TradeType.MARKET,
pair=USD / BTC,
quantity=quantity,
portfolio=portfolio)
order = order.add_recipe(
Recipe(
side=TradeSide.SELL,
trade_type=TradeType.MARKET,
pair=USD/BTC,
criteria=StopLoss(direction=StopDirection.EITHER, up_percent=0.02, down_percent=0.10)
)
)
broker.submit(order)
def test_init():
wallets = [
Wallet(exchange, 10000 * USD),
Wallet(exchange, 0 * BTC)
]
portfolio = Portfolio(base_instrument=USD, wallets=wallets)
base_wallet = portfolio.get_wallet(exchange.id, USD)
quantity = (1 / 10) * base_wallet.balance
order = Order(side=TradeSide.BUY,
trade_type=TradeType.MARKET,
pair=USD/BTC,
quantity=quantity,
portfolio=portfolio)
assert order
assert order.id
assert order.path_id
assert order.quantity.instrument == USD
assert order.filled_size == 0
assert order.remaining_size == order.quantity
assert isinstance(order.pair, TradingPair)
assert order.pair.base == USD
assert order.pair.quote == BTC
def test_order_runs_through_broker():
wallets = [
Wallet(exchange, 10000 * USD),
Wallet(exchange, 0 * BTC)
]
portfolio = Portfolio(base_instrument=USD, wallets=wallets)
exchange.reset()
portfolio.reset()
broker.reset()
base_wallet = portfolio.get_wallet(exchange.id, USD)
quantity = (1 / 10) * base_wallet.balance
order = Order(side=TradeSide.BUY,
trade_type=TradeType.MARKET,
pair=USD / BTC,
quantity=quantity,
portfolio=portfolio)
base_wallet -= quantity.size * order.pair.base
base_wallet += order.quantity
broker.submit(order)
broker.update()
portfolio.update()
def test_adding_recipe():
wallets = [
Wallet(exchange, 10000 * USD),
Wallet(exchange, 0 * BTC)
]
portfolio = Portfolio(base_instrument=USD, wallets=wallets)
base_wallet = portfolio.get_wallet(exchange.id, USD)
quantity = (1 / 10) * base_wallet.balance
order = Order(side=TradeSide.BUY,
trade_type=TradeType.MARKET,
pair=USD/BTC,
quantity=quantity,
portfolio=portfolio)
order += Recipe(
side=TradeSide.SELL,
trade_type=TradeType.MARKET,
pair=BTC/USD,
criteria=StopLoss(direction=StopDirection.EITHER, up_percent=0.02, down_percent=0.10),
)
assert order.pair
def test_buy_on_exchange():
wallets = [
Wallet(exchange, 10000 * USD),
Wallet(exchange, 0 * BTC)
]
portfolio = Portfolio(base_instrument=USD, wallets=wallets)
base_wallet = portfolio.get_wallet(exchange.id, USD)
quote_wallet = portfolio.get_wallet(exchange.id, BTC)
quantity = (1 / 10) * base_wallet.balance
order = Order(side=TradeSide.BUY,
trade_type=TradeType.MARKET,
pair=USD/BTC,
quantity=quantity,
portfolio=portfolio)
current_price = 12390.90
base_wallet -= quantity.size * order.pair.base
base_wallet += order.quantity
trade = exchange._execute_buy_order(order, base_wallet, quote_wallet, current_price)
assert trade
assert trade.size == 1000 * USD
assert trade.commission == 3 * USD
def test_sell_on_exchange():
wallets = [
Wallet(exchange, 0 * USD),
Wallet(exchange, 10 * BTC)
]
portfolio = Portfolio(base_instrument=USD, wallets=wallets)
base_wallet = portfolio.get_wallet(exchange.id, USD)
quote_wallet = portfolio.get_wallet(exchange.id, BTC)
quantity = (1 / 10) * quote_wallet.balance
order = Order(side=TradeSide.SELL,
trade_type=TradeType.MARKET,
pair=USD / BTC,
quantity=quantity,
portfolio=portfolio)
current_price = 12390.90
quote_wallet -= quantity.size * order.pair.quote
quote_wallet += order.quantity
trade = exchange._execute_sell_order(order, base_wallet, quote_wallet, current_price)
assert trade
assert trade.size == 0.997 * BTC
assert trade.commission == 0.003 * BTC
(side, pair, criteria, size) = self._actions[action]
instrument = pair.base if side == TradeSide.BUY else pair.quote
wallet = portfolio.get_wallet(exchange.id, instrument=instrument)
price = exchange.quote_price(instrument)
amount = min(wallet.balance.amount, (wallet.balance.amount * size))
if amount < 10 ** -instrument.precision:
return None
quantity = amount * instrument
wallet -= quantity
order = Order(side=side,
trade_type=self._trade_type,
pair=pair,
price=price,
quantity=quantity,
portfolio=portfolio,
criteria=criteria)
quantity.lock_for(order.id)
wallet += quantity
if self._order_listener is not None:
order.attach(self._order_listener)
return order
(order, size) = self._actions[action]
instrument = order.pair.base if order.side == TradeSide.BUY else order.pair.quote
wallet = portfolio.get_wallet(exchange.id, instrument=instrument)
price = exchange.quote_price(instrument)
size = min(wallet.balance.size, (wallet.balance.size * size))
if size < 10 ** -instrument.precision:
return None
quantity = size * instrument
wallet -= quantity
order = Order(side=order.side,
trade_type=order.trade_type,
pair=order.pair,
price=price,
quantity=quantity,
portfolio=portfolio,
criteria=order.criteria,
followed_by=order.followed_by)
quantity.lock_for(order.id)
wallet += quantity
if self._order_listener is not None:
order.attach(self._order_listener)
return order
return None
(pair, stop_loss, take_profit, size) = self._actions[action]
base_instrument = pair.base if self._trade_side == TradeSide.BUY else pair.quote
base_wallet = portfolio.get_wallet(exchange.id, instrument=base_instrument)
price = exchange.quote_price(pair)
size = min(base_wallet.balance.size, (base_wallet.balance.size * size))
if size < 10 ** -base_instrument.precision:
return None
buy_quantity = size * base_instrument
order = Order(side=self._trade_side,
trade_type=self._trade_type,
pair=pair,
price=price,
quantity=buy_quantity,
portfolio=portfolio)
risk_criteria = StopLoss(direction='either',
up_percent=take_profit,
down_percent=stop_loss)
risk_management = Recipe(side=TradeSide.SELL if self._trade_side == TradeSide.BUY else TradeSide.BUY,
trade_type=TradeType.MARKET,
pair=pair,
criteria=risk_criteria)
order.add_recipe(risk_management)
(side, pair, criteria, size) = self._actions[action]
instrument = pair.base if side == TradeSide.BUY else pair.quote
wallet = portfolio.get_wallet(exchange.id, instrument=instrument)
price = exchange.quote_price(instrument)
size = min(wallet.balance.size, (wallet.balance.size * size))
if size < 10 ** -instrument.precision:
return None
quantity = size * instrument
wallet -= quantity
order = Order(side=side,
trade_type=self._trade_type,
pair=pair,
price=price,
quantity=quantity,
portfolio=portfolio,
criteria=criteria)
quantity.lock_for(order.id)
wallet += quantity
if self._order_listener is not None:
order.attach(self._order_listener)
return order