Ejemplo n.º 1
0
        public void OrderByMarginImpactDoesNotReturnTargetsForWhichUnorderedQuantityIsZeroBecauseOpenOrder()
        {
            var algorithm      = new FakeAlgorithm();
            var orderProcessor = new FakeOrderProcessor();

            algorithm.Transactions.SetOrderProcessor(orderProcessor);
            var symbol = new Symbol(SecurityIdentifier.GenerateEquity(_symbol, Market.USA), _symbol);
            var equity = algorithm.AddEquity(symbol);

            equity.Cache.AddData(new TradeBar(DateTime.UtcNow, symbol, 1, 1, 1, 1, 1));
            var collection = new PortfolioTargetCollection();
            var target     = new PortfolioTarget(symbol, 1);

            collection.Add(target);

            var openOrderRequest = new SubmitOrderRequest(OrderType.Market, symbol.SecurityType, symbol, 1, 0, 0, DateTime.UtcNow, "");

            openOrderRequest.SetOrderId(1);
            var openOrderTicket = new OrderTicket(algorithm.Transactions, openOrderRequest);

            orderProcessor.AddOrder(new MarketOrder(symbol, 1, DateTime.UtcNow));
            orderProcessor.AddTicket(openOrderTicket);

            var targets = collection.OrderByMarginImpact(algorithm);

            Assert.AreEqual(collection.Count, 1);
            Assert.IsTrue(targets.IsNullOrEmpty());
        }
Ejemplo n.º 2
0
        public void WontRemoveBecauseOpenOrder_Add()
        {
            var orderProvider   = new FakeOrderProcessor();
            var pendingRemovals = new PendingRemovalsManager(orderProvider);
            var security        = SecurityTests.GetSecurity();
            var universe        = new TestUniverse();

            orderProvider.AddOrder(new LimitOrder(security.Symbol, 1, 1, DateTime.UtcNow));

            Assert.IsNull(pendingRemovals.TryRemoveMember(security, universe));
            Assert.AreEqual(1, pendingRemovals.PendingRemovals.Keys.Count());
            Assert.AreEqual(1, pendingRemovals.PendingRemovals.Values.Count());
            Assert.AreEqual(universe, pendingRemovals.PendingRemovals.Keys.First());
            Assert.AreEqual(security, pendingRemovals.PendingRemovals.Values.First().First());
        }
Ejemplo n.º 3
0
        public void NonAccountCurrencyOption_Exercise()
        {
            var reference = new DateTime(2016, 02, 16, 11, 53, 30);
            SecurityPortfolioManager portfolio;
            var security = InitializeTest(reference, out portfolio);

            var cash = new Cash("EUR", 0, 10);

            portfolio.CashBook.Add("EUR", cash);
            var option = new Option(
                Symbols.SPY_C_192_Feb19_2016,
                SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc),
                cash,
                new OptionSymbolProperties(SymbolProperties.GetDefault("EUR")),
                portfolio.CashBook,
                RegisteredSecurityDataTypesProvider.Null,
                new SecurityCache()
                );

            option.Underlying = security;
            security.SetMarketPrice(new Tick {
                Value = 1000
            });
            portfolio.Securities.Add(option);
            var fakeOrderProcessor = new FakeOrderProcessor();

            portfolio.Transactions.SetOrderProcessor(fakeOrderProcessor);

            var fillPrice    = 1000m;
            var fillQuantity = 1;

            option.ExerciseSettlement = SettlementType.Cash;
            var orderFee = new OrderFee(new CashAmount(1, "EUR"));
            var order    = new OptionExerciseOrder(Symbols.SPY_C_192_Feb19_2016, fillQuantity, DateTime.UtcNow);

            fakeOrderProcessor.AddOrder(order);
            var orderDirection = fillQuantity > 0 ? OrderDirection.Buy : OrderDirection.Sell;
            var fill           = new OrderEvent(order.Id, option.Symbol, reference, OrderStatus.Filled, orderDirection, fillPrice, fillQuantity, orderFee);

            portfolio.ProcessFill(fill);

            // (1000 (price) - 192 (call strike)) * 1 quantity => 808 EUR
            Assert.AreEqual(10, option.Holdings.TotalFees); // 1 * 10 (conversion rate to account currency)
            // 808 - 1000 (price) - 1 fee
            Assert.AreEqual(-193, portfolio.CashBook["EUR"].Amount);
            // 100000 initial amount, no fee deducted
            Assert.AreEqual(100000, portfolio.CashBook[Currencies.USD].Amount);
        }
Ejemplo n.º 4
0
        public void WontBeReturnedBecauseReSelected()
        {
            var orderProvider   = new FakeOrderProcessor();
            var pendingRemovals = new PendingRemovalsManager(orderProvider);
            var security        = SecurityTests.GetSecurity();
            var universe        = new TestUniverse();

            orderProvider.AddOrder(new LimitOrder(security.Symbol, 1, 1, DateTime.UtcNow));
            pendingRemovals.TryRemoveMember(security, universe);

            Assert.IsFalse(pendingRemovals.CheckPendingRemovals(
                               new HashSet <Symbol> {
                security.Symbol
            }, universe).Any());

            // internally it was removed because it was reselected
            Assert.AreEqual(0, pendingRemovals.PendingRemovals.Keys.Count());
            Assert.AreEqual(0, pendingRemovals.PendingRemovals.Values.Count());
        }
Ejemplo n.º 5
0
        public void ReturnedRemoved_Check()
        {
            var orderProvider   = new FakeOrderProcessor();
            var pendingRemovals = new PendingRemovalsManager(orderProvider);
            var security        = SecurityTests.GetSecurity();
            var universe        = new TestUniverse();

            orderProvider.AddOrder(new LimitOrder(security.Symbol, 1, 1, DateTime.UtcNow));
            pendingRemovals.TryRemoveMember(security, universe);
            orderProvider.Clear();

            var result = pendingRemovals.CheckPendingRemovals(new HashSet <Symbol>(), universe);

            Assert.IsTrue(result.Any());
            Assert.AreEqual(universe, result.First().Universe);
            Assert.AreEqual(security, result.First().Security);
            Assert.IsFalse(pendingRemovals.CheckPendingRemovals(new HashSet <Symbol>(), universe).Any());
            Assert.AreEqual(0, pendingRemovals.PendingRemovals.Keys.Count());
            Assert.AreEqual(0, pendingRemovals.PendingRemovals.Values.Count());
        }
Ejemplo n.º 6
0
        public void WontRemoveBecauseOfUnderlying()
        {
            var orderProvider   = new FakeOrderProcessor();
            var pendingRemovals = new PendingRemovalsManager(orderProvider);
            var equity          = CreateSecurity(Symbols.SPY);
            var equityOption    = CreateSecurity(Symbols.SPY_C_192_Feb19_2016);

            // we add an order of the equity option
            orderProvider.AddOrder(new LimitOrder(equityOption.Symbol, 1, 1, DateTime.UtcNow));
            var universe = new TestUniverse();

            universe.AddMember(DateTime.UtcNow, equity);
            universe.AddMember(DateTime.UtcNow, equityOption);

            // we try to remove the equity
            Assert.IsNull(pendingRemovals.TryRemoveMember(equity, universe));
            Assert.AreEqual(1, pendingRemovals.PendingRemovals.Keys.Count());
            Assert.AreEqual(1, pendingRemovals.PendingRemovals.Values.Count());
            Assert.AreEqual(universe, pendingRemovals.PendingRemovals.Keys.First());
            Assert.AreEqual(equity, pendingRemovals.PendingRemovals.Values.First().First());
        }