Lean  $LEAN_TAG$
Period.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using static QuantConnect.StringExtensions;
18 
20 {
21  /// <summary>
22  /// Period constants for multi-period fields
23  /// </summary>
24  public static class Period
25  {
26  /// <summary>
27  /// Period constant for one month
28  /// </summary>
29  public const string OneMonth = "1M";
30 
31  /// <summary>
32  /// Period constant for two months
33  /// </summary>
34  public const string TwoMonths = "2M";
35 
36  /// <summary>
37  /// Period constant for three months
38  /// </summary>
39  public const string ThreeMonths = "3M";
40 
41  /// <summary>
42  /// Period constant for six months
43  /// </summary>
44  public const string SixMonths = "6M";
45 
46  /// <summary>
47  /// Period constant for nine months
48  /// </summary>
49  public const string NineMonths = "9M";
50 
51  /// <summary>
52  /// Period constant for twelve months
53  /// </summary>
54  public const string TwelveMonths = "12M";
55 
56  /// <summary>
57  /// Period constant for one year
58  /// </summary>
59  public const string OneYear = "1Y";
60 
61  /// <summary>
62  /// Period constant for two years
63  /// </summary>
64  public const string TwoYears = "2Y";
65 
66  /// <summary>
67  /// Period constant for three years
68  /// </summary>
69  public const string ThreeYears = "3Y";
70 
71  /// <summary>
72  /// Period constant for five years
73  /// </summary>
74  public const string FiveYears = "5Y";
75 
76  /// <summary>
77  /// Period constant for ten years
78  /// </summary>
79  public const string TenYears = "10Y";
80  }
81 
82  /// <summary>
83  /// Period constants for multi-period fields as bytes
84  /// </summary>
85  /// <remarks>For performance speed and memory using bytes versus strings.
86  /// This is the period we are going to store in memory</remarks>
87  internal static class PeriodAsByte
88  {
89  /// <summary>
90  /// Converts a byte period to its string equivalent
91  /// </summary>
92  public static string Convert(byte period)
93  {
94  switch (period)
95  {
96  case 0:
97  // no period case
98  return "";
99  case 1:
100  return Period.OneMonth;
101  case 2:
102  return Period.TwoMonths;
103  case 3:
104  return Period.ThreeMonths;
105  case 6:
106  return Period.SixMonths;
107  case 9:
108  return Period.NineMonths;
109  case 12:
110  return Period.TwelveMonths;
111  case 121:
112  return Period.OneYear;
113  case 24:
114  return Period.TwoYears;
115  case 36:
116  return Period.ThreeYears;
117  case 60:
118  return Period.FiveYears;
119  case 120:
120  return Period.TenYears;
121  default:
122  throw new InvalidOperationException(Invariant($"{period} is not a valid period value"));
123  }
124  }
125 
126  /// <summary>
127  /// Converts a string period to its byte equivalent
128  /// </summary>
129  public static byte Convert(string period)
130  {
131  switch (period)
132  {
133  case "":
134  // no period case
135  return NoPeriod;
136  case Period.OneMonth:
137  return OneMonth;
138  case Period.TwoMonths:
139  return TwoMonths;
140  case Period.ThreeMonths:
141  return ThreeMonths;
142  case Period.SixMonths:
143  return SixMonths;
144  case Period.NineMonths:
145  return NineMonths;
146  case Period.TwelveMonths:
147  return TwelveMonths;
148  case Period.OneYear:
149  return OneYear;
150  case Period.TwoYears:
151  return TwoYears;
152  case Period.ThreeYears:
153  return ThreeYears;
154  case Period.FiveYears:
155  return FiveYears;
156  case Period.TenYears:
157  return TenYears;
158  default:
159  throw new InvalidOperationException($"{period} is not a valid period value");
160  }
161  }
162 
163  /// <summary>
164  /// No Period constant
165  /// </summary>
166  public const byte NoPeriod = 0;
167 
168  /// <summary>
169  /// Period constant for one month
170  /// </summary>
171  public const byte OneMonth = 1;
172 
173  /// <summary>
174  /// Period constant for two months
175  /// </summary>
176  public const byte TwoMonths = 2;
177 
178  /// <summary>
179  /// Period constant for three months
180  /// </summary>
181  public const byte ThreeMonths = 3;
182 
183  /// <summary>
184  /// Period constant for six months
185  /// </summary>
186  public const byte SixMonths = 6;
187 
188  /// <summary>
189  /// Period constant for nine months
190  /// </summary>
191  public const byte NineMonths = 9;
192 
193  /// <summary>
194  /// Period constant for twelve months
195  /// </summary>
196  public const byte TwelveMonths = 12;
197 
198  /// <summary>
199  /// Period constant for one year
200  /// </summary>
201  public const byte OneYear = 121;
202 
203  /// <summary>
204  /// Period constant for two years
205  /// </summary>
206  public const byte TwoYears = 24;
207 
208  /// <summary>
209  /// Period constant for three years
210  /// </summary>
211  public const byte ThreeYears = 36;
212 
213  /// <summary>
214  /// Period constant for five years
215  /// </summary>
216  public const byte FiveYears = 60;
217 
218  /// <summary>
219  /// Period constant for ten years
220  /// </summary>
221  public const byte TenYears = 120;
222  }
223 }