public FileDataSimulator(Framework framework)
     : base(framework)
 {
     if (Environment.Is64BitProcess)
     {
         SevenZipBase.SetLibraryPath("7z64.dll");
     }
     else
     {
         SevenZipBase.SetLibraryPath("7z.dll");
     }
     RunOnSubscribe = true;
     id             = 50;
     name           = "QBDataSimulator";
     description    = "QuantBox Data Simulator";
     url            = "www.smartquant.cn";
     _barFilter     = new BarFilter();
     SubscribeAsk   = true;
     SubscribeBid   = true;
     SubscribeTrade = true;
     Series         = new List <IDataSeries>();
     Processor      = new DataProcessor();
     DateTime1      = DateTime.MinValue;
     DateTime2      = DateTime.MaxValue;
 }
 public QBExecutionSimulator(Framework framework)
     : base(framework)
 {
     Id                 = QuantBoxConst.PIdSimExec;
     Name               = "QuantBoxExecutionSimulator";
     url                = "www.quantbox.cn";
     this.framework     = framework;
     BarFilter          = new BarFilter();
     CheckSubPositions  = this.framework.Configuration.UseSubPositions;
     SlippageProvider   = new TickSizeSlippageProvider();
     CommissionProvider = new CommissionProvider();
 }
 private bool OnAucFill(Order order)
 {
     if (order.Type == OrderType.Limit)
     {
         int instrumentId = order.Instrument.Id;
         if (FillOnQuote)
         {
             Ask ask = framework.DataManager.GetAsk(instrumentId);
             if (ask != null && OnAsk(order, ask))
             {
                 return(true);
             }
             Bid bid = framework.DataManager.GetBid(instrumentId);
             if (bid != null && OnBid(order, bid))
             {
                 return(true);
             }
         }
         if (FillOnTrade)
         {
             Trade trade = framework.DataManager.GetTrade(instrumentId);
             if (trade != null && OnTrade(order, trade))
             {
                 return(true);
             }
         }
         if (FillOnBar)
         {
             Bar bar = framework.DataManager.GetBar(instrumentId);
             if (BarFilter.Count != 0 && !BarFilter.Contains(bar.Type, bar.Size))
             {
                 return(false);
             }
             if (bar != null && OnBar(order, bar))
             {
                 return(true);
             }
         }
         if (FillOnLevel2)
         {
             Level2Snapshot snapshot = framework.DataManager.GetAggregatedSnapshot(instrumentId);
             if (snapshot != null)
             {
                 return(OnLevel2(order, snapshot));
             }
         }
     }
     return(false);
 }
        private void DoSend(ExecutionCommand command)
        {
            Order order = command.Order;

            if (order.Qty == 0.0)
            {
                Reject(order, "Order amount can not be zero");
                return;
            }
            if (CheckSubPositions)
            {
                Position position = order.Portfolio.GetPosition(order.Instrument);
                if (order.Side == OrderSide.Sell && order.SubSide != SubSide.SellShort && position.LongPositionQty < order.Qty)
                {
                    Reject(order, "Order amount greater than amount of long position");
                    return;
                }
                if (order.Side == OrderSide.Buy && order.SubSide == SubSide.BuyCover && position.ShortPositionQty < order.Qty)
                {
                    Reject(order, "Order amount greater than amount of short position");
                    return;
                }
            }
            SetVWapFromat(order.Instrument);
            ExecutionReport report = new ExecutionReport(command);

            report.DateTime    = framework.Clock.DateTime;
            report.ExecType    = ExecType.ExecNew;
            report.OrdStatus   = OrderStatus.New;
            report.CumQty      = 0.0;
            report.LastQty     = 0.0;
            report.LeavesQty   = order.Qty;
            report.LastPx      = 0.0;
            report.AvgPx       = 0.0;
            _reports[order.Id] = new SimulatorExecutionReport(report);
            EmitExecutionReport(report, Queued);
            if (order.TimeInForce == TimeInForce.AUC)
            {
                _auctions.Add(order);
                if (_auctions.Count == 1)
                {
                    framework.Clock.AddReminder(OnAuction1Reminder, framework.Clock.DateTime.Date.Add(Auction1));
                    framework.Clock.AddReminder(OnAuction2Reminder, framework.Clock.DateTime.Date.Add(Auction2));
                }
                return;
            }
            int instrumentId = order.Instrument.Id;

            if (_orders[instrumentId] == null)
            {
                _orders[instrumentId] = new List <Order>();
            }
            _orders[instrumentId].Add(order);
            if (((order.Type == OrderType.Market || order.Type == OrderType.Pegged) && FillMarketOnNext) ||
                (order.Type == OrderType.Limit && FillLimitOnNext) ||
                (order.Type == OrderType.Stop && FillStopOnNext) ||
                (order.Type == OrderType.StopLimit && FillStopLimitOnNext))
            {
                return;
            }

            if (FillOnQuote)
            {
                Ask ask = framework.DataManager.GetAsk(instrumentId);
                if (ask != null && OnAsk(order, ask))
                {
                    RemoveDoneOrders();
                    return;
                }
                Bid bid = framework.DataManager.GetBid(instrumentId);
                if (bid != null && OnBid(order, bid))
                {
                    RemoveDoneOrders();
                    return;
                }
            }
            if (FillOnTrade)
            {
                Trade trade = framework.DataManager.GetTrade(instrumentId);
                if (trade != null && OnTrade(order, trade))
                {
                    RemoveDoneOrders();
                    return;
                }
            }
            if (FillOnBar)
            {
                Bar bar = framework.DataManager.GetBar(instrumentId);
                if (BarFilter.Count != 0 && !BarFilter.Contains(bar.Type, bar.Size))
                {
                    return;
                }
                if (bar != null && OnBar(order, bar))
                {
                    RemoveDoneOrders();
                    return;
                }
            }
            if (FillOnLevel2)
            {
                var snapshot = framework.DataManager.GetAggregatedSnapshot(instrumentId);
                if (snapshot != null && OnLevel2(order, snapshot))
                {
                    RemoveDoneOrders();
                }
            }
        }
        public void OnBarOpen(Bar bar)
        {
            if (_orders[bar.InstrumentId] == null || !FillOnBarOpen || (BarFilter.Count != 0 && !BarFilter.Contains(bar.Type, bar.Size)))
            {
                return;
            }
            for (int i = 0; i < _orders[bar.InstrumentId].Count; i++)
            {
                Order order = _orders[bar.InstrumentId][i];
                if (CheckDataProvider && !IsProviderPassed(order, bar.ProviderId))
                {
                    continue;
                }
                while (true)
                {
                    switch (order.Type)
                    {
                    case OrderType.Market:
                    case OrderType.Pegged:
                        Fill(order, bar.Open, (int)bar.Volume);
                        break;

                    case OrderType.Limit:
                        switch (order.Side)
                        {
                        case OrderSide.Buy:
                            if (bar.Open <= order.Price)
                            {
                                if (UseProbability && bar.Open == order.Price && _random.NextDouble() < Probability)
                                {
                                    return;
                                }
                                if (FillAtLimitPrice)
                                {
                                    Fill(order, order.Price, (int)bar.Volume);
                                }
                                else
                                {
                                    Fill(order, bar.Open, (int)bar.Volume);
                                }
                            }
                            break;

                        case OrderSide.Sell:
                            if (bar.Open >= order.Price)
                            {
                                if (UseProbability && bar.Open == order.Price && _random.NextDouble() < Probability)
                                {
                                    return;
                                }
                                if (FillAtLimitPrice)
                                {
                                    Fill(order, order.Price, (int)bar.Volume);
                                }
                                else
                                {
                                    Fill(order, bar.Open, (int)bar.Volume);
                                }
                            }
                            break;
                        }
                        break;

                    case OrderType.Stop:
                        switch (order.Side)
                        {
                        case OrderSide.Buy:
                            if (bar.Open >= order.StopPx)
                            {
                                if (!FillAtStopPrice)
                                {
                                    order.SetOrderType(OrderType.Market);
                                    continue;
                                }
                                Fill(order, order.StopPx, (int)bar.Volume);
                            }
                            break;

                        case OrderSide.Sell:
                            if (bar.Open <= order.StopPx)
                            {
                                if (!FillAtStopPrice)
                                {
                                    order.SetOrderType(OrderType.Market);
                                    continue;
                                }
                                Fill(order, order.StopPx, (int)bar.Volume);
                            }
                            break;
                        }
                        break;

                    case OrderType.StopLimit:
                        switch (order.Side)
                        {
                        case OrderSide.Buy:
                            if (bar.Open >= order.StopPx)
                            {
                                order.SetOrderType(OrderType.Limit);
                                continue;
                            }
                            break;

                        case OrderSide.Sell:
                            if (bar.Open <= order.StopPx)
                            {
                                order.SetOrderType(OrderType.Limit);
                                continue;
                            }
                            break;
                        }
                        break;
                    }
                    break;
                }
            }
            RemoveDoneOrders();
        }
 public void OnBar(Bar bar)
 {
     if (_orders[bar.InstrumentId] != null && FillOnBar && (BarFilter.Count == 0 || BarFilter.Contains(bar.Type, bar.Size)))
     {
         for (int i = 0; i < _orders[bar.InstrumentId].Count; i++)
         {
             Order order = _orders[bar.InstrumentId][i];
             OnBar(order, bar);
         }
         RemoveDoneOrders();
     }
 }
 public FileDataSimulator(Framework framework)
     : base(framework)
 {
     if (Environment.Is64BitProcess)
     {
         SevenZipBase.SetLibraryPath("7z64.dll");
     }
     else
     {
         SevenZipBase.SetLibraryPath("7z.dll");
     }
     RunOnSubscribe = true;
     id = 50;
     name = "QBDataSimulator";
     description = "QuantBox Data Simulator";
     url = "www.smartquant.cn";
     _barFilter = new BarFilter();
     SubscribeAsk = true;
     SubscribeBid = true;
     SubscribeTrade = true;
     Series = new List<IDataSeries>();
     Processor = new DataProcessor();
     DateTime1 = DateTime.MinValue;
     DateTime2 = DateTime.MaxValue;
 }