Beispiel #1
0
        public void CalculatePerformance()
        {
            double[] performance = performanceMatrix.Row(4).ToArray();
            double   sharpeRatio = performance.ToArray().SharpeRatio();
            double   averageRet  = performance.ToArray().AverageRawReturn();

            ret = 100 * (performance[performance.Length - 1] - performance[0]) / performance[0];

            if (Output)
            {
                List <string> symbols = new List <string>();
                symbols.Add("Performance");
                symbols.Add("Balance");
                symbols.Add("Equity");

                List <ChartOption> options = new List <ChartOption>();
                options.Add(new ChartOption()
                {
                    ChartType = "spline", Height = 300, YPosition = 0
                });
                options.Add(new ChartOption()
                {
                    ChartType = "spline", Height = 200, YPosition = 1
                });
                options.Add(new ChartOption()
                {
                    ChartType = "spline", Height = 0, YPosition = 1, Layover = true
                });

                int yPos = 3;
                foreach (var indicator in _strategies[0].indicatorList.Values)
                {
                    options.Add(new ChartOption()
                    {
                        ChartType = "spline", Height = 200, YPosition = yPos
                    });
                    symbols.Add(indicator.ToString());
                    yPos++;
                }


                Visualize.GenerateMultiPaneGraph(
                    symbols.ToArray(),
                    _multiQuantum.Keys.ToArray(),
                    performanceMatrix,
                    QSConstants.DEFAULT_DATA_FILEPATH + "result.html",
                    options.ToArray(),
                    _accountManager.Flags.ToArray()
                    );

                Console.WriteLine("Total return: " +
                                  100 * (performance[performance.Length - 1] - performance[0]) / performance[0] + "%");
                Console.WriteLine("Maximum drawdown: " + 100 * performance.ToArray().MaximumDrawdown(true) + "%");
                Console.WriteLine("Average return " + 100 * averageRet + "%");
                Console.WriteLine("Sharpe ratio " + sharpeRatio);

                double trades      = _accountManager.Trades.Count;
                double totalProfit = _accountManager.Trades.Sum(d1 => d1 > 0 ? d1 : 0);
                double totalLoss   = _accountManager.Trades.Sum(d1 => d1 < 0 ? d1 : 0);
                double ProfitCount = _accountManager.Trades.Count(d1 => d1 > 0);
                double LossCount   = _accountManager.Trades.Count(d1 => d1 < 0);

                Console.WriteLine("Number of trades closed: " + trades);
                Console.WriteLine("% Profit trades: " + ProfitCount / trades);
                Console.WriteLine("% Loss trades: " + LossCount / trades);
                Console.WriteLine("Average Profit/trade: " + totalProfit / ProfitCount);
                Console.WriteLine("Average Loss/trade: " + totalLoss / LossCount);
            }
        }
Beispiel #2
0
        public static void Process(FXSession session, string symbol1, string symbol2, string timeframe, int length)
        {
            HistoricPriceEngine h1 = new HistoricPriceEngine(session);

            h1.GetLongHistoricPrices(symbol1, timeframe, length);

            while (!h1.Complete)
            {
                Thread.Sleep(100);
            }

            HistoricPriceEngine h2 = new HistoricPriceEngine(session);

            h2.GetLongHistoricPrices(symbol2, timeframe, length);

            while (!h2.Complete)
            {
                Thread.Sleep(100);
            }
            //-----------------------

            var dateTimeList = new SortedList <DateTime, int>();

            Quantum q1 = h1.Data;
            Quantum q2 = h2.Data;


            var priceData = new DenseMatrix(2, q1.Data.Count);

            for (int j = 0; j < ((q1.Data.Count <= q2.Data.Count)?q1.Data.Count:q2.Data.Count); j++)
            {
                dateTimeList.Add(q1.Data.Values[j].Time, 1);
                priceData[0, j] = q1.Data.Values[j].BidClose;
                priceData[1, j] = q2.Data.Values[j].BidClose;
            }

            Vector <double> price1 = priceData.Row(0);
            Vector <double> price2 = priceData.Row(1);
            //Statistics.ApplyFunction((DenseVector)price1, Math.Log);
            //Statistics.ApplyFunction((DenseVector)price2, Math.Log);

            DenseVector norm1 = price1.ToArray().NormalizeZScore();
            DenseVector norm2 = price2.ToArray().NormalizeZScore();

            var newsym = new string[] { symbol1, symbol2, "spread" };

            var m = new DenseMatrix(6, norm1.Count);

            m.SetRow(0, norm1);
            m.SetRow(1, norm2);
            m.SetRow(2, (norm1 - norm2).ToArray().NormalizeZScore());

            string filename = symbol1.Replace('/', '_') + "-" + symbol2.Replace('/', '_') + ".html";


            Visualize.GenerateMultiPaneGraph(newsym, dateTimeList.Keys.ToArray(), m, QSConstants.DEFAULT_DATA_FILEPATH + filename,
                                             new ChartOption[] { new ChartOption(), new ChartOption()
                                                                 {
                                                                     Layover = true, YPosition = 0
                                                                 }, new ChartOption()
                                                                 {
                                                                     YPosition = 1
                                                                 } }, null, filename + ".json");

            FileUpload.UploadFileToFTP(QSConstants.DEFAULT_DATA_FILEPATH + filename, filename);
            FileUpload.UploadFileToFTP(QSConstants.DEFAULT_DATA_FILEPATH + filename + ".json", filename + ".json");

            double Spread = m[2, m.ColumnCount - 1];

            if (Spread > 2.0 && m[2, m.ColumnCount - 2] <= 2.0)
            {
                Emailer.SendEmail(symbol1 + "-" + symbol2 + " Spread Above 2.0", "Test");
            }

            if (Spread < -2.0 && m[2, m.ColumnCount - 2] >= -2.0)
            {
                Emailer.SendEmail(symbol1 + "-" + symbol2 + " Spread Below -2.0", "Test");
            }
        }