from abc import ABC, abstractmethod
from datetime import datetime, timezone

class AbstractBroker(ABC):
    """
    The ExecutionHandler abstract class handles the interaction
    between a set of order objects generated by a PortfolioHandler
    and the set of Fill objects that actually occur in the
    market.
    
    The handlers can be used to subclass simulated brokerages
    or live brokerages, with identical interfaces. This allows
    strategies to be backtested in a very similar manner to the
    live trading engine.
    """    
    @abstractmethod
    def execute_order(self):
        pass
    
    
    def save_to_db(self, fill_event):
        query = ("INSERT INTO FillEvent (Datetime, Ticker, Direction, Exchange, Price, Quantity, Commission)"
                "VALUES (%s, %s, %s, %s, %s, %s, %s)"
        )
        timestamp_converted = datetime.fromtimestamp(fill_event.timestamp, timezone.utc)
        data = (timestamp_converted, fill_event.ticker, fill_event.direction.name, 
                fill_event.exchange, fill_event.price, fill_event.quantity, fill_event.commission)
        cursor = self.db_client.cursor()
        cursor.execute(query, data)
        self.db_client.commit()   
