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)
while len(broker.unexecuted) > 0:
def execute_order(self, order: 'Order', portfolio: 'Portfolio'):
if order.type == TradeType.LIMIT and order.side == TradeSide.BUY:
executed_order = self._exchange.create_limit_buy_order(
order.symbol, order.size, order.price)
elif order.type == TradeType.MARKET and order.side == TradeSide.BUY:
executed_order = self._exchange.create_market_buy_order(order.symbol, order.size)
elif order.type == TradeType.LIMIT and order.side == TradeSide.SELL:
executed_order = self._exchange.create_limit_sell_order(
order.symbol, order.size, order.price)
elif order.type == TradeType.MARKET and order.side == TradeSide.SELL:
executed_order = self._exchange.create_market_sell_order(order.symbol, order.size)
else:
return order.copy()
max_wait_time = time.time() + self._max_trade_wait_in_sec
while order['status'] == 'open' and time.time() < max_wait_time:
executed_order = self._exchange.fetch_order(order.id)
the trade amount is determined by the multiplicity of the action.
For example:
0 = HOLD
1 = LIMIT_BUY|0.25
2 = MARKET_BUY|0.25
5 = HOLD
6 = LIMIT_BUY|0.5
7 = MARKET_BUY|0.5
etc.
"""
instrument_idx = int(action / self._actions_per_instrument)
instrument = self._instruments[instrument_idx]
n_splits = int(self._actions_per_instrument / len(TradeType))
trade_type = TradeType(action % len(TradeType))
trade_amount = int(action / len(TradeType)) * \
float(1 / n_splits) + (1 / n_splits)
trade_amount = trade_amount - instrument_idx
current_price = self._exchange.current_price(symbol=instrument)
base_precision = self._exchange.base_precision
instrument_precision = self._exchange.instrument_precision
amount = self._exchange.instrument_balance(instrument)
price = current_price
if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY:
price_adjustment = 1 + (self._max_allowed_slippage_percent / 100)
price = max(round(current_price * price_adjustment, base_precision), base_precision)
amount = round(self._exchange.balance * 0.99 *
trade_amount / price, instrument_precision)
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade:
action_type, trade_amount = action
trade_type = TradeType(int(action_type * len(TradeType)))
current_price = self._exchange.current_price(symbol=self._instrument)
base_precision = self._exchange.base_precision
instrument_precision = self._exchange.instrument_precision
amount = self._exchange.instrument_balance(self._instrument)
price = current_price
if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY:
price_adjustment = 1 + (self.max_allowed_slippage_percent / 100)
price = max(round(current_price * price_adjustment, base_precision), base_precision)
amount = round(self._exchange.balance * 0.99 *
trade_amount / price, instrument_precision)
elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL:
price_adjustment = 1 - (self.max_allowed_slippage_percent / 100)
instrument = self._instruments[instrument_idx]
n_splits = int(self._actions_per_instrument / len(TradeType))
trade_type = TradeType(action % len(TradeType))
trade_amount = int(action / len(TradeType)) * \
float(1 / n_splits) + (1 / n_splits)
trade_amount = trade_amount - instrument_idx
current_price = self._exchange.current_price(symbol=instrument)
base_precision = self._exchange.base_precision
instrument_precision = self._exchange.instrument_precision
amount = self._exchange.instrument_balance(instrument)
price = current_price
if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY:
price_adjustment = 1 + (self._max_allowed_slippage_percent / 100)
price = max(round(current_price * price_adjustment, base_precision), base_precision)
amount = round(self._exchange.balance * 0.99 *
trade_amount / price, instrument_precision)
elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL:
price_adjustment = 1 - (self._max_allowed_slippage_percent / 100)
price = round(current_price * price_adjustment, base_precision)
amount_held = self._exchange.portfolio.get(instrument, 0)
amount = round(amount_held * trade_amount, instrument_precision)
return Trade(current_step, instrument, trade_type, amount, price)
def _execute_buy_order(self, order: 'Order', base_wallet: 'Wallet', quote_wallet: 'Wallet', current_price: float) -> Trade:
price = self._contain_price(current_price)
if order.type == TradeType.LIMIT and order.price < current_price:
return None
commission = Quantity(order.pair.base, order.size * self._commission, order.path_id)
size = self._contain_size(order.size - commission.size)
if order.type == TradeType.MARKET:
size = self._contain_size(order.price / price * order.size - commission.size)
quantity = Quantity(order.pair.base, size, order.path_id)
trade = Trade(order_id=order.id,
exchange_id=self.id,
step=self.clock.step,
pair=order.pair,
side=TradeSide.BUY,
trade_type=order.type,
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade:
action_type, trade_amount = action
trade_type = TradeType(int(action_type * len(TradeType)))
current_price = self._exchange.current_price(symbol=self._instrument)
base_precision = self._exchange.base_precision
instrument_precision = self._exchange.instrument_precision
amount = self._exchange.instrument_balance(self._instrument)
price = current_price
if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY:
price_adjustment = 1 + (self.max_allowed_slippage_percent / 100)
price = max(round(current_price * price_adjustment, base_precision), base_precision)
amount = round(self._exchange.balance * 0.99 *
trade_amount / price, instrument_precision)
elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL:
price_adjustment = 1 - (self.max_allowed_slippage_percent / 100)
price = round(current_price * price_adjustment, base_precision)
amount_held = self._exchange.portfolio.get(self._instrument, 0)
amount = round(amount_held * trade_amount, instrument_precision)
return Trade(current_step, self._instrument, trade_type, amount, price)