public static string GetBarDuration(BarDuration duration)
 {
     string durName = "";
     switch (duration)
     {
         case BarDuration.OneMinute:
             durName = "1 Minute";
             break;
         case BarDuration.ThreeMinute:
             durName = "3 Minute";
             break;
         case BarDuration.FiveMinute:
             durName = "5 Minute";
             break;
         case BarDuration.TenMinute:
             durName = "10 Minute";
             break;
         case BarDuration.ThirtyMinute:
             durName = "30 Minute";
             break;
         case BarDuration.Hourly:
             durName = "60 Minute";
             break;
         case BarDuration.Daily:
             durName = "Daily";
             break;
     }
     return durName;
 }
Exemple #2
0
        public static int GetNumberOfDays(BarDuration minutes)
        {
            switch (minutes)
            {
            case BarDuration.OneMinute:
                return(5);

            case BarDuration.ThreeMinute:
                return(15);

            case BarDuration.FiveMinute:
                return(25);

            case BarDuration.TenMinute:
                return(60);

            case BarDuration.ThirtyMinute:
                return(150);

            case BarDuration.Hourly:
                return(210);

            case BarDuration.Daily:
                return(730);        // 2 years

            default:
                return(100);
            }
        }
Exemple #3
0
        public static int GetTimeDuration(BarDuration duration)
        {
            switch (duration)
            {
            case BarDuration.OneMinute:
                return(1);

            case BarDuration.ThreeMinute:
                return(3);

            case BarDuration.FiveMinute:
                return(5);

            case BarDuration.TenMinute:
                return(10);

            case BarDuration.ThirtyMinute:
                return(30);

            case BarDuration.Hourly:
                return(60);

            case BarDuration.Daily:
                return(1440);

            default:
                return(0);
            }
        }
Exemple #4
0
        public static int GetNumberOfBars(BarDuration minutes)
        {
            switch (minutes)
            {
            case BarDuration.OneMinute:
                return(7200);       // 5 days

            case BarDuration.ThreeMinute:
                return(7200);       // 15 days

            case BarDuration.FiveMinute:
                return(7200);       // 25 days

            case BarDuration.TenMinute:
                return(8640);       // 60 days

            case BarDuration.ThirtyMinute:
                return(7200);       // 150 days

            case BarDuration.Hourly:
                return(5040);       // 210 days

            case BarDuration.Daily:
                return(730);        // 2 years

            default:
                return(100);
            }
        }
        private void ProcessChangeDuration(BarDuration duration)
        {
            barDuration = duration;

            /*
             *
             * BarTimeFrame = TimeSpan.FromMinutes(ChartUtility.GetTimeDuration(barDuration)).TotalSeconds;
             * InitialRangeNeedsCalculation = true;
             *
             * if (barDuration == BarDuration.Daily)
             *  XLabelFormatter = new CustomDateLabelFormatter();
             * else
             *  XLabelFormatter = new CustomDateTimeLabelFormatter();
             *
             * string durName = ChartUtility.GetBarDuration(barDuration);
             * ChangeChecked(durName);
             *
             * VolumeData.Clear();
             * _bidAskVolumeCumSeries.Clear();
             * _bidVolumeSeries.Clear();
             * _askVolumeSeries.Clear();
             * bidAskCumulativeVolume = 0;
             *
             * var keys = symbolToDataSeriesMap.Keys.ToList();
             * foreach (string key in keys)
             * {
             *  OhlcDataSeries<DateTime, double> newSeries = new OhlcDataSeries<DateTime, double>();
             *  symbolToDataSeriesMap[key] = newSeries;
             *
             *  XyDataSeries<DateTime, double> newLineSeries = new XyDataSeries<DateTime, double>();
             *  symbolToXYDataSeriesMap[key] = newLineSeries;
             *
             *  if (instrumentToRenderableSeriesMap.ContainsKey(key))
             *  {
             *      foreach (IRenderableSeries rSeries in instrumentToRenderableSeriesMap[key])
             *      {
             *          rSeries.DataSeries = newSeries;
             *      }
             *  }
             *  chartForm.sciChartControl.priceChart.InvalidateElement();
             *
             *  int numBars = (int)Math.Ceiling((DateTime.Now - DateTime.Now.AddDays(-(ChartUtility.GetNumberOfDays(barDuration) + 1))).TotalMinutes / ChartUtility.GetTimeDuration(barDuration));
             *
             *  if (synInstrumentMap.ContainsKey(key))
             *  {
             *      foreach (var symbol in synInstrumentMap[key].symbolToVarMap.Keys)
             *          ChartDataServerConnector.getInstance().RequestData(symbol, ChartUtility.GetTimeDuration(barDuration), "", "", numBars, RecvHistoricalData);
             *
             *      tempSyntheticInstrument = key;
             *      inSyntheticMode = true;
             *  }
             *  else
             *  {
             *      ChartDataServerConnector.getInstance().RequestData(key, ChartUtility.GetTimeDuration(barDuration), "", "", numBars, RecvHistoricalData);
             *  }
             * }
             */
        }
        /// <summary>
        /// Gets list of historical bars for single asset from Alpaca REST API endpoint.
        /// </summary>
        /// <param name="symbol">Asset name for data retrieval.</param>
        /// <param name="barDuration">Single historical bar duration.</param>
        /// <param name="startTimeInclusive">Start time for filtering (inclusive).</param>
        /// <param name="endTimeInclusive">End time for filtering (inclusive).</param>
        /// <returns>Read-only list of historical bars for specified asset.</returns>
        public Task <IAssetBars> ListBarsAsync(
            String symbol,
            BarDuration barDuration,
            DateTime?startTimeInclusive = null,
            DateTime?endTimeInclusive   = null)
        {
            var builder = new UriBuilder(_alpacaHttpClient.BaseAddress)
            {
                Path  = $"v1/assets/{symbol}/bars",
                Query = new QueryBuilder()
                        .AddParameter("timeframe ", barDuration.ToEnumString())
                        .AddParameter("start_dt", startTimeInclusive)
                        .AddParameter("end_dt", endTimeInclusive)
            };

            return(getSingleObjectAsync <IAssetBars, JsonAssetBars>(_alpacaHttpClient, builder));
        }
        /// <summary>
        /// Gets list of historical bars for several assets from Alpaca REST API endpoint.
        /// </summary>
        /// <param name="symbols">List of asset names for data retrieval.</param>
        /// <param name="barDuration">Single historical bar duration.</param>
        /// <param name="startTimeInclusive">Start time for filtering (inclusive).</param>
        /// <param name="endTimeInclusive">End time for filtering (inclusive).</param>
        /// <returns>Read-only list of historical bars lists for specified assets.</returns>
        public Task <IEnumerable <IAssetBars> > ListBarsAsync(
            IEnumerable <String> symbols,
            BarDuration barDuration,
            DateTime?startTimeInclusive = null,
            DateTime?endTimeInclusive   = null)
        {
            var builder = new UriBuilder(_alpacaHttpClient.BaseAddress)
            {
                Path  = "v1/bars",
                Query = new QueryBuilder()
                        .AddParameter("symbols", String.Join(",", symbols))
                        .AddParameter("timeframe", barDuration.ToEnumString())
                        .AddParameter("start_dt", startTimeInclusive)
                        .AddParameter("end_dt", endTimeInclusive)
            };

            return(getObjectsListAsync <IAssetBars, JsonAssetBars>(_alpacaHttpClient, builder));
        }
Exemple #8
0
        public async void ListMultiAssetBarsWorks(
            BarDuration barDuration,
            Int32 daysLookback)
        {
            var endTimeInclusive   = DateTime.UtcNow;
            var startTimeInclusive = endTimeInclusive
                                     .Subtract(TimeSpan.FromDays(daysLookback));

            var collection = await _restClient.ListBarsAsync(
                new [] { "AAPL", "GOOG", "MSFT" },
                barDuration, startTimeInclusive, endTimeInclusive);

            Assert.NotNull(collection);

            var list = collection.ToList();

            Assert.NotEmpty(list);
            Assert.Equal(3, list.Count);
        }
Exemple #9
0
        public async void ListSingleAssetBarsWorks(
            BarDuration barDuration,
            Int32 daysLookback)
        {
            var endTimeInclusive   = DateTime.UtcNow;
            var startTimeInclusive = endTimeInclusive
                                     .Subtract(TimeSpan.FromDays(daysLookback));

            var bars = await _restClient.ListBarsAsync(
                "AAPL", barDuration,
                startTimeInclusive, endTimeInclusive);

            Assert.NotNull(bars);
            Assert.Equal("AAPL", bars.Symbol);

            Assert.NotNull(bars.Items);
            Assert.NotEmpty(bars.Items);

            var first = bars.Items.First();
            var last  = bars.Items.Last();

            Assert.True(first.Time < last.Time);
        }
Exemple #10
0
        public static string GetBarDuration(BarDuration duration)
        {
            string durName = "";

            switch (duration)
            {
            case BarDuration.OneMinute:
                durName = "1 Minute";
                break;

            case BarDuration.ThreeMinute:
                durName = "3 Minute";
                break;

            case BarDuration.FiveMinute:
                durName = "5 Minute";
                break;

            case BarDuration.TenMinute:
                durName = "10 Minute";
                break;

            case BarDuration.ThirtyMinute:
                durName = "30 Minute";
                break;

            case BarDuration.Hourly:
                durName = "60 Minute";
                break;

            case BarDuration.Daily:
                durName = "Daily";
                break;
            }
            return(durName);
        }
        private void PreLoadDailyDataImpl(Instrument instrument, BarDuration  duration)
        {
            BarSeries series = null;
            string id = instrument.ToIdentifier();
            Dictionary<string, BarSeries> dictionaryToUse = null;

            if (duration == BarDuration.Daily)
                dictionaryToUse = dailyBarSeriesDictionary;
            else if (duration == BarDuration.Minutely)
                dictionaryToUse = minutelyBarSeriesDictionary;
            else
                throw new ArgumentOutOfRangeException("duration", duration, "Incorrect value for duration");

            dictionaryToUse.TryGetValue(id, out series);
            if (null != series && series.Count >= 1)
                return;

            DateTime start = DateTime.Now;
            if (duration == BarDuration.Daily)
                series = GetHistoricalBars("IB", Instrument, DateTime.Now.AddDays(-60), DateTime.Now, PeriodConstants.PERIOD_DAILY);
            else if (duration == BarDuration.Minutely)
                series = GetHistoricalBars("IB", instrument, DateTime.Now.AddDays(-5), DateTime.Now, PeriodConstants.PERIOD_MINUTE);
            DateTime end = DateTime.Now;

            LoggingUtility.WriteDebug(LoggingConfig, string.Format("Took {0}ms to retrieve data from IB for {1} data", end.Subtract(start).TotalMilliseconds, duration));

            dictionaryToUse[id] = series;

            start = DateTime.Now;
            foreach (Bar currentBar in series)
            {
                Bars.Add(currentBar);
                if (PersistHistoricalData)
                    DataManager.Add(Instrument, currentBar);
            }
            end = DateTime.Now;

            LoggingUtility.WriteDebug(LoggingConfig, string.Format("Took {0}ms to load data into memory for {1} data", end.Subtract(start).TotalMilliseconds, duration));
        }
        private void PreLoadDataImpl(BarDuration duration)
        {
            BarSeries series = null;

            DateTime start = DateTime.Now;
            if (duration == BarDuration.Daily)
            {
                DailyBarSeries = GetHistoricalBars("IB", Instrument, DateTime.Now.AddDays(-60), DateTime.Now,
                                                   PeriodConstants.PERIOD_DAILY);
                series = DailyBarSeries;
            }
            else if (duration == BarDuration.Minutely)
            {
                int dayToGet = Math.Abs(DaysToGoBackForMinutelyData)*-1;
                DateTime startDateForMinutelyData = EffectiveValidityTriggerTime.Date.AddDays(dayToGet);
                if (DaysToGoBackForMinutelyData != 0)
                {
                    startDateForMinutelyData = EffectiveValidityTriggerTime.Date.AddDays(dayToGet);
                }
                else
                {
                    dayToGet = -2;
                    startDateForMinutelyData = EffectiveValidityTriggerTime.Date.AddDays(dayToGet);
                    bool isHoliday = startDateForMinutelyData.Date.DayOfWeek == DayOfWeek.Saturday ||
                                     startDateForMinutelyData.Date.DayOfWeek == DayOfWeek.Sunday;

                    while (isHoliday)
                    {
                        startDateForMinutelyData = startDateForMinutelyData.AddDays(-1);
                        isHoliday = startDateForMinutelyData.Date.DayOfWeek == DayOfWeek.Saturday ||
                                    startDateForMinutelyData.Date.DayOfWeek == DayOfWeek.Sunday;
                    }
                }

                LoggingUtility.WriteDebug(LoggingConfig, string.Format("Trying to retrieve minutely data starting from {0} from IB", startDateForMinutelyData));

                MinutelyBarSeries = GetHistoricalBars("IB", Instrument, startDateForMinutelyData, DateTime.Now,
                                                      PeriodConstants.PERIOD_MINUTE);
                series = MinutelyBarSeries;
            }
            DateTime end = DateTime.Now;

            int retrievedBarCount = 0;
            if (null != series)
                retrievedBarCount = series.Count;

            LoggingUtility.WriteDebug(LoggingConfig,
                                      string.Format(
                                          "Took {0}ms to retrieve data from IB for {1} data. Total bars retrieved: {2}",
                                          end.Subtract(start).TotalMilliseconds, duration, retrievedBarCount));

            start = DateTime.Now;
            foreach (Bar currentBar in series)
            {
                Bars.Add(currentBar);
                if (PersistHistoricalData)
                    DataManager.Add(Instrument, currentBar);
            }
            end = DateTime.Now;

            LoggingUtility.WriteDebug(LoggingConfig, string.Format("Took {0}ms to load data into memory for {1} data", end.Subtract(start).TotalMilliseconds, duration));
        }
 public static int GetNumberOfBars(BarDuration minutes)
 {
     switch (minutes)
     {
         case BarDuration.OneMinute:
             return 7200;    // 5 days
         case BarDuration.ThreeMinute:
             return 7200;    // 15 days
         case BarDuration.FiveMinute:
             return 7200;    // 25 days
         case BarDuration.TenMinute:
             return 8640;    // 60 days
         case BarDuration.ThirtyMinute:
             return 7200;    // 150 days
         case BarDuration.Hourly:
             return 5040;    // 210 days
         case BarDuration.Daily:
             return 730;     // 2 years
         default:
             return 100;
     }
 }
Exemple #14
0
        public static bool GetBucket(DateTime tradeDate, BarDuration duration, out DateTime retDate)
        {
            retDate = new DateTime();
            bool valid = true;

            try
            {
                int tMin = tradeDate.Minute;
                int iRet = 0;
                switch (duration)
                {
                case BarDuration.OneMinute:
                    iRet    = tradeDate.Minute;
                    retDate = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day, tradeDate.Hour, tradeDate.Minute, 0);
                    break;

                case BarDuration.ThreeMinute:
                    iRet = tMin + (3 - tMin % 3);
                    break;

                case BarDuration.FiveMinute:
                    iRet = tMin + (5 - tMin % 5);
                    break;

                case BarDuration.TenMinute:
                    iRet = tMin + (10 - tMin % 10);
                    break;

                case BarDuration.ThirtyMinute:
                    iRet = tMin + (30 - tMin % 30);
                    break;

                case BarDuration.Hourly:
                    iRet = tMin + (60 - tMin % 60);
                    break;

                case BarDuration.Daily:
                    if (tradeDate.Hour < 17)
                    {
                        iRet    = tradeDate.Day;
                        retDate = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day, 0, 0, 0);
                    }
                    else
                    {
                        DateTime tDate = tradeDate.AddDays(1);
                        retDate = new DateTime(tDate.Year, tDate.Month, tDate.Day, 0, 0, 0);
                    }
                    break;
                }

                if (duration != BarDuration.OneMinute && duration != BarDuration.Daily)
                {
                    if (iRet == 60)
                    {
                        iRet = 0;
                    }

                    retDate = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day, tradeDate.Hour, iRet, 0);
                    if (iRet == 0)
                    {
                        retDate = retDate.AddHours(1);
                    }
                }
                if (retDate == DateTime.MinValue)
                {
                    valid = false;
                }
            }
            catch (Exception err)
            {
                Alerting.Alert.LoggerHome.GetLogger(new ChartUtility()).Error("Error in GetBucket " + tradeDate.ToString() + "," + duration.ToString(), err);
                valid = false;
            }

            return(valid);
        }
 public static int GetNumberOfDays(BarDuration minutes)
 {
     switch (minutes)
     {
         case BarDuration.OneMinute:
             return 5;
         case BarDuration.ThreeMinute:
             return 15;
         case BarDuration.FiveMinute:
             return 25;
         case BarDuration.TenMinute:
             return 60;
         case BarDuration.ThirtyMinute:
             return 150;
         case BarDuration.Hourly:
             return 210;
         case BarDuration.Daily:
             return 730;     // 2 years
         default:
             return 100;
     }
 }
 public static int GetTimeDuration(BarDuration duration)
 {
     switch (duration)
     {
         case BarDuration.OneMinute:
             return 1;
         case BarDuration.ThreeMinute:
             return 3;
         case BarDuration.FiveMinute:
             return 5;
         case BarDuration.TenMinute:
             return 10;
         case BarDuration.ThirtyMinute:
             return 30;
         case BarDuration.Hourly:
             return 60;
         case BarDuration.Daily:
             return 1440;
         default:
             return 0;
     }
 }
        private void ProcessChangeDuration(BarDuration duration)
        {
            barDuration = duration;
            /*

            BarTimeFrame = TimeSpan.FromMinutes(ChartUtility.GetTimeDuration(barDuration)).TotalSeconds;
            InitialRangeNeedsCalculation = true;

            if (barDuration == BarDuration.Daily)
                XLabelFormatter = new CustomDateLabelFormatter();
            else
                XLabelFormatter = new CustomDateTimeLabelFormatter();

            string durName = ChartUtility.GetBarDuration(barDuration);
            ChangeChecked(durName);

            VolumeData.Clear();
            _bidAskVolumeCumSeries.Clear();
            _bidVolumeSeries.Clear();
            _askVolumeSeries.Clear();
            bidAskCumulativeVolume = 0;

            var keys = symbolToDataSeriesMap.Keys.ToList();
            foreach (string key in keys)
            {
                OhlcDataSeries<DateTime, double> newSeries = new OhlcDataSeries<DateTime, double>();
                symbolToDataSeriesMap[key] = newSeries;

                XyDataSeries<DateTime, double> newLineSeries = new XyDataSeries<DateTime, double>();
                symbolToXYDataSeriesMap[key] = newLineSeries;

                if (instrumentToRenderableSeriesMap.ContainsKey(key))
                {
                    foreach (IRenderableSeries rSeries in instrumentToRenderableSeriesMap[key])
                    {
                        rSeries.DataSeries = newSeries;
                    }
                }
                chartForm.sciChartControl.priceChart.InvalidateElement();

                int numBars = (int)Math.Ceiling((DateTime.Now - DateTime.Now.AddDays(-(ChartUtility.GetNumberOfDays(barDuration) + 1))).TotalMinutes / ChartUtility.GetTimeDuration(barDuration));

                if (synInstrumentMap.ContainsKey(key))
                {
                    foreach (var symbol in synInstrumentMap[key].symbolToVarMap.Keys)
                        ChartDataServerConnector.getInstance().RequestData(symbol, ChartUtility.GetTimeDuration(barDuration), "", "", numBars, RecvHistoricalData);

                    tempSyntheticInstrument = key;
                    inSyntheticMode = true;
                }
                else
                {
                    ChartDataServerConnector.getInstance().RequestData(key, ChartUtility.GetTimeDuration(barDuration), "", "", numBars, RecvHistoricalData);
                }
            }
            */
        }
 private void ChangeDuration(BarDuration barDuration)
 {
     this.ProcessChangeDuration(barDuration);
 }
 private void ChangeDuration(BarDuration barDuration)
 {
     this.ProcessChangeDuration(barDuration);
 }
        public static bool GetBucket(DateTime tradeDate, BarDuration duration, out DateTime retDate)
        {
            retDate = new DateTime();
            bool valid = true;

            try
            {
                int tMin = tradeDate.Minute;
                int iRet = 0;
                switch (duration)
                {
                    case BarDuration.OneMinute:
                        iRet = tradeDate.Minute;
                        retDate = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day, tradeDate.Hour, tradeDate.Minute, 0);
                        break;

                    case BarDuration.ThreeMinute:
                        iRet = tMin + (3 - tMin % 3);
                        break;

                    case BarDuration.FiveMinute:
                        iRet = tMin + (5 - tMin % 5);
                        break;

                    case BarDuration.TenMinute:
                        iRet = tMin + (10 - tMin % 10);
                        break;

                    case BarDuration.ThirtyMinute:
                        iRet = tMin + (30 - tMin % 30);
                        break;

                    case BarDuration.Hourly:
                        iRet = tMin + (60 - tMin % 60);
                        break;

                    case BarDuration.Daily:
                        if (tradeDate.Hour < 17)
                        {
                            iRet = tradeDate.Day;
                            retDate = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day, 0, 0, 0);
                        }
                        else
                        {
                            DateTime tDate = tradeDate.AddDays(1);
                            retDate = new DateTime(tDate.Year, tDate.Month, tDate.Day, 0, 0, 0);
                        }
                        break;
                }

                if (duration != BarDuration.OneMinute && duration != BarDuration.Daily)
                {
                    if (iRet == 60)
                        iRet = 0;

                    retDate = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day, tradeDate.Hour, iRet, 0);
                    if (iRet == 0)
                        retDate = retDate.AddHours(1);
                }
                if (retDate == DateTime.MinValue)
                    valid = false;
            }
            catch (Exception err)
            {
                Alerting.Alert.LoggerHome.GetLogger(new ChartUtility()).Error("Error in GetBucket " + tradeDate.ToString() + "," + duration.ToString(), err);
                valid = false;
            }

            return valid;
        }