Example #1
0
 def create_ExecutionFilter(self):
     filter = ExecutionFilter()
     filter.clientId = 999
     filter.acctCode = self.account
     yesterday = datetime.datetime.now() - datetime.timedelta(1)
     filter.time = yesterday.strftime('%Y%m%d-18:00:00')
     return filter
Example #2
0
    def commission_report(acctCode, time):
        class TestApp(EWrapper, EClient):
            def __init__(self):
                EClient.__init__(self, self)

                self.executed_orders = pd.DataFrame(columns=[
                    'ticker', 'time', 'shares', 'action', 'price',
                    'marketValue', 'RealizedPNL', 'commission'
                ])
                self.val = 0
                self.val2 = 0

            def error(self, reqId: TickerId, errorCode: int, errorString: str):
                if reqId > -1:
                    print("Error. Id: ", reqId, " Code: ", errorCode, " Msg: ",
                          errorString)

            def execDetails(self, reqId, contract, execution):
                super().execDetails(reqId, contract, execution)

                self.executed_orders.loc[self.val, [
                    'ticker', 'time', 'shares', 'action', 'price',
                    'marketValue'
                ]] = [
                    contract.symbol,
                    pd.to_datetime(execution.time), execution.shares,
                    execution.side, execution.price,
                    execution.shares * execution.price
                ]
                self.val = self.val + 1

            def commissionReport(self, commissionReport):
                super().commissionReport(commissionReport)

                self.executed_orders.loc[self.val2,
                                         ['RealizedPNL', 'commission']] = [
                                             float(
                                                 commissionReport.realizedPNL),
                                             float(commissionReport.commission)
                                         ]

                self.val2 = self.val2 + 1

            def execDetailsEnd(self, reqId):
                super().execDetailsEnd(reqId)
                self.disconnect()

        app = TestApp()
        app.connect('127.0.0.1', 7497, 0)

        execution_filter = ExecutionFilter()
        execution_filter.acctCode = acctCode
        execution_filter.time = time

        app.reqExecutions(0, execution_filter)
        sleep(sleeptime)

        df = app.executed_orders
        app.run()
        sleep(sleeptime)

        df.set_index('time', inplace=True)
        df.sort_index(inplace=True)
        df['RealizedPNL'][df['RealizedPNL'] > 1000000] = 'OPEN'

        return df