Exemple #1
0
        public QuoteSeries GetHistoricalQuotes(IHistoricalDataProvider provider, Instrument instrument, DateTime datetime1, DateTime datetime2)
        {
            if (provider.IsDisconnected)
            {
                provider.Connect();
            }
            DataNotifier @class = new DataNotifier();
            string       text   = Guid.NewGuid().ToString();

            lock (this.dictionary_0)
                this.dictionary_0.Add(text, @class);

            provider.Send(new HistoricalDataRequest(instrument, datetime1, datetime2, DataObjectType.Quote)
            {
                RequestId = text
            });
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
                this.dictionary_0.Remove(text);

            var qs = new QuoteSeries("");

            foreach (var data in @class.Data)
            {
                var objs = data.Objects;
                for (int i = 0; i < objs.Length; i++)
                {
                    qs.Add((Quote)objs[i]);
                }
            }
            return(qs);
        }
Exemple #2
0
        public TickSeries GetHistoricalTicks(IHistoricalDataProvider provider, TickType type, Instrument instrument, DateTime dateTime1, DateTime dateTime2)
        {
            if (provider.IsDisconnected)
            {
                provider.Connect();
            }

            DataNotifier @class = new DataNotifier();
            string       text   = Guid.NewGuid().ToString();

            lock (this.dictionary_0)
            {
                this.dictionary_0.Add(text, @class);
            }
            HistoricalDataRequest request = null;

            switch (type)
            {
            case TickType.Bid:
                request = new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Bid);
                break;

            case TickType.Ask:
                request = new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Ask);
                break;

            case TickType.Trade:
                request = new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Trade);
                break;
            }
            request.RequestId = text;
            provider.Send(request);
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Remove(text);
            }
            var ts = new TickSeries("", "");

            foreach (var data in @class.Data)
            {
                var objs = data.Objects;
                foreach (var tick in objs)
                {
                    ts.Add((Tick)tick);
                }
            }
            return(ts);
        }
Exemple #3
0
        private static ArrayList r6ZT8iFUv(IHistoricalDataProvider provider, Instrument instrument, DataManager.EDataSeries dataType, DateTime beginDate, DateTime endDate, long barSize)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("Provider is null");
            }
            if (instrument == null)
            {
                throw new ArgumentNullException("Instrument is null");
            }

            if (!provider.IsConnected)
            {
                provider.Connect(10000);
                if (!provider.IsConnected)
                {
                    throw new InvalidOperationException("Provider cannot make a connection");
                }
            }

            HistoricalDataRequest request = new HistoricalDataRequest();

            request.Instrument = instrument;
            switch (dataType)
            {
            case DataManager.EDataSeries.Daily:
                request.DataType = HistoricalDataType.Daily;
                break;

            case DataManager.EDataSeries.Trade:
                request.DataType = HistoricalDataType.Trade;
                break;

            case DataManager.EDataSeries.Quote:
                request.DataType = HistoricalDataType.Quote;
                break;

            case DataManager.EDataSeries.Bar:
                request.DataType = HistoricalDataType.Bar;
                request.BarSize  = barSize;
                break;
            }
            request.BeginDate = beginDate;
            request.EndDate   = endDate;
            return(new HistoricalDataGetter(provider, request).GetData());
        }
Exemple #4
0
        public BarSeries GetHistoricalBars(IHistoricalDataProvider provider, Instrument instrument, DateTime dateTime1, DateTime dateTime2, BarType barType, long barSize)
        {
            if (provider.IsDisconnected)
            {
                provider.Connect();
            }

            DataManager.DataNotifier @class = new DataManager.DataNotifier();
            string text = Guid.NewGuid().ToString();

            lock (this.dictionary_0)
            {
                this.dictionary_0.Add(text, @class);
            }
            provider.Send(new HistoricalDataRequest(instrument, dateTime1, dateTime2, 6)
            {
                RequestId = text,
                BarType   = new BarType?(barType),
                BarSize   = new long?(barSize)
            });
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Remove(text);
            }
            var bs = new BarSeries("", "", -1, -1);

            foreach (var data in @class.Data)
            {
                var objs = data.Objects;
                for (int i = 0; i < objs.Length; i++)
                {
                    bs.Add((Bar)objs[i]);
                }
            }
            return(bs);
        }
Exemple #5
0
        public List <Fundamental> GetHistoricalFundamentals(IHistoricalDataProvider provider, Instrument instrument, DateTime dateTime1, DateTime dateTime2)
        {
            if (provider.IsDisconnected)
            {
                provider.Connect();
            }
            var @class = new DataNotifier();
            var guid   = Guid.NewGuid().ToString();

            lock (this.dictionary_0)
            {
                this.dictionary_0.Add(guid, @class);
            }
            provider.Send(new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Fundamental)
            {
                RequestId = guid
            });
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Remove(guid);
            }
            return(@class.Data.SelectMany(current => current.Objects).Cast <Fundamental>().ToList());
        }
Exemple #6
0
        private static ArrayList r6ZT8iFUv(IHistoricalDataProvider provider, Instrument instrument, DataManager.EDataSeries dataType, DateTime beginDate, DateTime endDate, long barSize)
        {
            if (provider == null)
                throw new ArgumentNullException("Provider is null");
            if (instrument == null)
                throw new ArgumentNullException("Instrument is null");

            if (!provider.IsConnected)
            {
                provider.Connect(10000);
                if (!provider.IsConnected)
                    throw new InvalidOperationException("Provider cannot make a connection");
            }

            HistoricalDataRequest request = new HistoricalDataRequest();
            request.Instrument = instrument;
            switch (dataType)
            {
                case DataManager.EDataSeries.Daily:
                    request.DataType = HistoricalDataType.Daily;
                    break;
                case DataManager.EDataSeries.Trade:
                    request.DataType = HistoricalDataType.Trade;
                    break;
                case DataManager.EDataSeries.Quote:
                    request.DataType = HistoricalDataType.Quote;
                    break;
                case DataManager.EDataSeries.Bar:
                    request.DataType = HistoricalDataType.Bar;
                    request.BarSize = barSize; 
                    break;
            }
            request.BeginDate = beginDate;
            request.EndDate = endDate;
            return new HistoricalDataGetter(provider, request).GetData();
        }
        public override void Run()
        {
            // Prepare running.
            Console.WriteLine("Prepare running in {0} mode...", framework.StrategyManager.Mode);

            // Get trading instruments.
            Instrument ins1 = InstrumentManager.Instruments["IF1612"];

            // Create SMA Crossover with Loading data on start strategy.
            // and add trading instruments.
            DoubleMA_Crossover smaCrossoverLOS = new DoubleMA_Crossover(framework, "SMACrossoverLOS");

            smaCrossoverLOS.Instruments.Add(ins1);

            // Set strategy as main.
            strategy = smaCrossoverLOS;

            Console.WriteLine("Prepare running in {0} mode...", framework.StrategyManager.Mode);

            // 开始时间是前一个交易日,这个地方要按自己策略的实际情况进行调整
            DateTime startDate = DateTime.Now.DayOfWeek == DayOfWeek.Monday ? DateTime.Now.AddDays(-3).Date : DateTime.Now.AddDays(-1).Date;
            DateTime historicalData1EndTime = startDate;

            // 取本地的数据的最后时间
            DataSeries ins1DataSeries = framework.DataManager.GetDataSeries(ins1, DataObjectType.Trade);

            if (ins1DataSeries != null && ins1DataSeries.Count > 0)
            {
                historicalData1EndTime = ins1DataSeries.DateTime2;
            }

            // 以两个时间的最大值为起点
            historicalData1EndTime = new DateTime(Math.Max(historicalData1EndTime.Ticks, startDate.Ticks));


            // Load and save historical trades from QuantBase provider.
            IHistoricalDataProvider quantBase = framework.ProviderManager.GetHistoricalDataProvider(94);

            if (quantBase.Status == ProviderStatus.Disconnected)
            {
                quantBase.Connect();
            }
            // 等待连接成功,订阅太快了不行
            while (!quantBase.IsConnected)
            {
                Thread.Sleep(1000);
            }


            // Load historical trades.
            Console.WriteLine("Load historical data.");
            TickSeries ins1TickSeries = framework.DataManager.GetHistoricalTrades(quantBase, ins1, historicalData1EndTime, DateTime.Now);

            Console.WriteLine("Save historical data.");
            // Save historical trades.
            foreach (Trade trade in ins1TickSeries)
            {
                framework.DataManager.Save(ins1, trade);
            }

            // Set DataSimulator's dates.
            DataSimulator.DateTime1 = startDate;
            DataSimulator.DateTime2 = DateTime.Now;

            // Set null for event filter.
            framework.EventManager.Filter = null;

            // Set property for suspend trading during simulation.
            DoubleMA_Crossover.SuspendTrading = true;

            // Add 5 minute bars (300 seconds) for trading instruments.
            BarFactory.Add(ins1, SmartQuant.BarType.Time, barSize);

            // Run in simulation.
            Console.WriteLine("Run in Backtest mode.");

            // Save current strategy mode.
            StrategyMode mode = framework.StrategyManager.Mode;

            // Set backtest mode.
            framework.StrategyManager.Mode = StrategyMode.Backtest;

            StartStrategy(StrategyMode.Backtest);

            // Run.
            Console.WriteLine("Run in {0} mode.", framework.StrategyManager.Mode);



            // Restore strategy mode.
            framework.StrategyManager.Mode = mode;

            // Get provider for realtime.
            Provider quantRouter = framework.ProviderManager.GetProvider(99) as Provider;

            if (quantRouter.Status == ProviderStatus.Disconnected)
            {
                quantRouter.Connect();
            }
            while (!quantRouter.IsConnected)
            {
                Thread.Sleep(1000);
            }

            // Set property for trading.
            DoubleMA_Crossover.SuspendTrading = false;

            if (framework.StrategyManager.Mode == StrategyMode.Paper)
            {
                // Set QuantRouter as data provider.
                strategy.DataProvider = quantRouter as IDataProvider;
            }
            else if (framework.StrategyManager.Mode == StrategyMode.Live)
            {
                // Set QuantRouter as data and execution provider.
                strategy.DataProvider      = quantRouter as IDataProvider;
                strategy.ExecutionProvider = quantRouter as IExecutionProvider;
            }


            StartStrategy(framework.StrategyManager.Mode);
        }
        public QuoteSeries GetHistoricalQuotes(IHistoricalDataProvider provider, Instrument instrument, DateTime datetime1, DateTime datetime2)
        {
            if (provider.IsDisconnected)
                provider.Connect();
            DataNotifier @class = new DataNotifier();
            string text = Guid.NewGuid().ToString();
            lock (this.dictionary_0)
                this.dictionary_0.Add(text, @class);

            provider.Send(new HistoricalDataRequest(instrument, datetime1, datetime2, DataObjectType.Quote) { RequestId = text });
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
                this.dictionary_0.Remove(text);

            var qs = new QuoteSeries("");
            foreach (var data in @class.Data)
            {
                var objs = data.Objects;
                for (int i = 0; i < objs.Length; i++)
                    qs.Add((Quote)objs[i]);
            }
            return qs;
        }
        public BarSeries GetHistoricalBars(IHistoricalDataProvider provider, Instrument instrument, DateTime dateTime1, DateTime dateTime2, BarType barType, long barSize)
        {
            if (provider.IsDisconnected)
                provider.Connect();

            DataManager.DataNotifier @class = new DataManager.DataNotifier();
            string text = Guid.NewGuid().ToString();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Add(text, @class);
            }
            provider.Send(new HistoricalDataRequest(instrument, dateTime1, dateTime2, 6)
            {
                RequestId = text,
                BarType = new BarType?(barType),
                BarSize = new long?(barSize)
            });
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Remove(text);
            }
            var bs = new BarSeries("", "", -1, -1);
            foreach (var data in @class.Data)
            {
                var objs = data.Objects;
                for (int i = 0; i < objs.Length; i++)
                    bs.Add((Bar)objs[i]);
            }
            return bs;
        }
 public List<Fundamental> GetHistoricalFundamentals(IHistoricalDataProvider provider, Instrument instrument, DateTime dateTime1, DateTime dateTime2)
 {
     if (provider.IsDisconnected)
     {
         provider.Connect();
     }
     var @class = new DataNotifier();
     var guid = Guid.NewGuid().ToString();
     lock (this.dictionary_0)
     {
         this.dictionary_0.Add(guid, @class);
     }
     provider.Send(new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Fundamental)
     {
         RequestId = guid
     });
     @class.ReadyEvent.WaitOne();
     lock (this.dictionary_0)
     {
         this.dictionary_0.Remove(guid);
     }
     return @class.Data.SelectMany(current => current.Objects).Cast<Fundamental>().ToList();
 }
        public TickSeries GetHistoricalTicks(IHistoricalDataProvider provider, TickType type, Instrument instrument, DateTime dateTime1, DateTime dateTime2)
        {
            if (provider.IsDisconnected)
                provider.Connect();

            DataNotifier @class = new DataNotifier();
            string text = Guid.NewGuid().ToString();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Add(text, @class);
            }
            HistoricalDataRequest request = null;
            switch (type)
            {
                case TickType.Bid:
                    request = new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Bid);
                    break;
                case TickType.Ask:
                    request = new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Ask);
                    break;
                case TickType.Trade:
                    request = new HistoricalDataRequest(instrument, dateTime1, dateTime2, DataObjectType.Trade);
                    break;
            }
            request.RequestId = text;
            provider.Send(request);
            @class.ReadyEvent.WaitOne();
            lock (this.dictionary_0)
            {
                this.dictionary_0.Remove(text);
            }
            var ts = new TickSeries("", "");
            foreach (var data in @class.Data)
            {
                var objs = data.Objects;
                foreach (var tick in objs)
                    ts.Add((Tick)tick);
            }
            return ts;
        }