在Kusto中的复合回报

huangapple go评论58阅读模式
英文:

Compounded Return In Kusto

问题

如果我有一个包含"Date"列和"Daily Return"列的表格,用于指定股票每日表现的数据。我该如何编写一个KQL查询来创建截止到当月、截止到当年和累计总收益?右边的三列是我想要复制的内容。为了我的示例,MTD(当月截止)和YTD(当年截止)是相同的,但在我的实际数据集中,显然有多年的数据,不同的月份。

日期 每日回报 MTD回报 YTD回报 累计总回报
2021年12月25日 5.00% 5.00% 5.00% 5.00%
2021年12月26日 2.00% 7.10% 7.10% 7.10%
2021年12月27日 3.00% 10.31% 10.31% 10.31%
2021年12月28日 2.00% 12.52% 12.52% 12.52%
2021年12月29日 1.00% 13.64% 13.64% 13.64%
2021年12月30日 3.00% 17.05% 17.05% 17.05%
2021年12月31日 4.00% 21.74% 21.74% 21.74%
2022年1月1日 2.00% 2.00% 2.00% 24.17%
2022年1月2日 2.00% 4.04% 4.04% 26.65%
2022年1月3日 5.00% 9.24% 9.24% 32.99%
英文:

if I have a table with a "Date" column and "Daily Return" column that specifies a stock's daily performance. How can I write a KQL query that would create Month to Date, year to date, and Total Cumulative returns? The right 3 columns are what I would be trying to replicate. For the sake of my example, MTD and YTD are the same, but in my actual datasets I obviously have several years worth of data with different months.

Date Daily Return MTD Return YTD Return Total Cumulative Return
12/25/2021 5.00% 5.00% 5.00% 5.00%
12/26/2021 2.00% 7.10% 7.10% 7.10%
12/27/2021 3.00% 10.31% 10.31% 10.31%
12/28/2021 2.00% 12.52% 12.52% 12.52%
12/29/2021 1.00% 13.64% 13.64% 13.64%
12/30/2021 3.00% 17.05% 17.05% 17.05%
12/31/2021 4.00% 21.74% 21.74% 21.74%
1/1/2022 2.00% 2.00% 2.00% 24.17%
1/2/2022 2.00% 4.04% 4.04% 26.65%
1/3/2022 5.00% 9.24% 9.24% 32.99%

答案1

得分: 1

以下是翻译好的部分:

要找到第n天的累积日收益率,公式如下:

第n天的累积日收益率 = [(1 + 日收益率1) * (1 + 日收益率2) *..........*(1+ 日收益率n)] - 1

示例:要计算第2天的累积收益率,应用上述公式中的值,
[(1+0.05)(1+0.02)]-1
=[1.05
1.02]-1
=1.071-1
=0.071

由于Kusto没有像row_cumsum()这样的窗口函数来进行累积乘积,我们可以使用row_cumsum()函数对日收益值的log10值进行求和,然后对结果值进行exp10操作。以下是计算总累积收益、每月累积收益和每年累积收益的代码。

代码:

let dailyReturns = datatable(Date:datetime, DailyReturn:real)
[
    datetime(2021-12-25), 0.05,
    datetime(2021-12-26), 0.02,
    datetime(2021-12-27), 0.03,
    datetime(2021-12-28), 0.02,
    datetime(2021-12-29), 0.01,
    datetime(2021-12-30), 0.03,
    datetime(2021-12-31), 0.04,
    datetime(2022-01-01), 0.02,
    datetime(2022-01-02), 0.02,
    datetime(2022-01-03), 0.05,
];
dailyReturns
| extend Month = getmonth(Date)
| extend Year = getyear(Date)
| order by Date asc 
| extend log_DR= log10(1+DailyReturn)
| extend month_cumsum_log_DR = row_cumsum(log_DR, Month != prev(Month))
| extend MTD_return=exp10(month_cumsum_log_DR)-1
| extend MTD_return = strcat(round(MTD_return * 100,2), "%")
| extend year_cumsum_log_DR = row_cumsum(log_DR, Year != prev(Year))
| extend YTD_return=exp10(year_cumsum_log_DR)-1
| extend YTD_return = strcat(round(YTD_return * 100,2), "%")
| extend total_cumsum_log_DR = row_cumsum(log_DR)
| extend Total_cumulative_return = exp10(total_cumsum_log_DR) - 1
| extend Total_cumulative_return = strcat(round(Total_cumulative_return * 100,2), "%")
| project Date,DailyReturn,MTD_return,YTD_return,Total_cumulative_return

输出:

Date DailyReturn MTD_return YTD_return Total_cumulative_return
2021-12-25T00:00:00Z 0.05 5.0% 5.0% 5.0%
2021-12-26T00:00:00Z 0.02 7.1% 7.1% 7.1%
2021-12-27T00:00:00Z 0.03 10.31% 10.31% 10.31%
2021-12-28T00:00:00Z 0.02 12.52% 12.52% 12.52%
2021-12-29T00:00:00Z 0.01 13.64% 13.64% 13.64%
2021-12-30T00:00:00Z 0.03 17.05% 17.05% 17.05%
2021-12-31T00:00:00Z 0.04 21.74% 21.74% 21.74%
2022-01-01T00:00:00Z 0.02 2.0% 2.0% 24.17%
2022-01-02T00:00:00Z 0.02 4.04% 4.04% 26.65%
2022-01-03T00:00:00Z 0.05 9.24% 9.24% 32.99%

Fiddle

英文:

To find the cumulative daily return for nth day, formula is

Cumulative Daily Return of nth day = [(1 + Daily Return 1) * (1 + Daily Return 2) *..........*(1+ Daily Return n)]- 1

Example: To calculate the Cumulative return of 2nd day, applying the value in the above formula,
[(1+0.05)(1+0.02)]-1
=[1.05
1.02]-1
=1.071-1
=0.071

Since Kusto doesn't have any window function like row_cumsum() for cumulative product, we can sum the log10 values of daily return value using row_cumsum() function and then take the exp10 for the resultant value. Below is the code to calculate the total cumulative returns, cumulative returns for each month and same for each year.

Code:

let dailyReturns = datatable(Date:datetime, DailyReturn:real)
[
    datetime(2021-12-25), 0.05,
    datetime(2021-12-26), 0.02,
    datetime(2021-12-27), 0.03,
    datetime(2021-12-28), 0.02,
    datetime(2021-12-29), 0.01,
    datetime(2021-12-30), 0.03,
    datetime(2021-12-31), 0.04,
    datetime(2022-01-01), 0.02,
    datetime(2022-01-02), 0.02,
    datetime(2022-01-03), 0.05,
];
dailyReturns
| extend Month = getmonth(Date)
| extend Year = getyear(Date)
| order by Date asc 
| extend log_DR= log10(1+DailyReturn)
| extend month_cumsum_log_DR = row_cumsum(log_DR, Month != prev(Month))
| extend MTD_return=exp10(month_cumsum_log_DR)-1
| extend MTD_return = strcat(round(MTD_return * 100,2), "%")
| extend year_cumsum_log_DR = row_cumsum(log_DR, Year != prev(Year))
| extend YTD_return=exp10(year_cumsum_log_DR)-1
| extend YTD_return = strcat(round(YTD_return * 100,2), "%")
| extend total_cumsum_log_DR = row_cumsum(log_DR)
| extend Total_cumulative_return = exp10(total_cumsum_log_DR) - 1
| extend Total_cumulative_return = strcat(round(Total_cumulative_return * 100,2), "%")
| project Date,DailyReturn,MTD_return,YTD_return,Total_cumulative_return

Output:

Date DailyReturn MTD_return YTD_return Total_cumulative_return
2021-12-25T00:00:00Z 0.05 5.0% 5.0% 5.0%
2021-12-26T00:00:00Z 0.02 7.1% 7.1% 7.1%
2021-12-27T00:00:00Z 0.03 10.31% 10.31% 10.31%
2021-12-28T00:00:00Z 0.02 12.52% 12.52% 12.52%
2021-12-29T00:00:00Z 0.01 13.64% 13.64% 13.64%
2021-12-30T00:00:00Z 0.03 17.05% 17.05% 17.05%
2021-12-31T00:00:00Z 0.04 21.74% 21.74% 21.74%
2022-01-01T00:00:00Z 0.02 2.0% 2.0% 24.17%
2022-01-02T00:00:00Z 0.02 4.04% 4.04% 26.65%
2022-01-03T00:00:00Z 0.05 9.24% 9.24% 32.99%

Fiddle

huangapple
  • 本文由 发表于 2023年6月16日 04:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定