Skip to content

Commit

Permalink
Merge pull request #329 from hugoch642/master
Browse files Browse the repository at this point in the history
Small modifications to allow multiple orders being executed in one environment step
  • Loading branch information
carlogrisetti committed Jun 19, 2021
2 parents 2c5cb65 + 1489399 commit 4a1ea95
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion tensortrade/oms/instruments/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class Quantity:

def __init__(self, instrument: 'Instrument', size: Union[Decimal, Number], path_id: str = None):
if size < 0:
raise InvalidNegativeQuantity(size)
if abs(size) > Decimal(10)**(-instrument.precision):
raise InvalidNegativeQuantity(size)
else:
size = 0

self.instrument = instrument
self.size = size if isinstance(size, Decimal) else Decimal(size)
Expand Down
6 changes: 5 additions & 1 deletion tensortrade/oms/orders/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ def update(self) -> None:
Then the broker will find any orders that are active, but expired, and
proceed to cancel them.
"""
executed_ids = []
for order in self.unexecuted:
if order.is_executable:
self.unexecuted.remove(order)
executed_ids.append(order.id)
self.executed[order.id] = order

order.attach(self)
order.execute()

for order_id in executed_ids:
self.unexecuted.remove(self.executed[order_id])

for order in self.unexecuted + list(self.executed.values()):
if order.is_active and order.is_expired:
Expand Down
4 changes: 2 additions & 2 deletions tensortrade/oms/orders/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ def proportion_order(portfolio: 'Portfolio',

pair = portfolio.base_instrument / target.instrument

order += OrderSpec(
order.add_order_spec(OrderSpec(
side=TradeSide.BUY,
trade_type=TradeType.MARKET,
exchange_pair=ExchangePair(exchange, pair),
criteria=None
)
))
return order
15 changes: 12 additions & 3 deletions tensortrade/oms/wallets/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def lock(self, quantity, order: 'Order', reason: str) -> 'Quantity':
raise DoubleLockedQuantity(quantity)

if quantity > self.balance:
raise InsufficientFunds(self.balance, quantity)
if (quantity-self.balance)>Decimal(10)**(-self.instrument.precision+2):
raise InsufficientFunds(self.balance, quantity)
else:
quantity = self.balance

self.balance -= quantity

Expand Down Expand Up @@ -230,12 +233,18 @@ def withdraw(self, quantity: 'Quantity', reason: str) -> 'Quantity':
if quantity.is_locked and self._locked.get(quantity.path_id, False):
locked_quantity = self._locked[quantity.path_id]
if quantity > locked_quantity:
raise InsufficientFunds(self._locked[quantity.path_id], quantity)
if (quantity-locked_quantity)>Decimal(10)**(-self.instrument.precision+2):
raise InsufficientFunds(locked_quantity, quantity)
else:
quantity = locked_quantity
self._locked[quantity.path_id] -= quantity

elif not quantity.is_locked:
if quantity > self.balance:
raise InsufficientFunds(self.balance, quantity)
if (quantity-self.balance)>Decimal(10)**(-self.instrument.precision+2):
raise InsufficientFunds(self.balance, quantity)
else:
quantity = self.balance
self.balance -= quantity

self.balance = self.balance.quantize()
Expand Down

0 comments on commit 4a1ea95

Please sign in to comment.