import pandas as pd class Taxes: def __init__(self, tax_rate=0.20): """ tax_rate: flat tax rate on positive profits (e.g., 0.20 for 20%) """ self.tax_rate = tax_rate def add_taxes_to_results_csv(self, input_csv, output_csv=None, profit_col='final_usd'): """ Reads a backtest results CSV, adds tax columns, and writes to a new CSV. - input_csv: path to the input CSV file - output_csv: path to the output CSV file (if None, overwrite input) - profit_col: column name for profit (default: 'final_usd') """ df = pd.read_csv(input_csv, delimiter=None) # Compute tax only on positive profits df['tax_paid'] = df[profit_col].apply(lambda x: self.tax_rate * x if x > 0 else 0) df['net_profit_after_tax'] = df[profit_col] - df['tax_paid'] df['cumulative_tax_paid'] = df['tax_paid'].cumsum() if not output_csv: output_csv = input_csv df.to_csv(output_csv, index=False) return output_csv