/// <summary> /// Slices this collection, returning a new collection containing only /// positions matching the specified <paramref name="comparison"/> and <paramref name="expiration"/> /// </summary> public OptionPositionCollection Slice(BinaryComparison comparison, DateTime expiration, bool includeUnderlying = true) { var expirations = comparison.Filter(_expirations, expiration); if (expirations.IsEmpty) { return(includeUnderlying && HasUnderlying?Empty.Add(UnderlyingPosition) : Empty); } var positions = ImmutableDictionary <Symbol, OptionPosition> .Empty; if (includeUnderlying) { OptionPosition underlyingPosition; if (_positions.TryGetValue(Underlying, out underlyingPosition)) { positions = positions.Add(Underlying, underlyingPosition); } } var sides = ImmutableDictionary <PositionSide, ImmutableHashSet <Symbol> > .Empty; var rights = ImmutableDictionary <OptionRight, ImmutableHashSet <Symbol> > .Empty; var strikes = ImmutableSortedDictionary <decimal, ImmutableHashSet <Symbol> > .Empty; foreach (var symbol in expirations.SelectMany(kvp => kvp.Value)) { var position = _positions[symbol]; sides = sides.Add(position.Side, symbol); positions = positions.Add(symbol, position); rights = rights.Add(symbol.ID.OptionRight, symbol); strikes = strikes.Add(symbol.ID.StrikePrice, symbol); } return(new OptionPositionCollection(positions, rights, sides, strikes, expirations)); }
public ActionConditionCompareParent() : base("Compare Parent") { // Objects[0] = SelectionSource.Original; // Objects[1] = SelectionSource.Original; // ObjectStrings[0] = "object type"; // ObjectStrings[1] = "object type"; argument = BinaryComparison.Equal; }
/// <summary> /// Initializes a new instance of the <see cref="OptionStrategyLegPredicate"/> class /// </summary> /// <param name="comparison">The <see cref="BinaryComparison"/> invoked</param> /// <param name="reference">The reference value, such as a strike price, encapsulated within the /// <see cref="IOptionStrategyLegPredicateReferenceValue"/> to enable resolving the value from different potential sets.</param> /// <param name="predicate">The compiled predicate expression</param> /// <param name="expression">The predicate expression, from which, all other values were derived.</param> public OptionStrategyLegPredicate( BinaryComparison comparison, IOptionStrategyLegPredicateReferenceValue reference, Func <IReadOnlyList <OptionPosition>, OptionPosition, bool> predicate, Expression <Func <IReadOnlyList <OptionPosition>, OptionPosition, bool> > expression ) { _reference = reference; _predicate = predicate; _comparison = comparison; _expression = expression; }
/// <summary> /// Creates a new <see cref="OptionStrategyLegPredicate"/> from the specified predicate <paramref name="expression"/> /// </summary> public static OptionStrategyLegPredicate Create( Expression <Func <IReadOnlyList <OptionPosition>, OptionPosition, bool> > expression ) { // expr must NOT include compound comparisons // expr is a lambda of one of the following forms: // (legs, position) => position.{target} {comparison} legs[i].{reference-target} // (legs, position) => legs[i].{reference-target} {comparison} position.{target} // (legs, position) => position.{target} {comparison} {literal-reference-target} // (legs, position) => {literal-reference-target} {comparison} position.{target} // we want to make the comparison of a common form, specifically: // position.{target} {comparison} {reference-target} // this is so when we invoke OptionPositionCollection we have the correct comparison type // for example, legs[0].Strike > position.Strike // needs to be inverted into position.Strike < legs[0].Strike // so we can call OptionPositionCollection.Slice(BinaryComparison.LessThan, legs[0].Strike); try { var legsParameter = expression.Parameters[0]; var positionParameter = expression.Parameters[1]; var binary = expression.OfType <BinaryExpression>().Single(e => e.NodeType.IsBinaryComparison()); var comparison = BinaryComparison.FromExpressionType(binary.NodeType); var leftReference = CreateReferenceValue(legsParameter, positionParameter, binary.Left); var rightReference = CreateReferenceValue(legsParameter, positionParameter, binary.Right); if (leftReference != null && rightReference != null) { throw new ArgumentException($"The provided expression is not of the required form: {expression}"); } // we want the left side to be null, indicating position.{target} // if not, then we need to flip the comparison operand var reference = rightReference; if (rightReference == null) { reference = leftReference; comparison = comparison.FlipOperands(); } return(new OptionStrategyLegPredicate(comparison, reference, expression.Compile(), expression)); } catch { // we can still handle arbitrary predicates, they just require a full search of the positions // as we're unable to leverage any of the pre-build indexes via Slice methods. return(new OptionStrategyLegPredicate(null, null, expression.Compile(), expression)); } }
public void EvaluatesFlippedOperandsComparison(ExpressionType type, bool expected1, bool expected2) { const int left1 = 1; const int right1 = 1; const int left2 = 2; const int right2 = 3; var comparison = BinaryComparison.FromExpressionType(type).FlipOperands(); var actual1 = comparison.Evaluate(left1, right1); Assert.AreEqual(expected1, actual1); var actual2 = comparison.Evaluate(left2, right2); Assert.AreEqual(expected2, actual2); }
public void BinaryComparison_Criteria() { var spec = new BinaryComparison<int>(5, Expression.Equal); var criteria = spec.Criteria; Assert.That(criteria.Body, Is.AssignableTo<BinaryExpression>()); var binary = (BinaryExpression)criteria.Body; Assert.That(binary.NodeType, Is.EqualTo(ExpressionType.Equal)); Assert.That(binary.Conversion, Is.Null); Assert.That(binary.Method, Is.Null); Assert.That(binary.IsLifted, Is.False); Assert.That(binary.IsLiftedToNull, Is.False); Assert.That(binary.Left, Is.AssignableTo<ParameterExpression>()); Assert.That(binary.Right, Is.TypeOf<ConstantExpression>()); var right = (ConstantExpression)binary.Right; Assert.That(right.Value, Is.EqualTo(5)); ExpressionWriter.Write(criteria); }
public override void CopyFrom(Action copy) { base.CopyFrom(copy); argument = (copy as ActionConditionCompareParent).argument; }
public override void LoadParameters(System.IO.BinaryReader reader) { base.LoadParameters(reader); argument = (BinaryComparison)reader.ReadInt32(); }
public List <bool> EvalConditionOperator(Dictionary <string, List <object> > map, int recordCount, BinaryComparison bc) { List <object> leftResults = Left.Eval(map, recordCount); List <object> rightResults = Right.Eval(map, recordCount); List <bool> finalResults = new List <bool>(leftResults.Count); for (int i = 0; i < leftResults.Count; i++) { finalResults.Add(bc(Convert.ToDouble(leftResults[i]), Convert.ToDouble(rightResults[i]))); } return(finalResults); }
string RenderConditions(ConditionBuilder where) { StringBuilder sql = new StringBuilder(); bool isFirst = true; foreach (var item in where.Items) { if (item is Conjunction && !isFirst) { var conjunction = (Conjunction)item; if (conjunction.TypeOfConjunction == Conjunction.ConjuntionType.Or) { sql.Append("or "); } else if (conjunction.TypeOfConjunction == Conjunction.ConjuntionType.And) { sql.Append("and "); } } else if (item is ConditionBuilder) { sql.Append("("); sql.Append(RenderConditions((ConditionBuilder)item)); sql.Append(") "); } else if (item is UninaryComparison) { UninaryComparison comparison = (UninaryComparison)item; sql.Append(comparison.Column + " "); switch (comparison.TypeOfComparion) { case UninaryComparison.UninaryComparisonType.IsNull: sql.Append("is null "); break; case UninaryComparison.UninaryComparisonType.IsNotNull: sql.Append("is not null "); break; default: throw new NotImplementedException(); } } else if (item is BinaryComparison) { BinaryComparison comparison = (BinaryComparison)item; string op = ""; switch (comparison.TypeOfComparison) { case BinaryComparison.BinaryComparisonType.Equal: op = "="; break; case BinaryComparison.BinaryComparisonType.NotEqual: op = "<>"; break; case BinaryComparison.BinaryComparisonType.Like: op = " like "; break; case BinaryComparison.BinaryComparisonType.LessThan: op = "<"; break; case BinaryComparison.BinaryComparisonType.LessThanOrEqual: op = "<="; break; case BinaryComparison.BinaryComparisonType.GreaterThan: op = ">"; break; case BinaryComparison.BinaryComparisonType.GreaterThanOrEqual: op = ">="; break; default: throw new NotImplementedException(); } sql.Append(comparison.LeftValue + op + comparison.RightValue + " "); } else if (item is SqlLiteralCondition) { sql.Append(((SqlLiteralCondition)item).SQL.Trim() + " "); } isFirst = false; } return(sql.ToString()); }