Exemplo n.º 1
0
 public EditContractsViewModel() : base()
 {
     selectedEmployee       = new Employee();
     selectedTypeContract   = new TypeContract();
     selectedCurrency       = new Currency();
     selectedDepartment     = new Department();
     selectedPosition       = new Position();
     selectedTypeRate       = new TypeRate();
     selectedTerminationWay = new TerminationWay();
 }
 public void Dispose()
 {
     date         = null;
     rate         = null;
     sma          = null;
     ema          = null;
     currencyList = null;
     count        = 0;
     parametrEMA  = 0;
     typeRate     = null;
 }
        public void Calculate(List <CurrencyRate> _currencyList, int _count, double _parametrEMA, TypeRate _typeRate)
        {
            currencyList = _currencyList;
            count        = _count;
            parametrEMA  = _parametrEMA;
            typeRate     = _typeRate;

            for (int i = 0; i < count; i++)
            {
                date.Add(currencyList[i].Date);
            }

            List <double> currencyRate = new List <double>();
            double        sum          = 0;

            switch (typeRate.Name)
            {
            case "Close":
                currencyRate.AddRange(currencyList.Select(item => item.Close));

                for (int i = 0; i < count; i++)
                {
                    sum += currencyRate[i];
                    sma.Add(sum / (i + 1));
                }
                ema.Add(sma[0]);
                for (int i = 1; i < count; i++)
                {
                    //                        EMA = (CLOSE(i) * P) + (EMA(i - 1) * (1 - P))
                    //                        где:
                    //                        CLOSE(i) — цена закрытия текущего периода;
                    //                        EMA(i - 1) — значение скользящего среднего предыдущего периода;
                    //                        P — доля использования значения цен.
                    ema.Add(currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA)));
                }
                break;

            case "Open":
                currencyRate.AddRange(currencyList.Select(item => item.Open));
                //double sum = 0;
                for (int i = 0; i < count; i++)
                {
                    sum += currencyRate[i];
                    sma.Add(sum / (i + 1));
                }
                ema.Add(sma[0]);
                for (int i = 1; i < count; i++)
                {
                    ema.Add(currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA)));
                }
                break;

            case "High":
                currencyRate.AddRange(currencyList.Select(item => item.High));
                for (int i = 0; i < count; i++)
                {
                    sum += currencyRate[i];
                    sma.Add(sum / (i + 1));
                }
                ema.Add(sma[0]);
                for (int i = 1; i < count; i++)
                {
                    ema.Add(currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA)));
                }
                break;

            default:
                currencyRate.AddRange(currencyList.Select(item => item.Low));
                for (int i = 0; i < count; i++)
                {
                    sum += currencyRate[i];
                    sma.Add(sum / (i + 1));
                }
                ema.Add(sma[0]);
                for (int i = 1; i < count; i++)
                {
                    ema.Add(currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA)));
                }
                break;
            }

            for (int i = 0; i < count; i++)
            {
                rate.Add(currencyRate[i]);
            }
        }
        private void BuildChartButton_Click(object sender, EventArgs e)
        {
            if (currencyList != null && currencyList.Count != 0)
            {
                if (periodTextBox.Text != "")
                {
                    count = Int32.Parse(periodTextBox.Text);
                }
                else
                {
                    count = 20;
                }

                if (parametrEmaTextBox.Text != "")
                {
                    parametrEMA = Double.Parse(parametrEmaTextBox.Text, new CultureInfo("en-US"));
                }
                else
                {
                    parametrEMA = 0.5;
                }

                TypeRate typeRate = typeRateComboBox.SelectedItem as TypeRate;

                MovingAverageCalculate calc = new MovingAverageCalculate();
                calc.Calculate(currencyList, count, parametrEMA, typeRate);

                //count = period;
                #region    calc
                //List<DateTime> date = new List<DateTime>();

                //for (int i = 0; i < count; i++)
                //{
                //    date.Add(currencyList[i].Date);
                //}

                //double[] sma = new double[count];
                //double[] ema = new double[count];

                //List<double> currencyRate = new List<double>();
                //double sum = 0;

                //switch (typeRate.Name)
                //{
                //    case "Close":
                //        currencyRate.AddRange(currencyList.Select(item => item.Close));

                //        for (int i = 0; i < count; i++)
                //        {
                //            sum += currencyRate[i];
                //            sma[i] = sum / (i + 1);
                //        }
                //        ema[0] = sma[0];
                //        for (int i = 1; i < count; i++)
                //        {
                //            //                        EMA = (CLOSE(i) * P) + (EMA(i - 1) * (1 - P))
                //            //                        где:
                //            //                        CLOSE(i) — цена закрытия текущего периода;
                //            //                        EMA(i - 1) — значение скользящего среднего предыдущего периода;
                //            //                        P — доля использования значения цен.
                //            ema[i] = currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA));
                //        }
                //        break;
                //    case "Open":
                //        currencyRate.AddRange(currencyList.Select(item => item.Open));
                //        //double sum = 0;
                //        for (int i = 0; i < count; i++)
                //        {
                //            sum += currencyRate[i];
                //            sma[i] = sum / (i + 1);
                //        }
                //        ema[0] = sma[0];
                //        for (int i = 1; i < count; i++)
                //        {
                //            ema[i] = currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA));
                //        }

                //        break;
                //    case "High":
                //        currencyRate.AddRange(currencyList.Select(item => item.High));
                //        for (int i = 0; i < count; i++)
                //        {
                //            sum += currencyRate[i];
                //            sma[i] = sum / (i + 1);
                //        }
                //        ema[0] = sma[0];
                //        for (int i = 1; i < count; i++)
                //        {
                //            ema[i] = currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA));
                //        }

                //        break;
                //    default:
                //        currencyRate.AddRange(currencyList.Select(item => item.Low));
                //        for (int i = 0; i < count; i++)
                //        {
                //            sum += currencyRate[i];
                //            sma[i] = sum / (i + 1);
                //        }
                //        ema[0] = sma[0];
                //        for (int i = 1; i < count; i++)
                //        {
                //            ema[i] = currencyRate[i] * parametrEMA + (ema[i - 1] * (1 - parametrEMA));
                //        }

                //        break;
                //}
                //List<double> rate = new List<double>();
                //for (int i = 0; i < count; i++)
                //{
                //    rate.Add(currencyRate[i]);
                //}
                #endregion

                chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 1;

                //chart1.ChartAreas[0].AxisY.StripLines.Add(new StripLine());
                //chart1.ChartAreas[0].AxisY.StripLines[0].BackColor = Color.FromArgb(80, 252, 180, 65);
                //chart1.ChartAreas[0].AxisY.StripLines[0].StripWidth = 4;
                //chart1.ChartAreas[0].AxisY.StripLines[0].Interval = 10;
                //chart1.ChartAreas[0].AxisY.StripLines[0].IntervalOffset = 20;

                chart1.ChartAreas[0].AxisY.Maximum = calc.rate.Max() + 2;
                chart1.ChartAreas[0].AxisY.Minimum = calc.rate.Min() - 2;


                chart1.Series[0].XValueType = ChartValueType.Date;
                chart1.Series[0].Points.DataBindXY(calc.date, calc.sma);


                chart1.Series[1].XValueType = ChartValueType.Date;
                chart1.Series[1].Points.DataBindXY(calc.date, calc.ema);


                chart1.Series[2].XValueType = ChartValueType.Date;
                chart1.Series[2].Points.DataBindXY(calc.date, calc.rate);

                calc.Dispose();
                //chart1.ChartAreas[0].AxisY.Maximum = rate.Max() + 2;
                //chart1.ChartAreas[0].AxisY.Minimum = rate.Min() - 2;

                //chart1.Series[0].XValueType = ChartValueType.Date;
                //chart1.Series[0].Points.DataBindXY(date, sma);

                //chart1.Series[1].XValueType = ChartValueType.Date;
                //chart1.Series[1].Points.DataBindXY(date, ema);

                //chart1.Series[2].XValueType = ChartValueType.Date;
                //chart1.Series[2].Points.DataBindXY(date, rate);
            }
        }