Пример #1
0
        public void Accounting1()
        {
            var a = new Account {
                Equity = 10000, MarginFactor = 1
            };
            var com = 5;

            a.Commission = size => com;
            var bars = new DataSeries <Bar>("ABCD", Lists.Create(
                                                new Bar(DateTime.Parse("12/10/2010"), 21.63, 21.50, 23.01, 22.90, 10000),                                                                         // long profit
                                                new Bar(DateTime.Parse("12/11/2010"), 23.50, 22.00, 24.01, 24.00, 10000),                                                                         // long stop loss, green bar
                                                new Bar(DateTime.Parse("12/12/2010"), 25.00, 24.00, 25.01, 24.01, 10000),                                                                         // long loss, red bar
                                                new Bar(DateTime.Parse("12/13/2010"), 23.90, 23.10, 23.95, 23.15, 10000),                                                                         // short profit
                                                new Bar(DateTime.Parse("12/14/2010"), 23.10, 22.50, 23.50, 22.60, 10000),                                                                         // short stop loss, red bar
                                                new Bar(DateTime.Parse("12/15/2010"), 22.39, 22.20, 24.00, 23.50, 10000)));                                                                       // short loss, green bar
            var actions = new Queue <Action <DataSeries <Bar> > >(Lists.Create <Action <DataSeries <Bar> > >(
                                                                      s => a.EnterLong("ABCD", (int)Math.Floor((a.BuyingPower - com) / s[0].Open), new ExitOnSessionClose(21.40), s.FromHere()),  // new Equity: 10576.74
                                                                      s => a.EnterLong("ABCD", (int)Math.Floor((a.BuyingPower - com) / s[0].Open), new ExitOnSessionClose(23.00), s.FromHere()),  // new Equity: 10342.24
                                                                      s => a.EnterLong("ABCD", (int)Math.Floor((a.BuyingPower - com) / s[0].Open), new ExitOnSessionClose(23.90), s.FromHere()),  // new Equity: 9923.37
                                                                      s => a.EnterShort("ABCD", (int)Math.Floor((a.BuyingPower - com) / s[0].Open), new ExitOnSessionClose(24.00), s.FromHere()), // new Equity: 10223.87
                                                                      s => a.EnterShort("ABCD", (int)Math.Floor((a.BuyingPower - com) / s[0].Open), new ExitOnSessionClose(23.25), s.FromHere()), // new Equity: 10147.57
                                                                      s => a.EnterShort("ABCD", (int)Math.Floor((a.BuyingPower - com) / s[0].Open), new ExitOnSessionClose(24.10), s.FromHere())  // new Equity: 9635.85
                                                                      ));

            var expectedEquity = Lists.Create <double>(10576.74, 10342.24, 9923.37, 10223.87, 10147.57, 9635.85);
            var actualEquity   = new List <double>();

            DataSeries.Walk(bars, pos => {
                actions.Dequeue()(bars);
                actualEquity.Add(a.Equity);
            });

            Assert.IsTrue(Lists.Equal(expectedEquity, actualEquity.Select(x => Math.Round(x, 2))));
        }
Пример #2
0
        public void Accounting2()
        {
            var a = new Account {
                Equity = 10000, MarginFactor = 4
            };

            Assert.IsTrue(a.BuyingPower == 40000);
            var com = 5;

            a.Commission = size => com;
            var bars = new DataSeries <Bar>("ABCD", Lists.Create(
                                                new Bar(DateTime.Parse("12/10/2010"), 21.63, 21.50, 23.01, 22.90, 10000),                                                                            // long profit
                                                new Bar(DateTime.Parse("12/13/2010"), 23.90, 23.10, 23.95, 23.15, 10000)));                                                                          // short profit
            var actions = new Queue <Action <DataSeries <Bar> > >(Lists.Create <Action <DataSeries <Bar> > >(
                                                                      s => a.EnterLong("ABCD", (int)Math.Floor((a.BuyingPower - com * 4) / s[0].Open), new ExitOnSessionClose(21.00), s.FromHere()), // new Equity: 12336.96
                                                                      s => a.EnterShort("ABCD", (int)Math.Floor((a.BuyingPower - com * 4) / s[0].Open), new ExitOnSessionClose(24.00), s.FromHere()) // new Equity: 13874.21
                                                                      ));

            var expectedEquity = Lists.Create <double>(12336.96, 13874.21);
            var actualEquity   = new List <double>();

            DataSeries.Walk(bars, pos => {
                actions.Dequeue()(bars);
                actualEquity.Add(a.Equity);
            });

            Assert.IsTrue(Lists.Equal(expectedEquity, actualEquity.Select(x => Math.Round(x, 2))));
        }
Пример #3
0
        public void SvdTest()
        {
            var points = Lists.Create <Vector>(
                new DenseVector(new double[] { 3.1, 2.9 }),
                new DenseVector(new double[] { 1.0, 0.99 }),
                new DenseVector(new double[] { 2.2, 2.1 }),
                new DenseVector(new double[] { 3.8, 4.1 }),
                new DenseVector(new double[] { 4.4, 4.5 }),
                new DenseVector(new double[] { 6.1, 6.0 }));
            var X = new DenseMatrix(points.Count, 2);

            for (int i = 0; i < points.Count; i++)
            {
                X.SetRow(i, points[i]);
            }
            var svd = X.Svd(true);
            var V   = svd.VT().Transpose();

            Trace.WriteLine(V);

            var a  = points[3];
            var p1 = V.Column(0);
            var b  = a.DotProduct(p1) * p1;
            var p2 = V.Column(1);
            var c  = a.DotProduct(p2) * p2;
        }
Пример #4
0
        public void Mesh1()
        {
            var a = new ListHolder <double> {
                List = Lists.Create(1.0, 2, 4, 5, 6, 7, 8, 9)
            };
            var b = new ListHolder <double> {
                List = Lists.Create(1.1, 2.1, 3.1)
            };
            var c = new ListHolder <double> {
                List = Lists.Create(3.2, 4.2, 5.2, 6.2)
            };
            var d = new ListHolder <double> {
                List = Lists.Create(4.3, 5.3)
            };
            var e = new ListHolder <double> {
                List = Lists.Create(8.4, 9.4)
            };
            //var a = Lists.Create(1.0, 2, 3, 4, 5, 6, 7, 8, 9);
            //var b = Lists.Create(1.1, 2.1, 3.1);
            //var c = Lists.Create(3.2, 4.2, 5.2, 6.2);
            //var d = Lists.Create(4.3, 5.3);
            //var e = Lists.Create(8.4, 9.4);

            var m  = Lists.Mesh(Lists.Create(a, b, c, d, e), x => x.List, n => (int)n, i => i + 1, (dummy, xs) => new List <double>(xs)).ToList();
            var m2 = Lists.Mesh(Lists.Create(b, e), x => x.List, n => (int)n, i => i + 1, (dummy, xs) => new List <double>(xs)).ToList();
        }
Пример #5
0
        void SetTrade(TradeRecord t, Bar bar)
        {
            Chart.ClearGraphs();
            var g = Chart.AddGraph();

            g.Plots.Add(new Plot {
                DataSeries = new DataSeries <Bar>(t.Symbol, Lists.Create(bar)),
                Type       = PlotType.Candlestick
            });
        }
Пример #6
0
        public void IdealSignalNextClose()
        {
            var bars = new DataSeries <Bar>("", new[] {
                new Bar(5, 0, 0, 10, 0),
                new Bar(6, 0, 0, 11, 0),
                new Bar(5, 0, 0, 12, 0),
                new Bar(4, 0, 0, 11, 0),
                new Bar(3, 0, 0, 10, 0),
                new Bar(1, 0, 0, 12, 0),
                new Bar(10, 0, 0, 12, 0)
            });

            var signal = bars.MapElements <Value>((s, _) => Signals.NextClose(s)).Select(x => x.Val).ToList();

            signal.ShouldEnumerateLike(Lists.Create <double>(0, 1, 1, -1, -1, 1, 1));
        }
Пример #7
0
        public void GenericCrossOver()
        {
            var a = new[] { 1, 2, 3, 4, 5 };
            var b = new[] { 10, 20, 30, 40, 50 };

            var z = Functions.CrossOver(a, b, (x, y) => Tuple2.Create(y, x), x => x.ToList());

            z.Item1.ShouldBeOfType <List <int> >();
            z.Item2.ShouldBeOfType <List <int> >();
            z.Item1.ShouldEnumerateLike(b);
            z.Item2.ShouldEnumerateLike(a);

            var q = Functions.CrossOver(a, b, (x, y) => x % 2 == 1 ? Tuple2.Create(x, y) : Tuple2.Create(y, x), x => x.ToList());

            q.Item1.ShouldEnumerateLike(Lists.Create(1, 20, 3, 40, 5));
            q.Item2.ShouldEnumerateLike(Lists.Create(10, 2, 30, 4, 50));
        }
Пример #8
0
        void Update()
        {
            var selected = StrategiesBox.SelectedItem;

            StrategiesBox.Items.Clear();
            if (!Directory.Exists(StrategyOptimizerReport.StrategyDir))
            {
                return;
            }
            foreach (var fn in Directory.EnumerateFiles(StrategyOptimizerReport.StrategyDir)
                     .OrderByDescending(x => new FileInfo(Path.Combine(StrategyOptimizerReport.StrategyDir, x)).LastWriteTime))
            {
                StrategiesBox.Items.Add(Path.GetFileNameWithoutExtension(fn));
            }
            StrategiesBox.SelectedItem = StrategiesBox.Items[0];

            Action <ComboBox> add = cb => {
                if (!cb.Items.Contains(cb.Text))
                {
                    cb.Items.Add(cb.Text);
                }
            };

            add(TrainingStartBox);
            add(TrainingEndBox);
            add(ValidationStartBox);
            add(ValidationEndBox);

            Action <string, ComboBox> serialize = (n, cb) => {
                if (cb.Text == "")
                {
                    return;
                }
                Settings.Default[n] = Lists.Create(cb.Text).Concat(cb.Items.Cast <string>().Except(Lists.Create("", cb.Text))).Distinct().Join(",");
            };

            serialize("RecentTrainingStartDates", TrainingStartBox);
            serialize("RecentTrainingEndDates", TrainingEndBox);
            serialize("RecentValidationStartDates", ValidationStartBox);
            serialize("RecentValidationEndDates", ValidationEndBox);
            Settings.Default.Save();
        }
Пример #9
0
        public void Drawdown()
        {
            var dd = BacktestHelper.CalcMaxDrawdownPercent(new DataSeries <Value>("Foo", Lists.Create(1.0, 2, 3, 4, 7, 5, 4, 5, 2, 10, 12, 11, 15).Select(x => new Value(DateTime.MinValue, x))));

            Assert.IsTrue(dd == (7 - 2) / 7.0);
        }