/// <summary>
        ///     Updates the journal data from the backtester
        /// </summary>
        private void UpdateJournalData()
        {
            if (!Data.IsResult)
            {
                return;
            }

            journalData   = new string[positions, columns];
            positionIcons = new Image[shownPos];

            for (int pos = firstPos; pos < firstPos + shownPos; pos++)
            {
                int row = pos - firstPos;

                journalData[row, 0] = (StatsBuffer.PosNumb(SelectedBar, pos) + 1).ToString(CultureInfo.InvariantCulture);
                journalData[row, 1] = Language.T(StatsBuffer.PosTransaction(SelectedBar, pos).ToString());
                journalData[row, 2] = Language.T(StatsBuffer.PosDir(SelectedBar, pos).ToString());
                journalData[row, 3] = Configs.AccountInMoney
                                          ? (StatsBuffer.PosDir(SelectedBar, pos) == PosDirection.Short ? "-" : "") +
                                      (StatsBuffer.PosLots(SelectedBar, pos) * Data.InstrProperties.LotSize)
                                          : StatsBuffer.PosLots(SelectedBar, pos).ToString(CultureInfo.InvariantCulture);
                journalData[row, 4] =
                    (StatsBuffer.PosOrdNumb(SelectedBar, pos) + 1).ToString(CultureInfo.InvariantCulture);
                journalData[row, 5] = StatsBuffer.PosOrdPrice(SelectedBar, pos).ToString(Data.Ff);
                journalData[row, 6] = StatsBuffer.PosPrice(SelectedBar, pos).ToString(Data.Ff);

                // Profit Loss
                if (StatsBuffer.PosTransaction(SelectedBar, pos) == Transaction.Close ||
                    StatsBuffer.PosTransaction(SelectedBar, pos) == Transaction.Reduce ||
                    StatsBuffer.PosTransaction(SelectedBar, pos) == Transaction.Reverse)
                {
                    journalData[row, 7] = Configs.AccountInMoney
                                              ? StatsBuffer.PosMoneyProfitLoss(SelectedBar, pos).ToString("F2")
                                              : Math.Round(StatsBuffer.PosProfitLoss(SelectedBar, pos))
                                          .ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    journalData[row, 7] = "-";
                }

                // Floating Profit Loss
                if (pos == positions - 1 && StatsBuffer.PosTransaction(SelectedBar, pos) != Transaction.Close)
                {
                    // Last bar position only
                    journalData[row, 8] = Configs.AccountInMoney
                                              ? StatsBuffer.PosMoneyFloatingPL(SelectedBar, pos).ToString("F2")
                                              : Math.Round(StatsBuffer.PosFloatingPL(SelectedBar, pos))
                                          .ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    journalData[row, 8] = "-";
                }

                // Icons
                positionIcons[row] = Position.PositionIconImage(StatsBuffer.PosIcon(SelectedBar, pos));
            }
        }
        /// <summary>
        ///     Calculates Data to draw in histogram
        /// </summary>
        private void CalculateHistogramData()
        {
            InitChartData();

            for (int bar = 0; bar < Data.Bars; bar++)
            {
                int positions = StatsBuffer.Positions(bar);
                for (int pos = 0; pos < positions; pos++)
                {
                    if (!IsTrade(StatsBuffer.PosTransaction(bar, pos)))
                    {
                        continue;
                    }

                    double result  = GetProfit(bar, pos);
                    var    index   = (int)Math.Round(result);
                    bool   isIndex = chartData.ContainsKey(index);
                    int    count   = isIndex ? chartData[index].TradesCount + 1 : 1;
                    double total   = isIndex ? chartData[index].TotalResult + result : result;
                    var    data    = new HistogramData {
                        TradesCount = count, Result = result, TotalResult = total
                    };

                    if (isIndex)
                    {
                        chartData[index] = data;
                    }
                    else
                    {
                        chartData.Add(index, data);
                    }

                    SetMinMaxValues(index, count, total);
                }
            }

            chartData.Sort();
        }
Exemple #3
0
        /// <summary>
        /// Calculates Data to draw in histogram
        /// </summary>
        private static void CalculateHistogramData()
        {
            // crummy way to get number of trades for init array
            // TBD -- find better property
            int ctr = 0;

            for (int bar = 0; bar < Data.Bars; bar++)
            {
                for (int pos = 0; pos < StatsBuffer.Positions(bar); pos++)
                {
                    Transaction transaction = StatsBuffer.PosTransaction(bar, pos);
                    if (transaction == Transaction.Close ||
                        transaction == Transaction.Reduce ||
                        transaction == Transaction.Reverse)
                    {
                        ctr++;
                    }
                }
            }

            _tradeResults = new int[ctr];
            ctr           = 0;
            for (int bar = 0; bar < Data.Bars; bar++)
            {
                for (int pos = 0; pos < StatsBuffer.Positions(bar); pos++)
                {
                    Transaction transaction = StatsBuffer.PosTransaction(bar, pos);
                    if (transaction == Transaction.Close ||
                        transaction == Transaction.Reduce ||
                        transaction == Transaction.Reverse)
                    {
                        _tradeResults[ctr] = (int)StatsBuffer.PosProfitLoss(bar, pos);
                        ctr++;
                    }
                }
            }

            int min = 0;
            int max = 0;

            foreach (int result in _tradeResults)
            {
                if (min > result)
                {
                    min = result;
                }
                if (max < result)
                {
                    max = result;
                }
            }
            _tradeIndexes     = new int[(max - min) + 1];
            _tradeCounts      = new int[(max - min) + 1];
            _tradeCumulatives = new int[(max - min) + 1];

            // fill _tradeIndexes with index values, then count how many in _tradeResults
            for (int ctr1 = 0; ctr1 < _tradeIndexes.Length; ctr1++)
            {
                _tradeIndexes[ctr1] = min + ctr1;
                int count = 0;
                for (int ctr2 = 0; ctr2 < _tradeResults.Length; ctr2++)
                {
                    if (_tradeResults[ctr2] == _tradeIndexes[ctr1])
                    {
                        count++;
                    }
                }
                _tradeCounts[ctr1]      = count;
                _tradeCumulatives[ctr1] = _tradeIndexes[ctr1] * count;
            }
        }
        /// <summary>
        /// Sets the journal's current data
        /// </summary>
        public void SetUpJournal()
        {
            if (ShowTransfers)
            {
                _positions = StatsBuffer.PositionsTotal;
            }
            else
            {
                _posNumbers = StatsBuffer.PositionsTotal > 0 ? new int[StatsBuffer.PositionsTotal] : new int[1];
                _positions  = 0;
                for (int bar = 0; bar < Data.Bars; bar++)
                {
                    for (int pos = 0; pos < StatsBuffer.Positions(bar); pos++)
                    {
                        Transaction transaction = StatsBuffer.PosTransaction(bar, pos);
                        if (transaction == Transaction.None || transaction == Transaction.Transfer)
                        {
                            continue;
                        }
                        _posNumbers[_positions] = StatsBuffer.PosNumb(bar, pos);
                        _positions++;
                    }
                }
            }

            if (_positions == 0)
            {
                _firstPos    = 0;
                _lastPos     = 0;
                _shownPos    = 0;
                _selectedRow = 0;

                _vScrollBar.Enabled = false;
            }
            else if (_positions < _rows)
            {
                _firstPos    = 0;
                _lastPos     = _rows;
                _shownPos    = _positions;
                _selectedRow = 0;

                _vScrollBar.Enabled = false;
            }
            else
            {
                _vScrollBar.Enabled = true;
                _vScrollBar.Maximum = _positions - 1;

                _firstPos = _vScrollBar.Value;
                if (_firstPos + _rows > _positions)
                {
                    _lastPos  = _positions - 1;
                    _shownPos = _lastPos - _firstPos + 1;
                }
                else
                {
                    _shownPos = _rows;
                    _lastPos  = _firstPos + _shownPos - 1;
                }
            }

            _selectedRow = Math.Min(_selectedRow, _shownPos - 1);
            _selectedRow = Math.Max(_selectedRow, 0);

            UpdateJournalData();
            SetJournalColors();
        }
Exemple #5
0
        /// <summary>
        ///     Sets the journal's current data
        /// </summary>
        public void SetUpJournal()
        {
            if (!StatsBuffer.IsStatsBufferValid)
            {
                return;
            }

            if (ShowTransfers)
            {
                positions = StatsBuffer.PositionsTotal;
            }
            else
            {
                posNumbers = StatsBuffer.PositionsTotal > 0 ? new int[StatsBuffer.PositionsTotal] : new int[1];
                positions  = 0;
                for (int bar = 0; bar < Data.Bars; bar++)
                {
                    for (int pos = 0; pos < StatsBuffer.Positions(bar); pos++)
                    {
                        Transaction transaction = StatsBuffer.PosTransaction(bar, pos);
                        if (transaction == Transaction.None || transaction == Transaction.Transfer)
                        {
                            continue;
                        }
                        posNumbers[positions] = StatsBuffer.PosNumb(bar, pos);
                        positions++;
                    }
                }
            }

            if (positions == 0)
            {
                firstPos    = 0;
                lastPos     = 0;
                shownPos    = 0;
                selectedRow = 0;

                vScrollBar.Enabled = false;
            }
            else if (positions < rows)
            {
                firstPos    = 0;
                lastPos     = rows;
                shownPos    = positions;
                selectedRow = 0;

                vScrollBar.Enabled = false;
            }
            else
            {
                vScrollBar.Enabled = true;
                vScrollBar.Maximum = positions - 1;

                firstPos = vScrollBar.Value;
                if (firstPos + rows > positions)
                {
                    lastPos  = positions - 1;
                    shownPos = lastPos - firstPos + 1;
                }
                else
                {
                    shownPos = rows;
                    lastPos  = firstPos + shownPos - 1;
                }
            }

            selectedRow = Math.Min(selectedRow, shownPos - 1);
            selectedRow = Math.Max(selectedRow, 0);

            UpdateJournalData();
            SetJournalColors();
        }