in src/backend/domain/services/steps/calculate_trading.py [0:0]
def calculate_properties(apply_execution_cost: bool = False):
total_size = quantity + trading_stats_by_symbol[s]["total_size"][check_idx]
quote_ccy_value = total_size * array_close_price_by_symbol[s][idx]
acct_ccy_value = quote_ccy_value / array_fx_rates[fx_rate_symbol_by_symbol[s]][idx]
buy_alert = quantity > 1e-10
sell_alert = quantity < -1e-10
if apply_execution_cost:
base_price = list_trade_fill_price_by_symbol[s][idx]
execution_cost = (
(base_price + abs(base_price) * session_dict["execution_cost_bps"] / 10000)
if buy_alert
else ((base_price - abs(base_price) * session_dict["execution_cost_bps"] / 10000) if sell_alert else 0.0)
)
quote_ccy_cost = -quantity * execution_cost
else:
quote_ccy_cost = -quantity * list_trade_fill_price_by_symbol[s][idx]
acct_ccy_cost = quote_ccy_cost / array_fx_rates[fx_rate_symbol_by_symbol[s]][idx]
quote_ccy_total_cost = quote_ccy_cost + trading_stats_by_symbol[s]["quote_ccy_total_cost"][check_idx]
acct_ccy_total_cost = acct_ccy_cost + trading_stats_by_symbol[s]["acct_ccy_total_cost"][check_idx]
quote_ccy_pnl = quote_ccy_total_cost + quote_ccy_value
if apply_dividends:
cur_dividends = total_size * array_dividends_by_symbol[s][idx] if s in array_dividends_by_symbol else 0.0
cum_dividends = trading_stats_by_symbol[s]["cum_dividends"][check_idx] + cur_dividends
quote_ccy_pnl += cum_dividends
quote_ccy_day_pnl = quote_ccy_pnl - trading_stats_by_symbol[s]["quote_ccy_pnl"][check_idx]
acct_ccy_pnl = acct_ccy_total_cost + acct_ccy_value
if apply_dividends:
acct_ccy_cum_dividends = (
trading_stats_by_symbol[s]["acct_ccy_cum_dividends"][check_idx]
+ cur_dividends / array_fx_rates[fx_rate_symbol_by_symbol[s]][idx]
)
acct_ccy_pnl += acct_ccy_cum_dividends
acct_ccy_day_pnl = acct_ccy_pnl - trading_stats_by_symbol[s]["acct_ccy_pnl"][check_idx]
properties = {
"quote_ccy_cost": quote_ccy_cost,
"acct_ccy_cost": acct_ccy_cost,
"quote_ccy_total_cost": quote_ccy_total_cost,
"acct_ccy_total_cost": acct_ccy_total_cost,
"total_size": total_size,
"quote_ccy_value": quote_ccy_value,
"acct_ccy_value": acct_ccy_value,
"quote_ccy_pnl": quote_ccy_pnl,
"quote_ccy_day_pnl": quote_ccy_day_pnl,
"acct_ccy_pnl": acct_ccy_pnl,
"acct_ccy_day_pnl": acct_ccy_day_pnl,
"buy_alert": buy_alert,
"sell_alert": sell_alert,
}
if apply_dividends:
properties["cum_dividends"] = cum_dividends
properties["acct_ccy_cum_dividends"] = acct_ccy_cum_dividends
return properties