-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathib_broker_commissions.py
78 lines (60 loc) · 2.56 KB
/
ib_broker_commissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from sysbrokers.IB.ib_connection import connectionIB
from sysbrokers.IB.ib_orders import ibExecutionStackData
from sysbrokers.IB.ib_translate_broker_order_objects import tradeWithContract
from sysbrokers.broker_contract_commission_data import (
brokerFuturesContractCommissionData,
)
from sysdata.data_blob import dataBlob
from sysexecution.orders.broker_orders import brokerOrder
from sysobjects.contracts import futuresContract
from syslogging.logger import *
from syscore.genutils import quickTimer
from sysobjects.spot_fx_prices import currencyValue
class ibFuturesContractCommissionData(brokerFuturesContractCommissionData):
"""
Extends the baseData object to a data source that reads in and writes prices for specific futures contracts
This gets HISTORIC data from interactive brokers. It is blocking code
In a live production system it is suitable for running on a daily basis to get end of day prices
"""
def __init__(
self,
ibconnection: connectionIB,
data: dataBlob,
log=get_logger("ibFuturesContractCommissionData"),
):
super().__init__(log=log, data=data)
self._ibconnection = ibconnection
def __repr__(self):
return "IB Futures commissions data %s" % str(self.ibconnection)
@property
def ibconnection(self) -> connectionIB:
return self._ibconnection
@property
def execution_stack(self) -> ibExecutionStackData:
return self.data.broker_execution_stack
def get_commission_for_contract(
self, futures_contract: futuresContract
) -> currencyValue:
instrument_code = futures_contract.instrument_code
contract_date = futures_contract.contract_date.list_of_date_str[0]
broker_order = brokerOrder(
test_commission_strategy, instrument_code, contract_date, size_of_test_trade
)
order = self.execution_stack.what_if_order(broker_order)
timer = quickTimer(5)
comm_currency_value = currencyValue(currency="", value=0)
while timer.unfinished:
try:
comm_currency_value = get_commission_and_currency_from_ib_order(order)
except:
continue
return comm_currency_value
def get_commission_and_currency_from_ib_order(
ib_order: tradeWithContract,
) -> currencyValue:
return currencyValue(
value=ib_order.trade.commission / size_of_test_trade,
currency=ib_order.trade.commissionCurrency,
)
test_commission_strategy = "testCommmission" ## whatever not put on stack
size_of_test_trade = 10 ## arbitrary