Skip to content

Commit

Permalink
comission report
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Carver committed Mar 19, 2024
1 parent eaa9542 commit 69b90ae
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 32 deletions.
22 changes: 19 additions & 3 deletions sysproduction/data/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,31 @@ def get_block_commissions_as_series(self) -> pd.Series:
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)
self.get_block_commission_for_instrument_in_base_currency(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:
def get_block_commission_for_instrument_in_base_currency(self, instrument_code: str) -> float:
ccy_value = self.get_block_commission_for_instrument_as_currency_value(instrument_code)
currency_data = dataCurrency(self.data)
base_value = currency_data.currency_value_in_base(ccy_value)

return base_value

def get_block_commission_for_instrument_as_currency_value(self, instrument_code: str) -> currencyValue:
currency = self.get_currency(instrument_code)
block_commission = self.get_block_commission_for_instrument_as_in_instrument_currency(instrument_code)
ccy_value = currencyValue(currency=currency, value=block_commission)

return ccy_value

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

return block_commission

def has_percentage_commission(self, instrument_code: str) -> float:
costs = self.get_cost_object(instrument_code)
Expand Down
33 changes: 20 additions & 13 deletions sysproduction/interactive_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
remove_markets_report_config,
market_monitor_report_config,
account_curve_report_config,
commission_report_config
)


Expand Down Expand Up @@ -126,13 +127,14 @@ def interactive_diagnostics():
66: "Risk report",
67: "Costs report",
68: "Slippage report",
69: "Liquidity report",
70: "All instrument risk",
71: "Minimum capital required",
72: "Duplicate markets",
73: "Remove markets",
74: "Market monitor",
75: "P&L account curve",
69: "Commission report",
70: "Liquidity report",
71: "All instrument risk",
72: "Minimum capital required",
73: "Duplicate markets",
74: "Remove markets",
75: "Market monitor",
76: "P&L account curve",
},
}

Expand Down Expand Up @@ -231,6 +233,10 @@ def slippage_report(data):
report_config.modify_kwargs(start_date=start_date, end_date=end_date)
run_report(report_config, data=data)

def commission_report(data):
report_config = email_or_print_or_file(commission_report_config)
run_report(report_config, data=data)


def liquidity_report(data):
report_config = email_or_print_or_file(liquidity_report_config)
Expand Down Expand Up @@ -740,12 +746,13 @@ def get_trading_hours_for_instrument(
67: cost_report,
68: slippage_report,
69: liquidity_report,
70: instrument_risk_report,
71: min_capital_report,
72: duplicate_market_report,
73: remove_markets_report,
74: market_monitor_report,
75: account_curve_report,
70: commission_report,
71: instrument_risk_report,
72: min_capital_report,
73: duplicate_market_report,
74: remove_markets_report,
75: market_monitor_report,
76: account_curve_report,
}

if __name__ == "__main__":
Expand Down
19 changes: 19 additions & 0 deletions sysproduction/reporting/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
get_position_limits_as_df,
)
from sysproduction.reporting.data.volume import get_liquidity_data_df
from sysproduction.reporting.data.commissions import df_of_configure_and_broker_block_cost_with_ratio_sorted

REPORT_DATETIME_FORMAT = "%d/%m/%Y %H:%M"

Expand Down Expand Up @@ -939,6 +940,24 @@ def _combined_df_costs(self):

return combined_df_costs

##### COMMISSIONS ####
def table_of_commissions(self):
df = self.df_commissions()
df_as_formatted_table = table(
"Commissions in base currency, configure and from broker", df
)

return df_as_formatted_table

def df_commissions(self):
return self.cache.get(self._df_commissions)

def _df_commissions(self):
combined_df_costs = df_of_configure_and_broker_block_cost_with_ratio_sorted(self.data)

return combined_df_costs


##### TRADES ######
def table_of_orders_overview(self):
broker_orders = self.broker_orders
Expand Down
33 changes: 33 additions & 0 deletions sysproduction/reporting/commissions_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from sysdata.data_blob import dataBlob

from syscore.constants import arg_not_supplied
from sysproduction.reporting.reporting_functions import body_text
from sysproduction.reporting.api import reportingApi

REPORT_TEXT = body_text(
"Commissions report, columns are commissions from configuration, broker, and ratio:\n"
)


def commissions_report(
data: dataBlob = arg_not_supplied,
):
if data is arg_not_supplied:
data = dataBlob()

reporting_api = reportingApi(
data,
)

formatted_output = []

formatted_output.append(reporting_api.std_header("Commission report"))
formatted_output.append(REPORT_TEXT)
formatted_output.append(reporting_api.table_of_commissions())
formatted_output.append(reporting_api.footer())

return formatted_output


if __name__ == "__main__":
commissions_report()
64 changes: 48 additions & 16 deletions sysproduction/reporting/data/commissions.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
from typing import List

import pandas as pd
import numpy as np

from sysproduction.data.instruments import diagInstruments
from sysproduction.data.broker import dataBroker
from sysproduction.data.contracts import dataContracts
from sysobjects.contracts import futuresContract
from sysdata.data_blob import dataBlob

def df_of_configure_and_broker_block_cost(data) -> pd.DataFrame:
configured = get_current_configured_block_costs(data)
list_of_instruments = configured.index
def df_of_configure_and_broker_block_cost_with_ratio_sorted(data: dataBlob) -> pd.DataFrame:
df = df_of_configure_and_broker_block_cost(data)
ratio = df[BROKER_COLUMN] / df[CONFIGURED_COLUMN]
df[RATIO_COLUMN] = ratio

list_of_broker = get_broker_block_costs(list_of_instruments)
df.sort_values(by=RATIO_COLUMN)

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

def get_current_configured_block_costs(data) -> pd.Series:
diag_instruments = diagInstruments(data)
def df_of_configure_and_broker_block_cost(data: dataBlob) -> pd.DataFrame:
configured = get_current_configured_block_costs(data)
list_of_instrument_codes = configured.index
list_of_broker = get_broker_block_costs(data=data, list_of_instrument_codes=list_of_instrument_codes)
both = pd.concat([configured, list_of_broker], axis=1)
both.columns = [CONFIGURED_COLUMN, BROKER_COLUMN]
return both

return diag_instruments.get_block_commissions_as_series()
CONFIGURED_COLUMN = 'configured'
BROKER_COLUMN = 'broker'
RATIO_COLUMN = 'ratio'

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

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

return pd.Series(
[db.get_commission_for_contract(contract) for contract in list_of_priced_contracts],
index = list_of_instruments_codes
)
def get_broker_block_costs(data: dataBlob, list_of_instrument_codes: List[str]) ->pd.Series:
priced_contracts = get_series_of_priced_contracts(data=data, list_of_instrument_codes=list_of_instrument_codes)
block_costs = get_costs_given_priced_contracts(data=data, priced_contracts=priced_contracts)
return pd.Series(block_costs)

def get_series_of_priced_contracts(data: dataBlob, list_of_instrument_codes: List[str]) -> pd.Series:
dc = dataContracts(data)
list_of_priced_contracts = {}
for instrument_code in list_of_instrument_codes:
try:
contract = futuresContract(instrument_code, dc.get_priced_contract_id(instrument_code))
except:
continue
list_of_priced_contracts[instrument_code] = contract
return pd.Series(list_of_priced_contracts)

def get_costs_given_priced_contracts(data: dataBlob, priced_contracts: pd.Series)-> pd.Series:
db = dataBroker(data)
block_costs = {}
for instrument_code, contract in priced_contracts.items():
try:
costs = db.get_commission_for_contract(contract)
except:
costs = np.nan
block_costs[instrument_code] = costs
return pd.Series(block_costs)
7 changes: 7 additions & 0 deletions sysproduction/reporting/report_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ def modify_kwargs(self, **kwargs):
output="email",
)

commission_report_config = reportConfig(
title="Commission report",
function="sysproduction.reporting.commissions_report.commissions_report",
output="email",
)

instrument_risk_report_config = reportConfig(
title="Instrument risk report",
function="sysproduction.reporting.instrument_risk_report.instrument_risk_report",
Expand Down Expand Up @@ -157,4 +163,5 @@ def modify_kwargs(self, **kwargs):
remove_markets_report=remove_markets_report_config,
market_monitor_report=market_monitor_report_config,
account_curve_report=account_curve_report_config,
commission_report_config=commission_report_config
)

0 comments on commit 69b90ae

Please sign in to comment.