Ejemplo n.º 1
0
        public bool operateInstrument(OpType op, string instrumentId, double price, int limitMin)
        {
            if (op == OpType.BUY_OPEN || op == OpType.SELL_OPEN)
            {
                int        orderId    = _orderId++;
                OrderField orderField = new OrderField();
                orderField.OrderID      = orderId;
                orderField.InstrumentID = instrumentId;
                orderField.LimitPrice   = price;
                if (op == OpType.BUY_OPEN)
                {
                    orderField.Direction = DirectionType.Buy;
                }
                else
                {
                    orderField.Direction = DirectionType.Sell;
                }

                _tradeOrders.Add(orderId, orderField);

                if (limitMin > 0)
                {
                    _waitingOrder.Add(instrumentId, new HashSet <string>());
                    _waitingMin[instrumentId] = limitMin;
                }
                else
                {
                    _waitingMin[_instrumentId] = 0;
                }

                StrategyManager.getInstance().onOrder(orderField);
            }
            else
            {
                if (_holding != 0)
                {
                    TradeArgs tradeArgs = new TradeArgs();
                    tradeArgs.Value = new TradeField();
                    if (_holdingOrder.Direction == DirectionType.Buy)
                    {
                        tradeArgs.Value.Direction = DirectionType.Sell;
                    }
                    else
                    {
                        tradeArgs.Value.Direction = DirectionType.Buy;
                    }

                    tradeArgs.Value.InstrumentID = _instrumentId;
                    tradeArgs.Value.Price        = _currentLastPrice;
                    tradeArgs.Value.TradeTime    = _currentTime;
                    tradeArgs.Value.OrderID      = _holdingOrder.OrderID;
                    tradeArgs.Value.Offset       = OffsetType.Close;
                    StrategyManager.getInstance().onTrade(tradeArgs);
                    _holding      = 0;
                    _holdingOrder = null;
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        public void init(string instrumentId)
        {
            _instrumentId = instrumentId;
            BreakStrategy breakStrategy = new BreakStrategy(this);

            breakStrategy.mMinSpan   = 30;
            breakStrategy.mTotalSize = 9;
            StrategyManager.getInstance().addStrategy(breakStrategy);
        }
Ejemplo n.º 3
0
        public void start()
        {
            FileStream fs = new FileStream(FileUtil.getTestDataFilePath(_instrumentId + "_Tick.csv"), System.IO.FileMode.Open, System.IO.FileAccess.Read);

            //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            //string fileContent = sr.ReadToEnd();
            //encoding = sr.CurrentEncoding;
            //记录每次读取的一行记录
            string strLine = "";

            //记录每行记录中的各字段内容
            string[] aryLine = null;
            //标示列数
            int columnCount = 0;
            //标示是否是读取的第一行
            bool IsFirst = true;
            //逐行读取CSV中的数据
            int count = 0;

            while ((strLine = sr.ReadLine()) != null)
            {
                //strLine = Common.ConvertStringUTF8(strLine, encoding);
                //strLine = Common.ConvertStringUTF8(strLine);

                {
                    aryLine = strLine.Split(',');
                    TickEventArgs tick = new TickEventArgs();
                    tick.Tick = new MarketData();
                    bool result = double.TryParse(aryLine[8], out tick.Tick.LastPrice);
                    if (result == false || tick.Tick.LastPrice <= 0)
                    {
                        continue;
                    }
                    _currentLastPrice = tick.Tick.LastPrice;

                    tick.Tick.InstrumentID = _instrumentId;
                    string date = aryLine[0].Substring(0, 4) + "-" + aryLine[0].Substring(4, 2) + "-" + aryLine[0].Substring(6);
                    string time;
                    string temparyLine1 = aryLine[1];
                    string temparyLine2 = aryLine[1];
                    if (temparyLine1.IndexOf('E') >= 0)
                    {
                        Decimal dec = ChangeDataToD(temparyLine1);
                        temparyLine2 = dec.ToString();
                    }

                    temparyLine2 += "00000000";

                    time = temparyLine2.Substring(2, 2) + ":" + temparyLine2.Substring(4, 2) + ":" + temparyLine2.Substring(6, 2);


                    tick.Tick.UpdateTime = date + " " + time;
                    _currentTime         = tick.Tick.UpdateTime;

                    OrderField order = getOpeningOrder(_instrumentId);
                    if (order != null)
                    {
                        if ((order.Direction == DirectionType.Buy &&
                             order.LimitPrice >= tick.Tick.LastPrice) ||
                            (order.Direction == DirectionType.Sell &&
                             order.LimitPrice <= tick.Tick.LastPrice))
                        {
                            TradeArgs tradeArgs = new TradeArgs();
                            tradeArgs.Value              = new TradeField();
                            tradeArgs.Value.Direction    = order.Direction;
                            tradeArgs.Value.InstrumentID = _instrumentId;
                            tradeArgs.Value.Price        = tick.Tick.LastPrice;
                            tradeArgs.Value.TradeTime    = tick.Tick.UpdateTime;
                            tradeArgs.Value.OrderID      = order.OrderID;
                            tradeArgs.Value.Offset       = order.Offset;
                            StrategyManager.getInstance().onTrade(tradeArgs);
                            _holding      = 1;
                            _holdingOrder = order;
                            removeOrder(order.OrderID);
                            removeWaitingOrder(_instrumentId);
                        }
                    }
                    StrategyManager.getInstance().onTick(tick);
                    if (hasWaitingOrder(_instrumentId))
                    {
                        if (_waitingOrder.ContainsKey(_instrumentId))
                        {
                            HashSet <string> tickTime = _waitingOrder[_instrumentId];
                            tickTime.Add(tick.Tick.UpdateTime);
                            if (tickTime.Count == 1 && order != null)
                            {
                                order.InsertTime = tick.Tick.UpdateTime;
                            }

                            if (tickTime.Count > _waitingMin[_instrumentId] * 60)
                            {
                                removeOrder(order.OrderID);
                                removeWaitingOrder(_instrumentId);
                                order.InsertTime = tick.Tick.UpdateTime;
                                StrategyManager.getInstance().onOrderCancel(order);
                            }
                        }
                    }
                }
            }


            sr.Close();
            fs.Close();
        }