Skip to content

Commit

Permalink
update commission stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Carver committed Mar 19, 2024
1 parent c89a279 commit eaa9542
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
22 changes: 11 additions & 11 deletions sysbrokers/IB/ib_broker_commissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from syslogging.logger import *
from syscore.genutils import quickTimer
from sysobjects.spot_fx_prices import currencyValue


class ibFuturesContractCommissionData(brokerFuturesContractCommissionData):
"""
Expand Down Expand Up @@ -38,29 +40,27 @@ def ibconnection(self) -> connectionIB:
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
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.put_what_if_order_on_stack(broker_order)
order = self.execution_stack.what_if_order(broker_order)

timer = quickTimer(5)
comm_currency_value = currencyValue(currency='', value=0)
while timer.unfinished:
## could last forever!
try:
commission, commission_ccy = get_commission_and_currency_from_ib_order(order)
comm_currency_value = get_commission_and_currency_from_ib_order(order)
except:
continue

return commission_ccy, commission / 10.0
return comm_currency_value

def get_commission_and_currency_from_ib_order(ib_order: tradeWithContract):
return (ib_order.trade.commission, ib_order.trade.commissionCurrency)
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"
test_commission_strategy = "testCommmission" ## whatever not put on stack
size_of_test_trade = 10 ## arbitrary
2 changes: 1 addition & 1 deletion sysbrokers/IB/ib_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def put_order_on_stack(self, broker_order: brokerOrder) -> ibOrderWithControls:

return placed_broker_order_with_controls

def put_what_if_order_on_stack(self, broker_order: brokerOrder) -> tradeWithContract:
def what_if_order(self, broker_order: brokerOrder) -> tradeWithContract:
"""
:param broker_order: key properties are instrument_code, contract_id, quantity
Expand Down
3 changes: 2 additions & 1 deletion sysbrokers/broker_contract_commission_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from sysobjects.contracts import futuresContract

from syslogging.logger import *
from sysobjects.spot_fx_prices import currencyValue


class brokerFuturesContractCommissionData(futuresContractData):
def __init__(self, data: dataBlob, log=get_logger("brokerFuturesContractCommissionData")):
super().__init__(log=log)
self._data = data

def get_commission_for_contract(self, contract: futuresContract) -> float:
def get_commission_for_contract(self, contract: futuresContract) -> currencyValue:
raise NotImplementedError

@property
Expand Down
17 changes: 17 additions & 0 deletions sysdata/csv/csv_spread_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ def get_spread_costs_as_series(self) -> pd.Series:

return spread_cost_series

def get_commissions_as_series(self) -> pd.Series:
try:
spread_cost_data = pd.read_csv(self.config_file)
except BaseException:
raise Exception("Can't read file %s" % self.config_file)

try:
spread_cost_data.index = spread_cost_data[INSTRUMENT_COLUMN_NAME]
spread_cost_series = spread_cost_data[]

except BaseException:
raise Exception("Badly configured file %s" % (self._config_file))

return spread_cost_series



def write_all_instrument_spreads(self, spread_cost_as_series: pd.Series):
spread_cost_as_df = pd.DataFrame(spread_cost_as_series)
spread_cost_as_df.columns = [SPREAD_COST_COLUMN_NAME]
Expand Down
1 change: 1 addition & 0 deletions sysdata/futures/spread_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def get_spread_cost(self, instrument_code: str) -> float:
def get_spread_costs_as_series(self) -> pd.Series:
raise NotImplementedError


def _get_spread_cost_if_series_provided(self, instrument_code: str) -> float:
all_data = self.get_spread_costs_as_series()
return all_data[instrument_code]
Expand Down
12 changes: 10 additions & 2 deletions sysproduction/data/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from sysobjects.contracts import futuresContract
from sysobjects.instruments import futuresInstrumentWithMetaData
from sysobjects.production.positions import contractPosition, listOfContractPositions
from sysobjects.spot_fx_prices import fxPrices
from sysobjects.spot_fx_prices import fxPrices, currencyValue
from sysobjects.futures_per_contract_prices import futuresContractPrices
from sysproduction.data.positions import diagPositions
from sysproduction.data.currency_data import dataCurrency
Expand Down Expand Up @@ -102,8 +102,16 @@ def broker_futures_contract_commission(self) -> brokerFuturesContractCommissionD
def diag_controls(self) -> diagControlProcess:
return self._diag_controls

def get_commission_for_contract_in_base(self, contract: futuresContract) -> float:
currency_data = dataCurrency(self.data)
ccy_value = self.broker_futures_contract_commission.get_commission_for_contract(contract)
base_value = currency_data.currency_value_in_base(ccy_value)

return base_value

## Methods
def get_commission_for_contract(self, contract: futuresContract) -> float:
def get_commission_for_contract(self, contract: futuresContract) -> currencyValue:

return self.broker_futures_contract_commission.get_commission_for_contract(contract)

def get_list_of_contract_dates_for_instrument_code(
Expand Down
22 changes: 22 additions & 0 deletions sysproduction/data/instruments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pandas as pd

from sysdata.data_blob import dataBlob
from sysdata.futures.instruments import futuresInstrumentData
from sysdata.futures.spread_costs import spreadCostData
Expand Down Expand Up @@ -49,6 +51,26 @@ def _add_required_classes_to_data(self, data: dataBlob) -> dataBlob:
def get_spread_costs_as_series(self):
return self.db_spread_cost_data.get_spread_costs_as_series()

def get_block_commissions_as_series(self) -> pd.Series:
all_instruments = self.db_futures_instrument_data.get_list_of_instruments()
all_instruments_excluding_percentage_commission = [instrument_code
for instrument_code in all_instruments
if not self.has_percentage_commission(instrument_code)]
all_block_commissions = pd.Series([
self.get_block_commission_for_instrument(instrument_code)
for instrument_code in all_instruments_excluding_percentage_commission
], index=all_instruments_excluding_percentage_commission)

return all_block_commissions

def get_block_commission_for_instrument(self, instrument_code: str) -> float:
costs = self.get_cost_object(instrument_code)
return costs.value_of_block_commission

def has_percentage_commission(self, instrument_code: str) -> float:
costs = self.get_cost_object(instrument_code)
return costs.percentage_cost>0

def get_cost_object(self, instrument_code: str) -> instrumentCosts:
meta_data = self.get_meta_data(instrument_code)
spread_cost = self.get_spread_cost(instrument_code)
Expand Down
31 changes: 31 additions & 0 deletions sysproduction/reporting/data/commissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List

import pandas as pd
from sysproduction.data.instruments import diagInstruments
from sysproduction.data.broker import dataBroker
from sysproduction.data.contracts import dataContracts
from sysobjects.contracts import futuresContract

def df_of_configure_and_broker_block_cost(data) -> pd.DataFrame:
configured = get_current_configured_block_costs(data)
list_of_instruments = configured.index

list_of_broker = get_broker_block_costs(list_of_instruments)

return pd.concat([configured, list_of_broker], axis=1)

def get_current_configured_block_costs(data) -> pd.Series:
diag_instruments = diagInstruments(data)

return diag_instruments.get_block_commissions_as_series()

def get_broker_block_costs(list_of_instruments_codes: List[str]) ->pd.Series:
db = dataBroker()
dc = dataContracts()

list_of_priced_contracts = [futuresContract(instrument_code, dc.get_priced_contract_id(instrument_code)) for instrument_code in list_of_instruments_codes]

return pd.Series(
[db.get_commission_for_contract(contract) for contract in list_of_priced_contracts],
index = list_of_instruments_codes
)

0 comments on commit eaa9542

Please sign in to comment.