forked from Hummel83/IB_Tax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIBKR_Tax.py
176 lines (133 loc) · 4.38 KB
/
IBKR_Tax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# %% [markdown]
# # IBKR Tax
# %% [markdown]
# Requirements:
# - English activity statement
# - Year of activity statement 2020 and older
# - All Options, Futures, CFDs closed before year end
# - Only single short put and short call (Line 25 Losses from the disposal of worthless assets as per section 20(1) of the Income Tax Act not implemented)
# - Manual credit of Withholding tax
# - No classification of REITs as investment fund required
# %%
import pandas as pd
from IPython.display import display
from utils import *
# %% [markdown]
# # Read Data
# %%
myFile = "MY_ACTIVITY_STATEMENT.csv"
myFile = "Data/ibkr/main_2020.csv"
df = parse_activityStatement(myFile)
# %% [markdown]
# # Trades
# %%
tradesStatement = get_trades(df)
PL_Trades = tradesStatement.pl
PL_TradesDet = tradesStatement.plDet
df_trades = tradesStatement.trades
df_futures = tradesStatement.futures
df_options = tradesStatement.options
df_cfd = tradesStatement.cfd
display(PL_TradesDet, PL_Trades)
# %% [markdown]
# # Dividend
# %%
WithholdingTax, Dividends = get_dividends(df)
display(WithholdingTax, Dividends)
# %% [markdown]
# # Interest
# %%
Interest = get_interest(df)
Interest
# %% [markdown]
# # Futures / Option / CFD Profit and Loss
# %% [markdown]
# - Only applies if every position is closed and opened during the year (i.e. Options)
# %%
multiplier = 1
Futures, FuturesDet = getTradesPnl(df_futures.copy(), multiplier)
display(Futures, FuturesDet)
# %%
multiplier = 100
Options, OptionsDet = getTradesPnl(df_options.copy(), multiplier)
display(Options, OptionsDet)
# %%
multiplier = 1
Cfd, CfdDet = getTradesPnl(df_cfd.copy(), multiplier)
display(Cfd, CfdDet)
# %% [markdown]
# # Anlage KAP
activityStatement = namedtuple('statement', ['pl', 'plDet', 'options', 'optionsDet', 'dividends', 'interest'])
KAP = get_kap(activityStatement(PL_Trades, PL_TradesDet, Options, OptionsDet, Dividends, Interest))
KAP
# %% [markdown]
# # Results
# %% Add sum row
WithholdingTax = addSumRow(WithholdingTax)
Dividends = addSumRow(Dividends)
Interest = addSumRow(Interest)
PL_Trades = addSumRow(PL_Trades)
PL_TradesDet = addSumRow(PL_TradesDet)
OptionsDet = addSumRow(OptionsDet) if not OptionsDet.empty else OptionsDet
FuturesDet = addSumRow(FuturesDet) if not FuturesDet.empty else FuturesDet
CfdDet = addSumRow(CfdDet) if not CfdDet.empty else CfdDet
# %%
Interest
# %%
WithholdingTax if not WithholdingTax.empty else "No Withholding Tax"
# %%
Dividends
# %%
PL_Trades
# %%
PL_TradesDet
# %% [markdown]
# # Detailed Results
# %%
display(Options, OptionsDet)
# %%
display(Futures, FuturesDet)
# %%
display(Cfd, CfdDet)
# %% [markdown]
# ---
# %% [markdown]
# # P/L Forex
# %% [markdown]
# ## Experimental stuff
# Not fully tested
# %% [markdown]
# ### Only applies if every position is closed and opened during the year (i.e. Options)
# %% [markdown]
# Use > Realized & Unrealized Performance Summary > Forex
# %%
assets = df_trades.Asset.unique()
results = []
for asset in assets:
try:
df_asstes = df_trades[df_trades.Asset == asset].copy()
df_asstes["Basis"] = df_asstes["Basis"].astype(float)
df_asstes["Basis [€]"] = df_asstes.apply(lambda row: c.convert(
row["Basis"] , row.Currency, date=row["Date/Time"]), axis=1)
currencies = df_asstes.Currency.unique()
for curr in currencies:
df_curr = df_asstes[df_asstes.Currency==curr].copy()
open_position = df_curr["Basis"].round(2).sum()
pl_forex = df_curr["Basis [€]"].round(2).sum()
avg_rate = df_asstes.apply(lambda row: c.convert(
1 , row.Currency, date=row["Date/Time"]), axis=1).mean()
results.append([curr, avg_rate, pl_forex, open_position, asset])
#results.append([curr, pl_forex, open_position, asset])
except Exception as e:
print(f"Failed for {asset} with error: {e}")
df_forex = pd.DataFrame(results).T
df_forex, df_forex.columns = df_forex.iloc[1:] , df_forex.iloc[0]
df_forex = df_forex.T
df_forex.columns = ["Average Rate", "PL_Forex [€]", "Open Position", "Asset"]
#df_forex.columns = ["PL_Forex [€]", "Open Position", "Asset"]
df_forex.index.name = None
df_forex["PL_Forex_ADJ [€]"] = df_forex["PL_Forex [€]"] - df_forex["Open Position"]
df_forex.loc[:,:] = df_forex.loc[:,:].apply(pd.to_numeric, errors = 'ignore')
df_forex.loc['Column_Total'] = df_forex.sum(numeric_only=True, axis=0)
PL_Forex = df_forex
PL_Forex