-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathib_contract_position_data.py
130 lines (100 loc) · 4.59 KB
/
ib_contract_position_data.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from syscore.exceptions import missingContract
from syslogging.logger import *
from sysbrokers.IB.ib_futures_contracts_data import ibFuturesContractData
from sysdata.data_blob import dataBlob
from sysbrokers.IB.client.ib_positions_client import ibPositionsClient
from sysbrokers.IB.ib_instruments_data import ibFuturesInstrumentData
from sysbrokers.IB.ib_connection import connectionIB
from sysbrokers.broker_contract_position_data import brokerContractPositionData
from syscore.constants import arg_not_supplied
from sysobjects.production.positions import contractPosition, listOfContractPositions
from sysobjects.contracts import futuresContract
class ibContractPositionData(brokerContractPositionData):
def __init__(
self,
ibconnection: connectionIB,
data: dataBlob,
log=get_logger("ibContractPositionData"),
):
super().__init__(log=log, data=data)
self._ibconnection = ibconnection
@property
def ibconnection(self) -> connectionIB:
return self._ibconnection
@property
def ib_client(self) -> ibPositionsClient:
client = getattr(self, "_ib_client", None)
if client is None:
client = self._ib_client = ibPositionsClient(
ibconnection=self.ibconnection, log=self.log
)
return client
def __repr__(self):
return "IB Futures per contract position data %s" % str(self.ib_client)
@property
def futures_contract_data(self) -> ibFuturesContractData:
return self.data.broker_futures_contract
@property
def futures_instrument_data(self) -> ibFuturesInstrumentData:
return self.data.broker_futures_instrument
def get_all_current_positions_as_list_with_contract_objects(
self, account_id=arg_not_supplied
) -> listOfContractPositions:
all_positions = self._get_all_futures_positions_as_raw_list(
account_id=account_id
)
current_positions = []
for position_entry in all_positions:
try:
contract_position_object = self._get_contract_position_for_raw_entry(
position_entry
)
except missingContract:
continue
else:
current_positions.append(contract_position_object)
list_of_contract_positions = listOfContractPositions(current_positions)
list_of_contract_positions_no_duplicates = (
list_of_contract_positions.sum_for_contract()
)
return list_of_contract_positions_no_duplicates
def _get_contract_position_for_raw_entry(self, position_entry) -> contractPosition:
position = position_entry["position"]
if position == 0:
raise missingContract
expiry = position_entry["expiry"]
instrument_code = self._get_instrument_code_from_ib_position_entry(
position_entry
)
contract = futuresContract(instrument_code, expiry)
contract_position_object = contractPosition(position, contract)
return contract_position_object
def _get_instrument_code_from_ib_position_entry(self, position_entry) -> str:
ib_contract = position_entry["ib_contract"]
instrument_code = self.futures_instrument_data.get_instrument_code_from_broker_contract_object(
ib_contract
)
return instrument_code
def _get_all_futures_positions_as_raw_list(
self, account_id: str = arg_not_supplied
) -> list:
self.ib_client.refresh()
all_positions = self.ib_client.broker_get_positions(account_id=account_id)
positions = all_positions.get("FUT", [])
return positions
def get_position_as_df_for_contract_object(self, *args, **kwargs):
raise Exception("Only current position data available from IB")
def update_position_for_contract_object(self, *args, **kwargs):
raise Exception("IB position data is read only")
def delete_last_position_for_contract_object(self, *args, **kwargs):
raise Exception("IB position data is read only")
def _get_series_for_args_dict(self, *args, **kwargs):
raise Exception("Only current position data available from IB")
def _update_entry_for_args_dict(self, *args, **kwargs):
raise Exception("IB position data is read only")
def _delete_last_entry_for_args_dict(self, *args, **kwargs):
raise Exception("IB position data is read only")
def _get_list_of_args_dict(self) -> list:
raise Exception("Args dict not used for IB")
def get_list_of_instruments_with_any_position(self):
raise Exception("Not implemented for IB")