/// <summary> /// Submit orders for the specified portolio targets if the spread is tighter/equal to preset level /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="targets">The portfolio targets to be ordered</param> public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets) { // update the complete set of portfolio targets with the new targets _targetsCollection.AddRange(targets); // for performance we check count value, OrderByMarginImpact and ClearFulfilled are expensive to call if (_targetsCollection.Count > 0) { foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm)) { var symbol = target.Symbol; // calculate remaining quantity to be ordered var unorderedQuantity = OrderSizing.GetUnorderedQuantity(algorithm, target); if (unorderedQuantity != 0) { // get security object var security = algorithm.Securities[symbol]; // check order entry conditions if (PriceIsFavorable(security)) { algorithm.MarketOrder(symbol, unorderedQuantity); } } } _targetsCollection.ClearFulfilled(algorithm); } }
/// <summary> /// Immediately submits orders for the specified portfolio targets. /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="targets">The portfolio targets to be ordered</param> public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets) { _targetsCollection.AddRange(targets); // for performance we check count value, OrderByMarginImpact and ClearFulfilled are expensive to call if (_targetsCollection.Count > 0) { foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm)) { var security = algorithm.Securities[target.Symbol]; // calculate remaining quantity to be ordered var quantity = OrderSizing.GetUnorderedQuantity(algorithm, target, security); if (quantity != 0) { if (security.BuyingPowerModel.AboveMinimumOrderMarginPortfolioPercentage(security, quantity, algorithm.Portfolio, algorithm.Settings.MinimumOrderMarginPortfolioPercentage)) { algorithm.MarketOrder(security, quantity); } } } _targetsCollection.ClearFulfilled(algorithm); } }
public void ClearFulfilledDoesNotRemoveUnreachedTarget() { var algorithm = new FakeAlgorithm(); var symbol = new Symbol(SecurityIdentifier.GenerateEquity(_symbol, Market.USA), _symbol); var equity = algorithm.AddEquity(symbol); var dummySecurityHolding = new FakeSecurityHolding(equity); equity.Holdings = dummySecurityHolding; var collection = new PortfolioTargetCollection(); var target = new PortfolioTarget(symbol, -1); collection.Add(target); collection.ClearFulfilled(algorithm); Assert.AreEqual(collection.Count, 1); }
/// <summary> /// Immediately submits orders for the specified portfolio targets. /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="targets">The portfolio targets to be ordered</param> public override void Execute(QCAlgorithmFramework algorithm, IPortfolioTarget[] targets) { _targetsCollection.AddRange(targets); foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm)) { var existing = algorithm.Securities[target.Symbol].Holdings.Quantity + algorithm.Transactions.GetOpenOrders(target.Symbol).Sum(o => o.Quantity); var quantity = target.Quantity - existing; if (quantity != 0) { algorithm.MarketOrder(target.Symbol, quantity); } } _targetsCollection.ClearFulfilled(algorithm); }
/// <summary> /// Immediately submits orders for the specified portfolio targets. /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="targets">The portfolio targets to be ordered</param> public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets) { _targetsCollection.AddRange(targets); // for performance we check count value, OrderByMarginImpact and ClearFulfilled are expensive to call if (_targetsCollection.Count > 0) { foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm)) { // calculate remaining quantity to be ordered var quantity = OrderSizing.GetUnorderedQuantity(algorithm, target); if (quantity != 0) { algorithm.MarketOrder(target.Symbol, quantity); } } _targetsCollection.ClearFulfilled(algorithm); } }
public void ClearFulfilledRemovesPositiveTarget() { var algorithm = new FakeAlgorithm(); algorithm.SetFinishedWarmingUp(); var symbol = new Symbol(SecurityIdentifier.GenerateEquity(_symbol, Market.USA), _symbol); var equity = algorithm.AddEquity(symbol); var dummySecurityHolding = new FakeSecurityHolding(equity); equity.Holdings = dummySecurityHolding; var collection = new PortfolioTargetCollection(); var target = new PortfolioTarget(symbol, 1); collection.Add(target); dummySecurityHolding.SetQuantity(1); collection.ClearFulfilled(algorithm); Assert.AreEqual(collection.Count, 0); }
/// <summary> /// Immediately submits orders for the specified portfolio targets. /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="targets">The portfolio targets to be ordered</param> public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets) { _targetsCollection.AddRange(targets); // for performance we check count value, OrderByMarginImpact and ClearFulfilled are expensive to call if (_targetsCollection.Count > 0) { foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm)) { var existing = algorithm.Securities[target.Symbol].Holdings.Quantity + algorithm.Transactions.GetOpenOrderTickets(target.Symbol) .Aggregate(0m, (d, ticket) => d + ticket.Quantity - ticket.QuantityFilled); var quantity = target.Quantity - existing; if (quantity != 0) { algorithm.MarketOrder(target.Symbol, quantity); } } _targetsCollection.ClearFulfilled(algorithm); } }
/// <summary> /// Immediately submits orders for the specified portfolio targets. /// </summary> /// <param name="algorithm">The algorithm instance</param> /// <param name="targets">The portfolio targets to be ordered</param> public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets) { _targetsCollection.AddRange(targets); foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm)) { var openQuantity = algorithm.Transactions.GetOpenOrders(target.Symbol) .Sum(x => x.Quantity); var existing = algorithm.Securities[target.Symbol].Holdings.Quantity + openQuantity; var quantity = target.Quantity - existing; // Liquidate positions in Crude Oil ETF that is no longer part of the highest-correlation pair if (_previousSymbol != null && target.Symbol != _previousSymbol) { algorithm.Liquidate(_previousSymbol); } if (quantity != 0) { algorithm.MarketOrder(target.Symbol, quantity); _previousSymbol = target.Symbol; } } _targetsCollection.ClearFulfilled(algorithm); }