예제 #1
0
        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));
        }
예제 #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bar"></param>
        /// <returns></returns>
        protected Bar GetPreviousBar(Instrument instrument, Bar bar, int period)
        {
            Bar retVal = null;
            BarSeries barsToUse = null;
            string instId = instrument.ToIdentifier();
            Dictionary<string, BarSeries> dictionaryToUse = null;

            bool isSessionOpenBar = bar.IsSessionOpenBar(Instrument.Type);
            bool isDailyPeriod = period == PeriodConstants.PERIOD_DAILY;

            if (isDailyPeriod) // || isSessionOpenBar
                dictionaryToUse = dailyBarSeriesDictionary;
            else
                dictionaryToUse = minutelyBarSeriesDictionary;

            barsToUse = dictionaryToUse[instId];

            if (barsToUse.Count > 0)
            {
                int idx = 0;
                bool found = false;

                while (!found && idx <= barsToUse.Count - 1)
                {
                    Bar prevBar = barsToUse.Ago(idx);
                    if ((prevBar.EndTime <= bar.BeginTime) && prevBar.IsWithinRegularTradingHours(Instrument.Type))
                    {
                        if (isSessionOpenBar || isDailyPeriod)
                        {
                            found = DateTime.Today.Subtract(prevBar.BeginTime.Date).TotalDays >= 1;
                            if (!found && DateTime.Now.IsPastRegularTradingHours(Instrument.Type))
                                found = DateTime.Today.Subtract(prevBar.BeginTime.Date).TotalDays >= 0;
                        }
                        else
                        {
                            found = true;
                        }
                    }

                    if (found)
                        retVal = prevBar;
                    else
                        idx++;
                }
            }

            if (retVal == null)
                throw new ApplicationException(string.Format("Count not retreive a period {0} bar to {1}", period, bar));

            LoggingUtility.WriteDebug(LoggingConfig, string.Format("Previous closing bar was {0}", retVal));

            return retVal;
        }
예제 #3
0
        protected Bar GetPreviousDayBar(Instrument instrument)
        {
            Bar retVal = null;
            string instId = instrument.ToIdentifier();
            BarSeries barsToUse = dailyBarSeriesDictionary[instId];

            if (barsToUse.Count > 0)
            {
                int idx = 0;
                bool found = false;

                while (!found && idx <= barsToUse.Count - 1)
                {
                    Bar prevBar = barsToUse.Ago(idx);
                    if (prevBar.EndTime.Date <= DateTime.Today)
                    {
                        if (prevBar.IsWithinRegularTradingHours(Instrument.Type))
                        {
                            found = DateTime.Today.Subtract(prevBar.BeginTime.Date).TotalDays >= 1;
                            if (!found && DateTime.Now.IsPastRegularTradingHours(Instrument.Type))
                                found = DateTime.Today.Subtract(prevBar.BeginTime.Date).TotalDays >= 0;
                        }
                    }

                    if (found)
                        retVal = prevBar;
                    else
                        idx++;
                }
            }

            if (retVal == null)
                throw new ApplicationException(string.Format("Count not retreive a daily bar prior to {0}", DateTime.Today));

            LoggingUtility.WriteDebug(LoggingConfig, string.Format("Previous closing bar was {0}", retVal));

            return retVal;
        }
예제 #4
0
        protected ATR GetAtr(Instrument instrument, int period)
        {
            BarSeries dailyBarSeries = null;

            string instId = instrument.ToIdentifier();
            dailyBarSeriesDictionary.TryGetValue(instId, out dailyBarSeries);

            if (dailyBarSeries == null || dailyBarSeries.Count <= 0)
                throw new ApplicationException("Daily bar series has not been initialized");

            string atrId = string.Format("{0}:{1}", instId, period);

            if (!atrDictionary.ContainsKey(atrId))
            {
                lock (LockObject)
                {
                    if (!atrDictionary.ContainsKey(atrId))
                    {
                        ATR atr = new ATR(dailyBarSeries, period);
                        atrDictionary.Add(atrId, atr);
                    }
                }
            }

            return atrDictionary[atrId];
        }