public void Initialize(
            EvaluationResultContext context,
            IDictionary <ParameterAttribute, object> parameterValues,
            DateTime startDate,
            DateTime endDate,
            TradeMetric metric)
        {
            if (context == null || parameterValues == null || metric == null)
            {
                throw new ArgumentNullException();
            }

            var serializableParameterValues = new SerializableParameterValues();

            serializableParameterValues.Initialize(parameterValues);

            for (var i = 0; i < serializableParameterValues.Parameters.Length; ++i)
            {
                _parameterValueProperties[i].SetValue(this, serializableParameterValues.Parameters[i].Value);
            }

            for (var i = 0; i < metric.ERatios.Length; ++i)
            {
                _eRatioProperties[i].SetValue(this, metric.ERatios[i]);
            }

            StartDate               = startDate;
            EndDate                 = endDate;
            InitialEquity           = metric.InitialEquity;
            FinalEquity             = metric.FinalEquity;
            NetProfit               = metric.NetProfit;
            ProfitRatio             = metric.ProfitRatio;
            AnnualProfitRatio       = metric.AnnualProfitRatio;
            TotalTradingTimes       = metric.TotalTradingTimes;
            ProfitTimesRatio        = metric.ProfitTimesRatio;
            AverageProfitPerTrading = metric.AverageProfitPerTrading;
            AverageLossPerTrading   = metric.AverageLossPerTrading;
            MaxDrawDown             = metric.MaxDrawDown;
            MaxDrawDownRatio        = metric.MaxDrawDownRatio;
            Mar         = metric.Mar;
            Expectation = ProfitTimesRatio * AverageProfitPerTrading - (1.0 - ProfitTimesRatio) * AverageLossPerTrading;

            var b = AverageProfitPerTrading / AverageLossPerTrading;

            BestFactor = (b * ProfitTimesRatio - (1.0 - ProfitTimesRatio)) / b;

            ContextId        = context.ContextId;
            ContextDirectory = context.RootDirectory;
        }
        public static void Initialize(IDictionary <ParameterAttribute, object> parameterValues, bool shouldOutputERatio)
        {
            if (parameterValues == null)
            {
                throw new ArgumentNullException();
            }

            var serializableParameterValues = new SerializableParameterValues();

            serializableParameterValues.Initialize(parameterValues);

            ParameterNames = serializableParameterValues.Parameters.Select(p => p.Name).ToArray();

            if (ParameterNames.Length > MaxParameterCount)
            {
                throw new ArgumentException(string.Format("# of parameters exceeds limit {0}", MaxParameterCount));
            }

            _parameterValueProperties = new PropertyInfo[MaxParameterCount];

            for (var i = 0; i < MaxParameterCount; ++i)
            {
                var name = string.Format("ParameterValue{0}", i + 1);

                _parameterValueProperties[i] = typeof(ResultSummary).GetProperty(name);
            }

            ResultSummary.ShouldOutputERatio = shouldOutputERatio;

            _eRatioProperties = new PropertyInfo[MaxERatioCount];

            for (var i = 0; i < MaxERatioCount; ++i)
            {
                var name = string.Format("ERatio{0}", i + 1);

                _eRatioProperties[i] = typeof(ResultSummary).GetProperty(name);
            }
        }
Exemple #3
0
        public void SaveResults(
            ITradingDataProvider dataProvider,
            IDictionary <ParameterAttribute, object> parameterValues,
            IEnumerable <Position> activePositions,
            IEnumerable <Transaction> transactions,
            IEnumerable <AuxiliaryData> auxiliaryData)
        {
            // save parameter values
            var searializedParameterValues = new SerializableParameterValues();

            searializedParameterValues.Initialize(parameterValues);

            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, ParameterValuesFileName),
                       false,
                       Encoding.UTF8))
            {
                var serializer = new XmlSerializer(searializedParameterValues.GetType());
                serializer.Serialize(writer, searializedParameterValues);
            }

            // save active positions
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, PositionFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteHeader <Position>();

                    foreach (var position in activePositions)
                    {
                        csvWriter.WriteRecord(position);
                    }
                }
            }

            // save transactions
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, TransactionsFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(transactions);
                }
            }

            // save auxiliary data
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, AuxiliaryDataFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(auxiliaryData);
                }
            }
        }
        public void SaveResults(
            IDictionary <ParameterAttribute, object> parameterValues,
            IEnumerable <TradeMetric> metrics,
            IEnumerable <Position> closePositions,
            IEnumerable <BlockTradingDetailSummarizer.BlockTradingDetail> details)
        {
            // save parameter values
            var searializedParameterValues = new SerializableParameterValues();

            searializedParameterValues.Initialize(parameterValues);

            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, ParameterValuesFileName),
                       false,
                       Encoding.UTF8))
            {
                var serializer = new XmlSerializer(searializedParameterValues.GetType());
                serializer.Serialize(writer, searializedParameterValues);
            }

            // save metrics
            var tradeMetrics = metrics as TradeMetric[] ?? metrics.ToArray();

            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, MetricsFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(tradeMetrics);
                }
            }

            // save closed positions
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, ClosePositionsFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(closePositions);
                }
            }

            // get the overall metric
            var overallMetric = tradeMetrics.First(m => m.Code == TradeMetric.CodeForAll);

            // save equities
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, EquitiesFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(overallMetric.OrderedEquitySequence);
                }
            }

            // save transactions
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, TransactionsFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(overallMetric.OrderedTransactionSequence);
                }
            }

            // save closed transactions
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, CompletedTransactionsFileName),
                       false,
                       Encoding.UTF8))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.WriteRecords(overallMetric.OrderedCompletedTransactionSequence);
                }
            }

            // save block trading details
            using (var writer = new StreamWriter(
                       Path.Combine(RootDirectory, BlockTradingDetailsFileName),
                       false,
                       Encoding.UTF8))
            {
                // write header
                string header = string.Join(
                    ",",
                    "CODE,TIME,BLOCK,UPRATE",
                    string.Join(",", TradeMetricsCalculator.ERatioWindowSizes.Select(i => string.Format("MFE{0}", i))),
                    string.Join(",", TradeMetricsCalculator.ERatioWindowSizes.Select(i => string.Format("MAE{0}", i))));

                writer.WriteLine(header);

                foreach (var detail in details)
                {
                    string line = string.Join(
                        ",",
                        detail.Code,
                        string.Format("{0:yyy-MM-dd}", detail.Time),
                        detail.Block,
                        string.Format("{0:0.00000}", detail.UpRateFromLowest),
                        string.Join(",", detail.Mfe.Select(d => string.Format("{0:0.00000}", d))),
                        string.Join(",", detail.Mae.Select(d => string.Format("{0:0.00000}", d))));

                    writer.WriteLine(line);
                }
            }
        }