public void PlaceOrder(Order order) { if (IsConnected) { if (order.Type == OrderTypeEnum.STOP) // если стоп-заявка, то изменение order.Price относительно последней котировки { Bar br = dbReader.SelectLastPrice(order.Symbol); double lastPrice = br.Close; // цена последней заключенной сделки if (order.Action == ActionEnum.BUY) { if (order.StopPrice <= lastPrice) { InformUser("Changing Stop Order " + order.Symbol + ": " + order.ToString()); WriteToLog("Changing Stop Order: " + order.OrderId); order.StopPrice = 0; order.Type = OrderTypeEnum.LIMIT; } } else if (order.Action == ActionEnum.SELL) { if (order.StopPrice >= lastPrice) { InformUser("Changing Stop Order " + order.Symbol + ": " + order.ToString()); WriteToLog("Changing Stop Order: " + order.OrderId); order.StopPrice = 0; order.Type = OrderTypeEnum.LIMIT; } } } PlaceOrder(order.Symbol, order.Action, order.Type, order.Price, order.Volume, order.StopPrice, order.Cookie); } }
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); }