public static List <StrategyParams> ReadConfig() { using (StreamReader r = new StreamReader(ConfigPath)) { string json = r.ReadToEnd(); CommonParams commonParams = JsonConvert.DeserializeObject <CommonParams>(json); ExcelReader.ExcelPositionFileName = commonParams.ExcelPositionFileName; ExcelReader.ExcelPositionSheetName = commonParams.ExcelPositionSheetName; ExcelReader.ExcelOrdersSheetName = commonParams.ExcelOrdersSheetName; ExcelReader.PositionTickerColumn = commonParams.PositionTickerColumn; ExcelReader.QtyColumn = commonParams.QtyColumn; ExcelReader.PlannedColumn = commonParams.PlannedColumn; ExcelReader.PositionSideColumn = commonParams.PositionSideColumn; ExcelReader.AvgPriceColumn = commonParams.AvgPriceColumn; ExcelReader.OrderTickerColumn = commonParams.OrderTickerColumn; ExcelReader.StatusColumn = commonParams.StatusColumn; ExcelReader.OrderSideColumn = commonParams.OrderSideColumn; ExcelReader.OrderIdColumn = commonParams.OrderIdColumn; ExcelReader.PriceColumn = commonParams.PriceColumn; ExcelReader.StopPriceColumn = commonParams.StopPriceColumn; ExcelReader.VolumeColumn = commonParams.VolumeColumn; ExcelReader.BalanceColumn = commonParams.BalanceColumn; PortfolioManager.GetInstance().SumToTrade = commonParams.SumToTrade; return(commonParams.Strategies); } }
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); }