I have this first in out capital gains/losses program but the results seem very low, over half of what I submitted on my taxes after manually calculating my capital gains (which could be wrong as well). If there are any finical people who could give my program a sanity check it would be greatly appreciated. I have included a dummy set of data.
#Create a dataframe with the transaction data
transactions = pd.DataFrame({
'Date': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05'],
'Operation': ['Buy', 'Buy', 'Buy', 'Sell', 'Sell'],
'Stock Price': [100.0, 110.0, 120.0, 130.0, 140.0],
'Shares': [10.0, 20.0, 30.0, 15.0, 25.0]
})
# Create a new column to store the cost basis (purchase price)
transactions['Cost Basis'] = transactions['Stock Price'] * transactions['Shares']
# Create a new column to store the capital gain or loss
transactions['Capital Gain/Loss'] = 0.0
# Create a new column to store the remaining shares
transactions['Remaining Shares'] = 0.0
# Initialize variables to keep track of the remaining shares and cost basis
remaining_shares = 0.0
cost_basis = 0.0
# Iterate through the transactions in reverse chronological order
for i, row in transactions.sort_values('Date', ascending=False).iterrows():
if row['Operation'] == 'Buy':
# If the operation is "Buy", add the shares to the remaining shares
remaining_shares = row['Shares']
cost_basis = row['Cost Basis']
transactions.at[i, 'Remaining Shares'] = remaining_shares
elif row['Operation'] == 'Sell':
# If the operation is "Sell", calculate the capital gain or loss and
# update the remaining shares and cost basis
if remaining_shares > 0:
if remaining_shares >= row['Shares']:
capital_gain_loss = row['Shares'] * (row['Stock Price'] - cost_basis/remaining_shares)
remaining_shares -= row['Shares']
cost_basis -= row['Shares'] * (cost_basis/remaining_shares)
else:
capital_gain_loss = remaining_shares * (row['Stock Price'] - cost_basis/remaining_shares)
remaining_shares = 0
cost_basis = 0
transactions.at[i, 'Capital Gain/Loss'] = capital_gain_loss
transactions.at[i, 'Remaining Shares'] = remaining_shares
#group the capital gain or loss by year
transactions['Year'] = pd.to_datetime(transactions['Date']).dt.year
result = transactions.groupby('Year')['Capital Gain/Loss'].sum()
print(result)
CodePudding user response:
There is a problem with your logic in determining your cost basis. Things are fine up until you start selling. At that point, you realise profits on 15 of you shares. This profit is based on your average cost (total cost base / total shares). Your error is the next step where you determine your new cost base:
cost_basis -= row['Shares'] * (cost_basis/remaining_shares)
Here you are calculating a new average cost price using the OLD cost basis but the NEW remaining shares. This gives you an incorrect average cost. You must add rows['Shares'] to remaining_shares in divisor so that you average cost remains the same:
cost_basis -= row['Shares'] * (cost_basis/(remaining_shares row['Shares']))