3 changed files with 85 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||
""" |
|||
Auto trading python bot. |
|||
""" |
@ -0,0 +1,52 @@ |
|||
""" |
|||
Define basic interfaces for trading strategies. |
|||
""" |
|||
from abc import ABC, abstractmethod |
|||
|
|||
|
|||
class Portfolio(ABC): |
|||
"""How may of each money do I have ?""" |
|||
|
|||
@abstractmethod |
|||
def content(self) -> dict: |
|||
"""return the content in each currency""" |
|||
|
|||
@abstractmethod |
|||
def widraw(self, amount: int, currency: str) -> None: |
|||
"""Withdraw money from the portfolio""" |
|||
|
|||
@abstractmethod |
|||
def deposit(self, amount: int, currency: str) -> None: |
|||
"""Deposit money into the portfolio""" |
|||
|
|||
@abstractmethod |
|||
def convert(self, amount: int, currency: str, to_currency: str) -> None: |
|||
"""Convert money from one currency to another""" |
|||
|
|||
|
|||
class Strategy(ABC): |
|||
"""When do I buy and how many ?""" |
|||
|
|||
@abstractmethod |
|||
def run(self, result: dict, ptf: Portfolio) -> None: |
|||
"""Run the strategy""" |
|||
|
|||
|
|||
class Broker(ABC): |
|||
"""Return the data""" |
|||
|
|||
@abstractmethod |
|||
def __bool__(self): |
|||
"""Return True if the broker has data to retrive""" |
|||
|
|||
@abstractmethod |
|||
def next(self): |
|||
"""Return the next data to process""" |
|||
|
|||
|
|||
class Predictor(ABC): |
|||
"""What is the future ?""" |
|||
|
|||
@abstractmethod |
|||
def predict(self, data: dict) -> dict: |
|||
"""Return the prediction""" |
@ -0,0 +1,30 @@ |
|||
""" |
|||
One script to rule them all, One script to find them, |
|||
One script to bring them all and in the darkness bind them |
|||
""" |
|||
from .interfaces import Portfolio, Strategy, Broker, Predictor |
|||
|
|||
|
|||
class Bot: |
|||
"""the main class""" |
|||
|
|||
def __init__(self, ptf: Portfolio, strategy: Strategy, broker: Broker, predictor: Predictor): |
|||
"""initialize the bot""" |
|||
self.ptf = ptf |
|||
self.strategy = strategy |
|||
self.broker = broker |
|||
self.predictor = predictor |
|||
|
|||
def run(self): |
|||
"""run the bot""" |
|||
while self.broker: |
|||
self.run_once() |
|||
|
|||
def run_once(self): |
|||
"""run the bot once""" |
|||
self.strategy.run(self.predictor.predict(self.broker.next()), self.ptf) |
|||
|
|||
def print_results(self): |
|||
"""print the results""" |
|||
for k,v in self.ptf.content().items(): |
|||
print(f"{k}: {v}") |
Loading…
Reference in new issue