Skip to content

Commit

Permalink
commision change
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Carver committed Mar 19, 2024
1 parent 57c8c23 commit 91a0dbb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
6 changes: 5 additions & 1 deletion sysbrokers/IB/client/ib_orders_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def broker_submit_order(
account_id: str = arg_not_supplied,
order_type: brokerOrderType = market_order_type,
limit_price: float = None,
what_if: bool = False
) -> tradeWithContract:
"""
Expand Down Expand Up @@ -121,7 +122,10 @@ def broker_submit_order(
limit_price=limit_price,
)

order_object = self.ib.placeOrder(ibcontract, ib_order)
if what_if:
order_object = self.ib.whatIfOrder(ibcontract, ib_order)
else:
order_object = self.ib.placeOrder(ibcontract, ib_order)

trade_with_contract = tradeWithContract(ibcontract_with_legs, order_object)

Expand Down
58 changes: 58 additions & 0 deletions sysbrokers/IB/ib_broker_commissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from sysbrokers.IB.ib_connection import connectionIB
from sysbrokers.IB.ib_orders import ibExecutionStackData
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 *

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) -> float:
## FOR NOW DO NOT RUN IF ANYTHING ELSE IS RUNNING
## NEEDS CODE TO TAKE THE TEST STRATEGY OFF THE STACK WHEN RETURNING ORDERS
size_of_test_trade = 10
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.put_what_if_order_on_stack(broker_order)

while not self.execution_stack.is_completed(order.order.order_id):
## could last forever!
continue

order_from_stack = self.execution_stack.get_order_with_id_from_stack(order_id=order.order.order_id)
return order_from_stack.commission / 10.0

test_commission_strategy = "testCommmission"
32 changes: 29 additions & 3 deletions sysbrokers/IB/ib_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,35 @@ def put_order_on_stack(self, broker_order: brokerOrder) -> ibOrderWithControls:
:param broker_order: key properties are instrument_code, contract_id, quantity
:return: ibOrderWithControls or missing_order
"""
trade_with_contract_from_ib = self._send_broker_order_to_IB(broker_order)
order_time = datetime.datetime.now()
trade_with_contract_from_ib = self._send_broker_order_to_IB(broker_order, what_if=False)

placed_broker_order_with_controls = self._return_place_order_given_ib_trade_with_contract(trade_with_contract_from_ib=trade_with_contract_from_ib,
broker_order=broker_order)

return placed_broker_order_with_controls

def put_what_if_order_on_stack(self, broker_order: brokerOrder) -> ibOrderWithControls:
"""
:param broker_order: key properties are instrument_code, contract_id, quantity
:return: ibOrderWithControls or missing_order
"""
trade_with_contract_from_ib = self._send_broker_order_to_IB(broker_order, what_if=True)

placed_broker_order_with_controls = self._return_place_order_given_ib_trade_with_contract(
trade_with_contract_from_ib=trade_with_contract_from_ib,
broker_order=broker_order)

return placed_broker_order_with_controls



def _return_place_order_given_ib_trade_with_contract(self, trade_with_contract_from_ib: tradeWithContract, broker_order: brokerOrder) -> ibOrderWithControls:
if trade_with_contract_from_ib is missing_order:
return missing_order

order_time = datetime.datetime.now()

placed_broker_order_with_controls = ibOrderWithControls(
trade_with_contract_from_ib,
ibclient=self.ib_client,
Expand All @@ -288,7 +311,8 @@ def put_order_on_stack(self, broker_order: brokerOrder) -> ibOrderWithControls:

return placed_broker_order_with_controls

def _send_broker_order_to_IB(self, broker_order: brokerOrder) -> tradeWithContract:

def _send_broker_order_to_IB(self, broker_order: brokerOrder, what_if: bool = False) -> tradeWithContract:
"""
:param broker_order: key properties are instrument_code, contract_id, quantity
Expand Down Expand Up @@ -317,6 +341,7 @@ def _send_broker_order_to_IB(self, broker_order: brokerOrder) -> tradeWithContra
account_id=account_id,
order_type=order_type,
limit_price=limit_price,
what_if=what_if
)
if placed_broker_trade_object is missing_order:
self.log.warning("Couldn't submit order", **log_attrs)
Expand All @@ -326,6 +351,7 @@ def _send_broker_order_to_IB(self, broker_order: brokerOrder) -> tradeWithContra

return placed_broker_trade_object


def match_db_broker_order_to_order_from_brokers(
self, broker_order_to_match: brokerOrder
) -> brokerOrder:
Expand Down
Empty file.

0 comments on commit 91a0dbb

Please sign in to comment.