示例#1
0
        private void applyBsPotterButton_Click(object sender, EventArgs e)
        {
            var flag = true;

            flag &= double.TryParse(bs_fc1.Text.Replace('.', ','), out var fc);
            flag &= double.TryParse(bs_fc_2.Text.Replace('.', ','), out var fc2);
            flag &= int.TryParse(bs_m.Text, out var m);
            flag &= double.TryParse(dtTextBox.Text.Replace('.', ','), out var dt);
            if (!flag)
            {
                MessageBox.Show("Не все данные в правильном формате!", "Ошибка", MessageBoxButtons.OK);
                return;
            }

            var curve = _controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList[0];
            var x     = new PointPairList();

            for (var i = 0; i < curve.Points.Count; i++)
            {
                x.Add(curve.Points[i]);
            }

            var h = Analyser.GetHarryPotter_BS(fc, fc2, m, dt);
            var y = Analyser.GetConvolution(x, h);

            Plotter.Clear(_controlZgcList[(int)numericUpDownGraphNo.Value - 1]);
            Plotter.Draw(_controlZgcList[(int)numericUpDownGraphNo.Value - 1], y, "Pottered");
        }
示例#2
0
        private void PerfectFilterButton_Click(object sender, EventArgs e)
        {
            var flag = true;

            flag &= double.TryParse(FilterstextBox.Text.Replace('.', ','), out var alpha);

            if (!flag)
            {
                MessageBox.Show("Не все данные в правильном формате!", "Ошибка", MessageBoxButtons.OK);
                return;
            }

            var ppl   = Analyser.GetH();
            var curve = _controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList[0];
            var x     = new PointPairList();

            for (var i = 0; i < curve.Points.Count; i++)
            {
                x.Add(curve.Points[i]);
            }
            Plotter.Clear(_controlZgcList[(int)numericUpDownGraphNo.Value - 1]);
            if (alpha == 0)
            {
                var result = Analyser.PerfectFilter(x, ppl);
                Plotter.Draw(_controlZgcList[(int)numericUpDownGraphNo.Value - 1], result, "Исходная функция");
            }
            else
            {
                var result = Analyser.PerfectFilterNoise(x, ppl, alpha);
                Plotter.Draw(_controlZgcList[(int)numericUpDownGraphNo.Value - 1], result, "Исходная функция");
            }
        }
示例#3
0
        private void pltRxxButton_Click(object sender, EventArgs e)
        {
            if (_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList.Count == 0)
            {
                return;
            }

            var list = GraphConverter.GetPointPairListFromCurve(_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList[0]);
            var res  = Analyser.GetRxx(list);

            Plotter.Clear(rxxZedGraphControl);
            Plotter.Draw(rxxZedGraphControl, res, "Rxx");
        }
示例#4
0
        public void Files_SelectionChanged(SelectionChangedEventArgs args)
        {
            Transitions.Clear();
            Plotter.Clear();
            ImportedFromDatabase.Clear();

            SelectedFile = args.AddedItems.Cast <object>().Where(x => x is ListBoxFileItem).Count() == 1
                ? args.AddedItems.Cast <ListBoxFileItem>().First()
                : null;

            NotifyOfPropertyChange(() => CanDetectPeaks);
            NotifyOfPropertyChange(() => CanAddToDatabase);
        }
示例#5
0
        private void plotDistributionButton_Click(object sender, EventArgs e)
        {
            if (_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList.Count == 0)
            {
                return;
            }

            var list = GraphConverter.GetPointPairListFromCurve(_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList[0]);
            var res  = Analyser.GetDistribution(list);

            Plotter.Clear(distributionZedGraphControl);
            Plotter.Draw(distributionZedGraphControl, res, "Распределение");
        }
示例#6
0
        private void spectrePlotButton_Click(object sender, EventArgs e)
        {
            if (_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList.Count == 0)
            {
                return;
            }

            var list = GraphConverter.GetPointPairListFromCurve(_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList[0]);
            var res  = Analyser.GetSpectre(list, norm_checked.Checked);

            res.RemoveRange(list.Count / 2, list.Count - list.Count / 2);
            Plotter.Clear(spectreZedGraphControl);
            Plotter.Draw(spectreZedGraphControl, res, "Spectre");
        }
示例#7
0
        public void DetectPeaks()
        {
            Transitions.Clear();
            Plotter.Clear();

            string contents = ReadFromFile(SelectedFile.Path);

            if (!string.IsNullOrEmpty(contents))
            {
                ProcessFile(contents);
            }

            NotifyOfPropertyChange(() => CanAddToDatabase);
            NotifyOfPropertyChange(() => CanSaveImage);
            NotifyOfPropertyChange(() => CanSaveSpectrum);
        }
示例#8
0
        private void Fur1DButton_Click(object sender, EventArgs e)
        {
            if (_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList.Count == 0)
            {
                return;
            }

            var list = GraphConverter.GetPointPairListFromCurve(_controlZgcList[(int)numericUpDownGraphNo.Value - 1].GraphPane.CurveList[0]);
            var res  = Analyser.ListToComplex(list);

            res = Analyser.ReverseSpectre(Analyser.GetSpectre(res));

            var newlist = Analyser.ComplexToPointPairList(res);

            Plotter.Clear(spectreZedGraphControl);
            Plotter.Draw(spectreZedGraphControl, newlist, "Reversed Graph");
        }
        public override void Run()
        {
            //----- initialization

            StartTime = DateTime.Parse("01/01/1990", CultureInfo.InvariantCulture);
            EndTime   = DateTime.Now.Date - TimeSpan.FromDays(5);

            AddDataSource(BENCHMARK);
            foreach (AssetClass assetClass in _assetClasses)
            {
                foreach (string nick in assetClass.assets)
                {
                    AddDataSource(nick);
                }
            }

            Deposit(INITIAL_FUNDS);

            _plotter.Clear();

            //----- simulation loop

            foreach (DateTime simTime in SimTimes)
            {
                // evaluate instrument momentum for all known instruments,
                // we need to make sure to evaluate every instrument only once!
                Dictionary <Instrument, double> instrumentMomentum = Instruments
                                                                     .ToDictionary(i => i,
                                                                                   i => ScoringFunc(i));

                // skip if there are any missing instruments,
                // we want to make sure our strategy has all instruemnts available
                bool instrumentsMissing = _assetClasses
                                          .SelectMany(c => c.assets)
                                          .Distinct()
                                          .Where(n => Instruments.Where(i => i.Nickname == n).Count() == 0)
                                          .Count() > 0;

                if (instrumentsMissing)
                {
                    continue;
                }

                _benchmark = _benchmark ?? FindInstrument(BENCHMARK);

                // create empty structure for instrument weights
                Dictionary <Instrument, double> instrumentWeights = Instruments
                                                                    .ToDictionary(i => i, i => 0.0);

                // loop through all asset classes
                foreach (AssetClass assetClass in _assetClasses)
                {
                    List <Instrument> assetClassInstruments = assetClass.assets
                                                              .Select(n => FindInstrument(n))
                                                              .ToList();

                    var bestInstruments = assetClassInstruments
                                          .OrderByDescending(i => instrumentMomentum[i])
                                          .Take(assetClass.numpicks);

                    foreach (Instrument bestInstrument in bestInstruments)
                    {
                        instrumentWeights[bestInstrument] += assetClass.weight / assetClass.numpicks;
                    }
                }

                // execute trades once per month
                if (SimTime[0].Month != SimTime[1].Month)
                {
                    double totalWeight = _assetClasses
                                         .Sum(a => a.weight);
                    double equityUnit = NetAssetValue[0] / totalWeight;

                    string message = string.Format("{0:MM/dd/yyyy}: ", SimTime[0]);
                    foreach (var instrumentWeight in instrumentWeights)
                    {
                        message += string.Format("{0} = {1:P2}, ", instrumentWeight.Key.Symbol, instrumentWeight.Value);
                        //message += string.Format("{0:P2}, ", instrumentMomentum[instrumentWeight.Key]);
                        int targetShares  = (int)Math.Floor(instrumentWeight.Value * equityUnit / instrumentWeight.Key.Close[0]);
                        int currentShares = instrumentWeight.Key.Position;

                        Order newOrder = instrumentWeight.Key.Trade(targetShares - currentShares);

                        if (newOrder != null)
                        {
                            if (currentShares == 0)
                            {
                                newOrder.Comment = "open";
                            }
                            else if (targetShares == 0)
                            {
                                newOrder.Comment = "close";
                            }
                            else
                            {
                                newOrder.Comment = "rebalance";
                            }
                        }
                    }

                    if (TradingDays > 0)
                    {
                        Output.WriteLine(message);
                    }
                }

                // create plots on Sheet 1
                if (TradingDays > 0)
                {
                    _plotter.SelectChart(Name, "date");
                    _plotter.SetX(SimTime[0]);
                    _plotter.Plot("NAV", NetAssetValue[0]);
                    _plotter.Plot(_benchmark.Symbol, _benchmark.Close[0]);

                    // holdings on Sheet 2
                    _plotter.SelectChart(Name + " holdings", "date");
                    _plotter.SetX(SimTime[0]);
                    foreach (var i in Positions.Keys)
                    {
                        _plotter.Plot(i.Symbol, i.Position * i.Close[0] / NetAssetValue[0]);
                    }
                }
            }

            //----- post processing

            // create trading log on Sheet 3
            _plotter.SelectChart(Name + " trades", "date");
            foreach (LogEntry entry in Log)
            {
                _plotter.SetX(entry.BarOfExecution.Time);
                _plotter.Plot("action", entry.Action);
                _plotter.Plot("type", entry.InstrumentType);
                _plotter.Plot("instr", entry.Symbol);
                _plotter.Plot("qty", entry.OrderTicket.Quantity);
                _plotter.Plot("fill", entry.FillPrice);
                _plotter.Plot("gross", -entry.OrderTicket.Quantity * entry.FillPrice);
                _plotter.Plot("commission", -entry.Commission);
                _plotter.Plot("net", -entry.OrderTicket.Quantity * entry.FillPrice - entry.Commission);
                _plotter.Plot("comment", entry.OrderTicket.Comment ?? "");
            }
        }
示例#10
0
 private void clearButton_Click(object sender, EventArgs e)
 {
     Plotter.Clear(_controlZgcList[(int)numericUpDownGraphNo.Value - 1]);
 }
示例#11
0
        protected int B = 0; // breadth parameter
        #endregion

        #region public override void Run()
        public override void Run()
        {
            //----- initialization

            StartTime = DateTime.Parse("01/01/1990");
            EndTime   = DateTime.Now.Date - TimeSpan.FromDays(5);

            foreach (string nick in riskyUniverse.Concat(cashUniverse).Concat(protectiveUniverse))
            {
                AddDataSource(nick);
            }
            AddDataSource(BENCHMARK);

            Deposit(INITIAL_FUNDS);
            //CommissionPerShare = 0.015; // paper does not consider trade commissions

            _plotter.Clear();

            //----- simulation loop

            foreach (DateTime simTime in SimTimes)
            {
                // calculate 13612W momentum for all instruments
                Dictionary <Instrument, double> momentum13612W = Instruments
                                                                 .ToDictionary(
                    i => i,
                    i => 0.25 *
                    (12.0 * (i.Close[0] / i.Close[21] - 1.0)
                     + 4.0 * (i.Close[0] / i.Close[63] - 1.0)
                     + 2.0 * (i.Close[0] / i.Close[126] - 1.0)
                     + 1.0 * (i.Close[0] / i.Close[252] - 1.0)));

                // skip if there are any missing instruments
                // we want to make sure our strategy has all instruments available
                bool instrumentsMissing = riskyUniverse.Concat(cashUniverse).Concat(protectiveUniverse)
                                          .Where(n => Instruments.Where(i => i.Nickname == n).Count() == 0)
                                          .Count() > 0;

                if (instrumentsMissing)
                {
                    continue;
                }

                _benchmark = _benchmark ?? FindInstrument(BENCHMARK);

                // find T top risky assets
                IEnumerable <Instrument> topInstruments = Instruments
                                                          .Where(i => riskyUniverse.Contains(i.Nickname))
                                                          .OrderByDescending(i => momentum13612W[i])
                                                          .Take(T);

                // find single cash/ bond asset
                Instrument cashInstrument = Instruments
                                            .Where(i => cashUniverse.Contains(i.Nickname))
                                            .OrderByDescending(i => momentum13612W[i])
                                            .First();

                // determine number of bad assets in canary universe
                double b = Instruments
                           .Where(i => protectiveUniverse.Contains(i.Nickname))
                           .Sum(i => momentum13612W[i] < 0.0 ? 1.0 : 0.0);

                // calculate cash fraction
                //double CF = Math.Min(1.0, b / B) // standard calculation
                double CF = Math.Min(1.0, 1.0 / T * Math.Floor(b * T / B)); // Easy Trading

                // set instrument weights
                Dictionary <Instrument, double> weights = Instruments
                                                          .ToDictionary(i => i, i => 0.0);

                weights[cashInstrument] = CF;

                foreach (Instrument i in topInstruments)
                {
                    weights[i] += (1.0 - CF) / T;
                }

                // rebalance once per month
                if (SimTime[0].Month != SimTime[1].Month)
                {
                    foreach (Instrument i in Instruments)
                    {
                        int targetShares = (int)Math.Floor(weights[i] * NetAssetValue[0] / i.Close[0]);

                        Order newOrder = i.Trade(targetShares - i.Position);

                        if (newOrder != null)
                        {
                            if (i.Position == 0)
                            {
                                newOrder.Comment = "open";
                            }
                            else if (targetShares == 0)
                            {
                                newOrder.Comment = "close";
                            }
                            else
                            {
                                newOrder.Comment = "rebalance";
                            }
                        }
                    }
                }

                if (TradingDays > 0)
                {
                    // create plots on Sheet 1
                    _plotter.SelectChart(Name, "date");
                    _plotter.SetX(SimTime[0]);
                    _plotter.Plot("NAV", NetAssetValue[0]);
                    _plotter.Plot(_benchmark.Symbol, _benchmark.Close[0]);

                    // holdings on Sheet 2
                    _plotter.SelectChart(Name + " holdings", "date");
                    _plotter.SetX(SimTime[0]);
                    foreach (var i in Positions.Keys)
                    {
                        _plotter.Plot(i.Symbol, i.Position * i.Close[0] / NetAssetValue[0]);
                    }
                }
            }

            //----- post processing

            // create trading log on Sheet 3
            _plotter.SelectChart(Name + " trades", "date");
            foreach (LogEntry entry in Log)
            {
                _plotter.SetX(entry.BarOfExecution.Time);
                _plotter.Plot("action", entry.Action);
                _plotter.Plot("type", entry.InstrumentType);
                _plotter.Plot("instr", entry.Symbol);
                _plotter.Plot("qty", entry.OrderTicket.Quantity);
                _plotter.Plot("fill", entry.FillPrice);
                _plotter.Plot("gross", -entry.OrderTicket.Quantity * entry.FillPrice);
                _plotter.Plot("commission", -entry.Commission);
                _plotter.Plot("net", -entry.OrderTicket.Quantity * entry.FillPrice - entry.Commission);
                _plotter.Plot("comment", entry.OrderTicket.Comment ?? "");
            }

            // calculate Keller ratio
            double R = Math.Exp(
                252.0 / TradingDays * Math.Log(NetAssetValue[0] / INITIAL_FUNDS));
            double K50 = NetAssetValueMaxDrawdown <0.5 && R> 0.0
                ? R * (1.0 - NetAssetValueMaxDrawdown / (1.0 - NetAssetValueMaxDrawdown))
                : 0.0;
            double K25 = NetAssetValueMaxDrawdown <0.25 && R> 0.0
                ? R * (1.0 - 2.0 * NetAssetValueMaxDrawdown / (1.0 - 2 * NetAssetValueMaxDrawdown))
                : 0.0;

            FitnessValue = K25;
        }