Lean  $LEAN_TAG$
Field.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 QuantConnect.Data;
19 
20 namespace QuantConnect
21 {
22  /// <summary>
23  /// Provides static properties to be used as selectors with the indicator system
24  /// </summary>
25  public static partial class Field
26  {
27  /// <summary>
28  /// Gets a selector that selects the Open value
29  /// </summary>
30  public static Func<IBaseData, decimal> Open
31  {
32  get { return BaseDataBarPropertyOrValue(x => x.Open); }
33  }
34 
35  /// <summary>
36  /// Gets a selector that selects the High value
37  /// </summary>
38  public static Func<IBaseData, decimal> High
39  {
40  get { return BaseDataBarPropertyOrValue(x => x.High); }
41  }
42 
43  /// <summary>
44  /// Gets a selector that selects the Low value
45  /// </summary>
46  public static Func<IBaseData, decimal> Low
47  {
48  get { return BaseDataBarPropertyOrValue(x => x.Low); }
49  }
50 
51  /// <summary>
52  /// Gets a selector that selects the Close value
53  /// </summary>
54  public static Func<IBaseData, decimal> Close
55  {
56  get { return x => x.Value; }
57  }
58 
59  /// <summary>
60  /// Defines an average price that is equal to (O + H + L + C) / 4
61  /// </summary>
62  public static Func<IBaseData, decimal> Average
63  {
64  get { return BaseDataBarPropertyOrValue(x => (x.Open + x.High + x.Low + x.Close) / 4m); }
65  }
66 
67  /// <summary>
68  /// Defines an average price that is equal to (H + L) / 2
69  /// </summary>
70  public static Func<IBaseData, decimal> Median
71  {
72  get { return BaseDataBarPropertyOrValue(x => (x.High + x.Low) / 2m); }
73  }
74 
75  /// <summary>
76  /// Defines an average price that is equal to (H + L + C) / 3
77  /// </summary>
78  public static Func<IBaseData, decimal> Typical
79  {
80  get { return BaseDataBarPropertyOrValue(x => (x.High + x.Low + x.Close) / 3m); }
81  }
82 
83  /// <summary>
84  /// Defines an average price that is equal to (H + L + 2*C) / 4
85  /// </summary>
86  public static Func<IBaseData, decimal> Weighted
87  {
88  get { return BaseDataBarPropertyOrValue(x => (x.High + x.Low + 2 * x.Close) / 4m); }
89  }
90 
91  /// <summary>
92  /// Defines an average price that is equal to (2*O + H + L + 3*C)/7
93  /// </summary>
94  public static Func<IBaseData, decimal> SevenBar
95  {
96  get { return BaseDataBarPropertyOrValue(x => (2*x.Open + x.High + x.Low + 3*x.Close)/7m); }
97  }
98 
99  /// <summary>
100  /// Gets a selector that selectors the Volume value
101  /// </summary>
102  public static Func<IBaseData, decimal> Volume
103  {
104  get { return BaseDataBarPropertyOrValue(x => x is TradeBar ? ((TradeBar)x).Volume : 0m, x => 0m); }
105  }
106 
107  private static Func<IBaseData, decimal> BaseDataBarPropertyOrValue(Func<IBaseDataBar, decimal> selector, Func<IBaseData, decimal> defaultSelector = null)
108  {
109  return x =>
110  {
111  var bar = x as IBaseDataBar;
112  if (bar != null)
113  {
114  return selector(bar);
115  }
116  defaultSelector = defaultSelector ?? (data => data.Value);
117  return defaultSelector(x);
118  };
119  }
120  }
121 }