pareeohnos / ktrade

A simple UI for managing your trades
MIT License
7 stars 5 forks source link

Pull in account details #8

Closed pareeohnos closed 3 years ago

pareeohnos commented 3 years ago

In order to correctly calculate position sizes when buying, we need to know details about the users account - specifically how large it is. The max size and risk of a trade is a percentage of your entire account, so without knowing what that is it cannot calculate the position size. Some code to pull this in thanks to Dolivent

class IB(EWrapper, EClient):
    def __init__(self):
        EWrapper.__init__(self)
        EClient.__init__(self, wrapper=self)
        self.connected = threading.Event()
        self.connect("127.0.0.1", 7496, 1)  # TODO pull address and port from config
        self.liquidityEstablished = threading.Event()
        self.liquidity = None

    def __del__(self):
        self.disconnect()

    # called when connection is actually established with TWS
    def nextValidId(self, orderId:int):
        self.connected.set()

    def getLiquidity(self):
        self.connected.wait()
        self.reqAccountSummary(9002, "All", "$LEDGER:USD")
        self.liquidityEstablished.wait()
        return self.liquidity

    def accountSummary(self, reqId:int, account:str, tag:str, value:str,
                       currency:str):
        if tag == "NetLiquidationByCurrency":
            self.liquidity = float(value)
            self.liquidityEstablished.set()