Exemplo n.º 1
0
        public override List<Order> Match(MarketData marketData)
        {
            base.Match(marketData);

            newOrders.Clear();

            var instrumentId = marketData.InstrumentId;

            currMarketData = marketData;

            tick++;

            if (tick < 10) return newOrders;

            tick = 0;

            List<Order> orders = GetStrategyOrders(instrumentId);

            if (!orders.Exists(o => o.StatusType != EnumOrderStatus.已平仓))
            {
                OpenOrder();
                return newOrders;
            }

            //CloseOrder(orders.FindAll(o => o.StatusType == EnumOrderStatus.已开仓));
            return newOrders;
        }
Exemplo n.º 2
0
 protected double GetAnyPrice(MarketData marketData, TThostFtdcDirectionType direction)
 {
     if (direction == TThostFtdcDirectionType.Buy)
     {
         return marketData.LastPrice + InstrumentStrategy.PriceTick * 3;
     }
     else
     {
         return marketData.LastPrice - InstrumentStrategy.PriceTick * 3;
     }
 }
Exemplo n.º 3
0
        public override List<Order> Match(MarketData marketData)
        {
            var instrumentId = marketData.InstrumentId;

             List<Order> orders = GetOrders(instrumentId);

            var list = new List<Order>();

            foreach (var order in orders.FindAll(o => o.StatusType == EnumOrderStatus.已开仓))
            {
                if (order.PositionProfit > maxProfit) maxProfit = order.PositionProfit;

                if (order.PositionProfit < 0 || maxProfit < MinProfit) continue;

                if ((maxProfit - order.PositionProfit) / maxProfit > Percent)
                {
                    var neworder = new Order();
                    neworder.OffsetFlag = TThostFtdcOffsetFlagType.CloseToday;
                    neworder.Direction = order.Direction == TThostFtdcDirectionType.Buy ? TThostFtdcDirectionType.Sell : TThostFtdcDirectionType.Buy;
                    neworder.InstrumentId = marketData.InstrumentId;
                    neworder.LastPrice = marketData.LastPrice;
                    neworder.Price = GetAnyPrice(marketData, neworder.Direction);
                    neworder.Volume = order.Volume;
                    neworder.StrategyType = GetType().ToString();

                    var strategyLog = new FallBackStopStrategyLog
                    {
                        MinProfit = MinProfit,
                        Percent = Percent,
                        MaxProfit = maxProfit,
                        PositionProfit = order.PositionProfit,
                        LastPrice = marketData.LastPrice,
                        UpdateTime = marketData.UpdateTimeSec
                    };

                    neworder.StrategyLogs.Add(strategyLog);

                    order.CloseOrders.Add(neworder);

                    list.Add(order);

                    maxProfit = 0d;

                    log.Info(String.Format("{0}:{1}:{2}:{3}", ToString(), marketData.InstrumentId, marketData.LastPrice,
                        orders.Count()));
                }
            }

            return list;
        }
Exemplo n.º 4
0
        public override List<Order> Match(MarketData marketData)
        {
            var instrumentId = marketData.InstrumentId;

            List<Order> orders =
                OrderManager.getOrders()
                    .Where(o => o.InstrumentId == marketData.InstrumentId)
                    .ToList();

            var list = new List<Order>();

            foreach (var order in orders)
            {
                if (order.StatusType == EnumOrderStatus.已开仓 && Price + order.PositionProfit <= 0)
                {
                    var neworder = new Order();
                    neworder.OffsetFlag = TThostFtdcOffsetFlagType.CloseToday;
                    neworder.Direction = order.Direction == TThostFtdcDirectionType.Buy ? TThostFtdcDirectionType.Sell : TThostFtdcDirectionType.Buy;
                    neworder.InstrumentId = marketData.InstrumentId;
                    neworder.Price = GetAnyPrice(marketData, neworder.Direction);
                    neworder.Volume = order.Volume;
                    neworder.StrategyType = GetType().ToString();

                    var strategyLog = new PriceStopLossStrategyLog
                    {
                        Price = Price,
                        PositionProfit = order.PositionProfit,
                        LastPrice = marketData.LastPrice,
                        UpdateTime = marketData.UpdateTimeSec
                    };

                    neworder.StrategyLogs.Add(strategyLog);

                    order.CloseOrders.Add(neworder);

                    list.Add(order);

                    log.Info(String.Format("{0}:{1}:{2}:{3}", ToString(), marketData.InstrumentId, marketData.LastPrice,
                        orders.Count()));
                }
            }

            return list;
        }
Exemplo n.º 5
0
        public override List<Order> Match(MarketData marketData)
        {
            var instrumentId = marketData.InstrumentId;

             List<Order> orders = GetOrders(instrumentId);

            var list = new List<Order>();

            foreach (var order in orders)
            {
                if (order.Volume == order.RemainVolume) tmpPrice = Price;

                if (order.RemainVolume == order.Volume/4) break;

                if (order.StatusType == EnumOrderStatus.已开仓 && order.PositionProfit/(InstrumentStrategy.VolumeMultiple*order.RemainVolume) >= tmpPrice)
                {
                    var neworder = new Order();
                    neworder.OffsetFlag = TThostFtdcOffsetFlagType.CloseToday;
                    neworder.Direction = order.Direction == TThostFtdcDirectionType.Buy ? TThostFtdcDirectionType.Sell : TThostFtdcDirectionType.Buy;
                    neworder.InstrumentId = marketData.InstrumentId;
                    neworder.Price = marketData.LastPrice;
                    neworder.Volume = order.Volume / 4;
                    neworder.StrategyType = GetType().ToString();

                    order.CloseOrders.Add(neworder);

                    list.Add(order);

                    log.Info(String.Format("{0}:{1}:{2}:{3}", ToString(), marketData.InstrumentId, marketData.LastPrice,
                        orders.Count()));

                    tmpPrice += 1;
                }
            }

            return list;
        }
Exemplo n.º 6
0
 public abstract List<Order> Match(MarketData marketData);
Exemplo n.º 7
0
 public MarketDataEventArgs(MarketData marketData)
 {
     this.marketData = marketData;
 }
Exemplo n.º 8
0
        public void Subscribe(string instrumentID)
        {
            var marketData = new MarketData(instrumentID);
            marketData.Unit = GetInstrument(instrumentID).VolumeMultiple;

            StrategyManager.InitStrategies(GetInstrument(instrumentID));

            marketDatas.Add(marketData);
            instrumentDictionary.Add(instrumentID, marketData);

            mdApi.Subscribe(instrumentID, "");
        }
Exemplo n.º 9
0
        public void Subscribe()
        {
            mdApi.OnRtnDepthMarketData += mdApi_OnRtnDepthMarketData;
            mdApi.OnRspError += mdApi_OnRspError;

            string ppInstrumentId = "";

            foreach (Instrument instrument in instruments.Where(instrument => instrument.AutoTrade))
            {
                string instrumentID = instrument.InstrumentID;

                var marketData = new MarketData(instrumentID);
                marketData.Unit = instrument.VolumeMultiple;
                marketData.ExchangeID = instrument.ExchangeID;

                marketDatas.Add(marketData);
                instrumentDictionary.Add(instrumentID, marketData);
                ppInstrumentId += instrumentID + ",";

                StrategyManager.InitStrategies(instrument);
            }

            mdApi.Subscribe(ppInstrumentId, "");

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    CThostFtdcDepthMarketDataField pDepthMarketData = marketQueue.Dequeue();

                    MarketData marketData;

                    if (pDepthMarketData.InstrumentID == null) continue;

                    if (instrumentDictionary.ContainsKey(pDepthMarketData.InstrumentID))
                    {
                        marketData = instrumentDictionary[pDepthMarketData.InstrumentID];
                        marketData.CopyFrom(pDepthMarketData);
                    }
                    else
                    {
                        var instrument = GetInstrument(pDepthMarketData.InstrumentID);

                        marketData = new MarketData(pDepthMarketData);
                        marketData.Unit = instrument.VolumeMultiple;
                        marketData.ExchangeID = instrument.ExchangeID;
                        marketDatas.Add(marketData);
                        instrumentDictionary.Add(pDepthMarketData.InstrumentID, marketData);
                    }

                    if (!RecorDictionary.ContainsKey(marketData.InstrumentId))
                    {
                        var datas = new List<String>();
                        RecorDictionary.Add(marketData.InstrumentId, datas);
                    }

                    RecorDictionary[marketData.InstrumentId].Add(marketData.SimpleFormat());

                    //Task.Factory.StartNew(() =>
                    //{
                        strategyManager.PrcessData(marketData);

                        orderManager.ProcessData(marketData);

                        indicatorManager.ProcessData(marketData);
                   // });

                    //log.Info(marketQueue.Count());
                    //ObjectUtils.CopyStruct(pDepthMarketData, marketData);
                    //OnRtnMarketData(this, new MarketDataEventArgs(marketData));
                    //log.Info(marketData);
                }
            });
        }
Exemplo n.º 10
0
        public override List<Order> Match(MarketData marketData)
        {
            currMarketData = marketData;

            if (AllowStopLoss)
            {
                foreach (Order order in StopLosses.Select(stopLoss => stopLoss.Match(marketData)).Where(orders => orders != null).SelectMany(orders => orders))
                {
                    OrderManager.OrderInsert(order);
                }
            }

            if (AllowStopProfit)
            {
                foreach (
                    Order order in
                        StopProfits.Select(stopProfit => stopProfit.Match(marketData))
                            .Where(orders => orders != null)
                            .SelectMany(orders => orders))
                {
                    OrderManager.OrderInsert(order);
                }
            }

            return new List<Order>();
        }
Exemplo n.º 11
0
 private TThostFtdcDirectionType Cross(MarketData preMarketData, MarketData marketData)
 {
     if (preMarketData.LastPrice < preMarketData.AveragePrice &&
         marketData.LastPrice > marketData.AveragePrice)
     {
         return TThostFtdcDirectionType.Buy;
     }
     if (preMarketData.LastPrice > preMarketData.AveragePrice &&
         marketData.LastPrice < marketData.AveragePrice)
     {
         return TThostFtdcDirectionType.Sell;
     }
     return TThostFtdcDirectionType.Nothing;
 }
Exemplo n.º 12
0
        public override List<Order> Match(MarketData marketData)
        {
            base.Match(marketData);

            newOrders.Clear();

            log.Info("AVGPrice=" + GetAvgPrice());

            string instrumentId = marketData.InstrumentId;

            preMarketData = IndicatorManager.GetPreMarketData(instrumentId);

            if (preMarketData == null) return newOrders;

            var orders = GetStrategyOrders(instrumentId);

            CloseOrder(orders.FindAll(o => o.StatusType == EnumOrderStatus.已开仓));

            if (!orders.Exists(o => o.StatusType != EnumOrderStatus.已平仓))
                OpenOrder();

            if (lossThreshold >= MaxLossThreshold)
            {
                tick++;
                if (tick >= 2400)
                {
                    lossThreshold = 0;
                    tick = 0;
                }
            }

            log.Info("lossThreshold=" + lossThreshold);

            return newOrders;
        }
Exemplo n.º 13
0
 public override List<Order> Match(MarketData marketData)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 14
0
        public void ProcessData(MarketData marketData)
        {
            sw.EnterReadLock();
            foreach (
                Order order in
                    getOrders()
                        .Where(o => o.InstrumentId == marketData.InstrumentId && o.StatusType != EnumOrderStatus.开仓中))
            {
                double profit = (order.Direction == TThostFtdcDirectionType.Buy)
                    ? marketData.LastPrice - order.TradePrice
                    : order.TradePrice - marketData.LastPrice;

                order.LastPrice = marketData.LastPrice;
                order.PositionProfit = profit*order.Unit*order.RemainVolume;
                order.PositionTimeSpan =
                    DateTime.Now.Subtract(DateTime.ParseExact(order.ActualTradeDate, "yyyyMMdd HH:mm:ss",
                        CultureInfo.InvariantCulture));
            }

            AccountManager.Accounts[0].PositionProfit = orders.Sum(o => o.PositionProfit);
            AccountManager.Accounts[0].CloseProfit = orderlogs.Sum(o => o.CloseProfit);
            AccountManager.Accounts[0].CurrMargin =
                orders.Where(o => o.StatusType == EnumOrderStatus.已开仓).Sum(o => o.UseMargin);
            AccountManager.Accounts[0].FrozenMargin =
                orders.Where(o => o.StatusType == EnumOrderStatus.开仓中).Sum(o => o.UseMargin);

            sw.ExitReadLock();

            CheckStopLoss();
        }
Exemplo n.º 15
0
        public override List<Order> Match(MarketData marketData)
        {
            base.Match(marketData);

            newOrders.Clear();

            ma = IndicatorManager.GetMA(currMarketData.InstrumentId, Day, Period);

            if (ma == null) return newOrders;

            MAPrice = ma.Average;

            var orders = GetStrategyOrders(marketData.InstrumentId);

            OpenBuyOrder(orders);

            OpenSellOrder(orders);

            return newOrders;
        }
Exemplo n.º 16
0
        private void GetLog(String direction, MarketData preMarketData, MarketData marketData, int threshold)
        {
            var dayAverageLog = new DayAverageLog
            {
                Direction = direction,
                Threshold = threshold,
                PreLastPrice = preMarketData.LastPrice,
                PreAveragePrice = preMarketData.AveragePrice,
                PreUpdateTime = preMarketData.UpdateTimeSec,
                LastPrice = marketData.LastPrice,
                AveragePrice = marketData.AveragePrice,
                UpdateTime = marketData.UpdateTimeSec
            };
            dayAverageLogs.Add(dayAverageLog);

            log.Info(dayAverageLog.Direction + ":" + dayAverageLog.Threshold);
        }
Exemplo n.º 17
0
        public void PrcessData(MarketData marketData)
        {
            if (!isStart) return;

            InstrumentStrategy instrumentStrategy = dictStrategies[marketData.InstrumentId];

            if (!instrumentStrategy.StartTrade) return;

            foreach (UserStrategy strategy in instrumentStrategy.Strategies)
            {
                if (!strategy.AutoTrade) continue;

                if (needWait)
                {
                    tick++;
                    if (tick >= 240)
                    {
                        needWait = false;
                        tick = 0;
                    }
                    return;
                }

                var sc = (StringCollection) InstrumentType.Default[marketData.Code];

                var today = DateTime.Today;

                if (sc != null)
                {
                    foreach (var time in sc)
                    {
                        int hour = Convert.ToInt32(time.Split(':')[0]);
                        int minute = Convert.ToInt32(time.Split(':')[1]);

                        var endTime = new DateTime(today.Year, today.Month, today.Day, hour, minute, 0);

                        if (endTime > DateTime.Now && DateTime.Now > endTime.AddMinutes(-1))
                        {
                            OrderManager.CancelAllOrder();
                            needWait = true;
                            log.Info("canceling order");
                            return;

                        }
                    }
                }

                List<Order> orders = strategy.Match(marketData);

                foreach (Order order in orders)
                {
                    order.Unit = instrumentStrategy.VolumeMultiple;

                    if (order.Direction == TThostFtdcDirectionType.Buy)
                        order.UseMargin = order.Price*order.Unit*order.Volume*instrumentStrategy.LongMarginRatio;
                    else
                        order.UseMargin = order.Price*order.Unit*order.Volume*instrumentStrategy.ShortMarginRatio;

                    log.Info(order);
                    OrderManager.OrderInsert(order);
                }
            }
        }