private void BuyOrSell(ThostFtdcDepthMarketDataField data)
        {
            try
            {
                var instrumentId = data.InstrumentID;
                var dateTime     = Convert.ToDateTime(data.UpdateTime);
                var currentSwing = (data.HighestPrice - data.LowestPrice) / data.PreClosePrice;

                if (dateTime.Hour == 14 && dateTime.Minute == 59 && dateTime.Second >= 50)
                {
                    var bProcessed = false;

                    Utils.WriteLine(string.Format("当前价:{0},前收价:{1},开盘价:{2},最高价:{3},最低价:{4}", data.LastPrice, data.PreClosePrice, data.OpenPrice, data.HighestPrice, data.LowestPrice), true);

                    if (data.LastPrice < data.PreClosePrice && data.LastPrice < data.OpenPrice && _trader.ContainsPositionByInstrument(instrumentId, EnumPosiDirectionType.Long) && data.LastPrice < data.LowestPrice * 1.01)
                    {
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, "平多", false, 0);
                        Thread.Sleep(2000);
                    }

                    if (data.LastPrice > data.PreClosePrice && data.LastPrice > data.OpenPrice && _trader.ContainsPositionByInstrument(instrumentId, EnumPosiDirectionType.Short) && data.LastPrice > data.HighestPrice * 0.99)
                    {
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, "平空", false, 9999);
                        Thread.Sleep(2000);
                    }

                    if (data.LastPrice / data.OpenPrice > 1.0095 && data.LastPrice > data.HighestPrice * 0.99)
                    {
                        var reason = string.Format("{0}收盘看多信号,开多", instrumentId);
                        if (noOpening)
                        {
                            noOpening = false;
                            _trader.OpenLongPositionByInstrument(instrumentId, reason, 0, true, true, 0, data.UpperLimitPrice);
                            bProcessed = true;
                            noOpening  = true;
                        }
                        else
                        {
                            Utils.WriteLine("正在开仓", true);
                        }
                    }

                    if (data.LastPrice / data.OpenPrice < 0.9905 && data.LastPrice < data.LowestPrice * 1.01)
                    {
                        var reason = string.Format("{0}收盘看空信号,开空", instrumentId);
                        if (noOpening)
                        {
                            _trader.OpenShortPositionByInstrument(instrumentId, reason, 9999, true, true, 0, data.LowerLimitPrice);
                            bProcessed = true;
                            noOpening  = true;
                        }
                        else
                        {
                            Utils.WriteLine("正在开仓", true);
                        }
                    }

                    if (bProcessed)
                    {
                        _timerOrder.Stop();
                    }
                }
                else
                {
                    var highestPriceFile = Utils.AssemblyPath + "highestPrice.ini";
                    if (_trader.ContainsPositionByInstrument(instrumentId, EnumPosiDirectionType.Long))
                    {
                        //读取持仓高点
                        var highestPrice = data.HighestPrice;

                        if (File.Exists(highestPriceFile))
                        {
                            bool store = false;
                            var  sr    = new StreamReader(highestPriceFile, Encoding.UTF8);
                            var  line  = sr.ReadLine();
                            if (line != null)
                            {
                                var storedPrice = Convert.ToDouble(line);
                                if (storedPrice < highestPrice)
                                {
                                    store = true;
                                }
                                else
                                {
                                    highestPrice = storedPrice;
                                }
                            }
                            sr.Close();

                            if (store)
                            {
                                Utils.StorePrice(highestPriceFile, highestPrice);
                            }
                        }
                        else
                        {
                            Utils.StorePrice(highestPriceFile, data.HighestPrice);
                        }

                        if (data.LastPrice / highestPrice < 0.982 && currentSwing > 0.02)
                        {
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "最高价回落,平多", false, 0);
                        }

                        if (data.LastPrice / data.OpenPrice < 0.982)
                        {
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "平多", false, 0);
                        }
                    }
                    else
                    {
                        Utils.DeleteStorePrice(highestPriceFile);
                    }

                    var lowestPriceFile = Utils.AssemblyPath + "lowestPrice.ini";
                    if (_trader.ContainsPositionByInstrument(instrumentId, EnumPosiDirectionType.Short))
                    {
                        //读取持仓低点
                        var lowestPrice = data.LowestPrice;

                        if (File.Exists(lowestPriceFile))
                        {
                            bool store = false;
                            var  sr    = new StreamReader(lowestPriceFile, Encoding.UTF8);
                            var  line  = sr.ReadLine();
                            if (line != null)
                            {
                                var storedPrice = Convert.ToDouble(line);
                                if (storedPrice > lowestPrice)
                                {
                                    store = true;
                                }
                                else
                                {
                                    lowestPrice = storedPrice;
                                }
                            }
                            sr.Close();

                            if (store)
                            {
                                Utils.StorePrice(lowestPriceFile, lowestPrice);
                            }
                        }
                        else
                        {
                            Utils.StorePrice(lowestPriceFile, data.LowestPrice);
                        }

                        if (data.LastPrice / data.LowestPrice > 1.018 && currentSwing > 0.02)
                        {
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "最低价上涨,平空", false, 9999);
                        }

                        if (data.LastPrice / data.OpenPrice > 1.018)
                        {
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "平空", false, 9999);
                        }
                    }
                    else
                    {
                        Utils.DeleteStorePrice(lowestPriceFile);
                    }

                    if (data.LastPrice / data.OpenPrice > 1.015 && data.LastPrice > data.HighestPrice * 0.99)
                    {
                        var reason = string.Format("{0}盘中看多信号,开多", instrumentId);
                        if (noOpening)
                        {
                            noOpening = false;
                            _trader.OpenLongPositionByInstrument(instrumentId, reason, 0, true, true, 0, data.UpperLimitPrice);
                            noOpening = true;
                        }
                        else
                        {
                            Utils.WriteLine("正在开仓", true);
                        }
                    }

                    if (data.LastPrice / data.OpenPrice < 0.985 && data.LastPrice < data.LowestPrice * 1.01)
                    {
                        var reason = string.Format("{0}盘中看空信号,开空", instrumentId);
                        if (noOpening)
                        {
                            noOpening = false;
                            _trader.OpenShortPositionByInstrument(instrumentId, reason, 9999, true, true, 0, data.LowerLimitPrice);
                            noOpening = true;
                        }
                        else
                        {
                            Utils.WriteLine("正在开仓", true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }
        private void BuyOrSell(ThostFtdcDepthMarketDataField data)
        {
            try
            {
                if (Utils.InstrumentToStopLossPrices.ContainsKey(data.InstrumentID))
                {
                    var stopLossPrices = Utils.InstrumentToStopLossPrices[data.InstrumentID];

                    var stopLossValue = Utils.绝对止损点数;

                    //多仓止损
                    if (data.LastPrice < stopLossPrices.CostLong - stopLossValue)
                    {
                        var reason = string.Format("{0}从多仓的成本价{1}跌到了绝对止损值{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.CostLong, stopLossValue, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason, false, 0);
                    }

                    //空仓止损
                    if (data.LastPrice > stopLossPrices.CostShort + stopLossValue)
                    {
                        var reason = string.Format("{0}从空仓的成本价{1}涨到了绝对止损值{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.CostShort, stopLossValue, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason, false, 99999);
                    }

                    var longDistance               = stopLossPrices.ForLong - stopLossPrices.CostLong;
                    var currentLongDistance        = data.LastPrice - stopLossPrices.CostLong;
                    var movingStopLossValueForLong = longDistance * 0.5;

                    //多仓止损
                    if (longDistance > 10 && longDistance <= 20 && data.LastPrice < stopLossPrices.ForLong - movingStopLossValueForLong)
                    {
                        var reason = string.Format("{0}从多仓的最高盈利价{1}跌到了移动止损价{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.ForLong, stopLossPrices.ForLong - movingStopLossValueForLong, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason, false, 0);
                    }
                    else
                    {
                        if (longDistance > 20 && currentLongDistance < 10)
                        {
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "移动止损,平掉多仓", false, 0);
                        }
                    }

                    var shortDistance               = stopLossPrices.CostShort - stopLossPrices.ForShort;
                    var currentShortDistance        = stopLossPrices.CostShort - data.LastPrice;
                    var movingStopLossValueForShort = shortDistance * 0.5;

                    //空仓止损
                    if (shortDistance > 10 && shortDistance <= 20 && data.LastPrice > stopLossPrices.ForShort + movingStopLossValueForShort)
                    {
                        var reason = string.Format("{0}从空仓的最高盈利价{1}涨到了移动止损价{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.ForShort, stopLossPrices.ForShort + movingStopLossValueForShort, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason, false, 99999);
                    }
                    else
                    {
                        if (shortDistance > 20 && currentShortDistance < 10)
                        {
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "移动止损,平掉空仓", false, 99999);
                        }
                    }

                    if (stopLossPrices.ForLong - stopLossPrices.CostLong >= 20)
                    {
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, "多仓止盈", false, 0);
                    }

                    if (stopLossPrices.CostShort - stopLossPrices.ForShort >= 20)
                    {
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, "空仓止盈", false, 99999);
                    }

                    var stop = Utils.立即平仓点数;

                    if (data.LastPrice - stopLossPrices.CostLong >= stop)
                    {
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, "立即平多仓", false, stopLossPrices.CostLong + Utils.立即平仓点数);
                    }

                    if (stopLossPrices.CostShort - data.LastPrice >= stop)
                    {
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, "立即平空仓", false, stopLossPrices.CostShort - Utils.立即平仓点数);
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }
        private void BuyOrSell(ThostFtdcDepthMarketDataField data)
        {
            if (!Utils.IsInInstrumentTradingTime(data.InstrumentID))
            {
                return;
            }

            try
            {
                if (Utils.InstrumentUpDownCount.ContainsKey(data.InstrumentID))
                {
                    var countXX = Utils.InstrumentUpDownCount[data.InstrumentID];

                    //上涨信号出现,平空仓 开多仓
                    if (countXX.UpCount >= 3)
                    {
                        var reason = string.Format("{0}看多信号,平空", data.InstrumentID);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                        //暂时不设置涨跌幅条件限制
                        //if ((data.LastPrice / data.PreClosePrice) > 0.98)
                        //{
                        reason = string.Format("{0}看多信号,开多", data.InstrumentID);
                        _trader.OpenLongPositionByInstrument(data.InstrumentID, reason, 0, true, true, 0);
                        //}
                    }
                    //下跌信号出现,平多仓 开空仓
                    if (countXX.DownCount >= 3)
                    {
                        var reason = string.Format("{0}看空信号,平多", data.InstrumentID);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);

                        //if ((data.LastPrice / data.PreClosePrice) < 1.02)
                        //{
                        reason = string.Format("{0}看空信号,开空", data.InstrumentID);
                        _trader.OpenShortPositionByInstrument(data.InstrumentID, reason, 9999, true, true, 0);
                        //}
                    }
                }

                if (Utils.InstrumentToStopLossPrices.ContainsKey(data.InstrumentID))
                {
                    var stopLossPrices = Utils.InstrumentToStopLossPrices[data.InstrumentID];

                    var stopLossValue = 10;

                    //多仓止损
                    if (data.LastPrice < stopLossPrices.CostLong - stopLossValue)
                    {
                        var reason = string.Format("{0}从多仓的成本价{1}跌到了绝对止损值{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.CostLong, stopLossValue, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                    }

                    //空仓止损
                    if (data.LastPrice > stopLossPrices.CostShort + stopLossValue)
                    {
                        var reason = string.Format("{0}从空仓的成本价{1}涨到了绝对止损值{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.CostShort, stopLossValue, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                    }

                    var longDistance               = stopLossPrices.ForLong - stopLossPrices.CostLong;
                    var currentLongDistance        = data.LastPrice - stopLossPrices.CostLong;
                    var movingStopLossValueForLong = longDistance * 0.5;

                    //多仓止损
                    if (longDistance > 10 && longDistance <= 20 && data.LastPrice < stopLossPrices.ForLong - movingStopLossValueForLong)
                    {
                        var reason = string.Format("{0}从多仓的最高盈利价{1}跌到了移动止损价{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.ForLong, stopLossPrices.ForLong - movingStopLossValueForLong, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                    }
                    else
                    {
                        if (longDistance > 20 && currentLongDistance < 10)
                        {
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "移动止损,平掉多仓");
                        }
                    }

                    var shortDistance               = stopLossPrices.CostShort - stopLossPrices.ForShort;
                    var currentShortDistance        = stopLossPrices.CostShort - data.LastPrice;
                    var movingStopLossValueForShort = shortDistance * 0.5;

                    //空仓止损
                    if (shortDistance > 10 && shortDistance <= 20 && data.LastPrice > stopLossPrices.ForShort + movingStopLossValueForShort)
                    {
                        var reason = string.Format("{0}从空仓的最高盈利价{1}涨到了移动止损价{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.ForShort, stopLossPrices.ForShort + movingStopLossValueForShort, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                    }
                    else
                    {
                        if (shortDistance > 20 && currentShortDistance < 10)
                        {
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "移动止损,平掉空仓");
                        }
                    }

                    if (stopLossPrices.ForLong - stopLossPrices.CostLong >= 20)
                    {
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, "多仓止盈");
                    }

                    if (stopLossPrices.CostShort - stopLossPrices.ForShort >= 20)
                    {
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, "空仓止盈");
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }
        private void BuyOrSell(ThostFtdcDepthMarketDataField data)
        {
            try
            {
                #region 平仓策略

                if (Utils.InstrumentToStopLossPrices.ContainsKey(data.InstrumentID))
                {
                    var stopLossPrices = Utils.InstrumentToStopLossPrices[data.InstrumentID];

                    var stopLossValue = 2;

                    //多仓止损
                    if (data.LastPrice < stopLossPrices.CostLong - stopLossValue)
                    {
                        var reason = string.Format("{0}从多仓的成本价{1}跌到了绝对止损值{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.CostLong, stopLossValue, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                    }

                    //空仓止损
                    if (data.LastPrice > stopLossPrices.CostShort + stopLossValue)
                    {
                        var reason = string.Format("{0}从空仓的成本价{1}涨到了绝对止损值{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.CostShort, stopLossValue, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                    }

                    var minuteByMinute = Utils.InstrumentToMinuteByMinuteChart[data.InstrumentID];

                    Tuple <bool, double, double> isPointingUpMinuteLong2   = new Tuple <bool, double, double>(false, 0, 0);
                    Tuple <bool, double, double> isPointingDownMinuteLong2 = new Tuple <bool, double, double>(false, 0, 0);
                    Tuple <bool, double, double> isPointingUpMinuteHalf    = new Tuple <bool, double, double>(false, 0, 0);
                    Tuple <bool, double, double> isPointingDownMinuteHalf  = new Tuple <bool, double, double>(false, 0, 0);

                    if (minuteByMinute.Count >= Utils.MinuteByMinuteSizeLong)
                    {
                        var sizeHalf = Utils.MinuteByMinuteSizeLong / 2;
                        var count    = minuteByMinute.Count;

                        var minuteByMinuteQuotesLong = new List <double>();
                        for (var i = count - Utils.MinuteByMinuteSizeLong; i < count; i++)
                        {
                            if (minuteByMinute[i] != null)
                            {
                                minuteByMinuteQuotesLong.Add(minuteByMinute[i].Item2.LastPrice);
                            }
                        }

                        var minuteByMinuteQuotesHalf = new List <double>();
                        for (var i = count - sizeHalf; i < count; i++)
                        {
                            if (minuteByMinute[i] != null)
                            {
                                minuteByMinuteQuotesHalf.Add(minuteByMinute[i].Item2.LastPrice);
                            }
                        }

                        isPointingUpMinuteLong2 = MathUtils.IsPointingUp(Utils.MinuteLongXData,
                                                                         minuteByMinuteQuotesLong, MathUtils.Slope2);
                        isPointingDownMinuteLong2 = MathUtils.IsPointingDown(Utils.MinuteLongXData,
                                                                             minuteByMinuteQuotesLong, MathUtils.Slope2);

                        Utils.WriteLine(string.Format("检查当前长角度{0}", isPointingUpMinuteLong2.Item3));

                        var minuteHalfXData = new List <double>();

                        for (var i = 0; i < sizeHalf; i++)
                        {
                            minuteHalfXData.Add(i);
                        }

                        isPointingUpMinuteHalf = MathUtils.IsPointingUp(minuteHalfXData, minuteByMinuteQuotesHalf,
                                                                        MathUtils.Slope2);
                        isPointingDownMinuteHalf = MathUtils.IsPointingDown(minuteHalfXData, minuteByMinuteQuotesHalf,
                                                                            MathUtils.Slope2);
                        Utils.WriteLine(string.Format("检查当前短角度{0}", isPointingUpMinuteHalf.Item3));
                    }

                    double openTrendStartPoint = 0;

                    //从多仓的最高盈利跌了一定幅度,平掉多仓,保护盈利,忽略掉小波动
                    if (stopLossPrices.CostLong > 0 && stopLossPrices.ForLong > stopLossPrices.CostLong &&
                        stopLossPrices.ForLong > data.LastPrice)
                    {
                        var keyLongPosition = Utils.GetOpenTrendStartPointKey(data.InstrumentID,
                                                                              EnumPosiDirectionType.Long);

                        if (Utils.InstrumentToOpenTrendStartPoint.ContainsKey(keyLongPosition))
                        {
                            openTrendStartPoint = Utils.InstrumentToOpenTrendStartPoint[keyLongPosition];
                        }

                        var highestDistance = stopLossPrices.ForLong - stopLossPrices.CostLong;
                        var currentDistance = data.LastPrice - stopLossPrices.CostLong;

                        var keyLongAngle = Utils.GetOpenPositionKey(data.InstrumentID, EnumDirectionType.Buy);
                        //double limitAngle = 0;
                        //if (Utils.InstrumentToOpenAngle.ContainsKey(keyLongAngle))
                        //{
                        //    limitAngle = Utils.InstrumentToOpenAngle[keyLongAngle];
                        //}

                        if (currentDistance >= 2 && isPointingUpMinuteLong2.Item3 < MathUtils.Slope2)
                        {
                            var reason = string.Format("{0}的多仓开仓角度开始减小,平掉多仓,当前角{1},界限角{2}", data.InstrumentID, isPointingUpMinuteLong2.Item3, MathUtils.Slope2);
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                        }

                        //if (isPointingUpMinuteLong2.Item3 > limitAngle)
                        //{
                        //    Utils.InstrumentToOpenAngle[keyLongAngle] = limitAngle;
                        //}

                        if (highestDistance >= 5 && isPointingUpMinuteHalf.Item3 < MathUtils.Slope2 / 2)
                        {
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "多仓止盈");
                            Thread.Sleep(2000);
                            Utils.WriteLine("盈利目标达到,退出...", true);
                            Email.SendMail("盈利目标达到,退出...", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                            Utils.Exit(_trader);
                        }
                    }

                    //从空仓的最高盈利跌了一半,平掉空仓,保护盈利,忽略掉小波动
                    if (stopLossPrices.CostShort > 0 && stopLossPrices.ForShort < stopLossPrices.CostShort &&
                        stopLossPrices.ForShort < data.LastPrice)
                    {
                        var keyShortPosition = Utils.GetOpenTrendStartPointKey(data.InstrumentID,
                                                                               EnumPosiDirectionType.Short);

                        if (Utils.InstrumentToOpenTrendStartPoint.ContainsKey(keyShortPosition))
                        {
                            openTrendStartPoint = Utils.InstrumentToOpenTrendStartPoint[keyShortPosition];
                        }

                        var highestDistance = stopLossPrices.CostShort - stopLossPrices.ForShort;
                        var currentDistance = stopLossPrices.CostShort - data.LastPrice;

                        var keyShortAngle = Utils.GetOpenPositionKey(data.InstrumentID, EnumDirectionType.Sell);
                        //double limitAngle = 0;
                        //if (Utils.InstrumentToOpenAngle.ContainsKey(keyShortAngle))
                        //{
                        //    limitAngle = Utils.InstrumentToOpenAngle[keyShortAngle];
                        //}

                        if (currentDistance >= 2 && isPointingDownMinuteLong2.Item3 > -MathUtils.Slope2)
                        {
                            var reason = string.Format("{0}的空仓开仓角度开始减小,平掉空仓,当前角{1},界限角{2}", data.InstrumentID, isPointingDownMinuteLong2.Item3, -MathUtils.Slope2);
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                        }

                        //if (isPointingDownMinuteLong2.Item3 < limitAngle)
                        //{
                        //    Utils.InstrumentToOpenAngle[keyShortAngle] = limitAngle;
                        //}

                        if (highestDistance >= 5 && isPointingDownMinuteHalf.Item3 > -MathUtils.Slope2 / 2)
                        {
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "空仓止盈");
                            Thread.Sleep(2000);
                            Utils.WriteLine("盈利目标达到,退出...", true);
                            Email.SendMail("盈利目标达到,退出...", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                            Utils.Exit(_trader);
                        }
                    }
                }

                //接近涨停价,平掉空仓
                var upperLimitRange = (data.UpperLimitPrice + data.LowerLimitPrice) / 2;
                var lowerLimitRange = upperLimitRange;

                if (data.LastPrice > data.PreSettlementPrice + upperLimitRange * Utils.LimitCloseRange)
                {
                    var reason = string.Format("{0}最新价{1}上涨到了涨停价{2}的{3}以上,平掉空仓", data.InstrumentID, data.LastPrice,
                                               data.UpperLimitPrice, Utils.LimitCloseRange);
                    _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                }

                //接近跌停价,平掉多仓
                if (data.LastPrice < data.PreSettlementPrice - lowerLimitRange * Utils.LimitCloseRange)
                {
                    var reason = string.Format("{0}最新价{1}下跌到了跌停价{2}的{3}以下,平掉多仓", data.InstrumentID, data.LastPrice,
                                               data.LowerLimitPrice, Utils.LimitCloseRange);
                    _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                }

                #endregion

                #region 开仓策略

                if (Utils.InstrumentToMinuteByMinuteChart.ContainsKey(data.InstrumentID))
                {
                    lock (Utils.Locker2)
                    {
                        OpenStrategy(data, false);
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }
Exemplo n.º 5
0
        private void CheckOpenOrClose()
        {
            try
            {
                if (Utils.InstrumentToLastTick.ContainsKey(Utils.A1) && Utils.InstrumentToLastTick.ContainsKey(Utils.A2) && Utils.IsInInstrumentTradingTime(Utils.A1) && Utils.IsInInstrumentTradingTime(Utils.A2))
                {
                    var A1LastTick = Utils.InstrumentToLastTick[Utils.A1];
                    var A2LastTick = Utils.InstrumentToLastTick[Utils.A2];

                    var a1 = Utils.InstrumentToLastTick[Utils.A1].LastPrice;
                    var a2 = Utils.InstrumentToLastTick[Utils.A2].LastPrice;
                    var currentPriceDiff = a1 - a2;

                    if (currentPriceDiff < lowestPriceDiff)
                    {
                        lowestPriceDiff = currentPriceDiff;
                    }

                    if (currentPriceDiff > highestPriceDiff)
                    {
                        highestPriceDiff = currentPriceDiff;
                    }

                    if (priceDiffToCount.ContainsKey(currentPriceDiff))
                    {
                        priceDiffToCount[currentPriceDiff]++;
                    }
                    else
                    {
                        priceDiffToCount[currentPriceDiff] = 1;
                    }


                    double total = 0;
                    double count = 0;
                    foreach (var kv in priceDiffToCount)
                    {
                        total += kv.Key * kv.Value;
                        count += kv.Value;
                    }

                    averagePriceDiff = total / count;

                    Utils.WriteLine(string.Format("价差:{0:N2} - {1:N2} = {2:N2}, 平均价差:{3:N2}, 最低最高价差:{4:N2}, {5:N2}", a1, a2, currentPriceDiff, averagePriceDiff, lowestPriceDiff, highestPriceDiff), true);

                    if (a1 - a2 >= Utils.价差上限)  //卖a1买a2
                    {
                        if (!_trader.ContainsPositionByInstrument(Utils.A1, EnumPosiDirectionType.Short))
                        {
                            _trader.ReqOrderInsert(Utils.A1, EnumDirectionType.Sell, A1LastTick.LowerLimitPrice, Utils.OpenVolumePerTime, EnumOffsetFlagType.Open, EnumTimeConditionType.GFD, EnumVolumeConditionType.AV, "卖a1");
                            _trader.ReqOrderInsert(Utils.A2, EnumDirectionType.Buy, A2LastTick.UpperLimitPrice, Utils.OpenVolumePerTime, EnumOffsetFlagType.Open, EnumTimeConditionType.GFD, EnumVolumeConditionType.AV, "买a2");
                        }
                    }

                    if (a1 - a2 <= Utils.价差下限) //买a1卖a2
                    {
                        if (!_trader.ContainsPositionByInstrument(Utils.A1, EnumPosiDirectionType.Long))
                        {
                            _trader.ReqOrderInsert(Utils.A1, EnumDirectionType.Buy, A1LastTick.UpperLimitPrice, Utils.OpenVolumePerTime, EnumOffsetFlagType.Open, EnumTimeConditionType.GFD, EnumVolumeConditionType.AV, "买a1");
                            _trader.ReqOrderInsert(Utils.A2, EnumDirectionType.Sell, A2LastTick.LowerLimitPrice, Utils.OpenVolumePerTime, EnumOffsetFlagType.Open, EnumTimeConditionType.GFD, EnumVolumeConditionType.AV, "卖a2");
                        }
                    }

                    if (a1 - a2 <= Utils.价差上限 - 12)  //平a2多a1空
                    {
                        if (_trader.ContainsPositionByInstrument(Utils.A2, EnumPosiDirectionType.Long))
                        {
                            _trader.CloseLongPositionByInstrument(Utils.A2, "平a2多", false, 0);
                            _trader.CloseShortPositionByInstrument(Utils.A1, "平a1空", false, 9999);
                        }
                    }

                    if (a1 - a2 >= Utils.价差下限 + 12) //平a1多a2空
                    {
                        if (_trader.ContainsPositionByInstrument(Utils.A1, EnumPosiDirectionType.Long))
                        {
                            _trader.CloseLongPositionByInstrument(Utils.A1, "平a1多", false, 0);
                            _trader.CloseShortPositionByInstrument(Utils.A2, "平a2空", false, 9999);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }
Exemplo n.º 6
0
        private void BuyOrSell(ThostFtdcDepthMarketDataField data)
        {
            try
            {
                #region 平仓策略

                if (Utils.InstrumentToStopLossPrices.ContainsKey(data.InstrumentID))
                {
                    var stopLossPrices = Utils.InstrumentToStopLossPrices[data.InstrumentID];

                    //var directions = MinuteAllDirection(data.InstrumentID);

                    //if (directions.Item1)//大趋势向上,平空仓
                    //{
                    //    _trader.CloseShortPositionByInstrument(data.InstrumentID, "大趋势向上,平掉空仓");
                    //}

                    //if (directions.Item2)//大趋势向下,平多仓
                    //{
                    //    _trader.CloseLongPositionByInstrument(data.InstrumentID, "大趋势向下,平掉多仓");
                    //}

                    var stopLossValue = data.LastPrice * Utils.SwingLimit * 2;

                    //多仓止损
                    if (data.LastPrice < stopLossPrices.CostLong - stopLossValue)
                    {
                        var reason = string.Format("{0}从多仓的成本价{1}跌到了绝对止损值{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.CostLong, stopLossValue, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                    }

                    //空仓止损
                    if (data.LastPrice > stopLossPrices.CostShort + stopLossValue)
                    {
                        var reason = string.Format("{0}从空仓的成本价{1}涨到了绝对止损值{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.CostShort, stopLossValue, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                    }

                    double openTrendStartPoint = 0;

                    //从多仓的最高盈利跌了一定幅度,平掉多仓,保护盈利,忽略掉小波动
                    if (stopLossPrices.CostLong > 0 && stopLossPrices.ForLong > stopLossPrices.CostLong &&
                        stopLossPrices.ForLong > data.LastPrice)
                    {
                        var keyLongPosition = Utils.GetOpenTrendStartPointKey(data.InstrumentID,
                                                                              EnumPosiDirectionType.Long);

                        if (Utils.InstrumentToOpenTrendStartPoint.ContainsKey(keyLongPosition))
                        {
                            openTrendStartPoint = Utils.InstrumentToOpenTrendStartPoint[keyLongPosition];
                        }

                        var highestDistance = Math.Abs(stopLossPrices.ForLong - stopLossPrices.CostLong);
                        var currentDistance = Math.Abs(data.LastPrice - stopLossPrices.CostLong);
                        var trendDistance   = Math.Abs(stopLossPrices.CostLong - openTrendStartPoint);

                        if (highestDistance > data.LastPrice * Utils.HighestDistanceConsiderLimit &&
                            highestDistance > currentDistance &&
                            currentDistance <= Utils.CurrentDistanceToHighestDistanceRatioLimit * highestDistance)
                        {
                            var reason = string.Format("{0}从多仓的最高趋势点{1}跌到了{2}以下,即{3},平掉多仓,多仓成本价{4},多仓最高盈利价{5}",
                                                       data.InstrumentID, highestDistance + trendDistance,
                                                       Utils.CurrentDistanceToHighestDistanceRatioLimit, currentDistance + trendDistance,
                                                       stopLossPrices.CostLong, stopLossPrices.ForLong);
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "移动止损平多仓");
                        }

                        if (highestDistance > data.LastPrice * Utils.StopProfitRatio)
                        {
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, "多仓止盈");
                        }
                    }

                    //从空仓的最高盈利跌了一半,平掉空仓,保护盈利,忽略掉小波动
                    if (stopLossPrices.CostShort > 0 && stopLossPrices.ForShort < stopLossPrices.CostShort &&
                        stopLossPrices.ForShort < data.LastPrice)
                    {
                        var keyShortPosition = Utils.GetOpenTrendStartPointKey(data.InstrumentID,
                                                                               EnumPosiDirectionType.Short);

                        if (Utils.InstrumentToOpenTrendStartPoint.ContainsKey(keyShortPosition))
                        {
                            openTrendStartPoint = Utils.InstrumentToOpenTrendStartPoint[keyShortPosition];
                        }

                        var highestDistance = Math.Abs(stopLossPrices.ForShort - stopLossPrices.CostShort);
                        var currentDistance = Math.Abs(data.LastPrice - stopLossPrices.CostShort);
                        var trendDistance   = Math.Abs(openTrendStartPoint - stopLossPrices.CostShort);

                        if (highestDistance > data.LastPrice * Utils.HighestDistanceConsiderLimit &&
                            highestDistance > currentDistance &&
                            currentDistance <= Utils.CurrentDistanceToHighestDistanceRatioLimit * highestDistance)
                        {
                            var reason = string.Format("{0}从空仓的最低趋势点{1}涨到了{2}以上,即{3},平掉空仓,空仓成本价{4},空仓最高盈利价{5}",
                                                       data.InstrumentID, highestDistance + trendDistance,
                                                       Utils.CurrentDistanceToHighestDistanceRatioLimit,
                                                       currentDistance + trendDistance, stopLossPrices.CostShort, stopLossPrices.ForShort);
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "移动止损平空仓");
                        }

                        if (highestDistance > data.LastPrice * Utils.StopProfitRatio)
                        {
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, "空仓止盈");
                        }
                    }
                }

                //接近涨停价,平掉空仓
                var upperLimitRange = (data.UpperLimitPrice + data.LowerLimitPrice) / 2;
                var lowerLimitRange = upperLimitRange;

                if (data.LastPrice > data.PreSettlementPrice + upperLimitRange * Utils.LimitCloseRange)
                {
                    var reason = string.Format("{0}最新价{1}上涨到了涨停价{2}的{3}以上,平掉空仓", data.InstrumentID, data.LastPrice,
                                               data.UpperLimitPrice, Utils.LimitCloseRange);
                    _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                }

                //接近跌停价,平掉多仓
                if (data.LastPrice < data.PreSettlementPrice - lowerLimitRange * Utils.LimitCloseRange)
                {
                    var reason = string.Format("{0}最新价{1}下跌到了跌停价{2}的{3}以下,平掉多仓", data.InstrumentID, data.LastPrice,
                                               data.LowerLimitPrice, Utils.LimitCloseRange);
                    _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                }

                #endregion

                #region 开仓策略

                if (Utils.InstrumentToMinuteByMinuteChart.ContainsKey(data.InstrumentID))
                {
                    OpenStrategy(data, false);
                }

                #endregion
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }
Exemplo n.º 7
0
        private void BuyOrSell(ThostFtdcDepthMarketDataField data)
        {
            try
            {
                if (Utils.InstrumentToMarketData.ContainsKey(data.InstrumentID))
                {
                    var marketDataList = Utils.InstrumentToMarketData[data.InstrumentID];

                    if (marketDataList.Count > 0)
                    {
                        var marketData = marketDataList[marketDataList.Count - 1];

                        if (marketData.信号 == 信号.多)
                        {
                            var reason = string.Format("{0}看多信号,平空", data.InstrumentID);
                            _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);

                            reason = string.Format("{0}看多信号,开多", data.InstrumentID);
                            _trader.OpenLongPositionByInstrument(data.InstrumentID, reason, 0, true, true, 0);
                        }

                        if (marketData.信号 == 信号.空)
                        {
                            var reason = string.Format("{0}看空信号,平多", data.InstrumentID);
                            _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);

                            reason = string.Format("{0}看空信号,开空", data.InstrumentID);
                            _trader.OpenShortPositionByInstrument(data.InstrumentID, reason, 9999, true, true, 0);
                        }
                    }
                }

                if (Utils.InstrumentToStopLossPrices.ContainsKey(data.InstrumentID))
                {
                    var stopLossPrices = Utils.InstrumentToStopLossPrices[data.InstrumentID];

                    var stopLossValue = data.LastPrice * 0.005;

                    //多仓止损
                    if (data.LastPrice < stopLossPrices.CostLong - stopLossValue)
                    {
                        var reason = string.Format("{0}从多仓的成本价{1}跌到了绝对止损值{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.CostLong, stopLossValue, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                    }

                    //空仓止损
                    if (data.LastPrice > stopLossPrices.CostShort + stopLossValue)
                    {
                        var reason = string.Format("{0}从空仓的成本价{1}涨到了绝对止损值{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.CostShort, stopLossValue, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                    }

                    var movingStopLossValueForLong = (stopLossPrices.ForLong - stopLossPrices.CostLong) * 0.5;

                    //多仓止损
                    if (stopLossPrices.ForLong - stopLossPrices.CostLong > 10 && data.LastPrice < stopLossPrices.ForLong - movingStopLossValueForLong)
                    {
                        var reason = string.Format("{0}从多仓的最高盈利价{1}跌到了移动止损价{2}以下,即{3},平掉多仓", data.InstrumentID,
                                                   stopLossPrices.ForLong, stopLossPrices.ForLong - movingStopLossValueForLong, data.LastPrice);
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, reason);
                    }

                    var movingStopLossValueForShort = (stopLossPrices.CostShort - stopLossPrices.ForShort) * 0.5;

                    //空仓止损
                    if (stopLossPrices.CostShort - stopLossPrices.ForShort > 10 && data.LastPrice > stopLossPrices.ForShort + movingStopLossValueForShort)
                    {
                        var reason = string.Format("{0}从空仓的最高盈利价{1}涨到了移动止损价{2}以上,即{3},平掉空仓", data.InstrumentID,
                                                   stopLossPrices.ForShort, stopLossPrices.ForShort + movingStopLossValueForShort, data.LastPrice);
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, reason);
                    }

                    if (stopLossPrices.ForLong - stopLossPrices.CostLong >= 20)
                    {
                        _trader.CloseLongPositionByInstrument(data.InstrumentID, "多仓止盈");
                    }

                    if (stopLossPrices.CostShort - stopLossPrices.ForShort >= 20)
                    {
                        _trader.CloseShortPositionByInstrument(data.InstrumentID, "空仓止盈");
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.WriteException(ex);
            }
        }