예제 #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());
        }
예제 #2
0
        private QCAlgorithm GetAlgorithm(IOrderProcessor orderProcessor)
        {
            var algorithm = new FakeAlgorithm();

            algorithm.SetFinishedWarmingUp();
            algorithm.Transactions.SetOrderProcessor(orderProcessor);
            return(algorithm);
        }
예제 #3
0
        public void DefaultInputSplit(List<object> samples, List<double> labels)
        {
            var people = samples.ConvertAll(x => (Person)x);
            var trainingSetSize = (int)Math.Ceiling(((double)2 / 3) * people.Count);
            var crossValidationSetSize = (int)Math.Ceiling(((double)2 / 3) * people.Count);

            var algorithm = new FakeAlgorithm(people, labels, new Settings {ScaleAndNormalize = false});

            AssertInput(algorithm.MyTrainingSetX, algorithm.MyTrainingSetY, people, labels, 0, trainingSetSize);
            AssertInput(algorithm.MyCrossValidationSetX, algorithm.MyCrossValidationSetY, people, labels, trainingSetSize, crossValidationSetSize);
            Assert.That(algorithm.MyTestSetX, Is.Null);
            Assert.That(algorithm.MyTestSetY, Is.Null);
        }
        public void ScaledInput(List <object> samples, List <double> labels)
        {
            var people = samples.ConvertAll(x => (Person)x);
            var age    = new { Mean = people.Select(x => x.Age).Average(), Deviation = Math.Sqrt(((double)1 / (people.Count - 1)) * people.Select(x => Math.Pow(x.Age - people.Select(xx => xx.Age).Average(), 2)).Sum()) };
            var height = new { Mean = people.Select(x => x.Height).Average(), Deviation = Math.Sqrt(((double)1 / (people.Count - 1)) * people.Select(x => Math.Pow(x.Height - people.Select(xx => xx.Height).Average(), 2)).Sum()) };

            var algorithm = new FakeAlgorithm(people, labels, new Settings {
                InputSplitRatio = InputSplitRatio.No
            });

            Assert.That(algorithm.MyTrainingSetX[0, 0], Is.EqualTo(1), "A column of ones should be added to the input matrix.");
            Assert.That(algorithm.MyTrainingSetX[0, 1], Is.EqualTo((people[0].Age - age.Mean) / age.Deviation));
            Assert.That(algorithm.MyTrainingSetX[0, 2], Is.EqualTo((people[0].Height - height.Mean) / height.Deviation));
            Assert.That(algorithm.MyTrainingSetY[0], Is.EqualTo(labels[0]));
        }
        public void DefaultInputSplit(List <object> samples, List <double> labels)
        {
            var people                 = samples.ConvertAll(x => (Person)x);
            var trainingSetSize        = (int)Math.Ceiling(((double)2 / 3) * people.Count);
            var crossValidationSetSize = (int)Math.Ceiling(((double)2 / 3) * people.Count);

            var algorithm = new FakeAlgorithm(people, labels, new Settings {
                ScaleAndNormalize = false
            });

            AssertInput(algorithm.MyTrainingSetX, algorithm.MyTrainingSetY, people, labels, 0, trainingSetSize);
            AssertInput(algorithm.MyCrossValidationSetX, algorithm.MyCrossValidationSetY, people, labels, trainingSetSize, crossValidationSetSize);
            Assert.That(algorithm.MyTestSetX, Is.Null);
            Assert.That(algorithm.MyTestSetY, Is.Null);
        }
예제 #6
0
        public void ClearRemovesUnreachedTarget()
        {
            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.Clear();
            Assert.AreEqual(collection.Count, 0);
        }
예제 #7
0
        public void OrderByMarginImpactDoesNotReturnTargetsWithNoData()
        {
            var algorithm = new FakeAlgorithm();
            var symbol    = new Symbol(SecurityIdentifier.GenerateEquity(_symbol, Market.USA), _symbol);

            algorithm.AddEquity(symbol);

            var collection = new PortfolioTargetCollection();
            var target     = new PortfolioTarget(symbol, -1);

            collection.Add(target);
            var targets = collection.OrderByMarginImpact(algorithm);

            Assert.AreEqual(collection.Count, 1);
            Assert.IsTrue(targets.IsNullOrEmpty());
        }
예제 #8
0
        public void OrderByMarginImpactDoesNotReturnTargetsForWhichUnorderdQuantityIsZeroBecauseTargetIsZero()
        {
            var algorithm = new FakeAlgorithm();

            algorithm.Transactions.SetOrderProcessor(new FakeOrderProcessor());
            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, 0);

            collection.Add(target);

            var targets = collection.OrderByMarginImpact(algorithm);

            Assert.AreEqual(collection.Count, 1);
            Assert.IsTrue(targets.IsNullOrEmpty());
        }
예제 #9
0
        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);
        }
예제 #10
0
        public void OrderByMarginImpactReturnsExpectedTargets()
        {
            var algorithm = new FakeAlgorithm();

            algorithm.Transactions.SetOrderProcessor(new FakeOrderProcessor());
            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 targets = collection.OrderByMarginImpact(algorithm);

            Assert.AreEqual(collection.Count, 1);
            Assert.AreEqual(targets.Count(), 1);
            Assert.AreEqual(targets.First(), target);
        }
예제 #11
0
        public void ScaledInput(List<object> samples, List<double> labels)
        {
            var people = samples.ConvertAll(x => (Person)x);
            var age = new { Mean = people.Select(x => x.Age).Average(), Deviation = Math.Sqrt( ((double)1/(people.Count - 1)) * people.Select(x => Math.Pow(x.Age - people.Select(xx => xx.Age).Average(), 2)).Sum() ) };
            var height = new { Mean = people.Select(x => x.Height).Average(), Deviation = Math.Sqrt(((double)1 / (people.Count - 1)) * people.Select(x => Math.Pow(x.Height - people.Select(xx => xx.Height).Average(), 2)).Sum()) };

            var algorithm = new FakeAlgorithm(people, labels, new Settings {InputSplitRatio = InputSplitRatio.No});

            Assert.That(algorithm.MyTrainingSetX[0, 0], Is.EqualTo(1), "A column of ones should be added to the input matrix.");
            Assert.That(algorithm.MyTrainingSetX[0, 1], Is.EqualTo(  (people[0].Age - age.Mean) / age.Deviation ));
            Assert.That(algorithm.MyTrainingSetX[0, 2], Is.EqualTo(  (people[0].Height - height.Mean) / height.Deviation ));
            Assert.That(algorithm.MyTrainingSetY[0], Is.EqualTo(labels[0]));
        }