2 changed files with 22 additions and 12 deletions
@ -1,36 +1,46 @@ |
|||
from typing import Dict |
|||
|
|||
from pandas import DataFrame # type: ignore |
|||
from typing import Dict, Callable |
|||
|
|||
from ..interfaces import PTF, PTFState |
|||
from ..orders import Long, Short |
|||
from ..bot import Bot |
|||
from ..errors import OrderFails |
|||
|
|||
|
|||
class InMemoryPortfolio(PTF): |
|||
"""Just store the value in memory.""" |
|||
|
|||
_blanance: float |
|||
_balance: float |
|||
_stocks: Dict[str, float] |
|||
|
|||
def __init__(self, bot_class: Bot): |
|||
def __init__(self, change_rate_getter: Callable[[], Dict[str, float]]): |
|||
"""Init the class with a pointer to the bot to retrieve the change rate.""" |
|||
self.bot = bot_class |
|||
self.change_rate_getter = change_rate_getter |
|||
|
|||
@property |
|||
def state(self): |
|||
return PTFState(0, self._stocks.copy()) |
|||
return PTFState(self._balance, self._stocks.copy()) |
|||
|
|||
@property |
|||
def change_rate(self): |
|||
return self.bot.broker.change_rate |
|||
return self.change_rate_getter() |
|||
|
|||
def execute_short(self, order: Short) -> None: |
|||
"""Sell actions.""" |
|||
... |
|||
if self._stocks[order.stock] < order.amount: |
|||
raise OrderFails("Not enough stock to sell.") |
|||
if self.change_rate[order.stock] < order.price: |
|||
raise OrderFails("You shell it too high.") |
|||
|
|||
self._balance += order.price * order.amount |
|||
self._stocks[order.stock] -= order.amount |
|||
|
|||
def execute_long(self, order: Long) -> None: |
|||
"""Buy actions.""" |
|||
... |
|||
if self.balance < order.amount * order.price: |
|||
raise OrderFails("Not enough money.") |
|||
if self.change_rate[order.stock] > order.price: |
|||
raise OrderFails("You buy it too low.") |
|||
|
|||
self._balance -= order.price * order.amount |
|||
self._stocks[order.stock] += order.amount |
|||
|
|||
executors = {Short: execute_short, Long: execute_long} |
|||
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 21 KiB |
Loading…
Reference in new issue