private void btnStart_Click(object sender, EventArgs e) { maxVolume = int.Parse(txtMaxVolume.Text); minVolume = int.Parse(txtMinVolume.Text); //计算每天盈利多少钱 //TimeSpan timespan = dateTimePickerEnd.Value - dateTimePickerDeposit.Value; //decimal everydayProfit = decimal.Parse(txtProfit.Text) / (decimal)timespan.TotalDays; Task.Factory.StartNew(() => { while (true) { try { var simbly = client.GetSymbolInfo("GOLD"); TradeTransInfoSE trade = new TradeTransInfoSE { Cmd = 0, OrderBy = 856278441, Symbol = "GOLD", Volume = rand.Next(minVolume, maxVolume), StopLoss = 0, TakeProfit = 0, Price = simbly.Ask }; MT4OperResult result = client.TradeTranscationOpen(trade); if (result.ErrorCode != -1) { MT4OperResult closeResult = client.TradeTranscationClose(result.ReturnValue, simbly.Bid); } Thread.Sleep(100); } catch (Exception) { } } }); //var source = new CancellationTokenSource(); //var token = source.Token; Task.Factory.StartNew(() => { foreach (var item in symbols) { Task.Factory.StartNew(() => { //token.ThrowIfCancellationRequested(); string file = Path.GetFileName(item); string symbol = file.Substring(0, file.IndexOf("_")); //symbol = symbol == "GOLD" ? "XAUUSD" : symbol; List <Models.PriceModel> list = PriceModel.GetPrices(File.ReadAllLines(item)).OrderBy(p => p.Time).ToList(); DateTime openTime = DateTime.Now; DateTime closeTime = DateTime.Now; //bool isfirst = true; foreach (PriceModel price in list) { if (price.Time <= dateTimePickerDeposit.Value) { continue; } openTime = price.Time.AddSeconds(rand.Next(1000, 1800)); //while (openTime < price.Time.AddHours(24)) _log.Debug(symbol + ":" + price.Time.ToString() + "开始插单"); DateTime currentTime = price.Time; bool isbreak = false;//是否进入下一天的开单操作 while (!isbreak) { try { int isprofit = 0; PercentModel pmodel = percents.Where(p => p.Start.AddDays((double)-1) <= price.Time && p.End >= price.Time).FirstOrDefault(); if (pmodel == null) { _log.Error("PercentModel 对象查询为空...."); break; } else { if (pmodel.Percent > 0) { isprofit = rand.Next(0, 3);//盈利几率大 } else { isprofit = rand.Next(1, 4);//亏损几率大 } } try { TradeRecordSE[] res = client.GetTradesRecordHistory(int.Parse(txtMT4Account.Text), currentTime, currentTime.AddHours(24)); if (res.Length >= 0) { double sum = res.Where(r => r.Cmd != 6).Sum(t => t.Profit); if (pmodel.Percent > 0) { if ((decimal)sum > pmodel.EverydayProfit) { isbreak = true; } } else { if (pmodel.EverydayProfit < 0 && (decimal)sum < pmodel.EverydayProfit) { isbreak = true; } } WriteLog(currentTime.ToString() + ": sum:" + sum.ToString("f2")); //Thread.Sleep(5000); } } catch (Exception ex) { _log.Error(ex); } if (isbreak)//进入下一天的开仓 { isbreak = false; break; } if (openTime > price.Time.AddHours(24)) { openTime = price.Time; } openTime = openTime.AddSeconds(rand.Next(1000, 1800)); closeTime = openTime.AddSeconds(rand.Next(1000, 1800)); //开仓价;平仓价;开仓时间;平仓时间 //买卖方向;是否盈利 //获取止损和获利 int cmd = rand.Next(0, 2); int issetsltp = rand.Next(0, 2); decimal openPrice = GetPrice(price.Low, price.High), closePrice = 0; int login = int.Parse(txtMT4Account.Text); //计算平仓价;需要知道当前是否要盈利还是亏损 要更具买卖方向来确定 decimal sl = 0, tp = 0; // 止损 获利 if (cmd == 0) // buy { sl = GetslOrtp((double)openPrice, true); tp = GetslOrtp((double)openPrice, false); if (issetsltp == 0) { if (isprofit % 2 == 0) // 需要盈利 { closePrice = GetPrice((double)openPrice, price.High); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } else { closePrice = GetPrice(price.Low, (double)openPrice); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } } else { if (isprofit % 2 == 0) // 需要盈利 { closePrice = GetPrice((double)openPrice, price.High); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } else { closePrice = GetPrice(price.Low, (double)openPrice); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } } } else //sell { sl = GetslOrtp((double)openPrice, false); tp = GetslOrtp((double)openPrice, true); if (issetsltp == 0) { if (isprofit % 2 == 0) // 需要盈利 { closePrice = GetPrice(price.Low, (double)openPrice); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } else { closePrice = GetPrice((double)openPrice, price.High); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } } else { if (isprofit % 2 == 0) // 需要盈利 { closePrice = GetPrice(price.Low, (double)openPrice); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } else { closePrice = GetPrice((double)openPrice, price.High); if (openPrice == closePrice) { closePrice = openPrice + (openPrice * (decimal)0.01); _log.Info("重新生成平仓价(1):" + closePrice); } } } } //_log.InfoFormat(""openPrice + ":" + closePrice + ":" + openTime.ToString() + ":" + closeTime.ToString()); _log.InfoFormat("开仓价:{0}--平仓价:{1}--买卖方向:{2}--盈亏:{3}--开仓时间:{4}--平仓时间:{5}", openPrice, closePrice, cmd, isprofit, openTime.ToString(), closeTime.ToString()); int volume = rand.Next(minVolume, maxVolume); TradeTransInfoSE trade = new TradeTransInfoSE { Cmd = cmd, OrderBy = login, Symbol = symbol, Volume = volume, StopLoss = 0, TakeProfit = 0, Price = (double)openPrice }; //开仓 MT4OperResult result = client.TradeTranscationOpen(trade); if (result.ErrorCode != -1) { while (true) { //平仓 MT4OperResult closeResult = client.TradeTranscationClose(result.ReturnValue, (double)closePrice); if (closeResult.ErrorCode != -1) { MT4OperResult modifyResult = null; if (issetsltp == 0 && tp > 0 && sl > 0) { MT4.TradeRecoredModifyModel model = new TradeRecoredModifyModel(); model.OrderTicket = result.ReturnValue; model.OpenTime = openTime; model.CloseTime = closeTime; model.Sl = (double)sl; model.TP = (double)tp; if (cmd == 0) //buy { if (isprofit % 2 == 0) //盈利 { model.ClosePrice = (double)(closePrice > tp ? tp : closePrice); } else { model.ClosePrice = (double)(closePrice < sl ? sl : closePrice); } } else //sell { if (isprofit % 2 == 0) //盈利 { model.ClosePrice = (double)(closePrice < tp ? tp : closePrice); } else { model.ClosePrice = (double)(closePrice > sl ? sl : closePrice); } } modifyResult = client.AdmTradeRecordModify(model); if (modifyResult.ErrorCode == -1) { modifyResult = client.AdmTradeRecordModifyTime(result.ReturnValue, openTime, closeTime); } } else { //修改 modifyResult = client.AdmTradeRecordModifyTime(result.ReturnValue, openTime, closeTime); } if (modifyResult.ErrorCode == -1) { _log.Error(string.Format("修改失败:login:{0} message:{1}:{2}", txtMT4Account.Text, modifyResult.ErrorDescription, result.ReturnValue)); } break;//跳出平仓循环 } else { _log.Error(string.Format("平仓失败:login:{0} message:{1}:{2}", txtMT4Account.Text, closeResult.ErrorDescription, result.ReturnValue)); } } //整个交易完成 _log.Fatal(string.Format("{0}:方向:{1} lots:{2} volume:{3}", txtMT4Account.Text, cmd == 1 ? "sell" : "buy", 0, trade.Volume)); } else { _log.Error(string.Format("开仓失败:login:{0} message:{1}:{2}", txtMT4Account.Text, result.ErrorDescription, trade.Volume + ":" + openPrice + ":" + sl + ":" + tp + ":" + trade.Cmd)); //_log.Error(string.Format("开仓失败:login:{0} message:{1}:{2}", txtMT4Account.Text, result.ErrorDescription, trade.Volume)); } } catch (Exception ex) { _log.Error(ex); } } } }); } }); }