示例#1
0
        private double CountWaveFunctions(double e)
        {
            InitSystems(e);

            _t   = new double[_nodesCount];
            _psi = new double[_nodesCount];
            _phi = new double[_nodesCount];

            var solver      = new Solver(_systemForward, -_l, _conditionValuesForward, _h, _nodesCount);
            var solutionPsi = solver.Solve();

            for (int i = 0; i < _nodesCount; i++)
            {
                _t[i]   = solutionPsi[i][0];
                _psi[i] = solutionPsi[i][2];
            }

            solver = new Solver(_systemBackward, _l, _conditionValuesBackward, -_h, _nodesCount);
            var solutionFi = solver.Solve();

            for (int i = 0; i < _nodesCount; i++)
            {
                _phi[i] = solutionFi[_nodesCount - 1 - i][2];
            }

            Scale(_psi);
            NormalizeMath();
            NormalizeQuant();

            _psiSpline = CubicSpline.InterpolateAkima(_t, _psi);
            _phiSpline = CubicSpline.InterpolateAkima(_t, _phi);

            return(_psiSpline.Differentiate(_t[_sewNode]) - _phiSpline.Differentiate(_t[_sewNode]));
        }
示例#2
0
        private void DrawGraphs()
        {
            chart1.Series.Clear();

            var psiSeries = new Series()
            {
                ChartType = SeriesChartType.Line, Color = Color.MediumVioletRed, BorderWidth = 3, LegendText = "ψ(x)"
            };
            var phiSeries = new Series()
            {
                ChartType = SeriesChartType.Line, Color = Color.LightSkyBlue, BorderWidth = 1, BorderDashStyle = ChartDashStyle.Dot, LegendText = "φ(x)"
            };
            var uSeries = new Series {
                ChartType = SeriesChartType.Line, Color = Color.LightSeaGreen, BorderWidth = 4, LegendText = "U(x)"
            };
            var uVerticalLeft = new Series {
                ChartType = SeriesChartType.Line, Color = Color.LightSeaGreen, IsVisibleInLegend = false
            };
            var uVerticalRight = new Series {
                ChartType = SeriesChartType.Line, Color = Color.LightSeaGreen, IsVisibleInLegend = false
            };

            var diffSeries = new Series {
                ChartType = SeriesChartType.Line, Color = Color.Pink
            };

            var nodesOfSewingSeries = new Series {
                ChartType = SeriesChartType.Point, Color = Color.Yellow, LegendText = "Узел сшивки"
            };

            nodesOfSewingSeries.Points.AddXY(_t[_sewNode], _psi[_sewNode]);

            for (var i = 0; i < _nodesCount; i++)
            {
                psiSeries.Points.AddXY(_t[i], _psi[i]);
                phiSeries.Points.AddXY(_t[i], _phi[i]);
                uSeries.Points.AddXY(_t[i], U(_t[i]));
                diffSeries.Points.AddXY(_t[i], _psiSpline.Differentiate(_t[i]) - _phiSpline.Differentiate(_t[i]));
            }

            uVerticalLeft.Points.AddXY(-5, U(-5));
            uVerticalLeft.Points.AddXY(-5, 1);

            uVerticalRight.Points.AddXY(5, 1);
            uVerticalRight.Points.AddXY(5, U(-5));

            chart1.Series.Add(psiSeries);
            chart1.Series.Add(phiSeries);
            chart1.Series.Add(nodesOfSewingSeries);
            chart1.Series.Add(uSeries);
            chart1.Series.Add(diffSeries);
            chart1.Series.Add(uVerticalLeft);
            chart1.Series.Add(uVerticalRight);
        }
示例#3
0
        public bool Derivative(CubicSpline spline, double t, out double derivative, out string errMsg)
        {
            bool success = true;

            errMsg     = "";
            derivative = 0.0;

            derivative = spline.Differentiate(t);

            return(success);
        }
示例#4
0
    static void Main(string[] args)
    {
        // Type code here.
        var         xvec = new DenseVector(new double[] { 0.0, 1.0, 2.0, 3.0, 4.0 });
        var         yvec = new DenseVector(new double[] { 3.0, 2.7, 2.2, 1.6, 0.2 });
        CubicSpline cs   = CubicSpline.InterpolateNatural(xvec, yvec);

        Console.WriteLine($"{"x",column_width} {"y",column_width} {"dy/dx",column_width}");
        for (int i = 0; i < xvec.Count; i++)
        {
            double dydx = cs.Differentiate(xvec[i]);
            Console.WriteLine($"{xvec[i],column_width:G5} {yvec[i],column_width:G5} {dydx,column_width:G5}");
        }
    }
示例#5
0
        public List <Tuple <DateTime, VolumeIndicator> > GetRangeOfVolumeIndicator(DateTime Start, DateTime End)
        {
            List <Tuple <DateTime, VolumeIndicator> > oVolumeIndicatorData = new List <Tuple <DateTime, VolumeIndicator> >();

            try
            {
                DateTime StartWithOffset = Start;
                if ((int)(End - Start).TotalDays < 20)
                {
                    StartWithOffset = Start.AddDays(-(20 - (int)(End - Start).TotalDays));
                }
                var HistData = GetDataForRange(StartWithOffset, End);
                if (HistData.Count() == 0)
                {
                    return(oVolumeIndicatorData);
                }

                double[] VolumeList   = HistData.Select(x => Convert.ToDouble(x.Volume)).ToArray();
                double   StdForVolume = VolumeList.StandardDeviation();
                double   StdForPrice  = HistData.Select(x => Convert.ToDouble(x.Close)).ToArray().StandardDeviation();

                CubicSpline oCSTotalData = CubicSpline.InterpolateNaturalSorted(
                    VolumeList.Select((s, i2) => new { i2, s })
                    .Select(t => Convert.ToDouble(t.i2)).ToArray(),
                    VolumeList);


                var ListOfDiff = VolumeList.Select((s, i2) => new { i2, s })
                                 .ToList().Select(f => oCSTotalData.Differentiate(Convert.ToDouble(f.i2))).ToArray();

                int i = 0;
                foreach (var hqd in HistData)
                {
                    VolumeIndicator oVi          = new VolumeIndicator();
                    bool            bHighVolume  = (ListOfDiff[i] > 0 && VolumeList[i] > StdForVolume);
                    bool            bLowVolume   = (ListOfDiff[i] <= 0 && VolumeList[i] < StdForVolume);
                    bool            bHighRange   = (Convert.ToDouble(hqd.Close - hqd.Open) > StdForPrice);
                    bool            bLowRange    = (-Convert.ToDouble(hqd.Open - hqd.Close) > StdForPrice);
                    bool            bUpBars      = (hqd.Close > hqd.Open);
                    bool            bNeutralBars = (hqd.Close.Equals(hqd.Open));

                    if (bHighVolume && bHighRange && bUpBars && !bNeutralBars)
                    {
                        oVi.VolumeIndicatorType = VolumeIndicatorType.VolumeClimaxUp;
                    }
                    else if (bHighVolume && bHighRange && !bUpBars && !bNeutralBars)
                    {
                        oVi.VolumeIndicatorType = VolumeIndicatorType.VolumeClimaxDown;
                    }
                    else if (bHighVolume && bHighRange && bNeutralBars)
                    {
                        oVi.VolumeIndicatorType = VolumeIndicatorType.VolumeClimaxPlusHighVolumeChurn;
                    }
                    else if (bHighVolume && !bHighRange)
                    {
                        oVi.VolumeIndicatorType = VolumeIndicatorType.HighVolumeChurn;
                    }
                    else if (!bHighVolume && bLowVolume)
                    {
                        oVi.VolumeIndicatorType = VolumeIndicatorType.LowVolume;
                    }
                    else
                    {
                        oVi.VolumeIndicatorType = VolumeIndicatorType.Unknown;
                    }
                    oVi.Strength = 100;
                    oVolumeIndicatorData.Add(new Tuple <DateTime, VolumeIndicator>(hqd.Date, oVi));

                    i++;
                }
            }catch (Exception ex)
            {
                ImperaturGlobal.GetLog().Error(string.Format("Error in GetRangeOfVolumeIndicator"), ex);
            }
            return(oVolumeIndicatorData);
        }
示例#6
0
        private List <Wave> GetListOfWavesFromRange(DateTime StartDate, DateTime EndDate)
        {
            List <double> oPriceData = GetRangeOfDataAsDoubleIncludingLowHigh(StartDate, EndDate);

            if (oPriceData.Count < 5)
            {
                return(new List <Wave>());
            }

            CubicSpline oCSTotalData = CubicSpline.InterpolateAkimaSorted(

                oPriceData.Select((s, i2) => new { i2, s })
                .Select(t => Convert.ToDouble(t.i2)).ToArray(),

                oPriceData.Select(s => Convert.ToDouble(s)).ToArray());


            //create list of Waves
            List <Wave> oWaves = new List <Wave>();

            var FirstVarDiff = oPriceData.Select((s, i2) => new { i2, s })
                               .ToList().Select(f => oCSTotalData.Differentiate(Convert.ToDouble(f.i2))).ToArray();

            int           i                   = 0;
            Momentum      Current             = Momentum.Neutral;
            Momentum      Old                 = Momentum.Neutral;
            List <double> oCalcDoublesForWave = new List <double>();
            bool          bFirst              = true;

            foreach (var fvd in FirstVarDiff)
            {
                if (fvd < 0)
                {
                    Current = Momentum.Negative;
                }
                else if (fvd == 0)
                {
                    Current = Momentum.Neutral;
                }
                else
                {
                    Current = Momentum.Positive;
                }

                if (bFirst)
                {
                    oCalcDoublesForWave.Add(oCSTotalData.Interpolate(i));
                    bFirst = false;
                }
                else
                {
                    if (Current != Old && oCalcDoublesForWave.Count > 1)
                    {
                        oWaves.Add(new Wave
                        {
                            End         = oCalcDoublesForWave.Last(),
                            Start       = oCalcDoublesForWave.First(),
                            Length      = oCalcDoublesForWave.Count,
                            Momentum    = Old,
                            SourceIndex = i
                        });
                        oCalcDoublesForWave.Clear();
                    }
                    else if (Current != Old && oCalcDoublesForWave.Count <= 1)
                    {
                        oCalcDoublesForWave.Clear();
                    }
                    oCalcDoublesForWave.Add(oCSTotalData.Interpolate(i));
                }
                Old = Current;

                i++;
            }
            return(oWaves);
        }
示例#7
0
        private TradingRecommendation GetTradingRecommendationForCrossOver()
        {
            int[] Intervals = { 50, 200 };
            TradingRecommendation Recommendation      = new TradingRecommendation();
            List <double[]>       MovingAverageObject = new List <double[]>();


            List <double> d1 = Statistics.MovingAverage(
                GetBusinessDayDataForRange(DateTime.Now, Intervals[0]).
                Select(s => Convert.ToDouble(s.Close)).ToArray(), Intervals[0]).ToList();
            List <double> d2 = Statistics.MovingAverage(
                GetBusinessDayDataForRange(DateTime.Now, Intervals[1]).
                Select(s => Convert.ToDouble(s.Close)).ToArray(), Intervals[1]).ToList();

            double[] xd = d1.ToArray();
            double[] yd = d2.Skip(d2.Count() - d1.Count()).ToArray();

            CubicSpline Curvedata = CubicSpline.InterpolateAkima(
                d1.Select((s, i2) => new { i2, s })
                .Select(t => Convert.ToDouble(t.i2)).ToArray(),
                d1);


            while (d1.Count() != d2.Count())
            {
                if (d1.Count() > d2.Count())
                {
                    d2.Insert(0, 0);
                }
                else
                {
                    d1.Insert(0, 0);
                }
            }

            double[] intersects0 = Polyfit(xd, yd, 0);


            List <Tuple <int, double> > Intersects = new List <Tuple <int, double> >();
            int i = 0;

            foreach (double di in yd)
            {
                if (di * 0.999 <= intersects0[0] && di * 1.001 >= intersects0[0])
                {
                    Intersects.Add(new Tuple <int, double>(i, di));
                }
                i++;
            }


            /*
             *
             *
             * CubicSpline Curvedata = CubicSpline.InterpolateAkima(
             * d1.Select((s, i2) => new { i2, s })
             * .Select(t => Convert.ToDouble(t.i2)).ToArray(),
             * d1);
             *
             *
             * while (d1.Count() != d2.Count())
             * {
             *  if (d1.Count() > d2.Count())
             *  {
             *      d2.Insert(0, double.NaN);
             *  }
             *  else
             *  {
             *      d1.Insert(0, double.NaN);
             *  }
             * }
             * List<Tuple<int, double>> Intersects = new List<Tuple<int, double>>();
             * int i = 0;
             * foreach (double di in d1)
             * {
             *  //need to add a little bit of gliding here as well...
             *  if (!double.IsNaN(di) && !double.IsNaN(d2[i]) && (di <= d2[i]*0.92 && di >= d2[i] * 1.07))
             *  {
             *      Intersects.Add(new Tuple<int, double>(i, di));
             *  }
             *  i++;
             * }*/
            try
            {
                int Sellmax = (Intersects.Where(x => Curvedata.Differentiate(x.Item2) < 0).Count() > 0) ? Intersects.Where(x => Curvedata.Differentiate(x.Item2) < 0).Last().Item1 : 0;
                int Buymax  = (Intersects.Where(x => Curvedata.Differentiate(x.Item2) > 0).Count() > 0) ? Intersects.Where(x => Curvedata.Differentiate(x.Item2) > 0).Last().Item1 : 0;

                if (Sellmax > Buymax && Sellmax > 0)
                {
                    return(new TradingRecommendation(
                               Instrument,
                               ImperaturGlobal.GetMoney(0, Instrument.CurrencyCode),
                               ImperaturGlobal.GetMoney(Convert.ToDecimal(Intersects.Where(x => x.Item1.Equals(Sellmax)).Last().Item2), Instrument.CurrencyCode),
                               DateTime.Now,
                               DateTime.Now,
                               TradingForecastMethod.Crossover
                               ));
                }
                if (Buymax > Sellmax && Buymax > 0)
                {
                    return(new TradingRecommendation(
                               Instrument,
                               ImperaturGlobal.GetMoney(Convert.ToDecimal(Intersects.Where(x => x.Item1.Equals(Buymax)).Last().Item2), Instrument.CurrencyCode),
                               ImperaturGlobal.GetMoney(0, Instrument.CurrencyCode),
                               DateTime.Now,
                               DateTime.Now,
                               TradingForecastMethod.Crossover
                               ));
                }
            }
            catch (Exception ex)
            {
                int gg = 0;
            }

            return(Recommendation);
        }