-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.py
More file actions
428 lines (367 loc) · 13.6 KB
/
performance.py
File metadata and controls
428 lines (367 loc) · 13.6 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""Module for performance report."""
import datetime as dt
import sys
from pathlib import Path
import pandas as pd
from openseries import OpenFrame, OpenTimeSeries, ValueType
from graphql_client import GraphqlClient, GraphqlError
CLIENT = ""
EXCLUDED_ACCOUNTS = []
MONTHS = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec",
}
def get_account_performance(
account_id: str,
gql_client: GraphqlClient,
start_dt: dt.date | None = None,
end_dt: dt.date | None = None,
*,
look_through: bool = False,
) -> OpenTimeSeries | None:
"""Fetch and compute performance series for an account from the GraphQL API."""
variables = {"accountId": account_id, "lookThrough": look_through}
query = """ query accountPerformance(
$accountId: GraphQLObjectId!
$startDate: GraphQLDateString
$endDate: GraphQLDateString
$lookThrough: Boolean
) {
accountPerformance(
accountId: $accountId
lookThrough: $lookThrough
filter: {startDate: $startDate, endDate: $endDate}
) {
currency
dates
values
cashFlows
}
} """
if start_dt:
variables.update({"startDate": start_dt.strftime("%Y-%m-%d")})
if end_dt:
variables.update({"endDate": end_dt.strftime("%Y-%m-%d")})
data, error = gql_client.query(query_string=query, variables=variables)
if error:
raise GraphqlError(str(error))
try:
dates = data["accountPerformance"]["dates"]
except TypeError:
msg = f"Account {account_id} has no valid performance data."
sys.stderr.write(f"{msg}\n")
return None
else:
values = data["accountPerformance"]["values"]
cashflows = data["accountPerformance"]["cashFlows"]
portfolio_df = pd.DataFrame(
{
"value": values,
"cashflow": cashflows,
},
index=pd.to_datetime(dates),
).sort_index()
if portfolio_df.empty:
msg = f"Account {account_id} has no performance data."
sys.stderr.write(f"{msg}\n")
return None
prev_value = portfolio_df["value"].shift(1)
portfolio_return = pd.Series(index=portfolio_df.index, dtype="float64")
nonzero_prev_value = prev_value != 0.0
portfolio_return.loc[nonzero_prev_value] = (
portfolio_df.loc[nonzero_prev_value, "value"]
- portfolio_df.loc[nonzero_prev_value, "cashflow"]
) / prev_value.loc[nonzero_prev_value] - 1.0
# Treat first row or zero prior value as a neutral return.
portfolio_df["portfolio_return"] = portfolio_return.fillna(0.0)
filtered_dates = portfolio_df.index.strftime("%Y-%m-%d").tolist()
cumulative_values = (1.0 + portfolio_df["portfolio_return"]).cumprod().tolist()
return OpenTimeSeries.from_arrays(
name=account_id,
dates=filtered_dates,
values=cumulative_values,
baseccy=data["accountPerformance"]["currency"],
valuetype=ValueType.PRICE,
)
def _select_price_close_timeseries(
time_series: list[dict[str, str | list[str] | list[float]]],
) -> dict[str, str | list[str] | list[float]]:
"""Return the time series item for Price(Close).
Args:
time_series: A list of time series dicts from GraphQL.
Returns:
The time series dict matching Price(Close).
Raises:
ValueError: When a Price(Close) time series is missing.
"""
series = next(
(item for item in time_series if item.get("type") == "Price(Close)"), None
)
if series is None:
err_msg = "Missing Price(Close) time series for benchmark instrument."
raise ValueError(err_msg)
return series
def _model_index_benchmark_for_account(
account: dict,
) -> OpenTimeSeries:
"""Build the model index benchmark series for an account.
For Sum accounts uses account["modelIndexBenchmark"].
For Physical accounts uses the benchmark in account["benchmarks"]
where mainBenchmark is true.
"""
if account["type"] == "Physical":
main_bmk = next(
(bmk for bmk in account["benchmarks"] if bmk.get("mainBenchmark")), None
)
if main_bmk and (
price_ts := _select_price_close_timeseries(
time_series=main_bmk["instrument"]["timeSeries"]
)
):
name = (
f"{main_bmk['comment'] or main_bmk['instrument']['longName']} "
f"(Index for {account['description']})"
)
return (
OpenTimeSeries.from_arrays(
name=name,
baseccy=main_bmk["currency"],
timeseries_id=price_ts["_id"],
instrument_id=main_bmk["instrument"]["_id"],
dates=price_ts["dates"],
values=[float(val) for val in price_ts["values"]],
)
.running_adjustment(adjustment=main_bmk["offset"])
.to_cumret()
)
mib = account["modelIndexBenchmark"]
return OpenTimeSeries.from_arrays(
name=mib["name"]
if mib["name"] != "ModelWeightedIndex"
else f"ModelWeightedIndex ({account['description']})",
baseccy=mib["currency"],
dates=[item["date"] for item in mib["timeSeries"]["items"]],
values=[float(item["value"]) for item in mib["timeSeries"]["items"]],
)
def get_accounts(
gql: GraphqlClient, client_id: str
) -> dict[str, dict[str, str | OpenTimeSeries | list[OpenTimeSeries]]]:
"""Get accounts from the Captor database.
Args:
gql: The GraphqlClient instance.
client_id: The client ID.
Returns:
A dictionary of accounts.
"""
query = """ query party($clientId: GraphQLObjectId) {
party(_id: $clientId) {
firstTradeDate
accounts {
name
_id
description
type
benchmarks {
offset
currency
comment
mainBenchmark
instrument {
_id
name
longName
timeSeries {
_id
type
dates
values
}
}
}
modelIndexBenchmark {
name
currency
timeSeries {
items {
date
value
}
}
}
}
}
} """
variables = {"clientId": client_id}
data, error = gql.query(query, variables=variables)
if error:
raise GraphqlError(str(error))
return {
account["_id"]: {
"description": account["description"],
"type": account["type"],
"firstTradeDate": data["party"]["firstTradeDate"],
"benchmarks": [
OpenTimeSeries.from_arrays(
name=bmk["comment"] or bmk["instrument"]["longName"],
baseccy=bmk["currency"],
timeseries_id=price_ts["_id"],
instrument_id=bmk["instrument"]["_id"],
dates=price_ts["dates"],
values=[float(val) for val in price_ts["values"]],
).running_adjustment(adjustment=bmk["offset"])
for bmk in account["benchmarks"]
if (
price_ts := _select_price_close_timeseries(
time_series=bmk["instrument"]["timeSeries"]
)
)
and (account["type"] != "Physical" or not bmk.get("mainBenchmark"))
],
"modelIndexBenchmark": _model_index_benchmark_for_account(account),
}
for account in data["party"]["accounts"]
}
def performance_report(
graphql: GraphqlClient,
client: str,
start_dt: dt.date | None = None,
end_dt: dt.date | None = None,
excluded_accounts: list[str] | None = None,
) -> OpenFrame:
"""Build an OpenFrame of cumulative return series for all client accounts.
Fetches performance data for each account from the GraphQL API, aligns each
account with its benchmarks and model index benchmark, truncates to the
requested date range (and the account's first trade date), and rebases all
series to 1 at the start of the period.
Args:
graphql: GraphQL client for querying account and performance data.
client: Client ID (party _id) whose accounts to include.
start_dt: Optional start date for the performance period.
end_dt: Optional end date for the performance period.
excluded_accounts: Account IDs to exclude from the report.
Returns:
OpenFrame of cumulative return time series for each included account,
their benchmarks, and model index benchmark, all rebased to 1.
Raises:
GraphqlError: If the account or performance GraphQL query fails.
Note:
Accounts in excluded_accounts are skipped. If a date-filtered
performance query raises GraphqlError, it is retried without date
filters. Accounts with no valid performance data are skipped and
reported to stderr.
"""
if excluded_accounts is None:
excluded_accounts = []
accounts = get_accounts(gql=graphql, client_id=client)
constituents = []
errors = []
for account_id, account_data in accounts.items():
start = start_dt
end = end_dt
if account_id in excluded_accounts:
continue
try:
tmp_performance = get_account_performance(
account_id=account_id,
gql_client=graphql,
start_dt=start,
end_dt=end,
)
except GraphqlError:
start = None
end = None
tmp_performance = get_account_performance(
account_id=account_id,
gql_client=graphql,
start_dt=None,
end_dt=None,
)
if not isinstance(tmp_performance, OpenTimeSeries):
continue
if start:
start = max(start, tmp_performance.first_idx)
if end:
end = min(end, tmp_performance.last_idx)
performance = get_account_performance(
account_id=account_id,
gql_client=graphql,
start_dt=start,
end_dt=end,
).set_new_label(lvl_zero=account_data["description"])
if performance is None:
errors.append(f"- Account '{account_data['description']}'")
continue
trunc_start = max(
v
for v in [
start,
start_dt,
performance.first_idx,
dt.datetime.strptime(account_data["firstTradeDate"], "%Y-%m-%d")
.astimezone()
.date(),
]
if v is not None
)
trunc_end = min(
v for v in [end, end_dt, performance.last_idx] if v is not None
)
tmp_frame = OpenFrame(
constituents=[performance]
+ account_data["benchmarks"]
+ [account_data["modelIndexBenchmark"]]
)
tmp_frame.trunc_frame(start_cut=trunc_start, end_cut=trunc_end)
tmp_frame.value_nan_handle().to_cumret()
constituents.extend(tmp_frame.constituents)
frame = OpenFrame(constituents=constituents)
if len(errors) > 0:
sys.stderr.write(f"Errors: {errors}\n")
return frame
if __name__ == "__main__": # pragma: no cover
graphql = GraphqlClient()
start = None # dt.date(2024, 12, 30)
end = None # dt.date(2025, 12, 30)
frame = performance_report(
graphql=graphql,
client=CLIENT,
excluded_accounts=EXCLUDED_ACCOUNTS,
start_dt=start,
end_dt=end,
)
first = frame.first_idx.strftime("%Y%m%d")
last = frame.last_idx.strftime("%Y%m%d")
filename_performance = f"performance_{first}_{last}"
dirpath = Path(__file__).parent
frame.to_xlsx(filename=f"{filename_performance}.xlsx", directory=dirpath)
thisyr = frame.last_idx.year
thismth = frame.last_idx.month
results = frame.value_ret_calendar_period(year=thisyr)
results.name = f"YTD {thisyr}"
results.index = results.index.droplevel(level=1)
mtd = frame.value_ret_calendar_period(year=thisyr, month=thismth)
mtd.name = f"MTD {MONTHS[thismth]} {thisyr}"
mtd.index = mtd.index.droplevel(level=1)
results = pd.concat([results, mtd], axis="columns")
for period, label in zip(
[12, 36, 60],
["1 year", "3 year", "5 year"],
strict=True,
):
retrn = frame.geo_ret_func(months_from_last=period)
retrn.name = label
retrn.index = retrn.index.droplevel(level=1)
results = pd.concat([results, retrn], axis="columns")
filename_summary = f"summary_{first}_{last}"
results_xlsx = dirpath / f"{filename_summary}.xlsx"
results.to_excel(excel_writer=results_xlsx, engine="openpyxl")