Exemple #1
0
 private Server(string login, string password, MainForm form)
 {
     //Login = login;
     //Password = password;
     Form         = form;
     Logger       = new LogWriter(LogPath, LogName);
     dbWriter     = new DBInputOutput.DBWriter();
     dbReader     = new DBInputOutput.DBReader();
     Strategies   = new List <StrategyAbst>();
     Collector    = new DataCollector();
     WasConnected = false;
     dbWriter.InsertSts(ServerTime.GetRealTime(), "Stocks", "Working");
 }
        private void getBarsButton_Click(object sender, EventArgs e)
        {
            DBInputOutput.DBReader dbReader   = new DBInputOutput.DBReader();
            Server                server      = Server.GetInstance();
            PortfolioManager      portManager = PortfolioManager.GetInstance();
            List <StrategyParams> pars        = ConfigReader.ReadConfig();

            double[]      ws         = pars.Select(par => par.ContractsToTrade > 0 ? par.StrategicWeight : -1).Where(w => w >= 0).ToArray();
            double[]      mults      = pars.Select(par => par.ContractsToTrade > 0 ? par.Mult : -1).Where(m => m != -1).ToArray();
            List <string> stratNames = pars.Select(par => par.ContractsToTrade > 0 ? par.Name : "").Where(s => !s.Equals("")).ToList();

            double[] gos   = stratNames.Select(name => server.GetStrategies().Find(strat => strat.Name.Equals(name)).GO).ToArray();
            double[] ps    = stratNames.Select(name => dbReader.SelectLastPrice(server.GetStrategies().Find(strat => strat.Name.Equals(name)).Symbol).Close).ToArray();
            double[] steps = pars.Select(par => par.ContractsToTrade > 0 ? par.Step : -1).Where(s => s != -1).ToArray();
            for (int i = 0; i < ws.Length; i++)
            {
                ws[i] = ws[i] / (ps[i] * mults[i] / gos[i]);
            }
            double sm = ws.Sum();

            portManager.TargetWs = ws.Select(w => w / sm).ToArray();
            //portManager.TargetWs = pars.Select(par => par.ContractsToTrade > 0 ? par.StrategicWeight : -1).Where(w => w >= 0).ToArray();
            if (Math.Abs(portManager.TargetWs.Sum() - 1.0) >= 0.001)
            {
                WriteToError("Sum of TargetWs != 1. Cannot optimize");
                return;
            }

            int[]  contractsToTrade = portManager.CalcContractsToTrade(gos);
            string message          = "Nearest Weights:\r\n";

            double sumToTrade = 0.0;
            double goToTrade  = 0.0;

            for (int i = 0; i < stratNames.Count; i++)
            {
                sumToTrade += contractsToTrade[i] * ps[i] * mults[i];
                goToTrade  += contractsToTrade[i] * gos[i];
            }


            for (int i = 0; i < stratNames.Count; i++)
            {
                double pct = Math.Round(contractsToTrade[i] * ps[i] * mults[i] / sumToTrade, 4) * 100;
                message += stratNames[i] + ": " + contractsToTrade[i] + " * " + ps[i] * mults[i] + " = " + contractsToTrade[i] * ps[i] * mults[i] +
                           " ₽ (" + pct + "%)\r\n";
            }
            message += "Total Sum: " + sumToTrade + " ₽ \r\n";
            message += "Total Money Used: " + goToTrade + " ₽ \r\n";
            WriteToError(message);
        }