示例#1
0
        public Bar[] GetBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, DateTime endTime)
        {
            try
            {
                var forexPeriodicity = StorageConvert.ToPeriodicity(period);
                var forexPriceType   = StorageConvert.ToFxPriceType(priceType);
                var manager          = this.storage.GetOrCreateHistoryManager(symbol);

                if (this.source != null)
                {
                    // online mode
                    if (!manager.BarsAreSynchronized(this.source, symbol, forexPeriodicity, forexPriceType, startTime, endTime, false))
                    {
                        manager.SynchronizeBars(this.source, symbol, forexPeriodicity, forexPriceType, startTime, endTime, false, NullCallback);
                    }
                }

                var bars = new List <HistoryBar>();
                if (startTime < endTime)
                {
                    ForwardFillBars(manager, symbol, forexPeriodicity, forexPriceType, startTime, endTime, bars);
                }
                else
                {
                    BackwardFillBars(manager, symbol, forexPeriodicity, forexPriceType, startTime, endTime, bars);
                }

                var result = bars.Select(o => StorageConvert.ToBar(o, period)).ToArray();
                return(result);
            }
            catch (StorageHistoryNotFoundException ex)
            {
                throw new HistoryNotFoundException("GetBars", ex);
            }
        }
        void OnDownload(object sender, EventArgs e)
        {
            if (this.downloader == null)
            {
                var outputType = m_storageType.SelectedItem.ToString();
                var location   = this.m_location.Text;
                var symbol     = this.m_symbols.Text;
                var from       = this.m_dateAndTimeFrom.Value;
                var to         = this.m_dateAndTimeTo.Value;

                if (this.m_quotesType.SelectedIndex == 0)
                {
                    this.downloader = new Downloader(quoteClient, outputType, location, symbol, from, to);
                }
                else if (this.m_quotesType.SelectedIndex == 1)
                {
                    this.downloader = new Downloader(quoteClient, outputType, location, symbol, from, to, true);
                }
                else if (this.m_quotesType.SelectedIndex == 2)
                {
                    this.downloader = new Downloader(quoteClient, outputType, location, symbol, from, to, false, true);
                }
                else
                {
                    var st    = this.m_quotesType.SelectedItem.ToString();
                    var match = Regex.Match(st, "^(Bid|Ask) ([^ ]+)$");
                    if (!match.Success)
                    {
                        var message = string.Format("Unexpected string format = {0}", st);
                        throw new ArgumentException(message);
                    }
                    var stPriceType = match.Groups[1].Value;
                    var priceType   = ("Bid" == stPriceType) ? PriceType.Bid : PriceType.Ask;

                    var stBarPeriod = match.Groups[2].Value;

                    var barPeriod = new BarPeriod(stBarPeriod);
                    this.downloader = new Downloader(quoteClient, outputType, location, symbol, from, to, priceType, barPeriod);
                }

                this.downloader.Message += this.OnMessage;
                this.downloader.Finish  += this.OnFinish;
                this.downloader.Start();
                this.m_download.Text       = "Break";
                this.m_browse.Enabled      = false;
                this.m_symbols.Enabled     = false;
                this.m_quotesType.Enabled  = false;
                this.m_storageType.Enabled = false;
            }
            else
            {
                this.downloader.CancelDownload();
                this.downloader            = null;
                this.m_download.Text       = "Download";
                this.m_browse.Enabled      = true;
                this.m_symbols.Enabled     = true;
                this.m_quotesType.Enabled  = true;
                this.m_storageType.Enabled = true;
            }
        }
示例#3
0
        void BackwardFillBars(string symbol, BarPeriod periodicity, PriceType priceType, DateTime startTime, int barsNumber, ICollection <Bar> bars)
        {
            var attempts = 7 * 24;

            for (; bars.Count < barsNumber;)
            {
                var endTime = CaculateNextDateTime(startTime, periodicity, -barsNumber);
                var items   = this.GetBars(symbol, priceType, periodicity, startTime, endTime);
                if (items.Length == 0)
                {
                    if (attempts == 0)
                    {
                        return;
                    }
                    --attempts;
                }
                foreach (var element in items)
                {
                    if (bars.Count == barsNumber)
                    {
                        break;
                    }
                    bars.Add(element);
                }
                startTime = endTime;
            }
        }
示例#4
0
        public static Periodicity ToPeriodicity(BarPeriod period)
        {
            var st     = period.ToString();
            var result = Periodicity.Parse(st);

            return(result);
        }
示例#5
0
        /// <summary>
        /// Returns history bars for a specified symbol
        /// </summary>
        /// <param name="symbol">a requested symbol</param>
        /// <param name="period">a requested period</param>
        /// <returns></returns>
        public Bar[] GetBars(string symbol, BarPeriod period)
        {
            var request = string.Format("{0}:{1}", symbol, period);

            Bar[] result;
            this.requestToBars.TryGetValue(request, out result);

            if (result != null)
            {
                return(result);
            }

            var priceType = PriceType.Bid;

            if (symbol.EndsWith(AskSuffix, StringComparison.InvariantCulture))
            {
                symbol    = symbol.Substring(0, symbol.Length - AskSuffix.Length);
                priceType = PriceType.Ask;
            }

            if (this.ServerDateTime > DateTime.MinValue)
            {
                result = storage.Online.GetBars(symbol, priceType, period, this.ServerDateTime, -RequestedBarsNumber);
            }
            else
            {
                result = new Bar[0];
            }

            this.requestToBars[request] = result;

            return(result);
        }
示例#6
0
        /// <summary>
        /// The constructor takes snapshot from manager.
        /// </summary>
        /// <param name="snapshot">a snapshot instance</param>
        /// <param name="storage"></param>
        /// <param name="symbol"></param>
        /// <param name="periodicity"></param>
        /// <param name="priceType"></param>
        /// <exception cref="System.ArgumentNullException">if snapshot is null</exception>
        public Snapshot(Snapshot snapshot, DataFeedStorage storage, string symbol, BarPeriod periodicity, PriceType priceType)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }

            this.IsFeedLoggedOn   = snapshot.IsFeedLoggedOn;
            this.IsTradeLoggedOn  = snapshot.IsTradeLoggedOn;
            this.AccountInfo      = snapshot.AccountInfo;
            this.FeedSessionInfo  = snapshot.FeedSessionInfo;
            this.TradeSessionInfo = snapshot.TradeSessionInfo;
            this.TradeRecords     = snapshot.TradeRecords;
            this.Positions        = snapshot.Positions;
            this.Quotes           = snapshot.Quotes;
            this.Symbols          = snapshot.Symbols;

            this.storage     = storage;
            this.symbol      = symbol;
            this.periodicity = periodicity;
            this.priceType   = priceType;

            this.synchronizer = snapshot.synchronizer;
        }
示例#7
0
        void OnStart(object sender, EventArgs e)
        {
            var contractSize = (int)this.m_contractSize.Value;
            var symbol       = m_symbols.SelectedItem.ToString();
            var from         = this.GetFrom();
            var to           = this.GetTo();

            if (this.m_sourceType.SelectedIndex == 0)
            {
                this.exporter = new Exporter(this.m_storageLocation.Text, this.m_outputFile.Text, symbol, null, from, to, contractSize, this.m_removeDuplicateEntries.Checked, this.m_progress.Maximum);
            }
            else
            {
                var period = new BarPeriod(this.m_sourceType.SelectedItem.ToString());
                this.exporter = new Exporter(this.m_storageLocation.Text, this.m_outputFile.Text, symbol, period, from, to, contractSize, this.m_removeDuplicateEntries.Checked, this.m_progress.Maximum);
            }

            this.exporter.Progress += this.OnProgress;
            this.exporter.Finish   += this.OnFinish;

            this.m_start.Enabled                  = false;
            this.m_stop.Enabled                   = true;
            this.m_open.Enabled                   = false;
            this.m_save.Enabled                   = false;
            this.m_dateFrom.Enabled               = false;
            this.m_dateTo.Enabled                 = false;
            this.m_contractSize.Enabled           = false;
            this.m_symbols.Enabled                = false;
            this.m_removeDuplicateEntries.Enabled = false;

            this.exporter.Start();
        }
示例#8
0
        internal static string CombinedBarsRangeTime(string symbol, BarPeriod barPeriod, DateTime startTime, DateTime endTime, double barCountDbl)
        {
            var isTimeZero = FdkHelper.IsTimeZero(startTime);


            BarData[] barsDataBid;
            BarData[] barsDataAsk;
            if (FdkHelper.IsTimeZero(startTime))
            {
                var barCount = (int)barCountDbl;
                if (barCount == 0)
                {
                    barCount = FdkBars.HugeCount;
                }
                barsDataAsk = CalculateBarsForSymbolArray(symbol, PriceType.Ask, endTime, barPeriod, barCount);
                barsDataBid = CalculateBarsForSymbolArray(symbol, PriceType.Bid, endTime, barPeriod, barCount);
            }
            else
            {
                barsDataAsk = CalculateBarsForSymbolArrayRangeTime(symbol, PriceType.Ask, startTime, endTime, barPeriod);
                barsDataBid = CalculateBarsForSymbolArrayRangeTime(symbol, PriceType.Bid, startTime, endTime, barPeriod);
            }
            ReversIfNeed(barsDataAsk);
            ReversIfNeed(barsDataBid);

            var barsData = FdkBarsMerger.ProcessedBarsResult(barsDataBid, barsDataAsk);
            //LogBars(barsData);
            var bars = FdkVars.RegisterVariable(barsData, "bars");

            return(bars);
        }
示例#9
0
文件: Manager.cs 项目: hombrevrc/FDK
 /// <summary>
 /// The method takes the full snapshot of the manager state.
 /// </summary>
 /// <returns></returns>
 public Snapshot TakeSnapshot(string symbol, PriceType priceType, BarPeriod periodicity)
 {
     lock (this.synchronizer)
     {
         var result = new Snapshot(this.Snapshot, this.storage, symbol, periodicity, priceType);
         return(result);
     }
 }
示例#10
0
文件: FxDataFeed.cs 项目: ifzz/FDK
        public DataHistoryInfo GetHistoryBars(string symbol, DateTime time, int barsNumber, PriceType priceType, BarPeriod period, int timeoutInMilliseconds)
        {
            this.VerifyInitialized();

            var info = Native.FeedServer.GetHistoryBars(this.handle, symbol, time, barsNumber, priceType, period.ToString(), (uint)timeoutInMilliseconds);
            foreach (var bar in info.Bars)
            {
                bar.To = bar.From + period;
            }
            return info;
        }
示例#11
0
        public static Bar ToBar(HistoryBar bar, BarPeriod period)
        {
            var from   = bar.Time;
            var to     = from + period;
            var open   = (double)bar.Open;
            var close  = (double)bar.Close;
            var low    = (double)bar.Low;
            var high   = (double)bar.Hi;
            var volume = bar.Volume;
            var result = new Bar(from, to, open, close, low, high, volume);

            return(result);
        }
示例#12
0
        /// <summary>
        /// The method synchronizes bars.
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="priceType"></param>
        /// <param name="period"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        public void Synchronize(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, DateTime endTime)
        {
            if (this.source == null)
            {
                throw new InvalidOperationException("Can't synchronize in offline mode.");
            }

            var manager     = this.GetOrCreateHistoryManager(symbol);
            var periodicity = StorageConvert.ToPeriodicity(period);
            var fxPriceType = StorageConvert.ToFxPriceType(priceType);

            manager.SynchronizeBars(this.source, symbol, periodicity, fxPriceType, startTime, endTime, false, NullCallback);
        }
示例#13
0
 public PairBar[] GetPairBars(string symbol, BarPeriod period, DateTime startTime, int barsNumber)
 {
     try
     {
         var bids   = this.GetBars(symbol, PriceType.Bid, period, startTime, barsNumber);
         var asks   = this.GetBars(symbol, PriceType.Ask, period, startTime, barsNumber);
         var bars   = new PairBars(bids, asks, barsNumber >= 0);
         var result = bars.ToArray();
         return(result);
     }
     catch (StorageHistoryNotFoundException ex)
     {
         throw new HistoryNotFoundException("GetPairBars", ex);
     }
 }
示例#14
0
        Bar[] GetOnlineBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, int barsNumber)
        {
            var bars = new List <Bar>();

            if (barsNumber > 0)
            {
                this.ForwardFillBars(symbol, period, priceType, startTime, barsNumber, bars);
            }
            else if (barsNumber < 0)
            {
                this.BackwardFillBars(symbol, period, priceType, startTime, -barsNumber, bars);
            }

            return(bars.ToArray());
        }
示例#15
0
        public Exporter(string storageLocation, string outputFile, string symbol, BarPeriod period, DateTime from, DateTime to, double contractSize, bool removeDuplicateEntries, int progressResolution)
        {
            this.m_storageLocation = storageLocation;
            this.m_outputFile      = outputFile;
            this.m_symbol          = symbol;
            this.m_period          = period;
            this.m_from            = from;
            this.m_to           = to;
            this.m_contractSize = contractSize;
            var interval = m_to - m_from;

            this.m_interval = interval.TotalSeconds;
            this.m_removeDuplicateEntries = removeDuplicateEntries;
            this.m_progressResolution     = progressResolution;
            this.thread = new Thread(ThreadMethod);
        }
示例#16
0
        void DownloadQuote(string quoteSymbol)
        {
            //if (this.downloader == null)
            {
                var outputType = m_storageType.SelectedItem.ToString();
                var location   = currentLocation;

                var        symbol = quoteSymbol;
                var        from   = this.m_dateAndTimeFrom.Value;
                var        to     = this.m_dateAndTimeTo.Value;
                Downloader downloader;
                if (this.m_quotesType.SelectedIndex == 0)
                {
                    downloader = new Downloader(quoteClient, outputType, location, symbol, from, to);
                }
                else if (this.m_quotesType.SelectedIndex == 1)
                {
                    downloader = new Downloader(quoteClient, outputType, location, symbol, from, to, true);
                }
                else if (this.m_quotesType.SelectedIndex == 2)
                {
                    downloader = new Downloader(quoteClient, outputType, location, symbol, from, to, false, true);
                }
                else
                {
                    var st    = this.m_quotesType.SelectedItem.ToString();
                    var match = Regex.Match(st, "^(Bid|Ask) ([^ ]+)$");
                    if (!match.Success)
                    {
                        var message = string.Format("Unexpected string format = {0}", st);
                        throw new ArgumentException(message);
                    }
                    var stPriceType = match.Groups[1].Value;
                    var priceType   = ("Bid" == stPriceType) ? PriceType.Bid : PriceType.Ask;

                    var stBarPeriod = match.Groups[2].Value;

                    var barPeriod = new BarPeriod(stBarPeriod);
                    downloader = new Downloader(quoteClient, outputType, location, symbol, from, to, priceType, barPeriod);
                }

                downloader.Message += this.OnMessage;
                downloader.Finish  += this.OnFinish;
                downloadsList.Add(downloader);
            }
        }
示例#17
0
        public HistoryInfo GetBarsInfo(string symbol, PriceType priceType, BarPeriod period)
        {
            try
            {
                var source      = this.GetRemoteOrLocalSource(symbol);
                var periodicity = StorageConvert.ToPeriodicity(period);
                var priceTypeEx = StorageConvert.ToFxPriceType(priceType);
                var info        = source.GetBarsHistoryInfo(symbol, periodicity, priceTypeEx);

                var result = new HistoryInfo(info.AvailableFrom.Value, info.AvailableTo.Value);
                return(result);
            }
            catch (StorageHistoryNotFoundException ex)
            {
                throw new HistoryNotFoundException("GetBarsInfo", ex);
            }
        }
示例#18
0
        /// <summary>
        /// Imports bars to storage.
        /// </summary>
        /// <param name="symbol">Symbol of importing bars</param>
        /// <param name="period">Period of importing bars</param>
        /// <param name="bars">Importing bars</param>
        /// <param name="priceType">Price type of importing bars</param>
        /// <param name="overwriteBarChainsWithEqualTime"></param>
        public void Import(string symbol, BarPeriod period, IEnumerable <Bar> bars, PriceType priceType, bool overwriteBarChainsWithEqualTime)
        {
            var provider    = this.GetOrCreateHistoryManager(symbol);
            var fxPeriod    = StorageConvert.ToPeriodicity(period);
            var fxPriceType = StorageConvert.ToFxPriceType(priceType);
            var fxBars      = bars.Select(StorageConvert.ToHistoryBar);

            provider.ImportBars(
                fxBars,
                symbol,
                fxPeriod,
                fxPriceType,
                true,
                overwriteBarChainsWithEqualTime ? BarsImportRules.Replace : BarsImportRules.Skip,
                null
                );
        }
示例#19
0
 public Bar[] GetBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, int barsNumber)
 {
     try
     {
         if (this.source != null)
         {
             return(this.GetOnlineBars(symbol, priceType, period, startTime, barsNumber));
         }
         else
         {
             return(this.GetOfflineBars(symbol, priceType, period, startTime, barsNumber));
         }
     }
     catch (StorageHistoryNotFoundException ex)
     {
         throw new HistoryNotFoundException("GetBars", ex);
     }
 }
示例#20
0
        public MarketHistoryItemsReport <HistoryBar> QueryBarHistory(DateTime to, int maxBars, string symbol, string periodicity, FxPriceType priceType)
        {
            var type   = FxPriceType.Bid == priceType ? PriceType.Bid : PriceType.Ask;
            var period = new BarPeriod(periodicity);
            var info   = this.dataFeed.Server.GetHistoryBars(symbol, to, -maxBars, type, period);

            var result = new MarketHistoryItemsReport <HistoryBar>
            {
                Items         = info.Bars.Select(StorageConvert.ToHistoryBar).ToList(),
                AvailableFrom = info.FromAll,
                AvailableTo   = info.ToAll,
                From          = info.From.Value,
                To            = info.To.Value,
                LastTickId    = !string.IsNullOrEmpty(info.LastTickId) ? FeedTickId.Parse(info.LastTickId) : default(FeedTickId?),
                Symbol        = symbol
            };

            return(result);
        }
示例#21
0
        Bar[] GetOfflineBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, int barsNumber)
        {
            var manager     = this.storage.GetOrCreateHistoryManager(symbol);
            var fxPriceType = StorageConvert.ToFxPriceType(priceType);

            var report = manager.QueryBarHistory(startTime, -barsNumber, symbol, period.ToString(), fxPriceType);

            var items = report.Items;

            if (barsNumber > 0)
            {
                return(items.Select(o => StorageConvert.ToBar(o, period)).ToArray());
            }
            else if (barsNumber < 0)
            {
                return(items.Select(o => StorageConvert.ToBar(o, period)).Reverse().ToArray());
            }

            return(Enumerable.Empty <Bar>().ToArray());
        }
示例#22
0
        static DateTime CaculateNextDateTime(DateTime time, BarPeriod periodicity, int count)
        {
            var result = time;

            for (var step = 0; step < count; ++step)
            {
                result = result + periodicity;
            }

            for (var step = 0; step > count; --step)
            {
                if (result < TimeThreshold)
                {
                    break;
                }
                result = result - periodicity;
            }

            if (count > 0)
            {
                var next = time.AddHours(1);
                if (next > result)
                {
                    result = next;
                }
            }
            else
            {
                var next = time.AddHours(-1);
                if (next < result)
                {
                    result = next;
                }
            }

            return(result);
        }
示例#23
0
        /// <summary>
        /// Imports bars to storage.
        /// </summary>
        /// <param name="symbol">Symbol of importing bars</param>
        /// <param name="period">Period of importing bars</param>
        /// <param name="bars">Importing bars</param>
        /// <param name="priceType">Price type of importing bars</param>
        /// <param name="overwriteBarChainsWithEqualTime"></param>
        public void Import(string symbol, BarPeriod period, IEnumerable<Bar> bars, PriceType priceType, bool overwriteBarChainsWithEqualTime)
        {
            var provider = this.GetOrCreateHistoryManager(symbol);
            var fxPeriod = StorageConvert.ToPeriodicity(period);
            var fxPriceType = StorageConvert.ToFxPriceType(priceType);
            var fxBars = bars.Select(StorageConvert.ToHistoryBar);

            provider.ImportBars(
                fxBars,
                symbol,
                fxPeriod,
                fxPriceType,
                true,
                overwriteBarChainsWithEqualTime ? BarsImportRules.Replace : BarsImportRules.Skip,
                null
                );
        }
示例#24
0
文件: SmartStorage.cs 项目: ifzz/FDK
        Bar[] GetOnlineBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, int barsNumber)
        {
            var bars = new List<Bar>();
            if (barsNumber > 0)
                this.ForwardFillBars(symbol, period, priceType, startTime, barsNumber, bars);
            else if (barsNumber < 0)
                this.BackwardFillBars(symbol, period, priceType, startTime, -barsNumber, bars);

            return bars.ToArray();
        }
示例#25
0
文件: SmartStorage.cs 项目: ifzz/FDK
        Bar[] GetOfflineBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, int barsNumber)
        {
            var manager = this.storage.GetOrCreateHistoryManager(symbol);
            var fxPriceType = StorageConvert.ToFxPriceType(priceType);

            var report = manager.QueryBarHistory(startTime, -barsNumber, symbol, period.ToString(), fxPriceType);

            var items = report.Items;

            if (barsNumber > 0)
                return items.Select(o => StorageConvert.ToBar(o, period)).ToArray();
            else if (barsNumber < 0)
                return items.Select(o => StorageConvert.ToBar(o, period)).Reverse().ToArray();

            return Enumerable.Empty<Bar>().ToArray();
        }
示例#26
0
文件: FdkBars.cs 项目: ihalankou/rFdk
 static HistoryInfo GetBarsInfo(string symbol, PriceType priceType, BarPeriod period)
 {
     return(FdkHelper.Wrapper.ConnectLogic.Storage.Online.GetBarsInfo(symbol, priceType, period));
 }
示例#27
0
文件: FdkBars.cs 项目: ihalankou/rFdk
 static Bar[] CalculateBarsForSymbolArrayRangeTime(
     string symbol, PriceType priceType, DateTime startTime, DateTime endTime, BarPeriod barPeriod)
 {
     return(FdkHelper.Wrapper.ConnectLogic.Storage.Online.GetBars(symbol, priceType, barPeriod, startTime, endTime).ToArray());
 }
示例#28
0
文件: SmartStorage.cs 项目: ifzz/FDK
 public PairBar[] GetPairBars(string symbol, BarPeriod period, DateTime startTime, int barsNumber)
 {
     try
     {
         var bids = this.GetBars(symbol, PriceType.Bid, period, startTime, barsNumber);
         var asks = this.GetBars(symbol, PriceType.Ask, period, startTime, barsNumber);
         var bars = new PairBars(bids, asks);
         var result = bars.ToArray();
         return result;
     }
     catch (StorageHistoryNotFoundException ex)
     {
         throw new HistoryNotFoundException("GetPairBars", ex);
     }
 }
示例#29
0
 public Downloader(DataFeed dataFeed, Type storageType, String location, String symbol, DateTime from, DateTime to, PriceType priceType, BarPeriod period, int retries)
     : this()
 {
     this.dataFeed    = dataFeed;
     this.storageType = storageType;
     this.location    = location;
     this.symbol      = symbol;
     this.from        = from;
     this.to          = to;
     this.priceType   = priceType;
     this.period      = period;
     this.retries     = retries;
 }
示例#30
0
文件: SmartStorage.cs 项目: ifzz/FDK
 void BackwardFillBars(string symbol, BarPeriod periodicity, PriceType priceType, DateTime startTime, int barsNumber, ICollection<Bar> bars)
 {
     var attempts = 7 * 24;
     for (; bars.Count < barsNumber; )
     {
         var endTime = CaculateNextDateTime(startTime, periodicity, -barsNumber);
         var items = this.GetBars(symbol, priceType, periodicity, startTime, endTime);
         if (items.Length == 0)
         {
             if (attempts == 0)
             {
                 return;
             }
             --attempts;
         }
         foreach (var element in items)
         {
             if (bars.Count == barsNumber)
             {
                 break;
             }
             bars.Add(element);
         }
         startTime = endTime;
     }
 }
示例#31
0
文件: SmartStorage.cs 项目: ifzz/FDK
        static DateTime CaculateNextDateTime(DateTime time, BarPeriod periodicity, int count)
        {
            var result = time;

            for (var step = 0; step < count; ++step)
            {
                result = result + periodicity;
            }

            for (var step = 0; step > count; --step)
            {
                if (result < TimeThreshold)
                {
                    break;
                }
                result = result - periodicity;
            }

            if (count > 0)
            {
                var next = time.AddHours(1);
                if (next > result)
                {
                    result = next;
                }
            }
            else
            {
                var next = time.AddHours(-1);
                if (next < result)
                {
                    result = next;
                }
            }

            return result;
        }
示例#32
0
 static PairBar[] GetPairBarsSymbolArray(string symbol, BarPeriod period, DateTime startTime, int barsNumber)
 {
     return(FdkHelper.Wrapper.ConnectLogic.Storage.Online.GetPairBars(symbol, period, startTime, barsNumber).ToArray());
 }
示例#33
0
		/// <summary>
		/// The method synchronizes bars.
		/// </summary>
		/// <param name="symbol"></param>
		/// <param name="priceType"></param>
		/// <param name="period"></param>
		/// <param name="startTime"></param>
		/// <param name="endTime"></param>
		public void Synchronize(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, DateTime endTime)
		{
            if (this.source == null)
                throw new InvalidOperationException("Can't synchronize in offline mode.");

            var manager = this.GetOrCreateHistoryManager(symbol);
			var periodicity = StorageConvert.ToPeriodicity(period);
			var fxPriceType = StorageConvert.ToFxPriceType(priceType);

            manager.SynchronizeBars(this.source, symbol, periodicity, fxPriceType, startTime, endTime, false, NullCallback);
		}
示例#34
0
文件: SmartStorage.cs 项目: ifzz/FDK
 public Bar[] GetBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, int barsNumber)
 {
     try
     {
         if (this.source != null)
             return this.GetOnlineBars(symbol, priceType, period, startTime, barsNumber);
         else
             return this.GetOfflineBars(symbol, priceType, period, startTime, barsNumber);
     }
     catch (StorageHistoryNotFoundException ex)
     {
         throw new HistoryNotFoundException("GetBars", ex);
     }
 }
示例#35
0
        public static Bar ToBar(HistoryBar bar, BarPeriod period)
		{
			var from = bar.Time;
			var to = from + period;
			var open = (double)bar.Open;
			var close = (double)bar.Close;
			var low = (double)bar.Low;
			var high = (double)bar.Hi;
			var volume = bar.Volume;
			var result = new Bar(from, to, open, close, low, high, volume);

			return result;
		}
示例#36
0
        private async Task <List <CustomLineSeries> > HandleLineChart(int numberOfReturnedValues, DateTime From, DateTime To, BarPeriod period, params int[] ids)
        {
            try
            {
                var result = new List <CustomLineSeries>();
                foreach (var meterId in ids)
                {
                    var queryResult = new List <LineChartItem>();
                    using (SqlConnection connection = new SqlConnection(_connectionString))
                    {
                        var command = new SqlCommand();
                        command.Connection  = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "Bar_Chart_Meter";

                        command.Parameters.Add(new SqlParameter("@Id", meterId));
                        command.Parameters.Add(new SqlParameter("@From", From));
                        command.Parameters.Add(new SqlParameter("@Period", (int)period));
                        command.Parameters.Add(new SqlParameter("@To", To));

                        await connection.OpenAsync();

                        using (SqlDataReader reader = await command.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                queryResult.Add(new LineChartItem
                                {
                                    Name     = GetValue <string>(reader["Name"], string.Empty),
                                    entityId = GetValue <int>(reader["entityID"], 0),
                                    value    = GetValue <double>(reader["FloatVALUE"], 0.0),
                                    date     = GetValue <DateTime>(reader["DateTimeStamp"], DateTime.MinValue)
                                });
                            }
                        }
                    }
                    var meter = queryResult.FirstOrDefault(m => m.entityId == meterId);
                    if (meter != null)
                    {
                        var dataResult = new List <LineSeriesData>();
                        while (queryResult.Count <= numberOfReturnedValues)
                        {
                            queryResult.Add(new LineChartItem
                            {
                                Name     = meter.Name,
                                entityId = meter.entityId,
                                value    = 0,
                                date     = (meter.date < CleanDateTimeWeek(To)) ? DateTime.MaxValue : DateTime.MinValue
                            });
                        }

                        queryResult = queryResult.OrderByDescending(m => m.date).ToList();
                        for (int i = 0; i < queryResult.Count - 1; i++)
                        {
                            dataResult.Add(new LineSeriesData
                            {
                                Y = queryResult[i].value - queryResult[i + 1].value
                            });
                        }
                        dataResult.Reverse();

                        result.Add(new CustomLineSeries
                        {
                            Name   = meter.Name,
                            things = dataResult
                        });
                    }
                    else
                    {
                        //
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#37
0
        // In Progress
        public async Task <List <CustomLineSeries> > GetLineChartMeters(ChartDetails details, DateTime fromDate, DateTime toDate, BarPeriod period, int[] ids)
        {
            try
            {
                List <CustomLineSeries> result = new List <CustomLineSeries>();
                switch (period)
                {
                case BarPeriod.Day:
                    fromDate         = DateTime.Now.AddHours(-25);
                    fromDate         = CleanDateTime(fromDate);
                    toDate           = CleanDateTime(DateTime.Now).AddSeconds(-1);
                    details.Title    = "By Day Report";
                    details.SubTitle = "Last 24 Hours Report";

                    (details.Dates = GenerateHours()).Reverse();
                    result = await HandleLineChart(24, fromDate, toDate, period, ids);

                    break;

                case BarPeriod.Week:
                    fromDate         = DateTime.Now.AddDays(-8);
                    fromDate         = CleanDateTimeWeek(fromDate);
                    toDate           = CleanDateTimeWeek(DateTime.Now).AddSeconds(-1);
                    details.Title    = "Week Report";
                    details.SubTitle = "Last 7 Days Report";
                    (details.Dates = GenerateDays()).Reverse();
                    result = await HandleLineChart(7, fromDate, toDate, period, ids);

                    break;

                case BarPeriod.Year:
                    fromDate         = DateTime.Now.AddMonths(-13);
                    fromDate         = CleanDateTimeYear(fromDate);
                    toDate           = CleanDateTimeYear(DateTime.Now).AddSeconds(-1);
                    details.Title    = "Year Report";
                    details.SubTitle = "Last 12 Months Report";
                    (details.Dates = GenerateMonths()).Reverse();
                    result = await HandleLineChart(12, fromDate, toDate, period, ids);

                    break;

                case BarPeriod.Custom:
                    double hours = (toDate - fromDate).TotalHours;
                    if (hours >= 2 && hours < 48)     //period more than 2 hours and less than 2 days
                    {
                        details.Title    = "Hours Report";
                        details.SubTitle = "Period from " + fromDate.ToString("dd/MM/yyyy HH:mm") + " to " + toDate.ToString("dd/MM/yyyy HH:mm") + " Report";
                        fromDate         = fromDate.AddHours(-1);
                        toDate           = toDate.AddHours(1);
                        (details.Dates = GenerateHours((int)hours + 1, toDate)).Reverse();
                        result = await HandleLineChart((int)hours + 1, fromDate, toDate, BarPeriod.Day, ids);
                    }
                    else if (hours >= 48 && hours <= 720)     //period more than 2 days and less than or equal to 1 month
                    {
                        int days = (int)hours / 24 + 1;
                        details.Title    = "Days Report";
                        details.SubTitle = "Period from " + fromDate.ToString("dd/MM/yyyy") + " to " + toDate.ToString("dd/MM/yyyy") + " Report";
                        fromDate         = fromDate.AddDays(-1);
                        toDate           = toDate.AddDays(1);
                        (details.Dates = GenerateDays(days, toDate)).Reverse();
                        result = await HandleLineChart(days, fromDate, toDate, BarPeriod.Week, ids);
                    }
                    else if (hours > 720)
                    {
                        int months = (int)hours / 720 + 1;
                        details.Title    = "Months Report";
                        details.SubTitle = "Period from " + fromDate.ToString("MM/yyyy") + " to " + toDate.ToString("MM/yyyy") + " Report";
                        fromDate         = fromDate.AddMonths(-1);
                        toDate           = toDate.AddMonths(1);
                        (details.Dates = GenerateMonths(months, toDate)).Reverse();
                        result = await HandleLineChart(months, fromDate, toDate, BarPeriod.Year, ids);
                    }
                    else
                    {
                        return(null);
                    }
                    break;

                default:
                    details.Dates = null;
                    break;
                }
                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#38
0
 public Downloader(QuoteStore quoteClient, string outputType, String location, String symbol, DateTime from, DateTime to, PriceType priceType, BarPeriod period)
     : this()
 {
     this.quoteClient = quoteClient;
     this.outputType  = outputType;
     this.location    = location;
     this.symbol      = symbol;
     this.from        = from;
     this.to          = to;
     this.priceType   = priceType;
     this.period      = period;
 }
示例#39
0
文件: SmartStorage.cs 项目: ifzz/FDK
        public HistoryInfo GetBarsInfo(string symbol, PriceType priceType, BarPeriod period)
        {
            try
            {
                var source = this.GetRemoteOrLocalSource(symbol);
                var periodicity = StorageConvert.ToPeriodicity(period);
                var priceTypeEx = StorageConvert.ToFxPriceType(priceType);
                var info = source.GetBarsHistoryInfo(symbol, periodicity, priceTypeEx);

                var result = new HistoryInfo(info.AvailableFrom.Value, info.AvailableTo.Value);
                return result;
            }
            catch (StorageHistoryNotFoundException ex)
            {
                throw new HistoryNotFoundException("GetBarsInfo", ex);
            }
        }
示例#40
0
 internal static PairBar[] GetPairBarsSymbolArrayRangeTime(string symbol, BarPeriod period, DateTime startTime, DateTime endTime)
 {
     return(FdkHelper.Wrapper.ConnectLogic.Storage.Online.GetPairBars(symbol, period, startTime, endTime).ToArray());
 }
示例#41
0
 static BarData[] CalculateBarsForSymbolArrayRangeTime(
     string symbol, PriceType priceType, DateTime startTime, DateTime endTime, BarPeriod barPeriod)
 {
     return(FdkHelper.Storage.Online.GetBars(symbol, priceType, barPeriod, startTime, endTime)
            .SelectToArray(bar => bar.ToBarData()));
 }
示例#42
0
        public static Periodicity ToPeriodicity(BarPeriod period)
		{
			var st = period.ToString();
			var result = Periodicity.Parse(st);
			return result;
		}
示例#43
0
文件: SmartStorage.cs 项目: ifzz/FDK
        public Bar[] GetBars(string symbol, PriceType priceType, BarPeriod period, DateTime startTime, DateTime endTime)
        {
            try
            {
                var forexPeriodicity = StorageConvert.ToPeriodicity(period);
                var forexPriceType = StorageConvert.ToFxPriceType(priceType);
                var manager = this.storage.GetOrCreateHistoryManager(symbol);

                if (this.source != null)
                {
                    // online mode
                    if (!manager.BarsAreSynchronized(this.source, symbol, forexPeriodicity, forexPriceType, startTime, endTime, false))
                        manager.SynchronizeBars(this.source, symbol, forexPeriodicity, forexPriceType, startTime, endTime, false, NullCallback);
                }

                var bars = new List<HistoryBar>();
                if (startTime < endTime)
                    ForwardFillBars(manager, symbol, forexPeriodicity, forexPriceType, startTime, endTime, bars);
                else
                    BackwardFillBars(manager, symbol, forexPeriodicity, forexPriceType, startTime, endTime, bars);

                var result = bars.Select(o => StorageConvert.ToBar(o, period)).ToArray();
                return result;
            }
            catch (StorageHistoryNotFoundException ex)
            {
                throw new HistoryNotFoundException("GetBars", ex);
            }
        }