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  private readonly static Func<IBaseData, decimal> _high = DataTypePropertyOrValue<IBaseDataBar>(x => x.High);
28  private readonly static Func<IBaseData, decimal> _low = DataTypePropertyOrValue<IBaseDataBar>(x => x.Low);
29  private readonly static Func<IBaseData, decimal> _open = DataTypePropertyOrValue<IBaseDataBar>(x => x.Open);
30  private readonly static Func<IBaseData, decimal> _bidClose = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Bid.Close);
31  private readonly static Func<IBaseData, decimal> _bidOpen = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Bid.Open);
32  private readonly static Func<IBaseData, decimal> _bidLow = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Bid.Low);
33  private readonly static Func<IBaseData, decimal> _bidHigh = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Bid.High);
34  private readonly static Func<IBaseData, decimal> _askClose = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Ask.Close);
35  private readonly static Func<IBaseData, decimal> _askOpen = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Ask.Open);
36  private readonly static Func<IBaseData, decimal> _askLow = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Ask.Low);
37  private readonly static Func<IBaseData, decimal> _askHigh = DataTypePropertyOrValue<QuoteBar>(x => ((QuoteBar)x).Ask.High);
38  private readonly static Func<IBaseData, decimal> _bidPrice = DataTypePropertyOrValue<Tick>(x => ((Tick)x).BidPrice, defaultQuoteSelector: x => ((QuoteBar)x).Bid.Close);
39  private readonly static Func<IBaseData, decimal> _askPrice = DataTypePropertyOrValue<Tick>(x => ((Tick)x).AskPrice, defaultQuoteSelector: x => ((QuoteBar)x).Ask.Close);
40  private readonly static Func<IBaseData, decimal> _volume = DataTypePropertyOrValue<TradeBar>(x => ((TradeBar)x).Volume, x => ((Tick)x).Quantity);
41  private readonly static Func<IBaseData, decimal> _average = DataTypePropertyOrValue<IBaseDataBar>(x => (x.Open + x.High + x.Low + x.Close) / 4m);
42  private readonly static Func<IBaseData, decimal> _median = DataTypePropertyOrValue<IBaseDataBar>(x => (x.High + x.Low) / 2m);
43  private readonly static Func<IBaseData, decimal> _typical = DataTypePropertyOrValue<IBaseDataBar>(x => (x.High + x.Low + x.Close) / 3m);
44  private readonly static Func<IBaseData, decimal> _weighted = DataTypePropertyOrValue<IBaseDataBar>(x => (x.High + x.Low + 2 * x.Close) / 4m);
45  private readonly static Func<IBaseData, decimal> _sevenBar = DataTypePropertyOrValue<IBaseDataBar>(x => (2 * x.Open + x.High + x.Low + 3 * x.Close) / 7m);
46 
47  /// <summary>
48  /// Gets a selector that selectes the Bid close price
49  /// </summary>
50  public static Func<IBaseData, decimal> BidClose
51  {
52  get { return _bidClose; }
53  }
54 
55  /// <summary>
56  /// Gets a selector that selectes the Bid open price
57  /// </summary>
58  public static Func<IBaseData, decimal> BidOpen
59  {
60  get { return _bidOpen; }
61  }
62 
63  /// <summary>
64  /// Gets a selector that selectes the Bid low price
65  /// </summary>
66  public static Func<IBaseData, decimal> BidLow
67  {
68  get { return _bidLow; }
69  }
70 
71  /// <summary>
72  /// Gets a selector that selectes the Bid high price
73  /// </summary>
74  public static Func<IBaseData, decimal> BidHigh
75  {
76  get { return _bidHigh; }
77  }
78 
79  /// <summary>
80  /// Gets a selector that selectes the Ask close price
81  /// </summary>
82  public static Func<IBaseData, decimal> AskClose
83  {
84  get { return _askClose; }
85  }
86 
87  /// <summary>
88  /// Gets a selector that selectes the Ask open price
89  /// </summary>
90  public static Func<IBaseData, decimal> AskOpen
91  {
92  get { return _askOpen; }
93  }
94 
95  /// <summary>
96  /// Gets a selector that selectes the Ask low price
97  /// </summary>
98  public static Func<IBaseData, decimal> AskLow
99  {
100  get { return _askLow; }
101  }
102 
103  /// <summary>
104  /// Gets a selector that selectes the Ask high price
105  /// </summary>
106  public static Func<IBaseData, decimal> AskHigh
107  {
108  get { return _askHigh; }
109  }
110 
111  /// <summary>
112  /// Gets a selector that selectes the Ask price
113  /// </summary>
114  public static Func<IBaseData, decimal> AskPrice
115  {
116  get { return _askPrice; }
117  }
118 
119  /// <summary>
120  /// Gets a selector that selectes the Bid price
121  /// </summary>
122  public static Func<IBaseData, decimal> BidPrice
123  {
124  get { return _bidPrice; }
125  }
126 
127  /// <summary>
128  /// Gets a selector that selects the Open value
129  /// </summary>
130  public static Func<IBaseData, decimal> Open
131  {
132  get { return _open; }
133  }
134 
135  /// <summary>
136  /// Gets a selector that selects the High value
137  /// </summary>
138  public static Func<IBaseData, decimal> High
139  {
140  get { return _high; }
141  }
142 
143  /// <summary>
144  /// Gets a selector that selects the Low value
145  /// </summary>
146  public static Func<IBaseData, decimal> Low
147  {
148  get { return _low; }
149  }
150 
151  /// <summary>
152  /// Gets a selector that selects the Close value
153  /// </summary>
154  public static Func<IBaseData, decimal> Close
155  {
156  get { return x => x.Value; }
157  }
158 
159  /// <summary>
160  /// Defines an average price that is equal to (O + H + L + C) / 4
161  /// </summary>
162  public static Func<IBaseData, decimal> Average
163  {
164  get { return _average; }
165  }
166 
167  /// <summary>
168  /// Defines an average price that is equal to (H + L) / 2
169  /// </summary>
170  public static Func<IBaseData, decimal> Median
171  {
172  get { return _median; }
173  }
174 
175  /// <summary>
176  /// Defines an average price that is equal to (H + L + C) / 3
177  /// </summary>
178  public static Func<IBaseData, decimal> Typical
179  {
180  get { return _typical; }
181  }
182 
183  /// <summary>
184  /// Defines an average price that is equal to (H + L + 2*C) / 4
185  /// </summary>
186  public static Func<IBaseData, decimal> Weighted
187  {
188  get { return _weighted; }
189  }
190 
191  /// <summary>
192  /// Defines an average price that is equal to (2*O + H + L + 3*C)/7
193  /// </summary>
194  public static Func<IBaseData, decimal> SevenBar
195  {
196  get { return _sevenBar; }
197  }
198 
199  /// <summary>
200  /// Gets a selector that selectors the Volume value
201  /// </summary>
202  public static Func<IBaseData, decimal> Volume
203  {
204  get { return _volume; }
205  }
206 
207  private static Func<IBaseData, decimal> DataTypePropertyOrValue<T>(Func<T, decimal> selector,
208  Func<IBaseData, decimal> defaultTickSelector = null,
209  Func<IBaseData, decimal> defaultQuoteSelector = null)
210  where T: class, IBaseData
211  {
212  return x =>
213  {
214  var dataType = x as T;
215  if (dataType != null)
216  {
217  return selector(dataType);
218  }
219 
220  var tick = x as Tick;
221  if (tick != null && defaultTickSelector != null)
222  {
223  return defaultTickSelector(x as Tick);
224  }
225 
226  var quoteBar = x as QuoteBar;
227  if (quoteBar != null && defaultQuoteSelector != null)
228  {
229  return defaultQuoteSelector(x as QuoteBar);
230  }
231 
232  var defaultSelector = new Func<IBaseData, decimal>(data => data.Value);
233  return defaultSelector(x);
234  };
235  }
236  }
237 }