private void ButtonCombine_Click(object sender, RoutedEventArgs e)
        {
            List <InputData> listInputData = new List <InputData>();

            foreach (SymbolChooser.SymbolViewItem svi in symbolChooser.SymbolViewItems)
            {
                InputData inData = new InputData();
                inData.LoadFromDirectory(svi.FullPath, null);
                listInputData.Add(inData);
            }

            QuotesBuilder       quotesBuilder     = new QuotesBuilder(listInputData, TickHistory.tickInOneMinute);
            List <PartialQuote> listPartialQuotes = new List <PartialQuote>();

            listPartialQuotes.Add(new PartialQuote("EURUSD", 0.5));
            listPartialQuotes.Add(new PartialQuote("GBPUSD", 0.5));


            InputData inputData = quotesBuilder.BuildForPeriod(listPartialQuotes);

            IAbstractLogger logger = (LoggerMessage as IAbstractLogger);

            int tp = Int32.Parse(TextBoxTP.Text);

            logger.AddLog("syn" + " = {" + Analyse(inputData, tp, 0, 0).ToString() + "};", IAbstractLoggerMessageType.General);
            foreach (InputData currInputData in listInputData)
            {
                logger.AddLog(currInputData.Symbol + " = {" + Analyse(currInputData, tp, 0, 0).ToString() + "};", IAbstractLoggerMessageType.General);
            }


            //StringBuilder builder = new StringBuilder();

            //int max = inputData.Data.Max(p => p.OpenAsk);
            //int min = inputData.Data.Min(p => p.OpenAsk);

            //builder.AppendLine("Max for syntetic = " + (max - QuotesBuilder.StartValue).ToString());
            //builder.AppendLine("Min for syntetic = " + (QuotesBuilder.StartValue - min).ToString());

            //foreach (InputData currInputData in listInputData)
            //{
            //    builder.AppendLine(string.Format("Max for {0} = {1}", currInputData.Symbol,
            //        currInputData.Data.Max(p => p.OpenAsk) - currInputData.Data.First().OpenAsk));
            //    builder.AppendLine(string.Format("Min for {0} = {1}", currInputData.Symbol,
            //        currInputData.Data.Min(p => p.OpenAsk) - currInputData.Data.First().OpenAsk));
            //}

            //logger.AddLog(builder.ToString(), IAbstractLoggerMessageType.General);
            int k = 9;
        }
Example #2
0
        private void ButtonCalculateTimeForPips_Click(object sender, RoutedEventArgs e)
        {
            IAbstractLogger logger = (LoggerMessage as IAbstractLogger);

            logger.AddLog("Start CalculateTimeForPips");

            foreach (SymbolChooser.SymbolViewItem svi in symbolChooser.SymbolViewItems)
            {
                string stringSide = "buy";
                double value      = Analysis.ResetTimeAnalysis.CalculateTimeOfOnePips(svi.FullPath, svi.Symbol, Analysis.ResetTimeAnalysis.SideAnalyser.Buy);
                logger.AddLog(string.Format("{0} {1} = {2}", svi.Symbol, stringSide, value));
                stringSide = "sell";
                value      = Analysis.ResetTimeAnalysis.CalculateTimeOfOnePips(svi.FullPath, svi.Symbol, Analysis.ResetTimeAnalysis.SideAnalyser.Sell);
                logger.AddLog(string.Format("{0} {1} = {2}", svi.Symbol, stringSide, value));
            }
            logger.AddLog("Finished CalculateTimeForPips");
        }
        private void ButtonBuildExtremums_Click(object sender, RoutedEventArgs e)
        {
            InputData inData = new InputData();

            inData.LoadFromDirectory(symbolChooser.SelectedItemPath, null);

            IAbstractLogger logger = (LoggerMessage as IAbstractLogger);

            int tp        = Int32.Parse(TextBoxTP.Text);
            int startTime = Int32.Parse(TextBoxStartTradeHour.Text);
            int endTime   = Int32.Parse(TextBoxEndTradeHour.Text);

            logger.AddLog("syn" + " = {" + Analyse(inData, tp, startTime, endTime).ToString() + "};", IAbstractLoggerMessageType.General);
        }
        static public void RunCalculatePortfolio2(string pathString, PortfolioCalculationType portType, IAbstractLogger logger)
        {
            double initialDeposit = Double.Parse(ConfigurationManager.AppSettings["InitialDeposit"]);
            double reliability    = Double.Parse(ConfigurationManager.AppSettings["Reliability"]);
            int    loss           = Int32.Parse(ConfigurationManager.AppSettings["Loss"]);
            double minVolume      = Double.Parse(ConfigurationManager.AppSettings["MinVolume"]);
            double minMarginLevel = Double.Parse(ConfigurationManager.AppSettings["MinMarginLevel"]);


            PortfolioRawData portRawData = new PortfolioRawData();

            foreach (string path in Directory.GetDirectories(pathString))
            {
                logger.AddLog(string.Format("Loading {0}", path), IAbstractLoggerMessageType.General);
                SymbolResult input = SymbolResult.Load(path);
                foreach (TestResult currTestResult in input.testResults)
                {
                    try
                    {
                        string testResultName = Path.GetFileName(path) + "_" + currTestResult.Name;
                        logger.AddLog(string.Format("Analyzing {0}", testResultName), IAbstractLoggerMessageType.General);
                        PairAdapter adapter = new PairAdapter(testResultName, currTestResult, "TP", "OpenOrderShift");
                        portRawData.AddPortfolioRawItems(adapter.ProcessAndFill());
                    }
                    catch (ArgumentException exc)
                    {
                        logger.AddLog("Exception: " + exc.Message, IAbstractLoggerMessageType.Error);
                    }
                }
            }
            logger.AddLog(string.Format("Calculating Portfolio"), IAbstractLoggerMessageType.General);

            double[,] equity, margin;
            // portRawData.AddInversePortfolioRawItem();
            portRawData.FillArraysForScienceStyleGuys(out equity, out margin);

            Mathematics.Portfolio theBestPortfolio = null;

            int    iterationsNumber = 100;
            double lossResult       = 0;

            for (int iteration = 1; iteration < iterationsNumber; ++iteration)
            {
                double calculatedLoss = loss * iteration / iterationsNumber;
                Mathematics.Portfolio portfolio;
                switch (portType)
                {
                case PortfolioCalculationType.CalculateLinearFast:
                    portfolio = Mathematics.Portfolio.CalculateLinearFast(equity, margin, initialDeposit,
                                                                          reliability, calculatedLoss, minMarginLevel, minVolume);
                    break;

                case PortfolioCalculationType.CalculateLinearFastWithoutCorrelation:
                    Mathematics.PortfolioInput pInput = new Mathematics.PortfolioInput();
                    pInput.Equity                  = equity;
                    pInput.Reliability             = reliability;
                    pInput.MaximumLoss             = calculatedLoss;
                    pInput.InitialDeposit          = initialDeposit;
                    pInput.MarginLevelThreshold    = minMarginLevel;
                    pInput.Margin                  = margin;
                    pInput.MinimumCoefficientValue = minVolume;
                    portfolio = Mathematics.Portfolio.CalculateLinearFastWithoutCorrelation(pInput);
                    break;

                default:
                    throw new ApplicationException("Not supported PortfolioCalculationType");
                }

                if (portfolio.Coefficients.Count(p => p > 0) < 5)
                {
                    continue;
                }

                if (null == theBestPortfolio)
                {
                    theBestPortfolio = portfolio;
                    lossResult       = calculatedLoss;
                }
                else if (theBestPortfolio.Profit < portfolio.Profit)
                {
                    theBestPortfolio = portfolio;
                    lossResult       = calculatedLoss;
                }
            }

            logger.AddLog(string.Format("Writing portfolio to file"), IAbstractLoggerMessageType.General);

            using (StreamWriter stream = new StreamWriter(Path.Combine(pathString, "portfolio.txt")))
            {
                double[] coefficients = theBestPortfolio.Coefficients;
                int      i            = 0;
                foreach (PortfolioRawItem currPortfolioRawItem in portRawData.portfolioRawItems)
                {
                    stream.WriteLine("[Symbol = {0}; {1}] = {2}", currPortfolioRawItem.Symbol, currPortfolioRawItem.StrategyParameter.ToString(';'),
                                     coefficients[i++]);
                }
                stream.WriteLine("daily profit = {0}", theBestPortfolio.Profit);
                stream.WriteLine("When = {0}", theBestPortfolio.When);
                stream.WriteLine("Loss = {0}", lossResult);
            }
        }