forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeedleUtil.cs
205 lines (176 loc) · 6.76 KB
/
DeedleUtil.cs
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
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Deedle;
using System;
using System.Collections.Generic;
using System.Linq;
namespace QuantConnect.Report
{
/// <summary>
/// Utility extension methods for Deedle series/frames
/// </summary>
public static class DeedleUtil
{
/// <summary>
/// Calculates the cumulative sum for the given series
/// </summary>
/// <param name="input">Series to calculate cumulative sum for</param>
/// <returns>Cumulative sum in series form</returns>
public static Series<DateTime, double> CumulativeSum(this Series<DateTime, double> input)
{
if (input.IsEmpty)
{
return input;
}
var prev = 0.0;
return input.SelectValues(current =>
{
var sum = prev + current;
prev = sum;
return sum;
});
}
/// <summary>
/// Calculates the cumulative product of the series. This is equal to the python pandas method: `df.cumprod()`
/// </summary>
/// <param name="input">Input series</param>
/// <returns>Cumulative product</returns>
public static Series<DateTime, double> CumulativeProduct(this Series<DateTime, double> input)
{
if (input.IsEmpty)
{
return input;
}
var prev = 1.0;
return input.SelectValues(current =>
{
var product = prev * current;
prev = product;
return product;
});
}
/// <summary>
/// Calculates the cumulative max of the series. This is equal to the python pandas method: `df.cummax()`.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Series<DateTime, double> CumulativeMax(this Series<DateTime, double> input)
{
if (input.IsEmpty)
{
return input;
}
var prevMax = double.NegativeInfinity;
var values = new List<double>();
foreach (var point in input.Values)
{
if (point > prevMax)
{
prevMax = point;
}
values.Add(prevMax);
}
return new Series<DateTime, double>(input.Keys, values);
}
/// <summary>
/// Calculates the percentage change from the previous value to the current
/// </summary>
/// <param name="input">Series to calculate percentage change for</param>
/// <returns>Percentage change in series form</returns>
/// <remarks>Equivalent to `df.pct_change()`</remarks>
public static Series<DateTime, double> PercentChange(this Series<DateTime, double> input)
{
if (input.IsEmpty)
{
return input;
}
var inputShifted = input.Shift(1);
return (input - inputShifted) / inputShifted;
}
/// <summary>
/// Calculates the cumulative returns series of the given input equity curve
/// </summary>
/// <param name="input">Equity curve series</param>
/// <returns>Cumulative returns over time</returns>
public static Series<DateTime, double> CumulativeReturns(this Series<DateTime, double> input)
{
if (input.IsEmpty)
{
return input;
}
return (input.PercentChange()
.Where(kvp => !double.IsInfinity(kvp.Value)) + 1)
.CumulativeProduct() - 1;
}
/// <summary>
/// Calculates the total returns over a period of time for the given input
/// </summary>
/// <param name="input">Equity curve series</param>
/// <returns>Total returns over time</returns>
public static double TotalReturns(this Series<DateTime, double> input)
{
var returns = input.CumulativeReturns();
if (returns.IsEmpty)
{
return double.NaN;
}
return returns.LastValue();
}
/// <summary>
/// Drops sparse columns only if every value is `missing` in the column
/// </summary>
/// <typeparam name="TRowKey">Frame row key</typeparam>
/// <typeparam name="TColumnKey">Frame column key</typeparam>
/// <param name="frame">Data Frame</param>
/// <returns>new Frame with sparse columns dropped</returns>
/// <remarks>Equivalent to `df.dropna(axis=1, how='all')`</remarks>
public static Frame<TRowKey, TColumnKey> DropSparseColumnsAll<TRowKey, TColumnKey>(this Frame<TRowKey, TColumnKey> frame)
{
var newFrame = frame.Clone();
foreach (var key in frame.ColumnKeys)
{
if (newFrame[key].DropMissing().ValueCount == 0)
{
newFrame.DropColumn(key);
}
}
return newFrame;
}
/// <summary>
/// Drops sparse rows if and only if every value is `missing` in the Frame
/// </summary>
/// <typeparam name="TRowKey">Frame row key</typeparam>
/// <typeparam name="TColumnKey">Frame column key</typeparam>
/// <param name="frame">Data Frame</param>
/// <returns>new Frame with sparse rows dropped</returns>
/// <remarks>Equivalent to `df.dropna(how='all')`</remarks>
public static Frame<TRowKey, TColumnKey> DropSparseRowsAll<TRowKey, TColumnKey>(this Frame<TRowKey, TColumnKey> frame)
{
if (frame.ColumnKeys.Count() == 0)
{
return Frame.CreateEmpty<TRowKey, TColumnKey>();
}
var newFrame = frame.Clone().Transpose();
foreach (var key in frame.RowKeys)
{
if (newFrame[key].DropMissing().ValueCount == 0)
{
newFrame.DropColumn(key);
}
}
return newFrame.Transpose();
}
}
}