/* * 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 System; using QuantConnect.Data; using QuantConnect.Data.Market; namespace QuantConnect { /// /// Provides static properties to be used as selectors with the indicator system /// public static partial class Field { private readonly static Func _high = DataTypePropertyOrValue(x => x.High); private readonly static Func _low = DataTypePropertyOrValue(x => x.Low); private readonly static Func _open = DataTypePropertyOrValue(x => x.Open); private readonly static Func _bidClose = DataTypePropertyOrValue(x => ((QuoteBar)x).Bid.Close); private readonly static Func _bidOpen = DataTypePropertyOrValue(x => ((QuoteBar)x).Bid.Open); private readonly static Func _bidLow = DataTypePropertyOrValue(x => ((QuoteBar)x).Bid.Low); private readonly static Func _bidHigh = DataTypePropertyOrValue(x => ((QuoteBar)x).Bid.High); private readonly static Func _askClose = DataTypePropertyOrValue(x => ((QuoteBar)x).Ask.Close); private readonly static Func _askOpen = DataTypePropertyOrValue(x => ((QuoteBar)x).Ask.Open); private readonly static Func _askLow = DataTypePropertyOrValue(x => ((QuoteBar)x).Ask.Low); private readonly static Func _askHigh = DataTypePropertyOrValue(x => ((QuoteBar)x).Ask.High); private readonly static Func _bidPrice = DataTypePropertyOrValue(x => ((Tick)x).BidPrice, defaultQuoteSelector: x => ((QuoteBar)x).Bid.Close); private readonly static Func _askPrice = DataTypePropertyOrValue(x => ((Tick)x).AskPrice, defaultQuoteSelector: x => ((QuoteBar)x).Ask.Close); private readonly static Func _volume = DataTypePropertyOrValue(x => ((TradeBar)x).Volume, x => ((Tick)x).Quantity); private readonly static Func _average = DataTypePropertyOrValue(x => (x.Open + x.High + x.Low + x.Close) / 4m); private readonly static Func _median = DataTypePropertyOrValue(x => (x.High + x.Low) / 2m); private readonly static Func _typical = DataTypePropertyOrValue(x => (x.High + x.Low + x.Close) / 3m); private readonly static Func _weighted = DataTypePropertyOrValue(x => (x.High + x.Low + 2 * x.Close) / 4m); private readonly static Func _sevenBar = DataTypePropertyOrValue(x => (2 * x.Open + x.High + x.Low + 3 * x.Close) / 7m); /// /// Gets a selector that selectes the Bid close price /// public static Func BidClose { get { return _bidClose; } } /// /// Gets a selector that selectes the Bid open price /// public static Func BidOpen { get { return _bidOpen; } } /// /// Gets a selector that selectes the Bid low price /// public static Func BidLow { get { return _bidLow; } } /// /// Gets a selector that selectes the Bid high price /// public static Func BidHigh { get { return _bidHigh; } } /// /// Gets a selector that selectes the Ask close price /// public static Func AskClose { get { return _askClose; } } /// /// Gets a selector that selectes the Ask open price /// public static Func AskOpen { get { return _askOpen; } } /// /// Gets a selector that selectes the Ask low price /// public static Func AskLow { get { return _askLow; } } /// /// Gets a selector that selectes the Ask high price /// public static Func AskHigh { get { return _askHigh; } } /// /// Gets a selector that selectes the Ask price /// public static Func AskPrice { get { return _askPrice; } } /// /// Gets a selector that selectes the Bid price /// public static Func BidPrice { get { return _bidPrice; } } /// /// Gets a selector that selects the Open value /// public static Func Open { get { return _open; } } /// /// Gets a selector that selects the High value /// public static Func High { get { return _high; } } /// /// Gets a selector that selects the Low value /// public static Func Low { get { return _low; } } /// /// Gets a selector that selects the Close value /// public static Func Close { get { return x => x.Value; } } /// /// Defines an average price that is equal to (O + H + L + C) / 4 /// public static Func Average { get { return _average; } } /// /// Defines an average price that is equal to (H + L) / 2 /// public static Func Median { get { return _median; } } /// /// Defines an average price that is equal to (H + L + C) / 3 /// public static Func Typical { get { return _typical; } } /// /// Defines an average price that is equal to (H + L + 2*C) / 4 /// public static Func Weighted { get { return _weighted; } } /// /// Defines an average price that is equal to (2*O + H + L + 3*C)/7 /// public static Func SevenBar { get { return _sevenBar; } } /// /// Gets a selector that selectors the Volume value /// public static Func Volume { get { return _volume; } } private static Func DataTypePropertyOrValue(Func selector, Func defaultTickSelector = null, Func defaultQuoteSelector = null) where T: class, IBaseData { return x => { var dataType = x as T; if (dataType != null) { return selector(dataType); } var tick = x as Tick; if (tick != null && defaultTickSelector != null) { return defaultTickSelector(x as Tick); } var quoteBar = x as QuoteBar; if (quoteBar != null && defaultQuoteSelector != null) { return defaultQuoteSelector(x as QuoteBar); } var defaultSelector = new Func(data => data.Value); return defaultSelector(x); }; } } }