Browse Source

spread dans ptf/in_memory.py

thibaudlabat-spread
thibaudlabat 7 months ago
parent
commit
27859841c1
  1. 17
      auto_trading/ptf/in_memory.py

17
auto_trading/ptf/in_memory.py

@ -15,6 +15,8 @@ class InMemoryPortfolio(PTF):
self,
change_rate_getter: Callable[[], Dict[str, float]],
base_balance: float = 0,
*,
short_spread:float=0.0,
**kwargs
):
"""Init the class with a pointer to the bot to retrieve the change rate."""
@ -22,6 +24,7 @@ class InMemoryPortfolio(PTF):
self._balance = base_balance
self._stocks = {}
self.change_rate_getter = change_rate_getter
self.short_spread = short_spread
@property
def state(self):
@ -32,25 +35,25 @@ class InMemoryPortfolio(PTF):
return self.change_rate_getter()
def execute_short(self, order: Short) -> None:
price_with_spread = order.price * (1-self.short_spread)
"""Sell actions."""
if self._stocks[order.stock] < order.amount:
if self._stocks.get(order.stock,0.0)< order.amount:
raise OrderFails("Not enough stock.")
if self.change_rate.get(order.stock, 0) < order.price:
raise OrderFails("You shell it too high.")
self._balance += order.price * order.amount
self._balance += price_with_spread * order.amount
self._stocks[order.stock] -= order.amount
def execute_long(self, order: Long) -> None:
"""Buy actions."""
SPREAD=1.0001
PRICE = order.price * SPREAD
if self.balance < order.amount * PRICE:
if self.balance < order.amount * order.price:
raise OrderFails("Not enough money.")
if self.change_rate.get(order.stock, 0) > PRICE:
if self.change_rate.get(order.stock, 0) > order.price:
raise OrderFails("You buy it too low.")
self._balance -= PRICE * order.amount
self._balance -= order.price * order.amount
self._stocks[order.stock] = order.amount + self._stocks.get(order.stock, 0)
executors = {Short: execute_short, Long: execute_long}

Loading…
Cancel
Save