void _timer_Tick(object sender, EventArgs e) { lock (_locker) { if (IsHandleCreated) { Invoke(new Action(() => { TopMost = false; BringToFront(); TopMost = true; if (_savedItem != null) { if (DateTime.Now - _savedItem.Time > new TimeSpan(0, 0, 0, 0, 0)) { AddMessage(_savedItem); _savedItem = null; } } if (cbAutoOpen.Checked) { if (cbAutoOpen.ForeColor == Color.Black) { cbAutoOpen.ForeColor = Color.Red; } else { cbAutoOpen.ForeColor = Color.Black; } } else { cbAutoOpen.ForeColor = Color.Black; } if (Utils.IsTraderReady) { toolStripStatusLabel4.Text = "已连接"; toolStripStatusLabel4.ForeColor = Color.Blue; } else { toolStripStatusLabel4.Text = "已断开"; toolStripStatusLabel4.ForeColor = Color.Red; } toolStripStatusLabel3.Text = Utils.availableMoney.ToString("f2"); if (_trader != null && _trader.UnFinishedOrderFields != null) { lvOrder.Items.Clear(); foreach (var kv in _trader.UnFinishedOrderFields) { //cbAutoOpen.Checked = false; var item = new ListViewItem(); item.UseItemStyleForSubItems = false; var color = Color.Red; if (kv.Value.Direction == EnumDirectionType.Sell) { color = Color.Green; } var ins = kv.Value.InstrumentID; var subIns = item.SubItems.Add(ins); subIns.ForeColor = color; var price = kv.Value.LimitPrice; var subPrice = item.SubItems.Add(price.ToString("f1")); subPrice.ForeColor = color; if (Utils.InstrumentToLastTick.ContainsKey(ins)) { var lastTick = Utils.InstrumentToLastTick[ins]; var dis = lastTick.LastPrice - price; var subDis = item.SubItems.Add(dis.ToString("f1")); subDis.ForeColor = color; } lvOrder.Items.Add(item); } } if (_trader != null && _trader.PositionFields != null) { listView2.Items.Clear(); var listIns = new List <string>(); foreach (var kv in Utils.PositionKeyToHighLowProfit) { if (!_trader.PositionFields.ContainsKey(kv.Key)) { listIns.Add(kv.Key); } } listIns.ForEach(l => { HighLowProfit highLowProfit; Utils.PositionKeyToHighLowProfit.TryRemove(l, out highLowProfit); Utils.WriteLine(string.Format("去掉{0}的最高最低盈利...", l)); }); var totalProfit = 0.0; if (_trader.PositionFields.Count <= 0) { lbHighTotal.Text = "-99999"; lbLowTotal.Text = "99999"; } //if (_trader.UnFinishedOrderFields.Count == 0) //{ // cbEnable.Checked = true; //} //else //{ // cbEnable.Checked = false; //} var insAlreadyProcessed = new List <string>(); foreach (var kv in _trader.PositionFields) { //cbAutoOpen.Checked = false; if (!Utils.IsShfeInstrument(kv.Value.InstrumentID) && insAlreadyProcessed.Contains(kv.Value.InstrumentID)) { continue; } var color = Color.Black; var dir = kv.Value.PosiDirection; if (dir == EnumPosiDirectionType.Long) { color = Color.Red; } else { color = Color.Green; } var item = new ListViewItem(); item.UseItemStyleForSubItems = false; var ins = kv.Value.InstrumentID; insAlreadyProcessed.Add(ins); var subIns = item.SubItems.Add(ins); subIns.ForeColor = color; var subLongShort = item.SubItems.Add(dir == EnumPosiDirectionType.Long ? "多" : "空"); subLongShort.ForeColor = color; var volume = kv.Value.Position; var subVolume = item.SubItems.Add(volume.ToString()); subVolume.ForeColor = color; if (!Utils.InstrumentToLastTick.ContainsKey(ins)) { var list = new List <string>(); list.Add(ins); ((QuoteAdapter)Utils.QuoteMain).SubscribeMarketData(list.ToArray()); } if (Utils.InstrumentToLastTick.ContainsKey(ins) && Utils.InstrumentToInstrumentInfo.ContainsKey(ins)) { var info = Utils.InstrumentToInstrumentInfo[ins]; var lastTick = Utils.InstrumentToLastTick[ins]; var rangepoint = (int)(lastTick.LastPrice * Utils.范围 * info.VolumeMultiple); nudWarningPoint.Value = rangepoint; nudLossPoint.Value = rangepoint * 2; var cost = kv.Value.OpenCost / info.VolumeMultiple / volume; item.SubItems.Add(cost.ToString("f1")); double profitPoint = 0; if (dir == EnumPosiDirectionType.Long) { if (lastTick.BidPrice1.Equals(0) || Math.Abs(lastTick.BidPrice1) > 999999) { profitPoint = (lastTick.LastPrice - cost) / info.PriceTick; } else { profitPoint = (lastTick.BidPrice1 - cost) / info.PriceTick; } } else { if (lastTick.AskPrice1.Equals(0) || Math.Abs(lastTick.AskPrice1) > 999999) { profitPoint = (-1) * (lastTick.LastPrice - cost) / info.PriceTick; } else { profitPoint = (-1) * (lastTick.AskPrice1 - cost) / info.PriceTick; } } //Utils.WriteLine(string.Format("{0}:当前持仓倍数:{1},当前点数:{2}", ins, volume / Utils.OpenVolumePerTime, profitPoint), true); //if (kv.Value.Position > Utils.OpenVolumePerTime) //已经触发多个委托,争取保本平仓 //{ // if (profitPoint >= 1) // { // ClosePositionByItem(item, "保本离场", "保本离场"); // } //} var profit = profitPoint * info.PriceTick * info.VolumeMultiple * volume; var subProfit = item.SubItems.Add(profit.ToString("f0")); var subProfitPoint = item.SubItems.Add(profitPoint.ToString("f1")); if (cbEnable.Checked) { if (profitPoint > stopProfitPoint) { ClosePositionByItem(item, "多仓止盈", "空仓止盈"); } if (profitPoint < stopLossPoint) { ClosePositionByItem(item, "多仓止损", "空仓止损"); } if (profitPoint > 0.9 * stopProfitPoint) { nudProfitPoint.BackColor = Color.Red; } else { nudProfitPoint.BackColor = Color.White; } if (profitPoint < 0.9 * stopLossPoint) { nudLossPoint.BackColor = Color.Green; } else { nudLossPoint.BackColor = Color.White; } } if ((dir == EnumPosiDirectionType.Long && cost + profitPoint * info.PriceTick < cost * (1 - Utils.止损比例))) { //ClosePositionByItem(item, "多仓止损", "空仓止损"); if (!isPromptSent) { isPromptSent = true; Email.SendMessage(true, "liu7788414", "15800377605", string.Format("{0}止损报警!", ins)); } } if ((dir == EnumPosiDirectionType.Short && cost - profitPoint * info.PriceTick > cost * (1 + Utils.止损比例))) { //ClosePositionByItem(item, "多仓止损", "空仓止损"); if (!isPromptSent) { isPromptSent = true; Email.SendMessage(true, "liu7788414", "15800377605", string.Format("{0}止损报警!", ins)); } } if (profit > 0) { subProfit.ForeColor = Color.Red; subProfitPoint.ForeColor = Color.Red; } else { if (profit < 0) { subProfit.ForeColor = Color.Green; subProfitPoint.ForeColor = Color.Green; } else { subProfit.ForeColor = Color.Black; subProfitPoint.ForeColor = Color.Black; } } if (profitPoint > (double)nudWarningPoint.Value) { try { nudWarningPoint.Value = Convert.ToDecimal(profitPoint); } catch { } } HighLowProfit highLowProfit; if (Utils.PositionKeyToHighLowProfit.ContainsKey(kv.Key)) { highLowProfit = Utils.PositionKeyToHighLowProfit[kv.Key]; if (profit > highLowProfit.High) { highLowProfit.High = profit; highLowProfit.HighTick = profitPoint; Utils.WriteLine(string.Format("设置{0}最高为{1}", kv.Key, profit)); } else { if (profit < highLowProfit.Low) { highLowProfit.Low = profit; highLowProfit.LowTick = profitPoint; Utils.WriteLine(string.Format("设置{0}最低为{1}", kv.Key, profit)); } } } else { highLowProfit = new HighLowProfit(); if (profit >= 0) { highLowProfit.High = profit; highLowProfit.HighTick = profitPoint; highLowProfit.Low = 0; highLowProfit.LowTick = 0; } else { highLowProfit.High = 0; highLowProfit.HighTick = 0; highLowProfit.Low = profit; highLowProfit.LowTick = profitPoint; } Utils.PositionKeyToHighLowProfit[kv.Key] = highLowProfit; Utils.WriteLine(string.Format("创建{0}最高最低盈利...", kv.Key)); } var subHigh = item.SubItems.Add(Math.Round(highLowProfit.High).ToString("f0")); subHigh.ForeColor = Color.Red; var subHighTick = item.SubItems.Add(Math.Round(highLowProfit.HighTick).ToString("f1")); subHighTick.ForeColor = Color.Red; var subLow = item.SubItems.Add(Math.Round(highLowProfit.Low).ToString("f0")); subLow.ForeColor = Color.Green; var subLowTick = item.SubItems.Add(Math.Round(highLowProfit.LowTick).ToString("f1")); subLowTick.ForeColor = Color.Green; if (highLowProfit.Low <= -4000) { subLow.BackColor = Color.Yellow; } //if (highLowProfit.HighTick >= 10 && profitPoint <= 2) //{ // ClosePositionByItem(item, "大回撤保本离场", "大回撤保本离场"); //} if (cbWarning.Checked && highLowProfit.HighTick >= warningTick) { subHigh.BackColor = Color.Yellow; if (profitPoint >= Convert.ToDouble(nudWarningPoint.Value)) { Utils.WriteLine(string.Format("当前价格{0},当前点数{1},当前最高点数{2}", lastTick.LastPrice, profitPoint, highLowProfit.HighTick), true); ClosePositionByItem(item, "警戒线止盈", "警戒线止盈"); } } if (!string.IsNullOrEmpty(kv.Value.TradingDay)) { try { var subInsValue = item.SubItems.Add((lastTick.LastPrice * volume * info.VolumeMultiple).ToString()); } catch { } } else { var subInsValue = item.SubItems.Add("0"); } var upRatio = (lastTick.LastPrice - lastTick.OpenPrice) / lastTick.OpenPrice; var subupRatio = item.SubItems.Add(upRatio.ToString("P")); if (upRatio > 0.01) { subupRatio.ForeColor = Color.Red; } else { if (upRatio < -0.01) { subupRatio.ForeColor = Color.Green; } } totalProfit += profit; } listView2.Items.Add(item); } textBox1.Text = totalProfit.ToString("f2"); if (totalProfit > 0) { textBox1.ForeColor = Color.Red; } else { if (totalProfit < 0) { textBox1.ForeColor = Color.Green; } else { textBox1.ForeColor = Color.Black; } } if (!totalProfit.Equals(0) && (totalProfit > Convert.ToDouble(lbHighTotal.Text))) { lbHighTotal.Text = totalProfit.ToString("f2"); } if (!totalProfit.Equals(0) && (totalProfit < Convert.ToDouble(lbLowTotal.Text))) { lbLowTotal.Text = totalProfit.ToString("f2"); } if (cbEnableTotal.Checked) { if (totalProfit > stopProfitTotal || totalProfit < stopLossTotal) { CloseAll("总盈利平仓", "总盈利平仓"); } } } else { lbHighTotal.Text = "-99999"; lbLowTotal.Text = "99999"; } })); } } }
void _timer_Tick(object sender, EventArgs e) { lock (_locker) { if (IsHandleCreated) { Invoke(new Action(() => { TopMost = false; BringToFront(); TopMost = true; if (cbAutoOpen.Checked) { if (cbAutoOpen.ForeColor == Color.Black) { cbAutoOpen.ForeColor = Color.Red; } else { cbAutoOpen.ForeColor = Color.Black; } } else { cbAutoOpen.ForeColor = Color.Black; } if (Utils.IsTraderReady) { toolStripStatusLabel4.Text = "已连接"; toolStripStatusLabel4.ForeColor = Color.Blue; } else { toolStripStatusLabel4.Text = "已断开"; toolStripStatusLabel4.ForeColor = Color.Red; } toolStripStatusLabel3.Text = Utils.availableMoney.ToString("f2"); if (_trader != null && _trader.UnFinishedOrderFields != null) { lvOrder.Items.Clear(); foreach (var kv in _trader.UnFinishedOrderFields) { cbAutoOpen.Checked = false; var item = new ListViewItem(); item.UseItemStyleForSubItems = false; var color = Color.Red; if (kv.Value.Direction == EnumDirectionType.Sell) { color = Color.Green; } var ins = kv.Value.InstrumentID; var subIns = item.SubItems.Add(ins); subIns.ForeColor = color; var price = kv.Value.LimitPrice; var subPrice = item.SubItems.Add(price.ToString("f1")); subPrice.ForeColor = color; if (Utils.InstrumentToLastTick.ContainsKey(ins)) { var lastTick = Utils.InstrumentToLastTick[ins]; var dis = lastTick.LastPrice - price; var subDis = item.SubItems.Add(dis.ToString("f1")); subDis.ForeColor = color; } lvOrder.Items.Add(item); } } if (_trader != null && _trader.PositionFields != null) { listView2.Items.Clear(); var listIns = new List <string>(); foreach (var kv in Utils.PositionKeyToHighLowProfit) { if (!_trader.PositionFields.ContainsKey(kv.Key)) { listIns.Add(kv.Key); } } listIns.ForEach(l => { HighLowProfit highLowProfit; Utils.PositionKeyToHighLowProfit.TryRemove(l, out highLowProfit); Utils.WriteLine(string.Format("去掉{0}的最高最低盈利...", l)); }); var totalProfit = 0.0; if (_trader.PositionFields.Count <= 0) { lbHighTotal.Text = lbLowTotal.Text = "0"; } foreach (var kv in _trader.PositionFields) { cbAutoOpen.Checked = false; var color = Color.Black; var dir = kv.Value.PosiDirection; if (dir == EnumPosiDirectionType.Long) { color = Color.Red; } else { color = Color.Green; } var item = new ListViewItem(); item.UseItemStyleForSubItems = false; var ins = kv.Value.InstrumentID; var subIns = item.SubItems.Add(ins); subIns.ForeColor = color; var subLongShort = item.SubItems.Add(dir == EnumPosiDirectionType.Long ? "多" : "空"); subLongShort.ForeColor = color; var volume = kv.Value.Position; var subVolume = item.SubItems.Add(volume.ToString()); subVolume.ForeColor = color; if (!Utils.InstrumentToLastTick.ContainsKey(ins)) { var list = new List <string>(); list.Add(ins); ((QuoteAdapter)Utils.QuoteMain).SubscribeMarketData(list.ToArray()); } if (Utils.InstrumentToLastTick.ContainsKey(ins) && Utils.InstrumentToInstrumentInfo.ContainsKey(ins)) { var lastTick = Utils.InstrumentToLastTick[ins]; var info = Utils.InstrumentToInstrumentInfo[ins]; var cost = kv.Value.OpenCost / info.VolumeMultiple / volume; item.SubItems.Add(cost.ToString("f1")); double profitPoint = 0; if (dir == EnumPosiDirectionType.Long) { profitPoint = (lastTick.BidPrice1 - cost) / info.PriceTick; } else { profitPoint = (-1) * (lastTick.AskPrice1 - cost) / info.PriceTick; } var profit = profitPoint * info.PriceTick * info.VolumeMultiple * volume; var subProfit = item.SubItems.Add(profit.ToString("f0")); var subProfitPoint = item.SubItems.Add(profitPoint.ToString("f1")); if (cbEnable.Checked) { if (profitPoint > stopProfitPoint) { ClosePositionByItem(item, "多仓止盈", "空仓止盈"); } if (profitPoint < stopLossPoint) { ClosePositionByItem(item, "多仓止损", "空仓止损"); } if (profitPoint > 0.9 * stopProfitPoint) { nudProfitPoint.BackColor = Color.Red; } else { nudProfitPoint.BackColor = Color.White; } if (profitPoint < 0.9 * stopLossPoint) { nudLossPoint.BackColor = Color.Green; } else { nudLossPoint.BackColor = Color.White; } } if (profit > 0) { subProfit.ForeColor = Color.Red; subProfitPoint.ForeColor = Color.Red; } else { if (profit < 0) { subProfit.ForeColor = Color.Green; subProfitPoint.ForeColor = Color.Green; } else { subProfit.ForeColor = Color.Black; subProfitPoint.ForeColor = Color.Black; } } HighLowProfit highLowProfit; if (Utils.PositionKeyToHighLowProfit.ContainsKey(kv.Key)) { highLowProfit = Utils.PositionKeyToHighLowProfit[kv.Key]; if (profit > highLowProfit.High) { highLowProfit.High = profit; highLowProfit.HighTick = profitPoint; Utils.WriteLine(string.Format("设置{0}最高为{1}", kv.Key, profit)); } else { if (profit < highLowProfit.Low) { highLowProfit.Low = profit; highLowProfit.LowTick = profitPoint; Utils.WriteLine(string.Format("设置{0}最低为{1}", kv.Key, profit)); } } } else { highLowProfit = new HighLowProfit(); if (profit >= 0) { highLowProfit.High = profit; highLowProfit.HighTick = profitPoint; highLowProfit.Low = 0; highLowProfit.LowTick = 0; } else { highLowProfit.High = 0; highLowProfit.HighTick = 0; highLowProfit.Low = profit; highLowProfit.LowTick = profitPoint; } Utils.PositionKeyToHighLowProfit[kv.Key] = highLowProfit; Utils.WriteLine(string.Format("创建{0}最高最低盈利...", kv.Key)); } var subHigh = item.SubItems.Add(Math.Round(highLowProfit.High).ToString("f0")); subHigh.ForeColor = Color.Red; var subHighTick = item.SubItems.Add(Math.Round(highLowProfit.HighTick).ToString("f1")); subHighTick.ForeColor = Color.Red; var subLow = item.SubItems.Add(Math.Round(highLowProfit.Low).ToString("f0")); subLow.ForeColor = Color.Green; var subLowTick = item.SubItems.Add(Math.Round(highLowProfit.LowTick).ToString("f1")); subLowTick.ForeColor = Color.Green; if (highLowProfit.Low <= -400) { subLow.BackColor = Color.Yellow; } if (cbWarning.Checked && highLowProfit.HighTick >= warningTick) { subHigh.BackColor = Color.Yellow; if (profitPoint <= highLowProfit.HighTick * closeRatio) { ClosePositionByItem(item, "警戒线止盈", "警戒线止盈"); } } if (!string.IsNullOrEmpty(kv.Value.TradingDay)) { try { var time = Convert.ToDateTime(kv.Value.TradingDay); var timeSpan = DateTime.Now - time; var subTime = item.SubItems.Add(timeSpan.TotalMinutes.ToString("f2")); if (timeSpan > new TimeSpan(0, 5, 0)) { subTime.BackColor = Color.Red; } if (timeSpan > new TimeSpan(0, 8, 0)) { subTime.BackColor = Color.Violet; } if (cbOverTime.Checked && timeSpan >= new TimeSpan(0, 10, 0) && profitPoint < overtimePoint) { ClosePositionByItem(item, "超时未盈利平仓", "超时未盈利平仓"); } } catch { } } else { var subTime = item.SubItems.Add("0"); } var upRatio = (lastTick.LastPrice - lastTick.OpenPrice) / lastTick.OpenPrice; var subupRatio = item.SubItems.Add(upRatio.ToString("P")); if (upRatio > 0.01) { subupRatio.ForeColor = Color.Red; } else { if (upRatio < -0.01) { subupRatio.ForeColor = Color.Green; } } totalProfit += profit; } listView2.Items.Add(item); } textBox1.Text = totalProfit.ToString("f2"); if (totalProfit > 0) { textBox1.ForeColor = Color.Red; if (totalProfit > Convert.ToDouble(lbHighTotal.Text)) { lbHighTotal.Text = totalProfit.ToString("f2"); } } else { if (totalProfit < 0) { textBox1.ForeColor = Color.Green; if (totalProfit < Convert.ToDouble(lbLowTotal.Text)) { lbLowTotal.Text = totalProfit.ToString("f2"); } } else { textBox1.ForeColor = Color.Black; } } if (cbEnableTotal.Checked) { if (totalProfit > stopProfitTotal || totalProfit < stopLossTotal) { CloseAll("总盈利平仓", "总盈利平仓"); } } } else { lbHighTotal.Text = lbLowTotal.Text = "0"; } })); } } }