Пример #1
0
        private void ChartUpdateTimerOnTick(object sender, EventArgs eventArgs)
        {
            if (IsRealtime.IsChecked == true && _lastPrice != 0m)
            {
                var step  = PriceStep.Value ?? 10;
                var price = Round(_lastPrice + (decimal)((RandomGen.GetDouble() - 0.5) * 5 * step), (decimal)step);
                AppendTick(_security, new ExecutionMessage
                {
                    ServerTime = _lastTime,
                    TradePrice = price,
                    Volume     = RandomGen.GetInt(50) + 1
                });
                _lastTime += TimeSpan.FromSeconds(10);
            }

            TimeFrameCandle[] candlesToUpdate;
            lock (_updatedCandles.SyncRoot)
            {
                candlesToUpdate = _updatedCandles.OrderBy(p => p.Key).Select(p => p.Value).ToArray();
                _updatedCandles.Clear();
            }

            _allCandles.AddRange(candlesToUpdate);

            candlesToUpdate.ForEach(c =>
            {
                Chart.Draw(c.OpenTime, new Dictionary <IChartElement, object>
                {
                    { _candleElement1, c },
                });
            });
        }
Пример #2
0
        private void ChartUpdateTimerOnTick(object sender, EventArgs eventArgs)
        {
            if (IsRealtime.IsChecked == true && _lastPrice != 0m)
            {
                var step  = _priceStep;
                var price = Round(_lastPrice + (decimal)((RandomGen.GetDouble() - 0.5) * 5 * (double)step), step);
                AppendTick(_security, new ExecutionMessage
                {
                    ServerTime  = _lastTime,
                    TradePrice  = price,
                    TradeVolume = RandomGen.GetInt(50) + 1,
                    OriginSide  = Sides.Buy,
                });
                _lastTime += TimeSpan.FromSeconds(10);
            }

            TimeFrameCandle[] candlesToUpdate;
            lock (_updatedCandles.SyncRoot)
            {
                candlesToUpdate = _updatedCandles.OrderBy(p => p.Key).Select(p => p.Value).ToArray();
                _updatedCandles.Clear();
            }

            var lastCandle = _allCandles.LastOrDefault();

            _allCandles.AddRange(candlesToUpdate.Where(c => lastCandle == null || c.OpenTime != lastCandle.OpenTime));

            var hasValue  = false;
            var chartData = new ChartDrawData();

            foreach (var candle in candlesToUpdate)
            {
                chartData.Group(candle.OpenTime).Add(_candleElement1, candle);
                hasValue = true;
            }

            if (hasValue)
            {
                Chart.Draw(chartData);
            }
        }
Пример #3
0
        private void ChartUpdateTimerOnTick(object sender, EventArgs eventArgs)
        {
            if (!_dataIsLoaded || IsRealtime.IsChecked != true || _lastPrice == 0m)
            {
                return;
            }

            _lastTime += TimeSpan.FromSeconds(10);
            var numCandles = _candles.Count;

            AppendTick(new ExecutionMessage
            {
                ServerTime  = _lastTime,
                TradePrice  = Round(_lastPrice + (decimal)((RandomGen.GetDouble() - 0.5) * 5 * _priceStep), _priceStep),
                TradeVolume = RandomGen.GetInt(50) + 1
            });

            TimeFrameCandle candle;
            var             lastLightCandle = _candles[_candles.Count - 1];

            if (_candles.Count != numCandles && _lastCandle != null)
            {
                _lastCandle.State = CandleStates.Finished;
                DrawCandle(_lastCandle);
            }

            if (_candles.Count != numCandles || _lastCandle == null)
            {
                _lastCandle = candle = lastLightCandle.ToCandle(_tfSpan, _security);
            }
            else
            {
                candle = _lastCandle;
                lastLightCandle.UpdateCandle(candle);
            }

            DrawCandle(candle);
        }
Пример #4
0
        private void ChartUpdateTimerOnTick(object sender, EventArgs eventArgs)
        {
            if (_checkRealtime.IsChecked == true && _lastPrice != 0m)
            {
                var step  = _updownPriceStep.Value ?? 10;
                var price = Round(_lastPrice + (decimal)((RandomGen.GetDouble() - 0.5) * 5 * step), (decimal)step);
                AppendTick(new ExecutionMessage
                {
                    ServerTime = _lastTime,
                    TradePrice = price,
                    Volume     = RandomGen.GetInt(50) + 1
                });
                _lastTime += TimeSpan.FromSeconds(10);
            }

            var candlesToUpdate = _updatedCandles.OrderBy(c => c.OpenTime).ToArray();

            _updatedCandles.Clear();

            candlesToUpdate.ForEach(c =>
            {
                _chartCombined.Draw(c.OpenTime, new Dictionary <IChartElement, object> {
                    { _candleElement1, c }
                });
                _chartCombined.Draw(c.OpenTime, new Dictionary <IChartElement, object> {
                    { _bvElement, c }
                });

                _chartBindVisibleRange.Draw(c.OpenTime, new Dictionary <IChartElement, object> {
                    { _candleElement2, c }
                });
                _chartBindVisibleRange.Draw(c.OpenTime, new Dictionary <IChartElement, object> {
                    { _cpElement, c }
                });
            });
        }
Пример #5
0
        private void ModifyAnnotation(bool isNew)
        {
            Brush RandomBrush()
            {
                var b = new SolidColorBrush(Color.FromRgb((byte)RandomGen.GetInt(0, 255), (byte)RandomGen.GetInt(0, 255), (byte)RandomGen.GetInt(0, 255)));

                b.Freeze();
                return(b);
            }

            if (_annotation == null)
            {
                return;
            }

            IComparable x1, x2, y1, y2;

            var mode = RandomGen.GetDouble() > 0.5 ? AnnotationCoordinateMode.Absolute : AnnotationCoordinateMode.Relative;

            if (_annotationData == null)
            {
                if (mode == AnnotationCoordinateMode.Absolute)
                {
                    GetMiddle(out var x0, out var y0);
                    x1 = x0 - TimeSpan.FromMinutes(RandomGen.GetInt(10, 60));
                    x2 = x0 + TimeSpan.FromMinutes(RandomGen.GetInt(10, 60));
                    y1 = y0 - RandomGen.GetInt(5, 10) * _security.PriceStep ?? 0.01m;
                    y2 = y0 + RandomGen.GetInt(5, 10) * _security.PriceStep ?? 0.01m;
                }
                else
                {
                    x1 = 0.5 - RandomGen.GetDouble() / 10;
                    x2 = 0.5 + RandomGen.GetDouble() / 10;
                    y1 = 0.5 - RandomGen.GetDouble() / 10;
                    y2 = 0.5 - RandomGen.GetDouble() / 10;
                }
            }
            else
            {
                mode = _annotationData.CoordinateMode.Value;

                if (mode == AnnotationCoordinateMode.Absolute)
                {
                    x1 = (DateTimeOffset)_annotationData.X1 - TimeSpan.FromMinutes(1);
                    x2 = (DateTimeOffset)_annotationData.X2 + TimeSpan.FromMinutes(1);
                    y1 = (decimal)_annotationData.Y1 + _security.PriceStep ?? 0.01m;
                    y2 = (decimal)_annotationData.Y2 - _security.PriceStep ?? 0.01m;
                }
                else
                {
                    x1 = ((double)_annotationData.X1) - 0.05;
                    x2 = ((double)_annotationData.X2) + 0.05;
                    y1 = ((double)_annotationData.Y1) - 0.05;
                    y2 = ((double)_annotationData.Y2) + 0.05;
                }
            }

            _dataThreadActions.Add(() =>
            {
                var data = new ChartDrawData.AnnotationData
                {
                    X1         = x1,
                    X2         = x2,
                    Y1         = y1,
                    Y2         = y2,
                    IsVisible  = true,
                    Fill       = RandomBrush(),
                    Stroke     = RandomBrush(),
                    Foreground = RandomBrush(),
                    Thickness  = new Thickness(RandomGen.GetInt(1, 5)),
                };

                if (isNew)
                {
                    data.Text = "random annotation #" + (++_annotationId);
                    data.HorizontalAlignment = HorizontalAlignment.Stretch;
                    data.VerticalAlignment   = VerticalAlignment.Stretch;
                    data.LabelPlacement      = LabelPlacement.Axis;
                    data.ShowLabel           = true;
                    data.CoordinateMode      = mode;
                }

                var dd = new ChartDrawData();
                dd.Add(_annotation, data);

                Chart.Draw(dd);
            });
        }
Пример #6
0
        public void Load(string filename)
        {
            try
            {
                if (IsLoadForLearning)
                {
                    TotalDataLines = GetDataLinesCount(filename);
                }


                using (var sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read), Encoding.GetEncoding(1251)))
                {
                    int    idx  = 0;
                    int    nrow = 0;
                    string nextline;
                    int    classNum = 0;
                    while ((nextline = sr.ReadLine()) != null)
                    {
                        idx++;
                        if (string.IsNullOrWhiteSpace(nextline))
                        {
                            continue;
                        }
                        string[] blocks = GetStringBlocks(nextline);

                        // header row
                        if (idx == 1)
                        {
                            for (int i = 0; i < blocks.Length; i++)
                            {
                                string cname = blocks[i]; // column name

                                if (!FileColumnByIdx.ContainsKey(i))
                                {
                                    FileColumnByIdx.Add(i, cname);
                                }
                                if (!FileIdxByColumn.ContainsKey(cname))
                                {
                                    FileIdxByColumn.Add(cname, i);
                                }
                                else
                                {
                                    Logger.Log("duplicate column name: " + cname + ", exiting");
                                }
                            }

                            if (TargetName != null)
                            {
                                if (!FileIdxByColumn.ContainsKey(TargetName))
                                {
                                    Logger.Log("Warning: data (" + filename + ") doesn't have a target (" + TargetName + ") column");
                                    //break;
                                }
                                else
                                {
                                    TargetIdx = FileIdxByColumn[TargetName]; // target column index
                                }
                            }

                            // id columns
                            foreach (var iname in Ids.Keys)
                            {
                                if (!FileIdxByColumn.ContainsKey(iname))
                                {
                                    throw new InvalidDataException("id column '" + iname + "' not found");
                                }
                                int sidx = FileIdxByColumn[iname];
                                if (!_idIdx.ContainsKey(sidx))
                                {
                                    _idIdx.Add(sidx, 1);
                                }
                            }
                            if (Ids.Count > 0)
                            {
                                IdName = GetStringId(Ids.Keys.ToArray());
                            }

                            // skip columns
                            var toDel = (from t in SkippedColumns.Keys where !FileIdxByColumn.ContainsKey(t) select t).ToList();
                            toDel.ForEach(c => SkippedColumns.Remove(c));

                            // count of variables except skipped
                            NVars = FileColumnByIdx.Count - SkippedColumns.Count;

                            continue;
                        }

                        // data row
                        nrow++;

                        if (blocks.Length > FileIdxByColumn.Count)
                        {
                            Logger.Log("error parsing row #" + nrow);
                            continue;
                        }

                        if (RandomGen.GetDouble() >= LoadFactor)
                        {
                            continue;
                        }

                        var row = new DataRow <T>();

                        // parse target
                        if (TargetName != null && TargetIdx >= 0)
                        {
                            row.Target = ParseValue(blocks[TargetIdx]);
                        }

                        // creating composite id
                        row.Id = GetStringId(blocks);
                        if (string.IsNullOrEmpty(row.Id))
                        {
                            row.Id = nrow.ToString();                               //using row_number if ids not set
                        }
                        // save stats for target value
                        if (!TargetStat.ContainsKey(row.Target))
                        {
                            TargetStat.Add(row.Target, 0);
                        }
                        TargetStat[row.Target]++;   // count by target

                        // class id by target
                        if (!ClassNumByValue.ContainsKey(row.Target))
                        {
                            ClassNumByValue.Add(row.Target, classNum++);
                        }

                        // target by class id
                        if (!ValueByClassNum.ContainsKey(ClassNumByValue[row.Target]))
                        {
                            ValueByClassNum.Add(ClassNumByValue[row.Target], row.Target);
                        }


                        // --------------------------- loading for learning -------------------------------
                        if (IsLoadForLearning)
                        {
                            if (LearnRows == null)
                            {
                                LearnRows = new T[TotalDataLines, NVars + 1]; // all variables +1 for target
                            }
                            for (int i = 0, k = 0; i < blocks.Length; i++)
                            {
                                string cval    = blocks[i];
                                string colname = FileColumnByIdx[i];
                                if (SkippedColumns.ContainsKey(colname))
                                {
                                    continue;
                                }

                                T pval = ParseValue(cval);
                                LearnRows[nrow - 1, k++] = pval;
                                SaveVarDistr(colname, pval);
                            }
                            LearnRows[nrow - 1, NVars] = row.Target;
                        }
                        else
                        {
                            // --------------------------- loading for analyse -----------------------------------
                            var carray = new T[NVars];

                            for (int i = 0, k = 0; i < blocks.Length; i++)
                            {
                                string cval = blocks[i];
                                if (FileColumnByIdx.ContainsKey(i))
                                {
                                    string colname = FileColumnByIdx[i];
                                    if (SkippedColumns.ContainsKey(colname))
                                    {
                                        continue;
                                    }

                                    if (!RowColumnByIdx.ContainsKey(k))
                                    {
                                        RowColumnByIdx.Add(k, colname);
                                    }

                                    if (!RowIdxByColumn.ContainsKey(colname))
                                    {
                                        RowIdxByColumn.Add(colname, k);
                                    }

                                    T pval = ParseValue(cval);
                                    carray[k] = pval;
                                    k++;
                                    SaveVarDistr(colname, pval);
                                }
                                else
                                {
                                    Logger.Log("error parsing id=" + row.Id);
                                }
                            }

                            row.Values = carray;
                            if (ProceedRowFunc == null) // don't save row in case of ProceedRowFunc not null
                            {
                                Rows.Add(row);
                            }
                            else
                            {
                                ProceedRowFunc(row);
                            }

                            TotalDataLines++;
                        }

                        if (idx % 12345 == 0)
                        {
                            Logger.Log(idx + " lines loaded");
                        }
                        if (MaxRowsLoaded != 0 && idx > MaxRowsLoaded)
                        {
                            break;
                        }
                    }

                    GetTargetProbs();
                    Logger.Log((idx - 1) + " lines loaded;");
                }
            }
            catch (Exception e)
            {
                Logger.Log(e);
                throw;
            }
        }
Пример #7
0
        public static DecisionTree CreateTree(int[] indexes, double[,] xy, int nclasses, double pcoeff, double vcoeff)
        {
            if (xy == null)
            {
                throw new ArgumentException("xy is null", nameof(xy));
            }

            int npoints = xy.GetLength(0);
            int nvars   = xy.GetLength(1) - 1;

            int modNpoints = (int)(npoints * pcoeff); // столько значений надо нагенерировать
            int modNvars   = (int)(nvars * vcoeff);   // столько переменных используем

            if (modNvars < 1)
            {
                throw new ArgumentException("vcoeff too small", nameof(vcoeff));
            }

            int[] vidxes = Enumerable.Range(0, nvars).ToArray();
            if (modNvars < nvars)
            {
                vidxes = Enumerable.Range(0, nvars).OrderBy(c => RandomGen.GetDouble()).Take(modNvars).ToArray();
            }

            double[,] nxy = new double[modNpoints, modNvars + 1]; // сами значения

            int nk     = 0;                                       // столько нагенерировали
            var exists = new Dictionary <int, int>();

            if (indexes == null)
            {
                // basic tree
                while (nk < modNpoints)
                {
                    for (int i = 0; i < modNpoints; i++)
                    {
                        int sn = (int)(RandomGen.GetDouble() * npoints); // selection distribution
                        if (sn >= npoints)
                        {
                            continue;
                        }

                        if (exists.ContainsKey(sn))
                        {
                            continue;                         // такой ключ уже был
                        }
                        exists.Add(sn, 0);
                        for (int j = 0; j < modNvars; j++)
                        {
                            nxy[i, j] = xy[sn, vidxes[j]];
                        }
                        nxy[i, modNvars] = xy[sn, nvars];
                        nk++;

                        if (nk >= modNpoints)
                        {
                            break;
                        }
                    }

                    if (nk >= modNpoints)
                    {
                        break;
                    }
                }
            }
            else
            {
                // tree, with distribution selection

                while (nk < modNpoints)
                {
                    for (int i = 0; i < modNpoints; i++)
                    {
                        int sn = (int)(RandomGen.GetTrangle() * npoints); // selection distribution
                        if (sn >= indexes.Length)
                        {
                            continue;
                        }

                        if (exists.ContainsKey(sn))
                        {
                            continue;                         // такой ключ уже был
                        }
                        exists.Add(sn, 0);
                        int sidx = indexes[sn];
                        for (int j = 0; j < modNvars; j++)
                        {
                            nxy[i, j] = xy[sidx, vidxes[j]];
                        }
                        nxy[i, modNvars] = xy[sidx, nvars];
                        nk++;

                        if (nk >= modNpoints)
                        {
                            break;
                        }
                    }

                    if (nk >= modNpoints)
                    {
                        break;
                    }
                }
            }

            var ridxes = exists.Keys.ToArray();

            return(CreateTree(nxy, nclasses, nvars, modNpoints, modNvars, vidxes, ridxes));
        }