DFS_Portfolio_Manager / global_func /trim_portfolio.py
James McCool
Enhance portfolio trimming logic in trim_portfolio.py by adding calculation for maximum ownership type value when conditions are not met. This adjustment improves the accuracy of the trimming process by ensuring all relevant ownership metrics are considered.
06c4d25
raw
history blame
2.05 kB
import pandas as pd
def trim_portfolio(portfolio: pd.DataFrame, trim_slack: float, performance_type: str, own_type: str, performance_threshold_high: float, performance_threshold_low: float, own_threshold_high: float, own_threshold_low: float):
if performance_type == 'Finish_percentile':
working_portfolio = portfolio.sort_values(by=performance_type, ascending = True).reset_index(drop=True)
else:
working_portfolio = portfolio.sort_values(by=performance_type, ascending = False).reset_index(drop=True)
rows_to_drop = []
curr_own_type_max = working_portfolio.loc[0, own_type] + (trim_slack * working_portfolio.loc[0, own_type])
for i in range(1, len(working_portfolio)):
if own_type == 'Similarity Score':
if working_portfolio.loc[i, own_type] < curr_own_type_max and \
working_portfolio.loc[i, performance_type] > performance_threshold_low and \
working_portfolio.loc[i, performance_type] <= performance_threshold_high and \
working_portfolio.loc[i, own_type] > own_threshold_low and \
working_portfolio.loc[i, own_type] <= own_threshold_high:
rows_to_drop.append(i)
else:
curr_own_type_max = working_portfolio.loc[i, own_type] + (trim_slack * working_portfolio.loc[i, own_type])
else:
if working_portfolio.loc[i, own_type] > curr_own_type_max and \
working_portfolio.loc[i, performance_type] > performance_threshold_low and \
working_portfolio.loc[i, performance_type] <= performance_threshold_high and \
working_portfolio.loc[i, own_type] > own_threshold_low and \
working_portfolio.loc[i, own_type] <= own_threshold_high:
rows_to_drop.append(i)
else:
curr_own_type_max = working_portfolio.loc[i, own_type] + (trim_slack * working_portfolio.loc[i, own_type])
working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
return working_portfolio