Esempio n. 1
0
        private void PopulateSectorTrendIndices(PriceBarSize priceBarSize)
        {
            foreach (string sector in Settings.Instance.MarketSectors)
            {
                Log(new LogMessage("IndexManager", $"Populating Trend Index [{sector} {priceBarSize.ToString()}]"));
                //
                // If a trend with the same identifiers was loaded, find and remove from local list
                //
                var existingTrendIndex =
                    SectorTrends.Find(x => x.IndexName == sector &&
                                      x.TrendPriceBarSize == priceBarSize &&
                                      x.IndexSwingpointBarCount == Settings.Instance.Sector_Trend_Bar_Count);

                if (existingTrendIndex != null)
                {
                    SectorTrends.Remove(existingTrendIndex);
                }

                //
                // Create and add the new trend to the list
                //
                var newTrend = SectorTrends.AddAndReturn(CreateSectorIndex(sector, RefDataManager.Instance.GetAllSecurities(), priceBarSize));

                //
                // Save to database - this will overwrite an existing trend with the same identifiers
                //
                Database.SetTrendIndex(newTrend);
            }

            Log(new LogMessage("IndexManager", $"Trend Index Population Complete"));
        }
Esempio n. 2
0
        public static PriceBar GetContainingBar(PriceBar smallerPriceBar, List <PriceBar> largerPriceBars)
        {
            PriceBarSize smallerBarType = smallerPriceBar.BarSize;
            PriceBarSize largerBarSize  = largerPriceBars.FirstOrDefault().BarSize;

            if (smallerBarType.ToInt() > largerBarSize.ToInt())
            {
                throw new UnknownErrorException();
            }

            // Convert the smaller bar date time to the start date of the larger bar
            switch (largerBarSize)
            {
            case PriceBarSize.Daily:
                return(smallerPriceBar);

            case PriceBarSize.Weekly:
                var newWeekDate = Calendar.FirstTradingDayOfWeek(smallerPriceBar.BarDateTime);
                return(largerPriceBars.SingleOrDefault(x => x.BarDateTime == newWeekDate));

            case PriceBarSize.Monthly:
                var newMonthDate = Calendar.FirstTradingDayOfMonth(smallerPriceBar.BarDateTime);
                return(largerPriceBars.SingleOrDefault(x => x.BarDateTime == newMonthDate));

            case PriceBarSize.Quarterly:
                var newQuarterDate = Calendar.FirstTradingDayOfQuarter(smallerPriceBar.BarDateTime);
                return(largerPriceBars.SingleOrDefault(x => x.BarDateTime == newQuarterDate));

            default:
                break;
            }
            return(null);
        }
Esempio n. 3
0
        public TrendIndex GetTrendIndexBySector(string sector, PriceBarSize priceBarSize)
        {
            if (Settings.Instance.MarketSectors.Find(x => x.ToUpper() == sector.ToUpper()) == null)
            {
                return(null);
            }

            if (SectorTrends == null)
            {
                PopulateSectorTrendIndices(priceBarSize);
            }

            var ret = SectorTrends.Find(x => x.IndexName == sector &&
                                        x.TrendPriceBarSize == priceBarSize &&
                                        x.IndexSwingpointBarCount == Settings.Instance.Sector_Trend_Bar_Count);

            if (ret == null)
            {
                PopulateSectorTrendIndices(priceBarSize);
            }

            return(SectorTrends.Find(x => x.IndexName == sector &&
                                     x.TrendPriceBarSize == priceBarSize &&
                                     x.IndexSwingpointBarCount == Settings.Instance.Sector_Trend_Bar_Count));
        }
Esempio n. 4
0
        public TrendIndex GetTrendIndex(string sector, PriceBarSize priceBarSize, int swingPointBarCount, bool create = true)
        {
            using (var db = new IndexDatabaseContext())
            {
                var ret = (from index in db.TrendIndices.AsNoTracking().Include(x => x.IndexEntries)
                           where index.IndexName == sector
                           where index.IndexSwingpointBarCount == swingPointBarCount
                           where index.TrendPriceBarSize == priceBarSize
                           select index).FirstOrDefault();

                if (ret != null || !create)
                    return ret;

                db.TrendIndices.Add(new TrendIndex(sector, swingPointBarCount, priceBarSize));
                db.SaveChanges();

                ret = (from index in db.TrendIndices.AsNoTracking().Include(x => x.IndexEntries)
                       where index.IndexName == sector
                       where index.IndexSwingpointBarCount == swingPointBarCount
                       where index.TrendPriceBarSize == priceBarSize
                       select index).FirstOrDefault();
                if (ret == null)
                    Console.WriteLine();

                return ret;
            }
        }
Esempio n. 5
0
 public Signal(Security security, PriceBarSize barsize, DateTime signalDate, SignalAction signalAction, double signalStrength = 0)
 {
     Security       = security ?? throw new ArgumentNullException(nameof(security));
     SignalBarSize  = barsize;
     SignalDate     = signalDate;
     SignalAction   = signalAction;
     SignalStrength = signalStrength;
 }
Esempio n. 6
0
        public void LoadSecurity(Security security, PriceBarSize priceBarSize, DateTime?start = null, DateTime?end = null)
        {
            if (security == null || security.DailyPriceBarData.Count == 0)
            {
                return;
            }

            this.Security     = security;
            this.PriceBarSize = priceBarSize;
            this.Start        = start ?? Security.GetFirstBar(PriceBarSize).BarDateTime;
            this.End          = end ?? Security.GetLastBar(PriceBarSize).BarDateTime;

            UpdateInfo();
        }
Esempio n. 7
0
        private void UpdateSectorTrendIndex(string sectorName, PriceBarSize priceBarSize)
        {
            Log(new LogMessage("IndexManager", $"Updating Trend Index [{sectorName} {priceBarSize.ToString()} {Settings.Instance.Sector_Trend_Bar_Count}]"));

            //
            // Find existing trend to update, or skip if none is found
            //
            var existingTrendIndex =
                SectorTrends.Find(x => x.IndexName == sectorName &&
                                  x.TrendPriceBarSize == priceBarSize &&
                                  x.IndexSwingpointBarCount == Settings.Instance.Sector_Trend_Bar_Count);

            TrendIndex updateIndexTrend = null;

            //
            // If the index exists, remove from local list, update, and re-add; otherwise create a new one and add
            //
            if (existingTrendIndex == null)
            {
                updateIndexTrend = CreateSectorIndex(sectorName, RefDataManager.Instance.GetAllSecurities(), priceBarSize);

                if (updateIndexTrend != null)
                {
                    SectorTrends.Add(updateIndexTrend);
                }
            }
            else
            {
                SectorTrends.Remove(existingTrendIndex);

                updateIndexTrend = UpdateSectorIndex(existingTrendIndex,
                                                     RefDataManager.Instance.GetAllSecurities().Where(x => x.Sector == sectorName).ToList());

                if (updateIndexTrend != null)
                {
                    SectorTrends.Add(updateIndexTrend);
                }
            }

            //
            // Save to database - this will overwrite an existing trend with the same identifiers
            //
            if (updateIndexTrend != null)
            {
                Database.SetTrendIndex(updateIndexTrend);
            }
        }
Esempio n. 8
0
 public SecurityTrendInfoPanel(Security security, PriceBarSize priceBarSize, DateTime?start = null, DateTime?end = null)
 {
     this.InitializeMe();
     LoadSecurity(security, priceBarSize, start, end);
 }
Esempio n. 9
0
        public void LoadSecurity(Security security, PriceBarSize priceBarSize, int swingpointBarCount)
        {
            this.Security = security;

            ChartPanel.LoadSecurity(this.Security, priceBarSize, swingpointBarCount);
        }
Esempio n. 10
0
 private static DateTime LatestDate(IEnumerable <Security> securities, PriceBarSize priceBarSize)
 {
     return((from sec in securities
             where sec.DailyPriceBarData.Count > 0
             select sec.GetLastBar(Settings.Instance.Sector_Trend_Bar_Size).BarDateTime).Max());
 }
Esempio n. 11
0
        public static TrendIndex CreateSectorIndex(string sectorName, List <Security> securities, PriceBarSize priceBarSize)
        {
            var trendIndex = IndexManager.Instance.Database.GetTrendIndex(sectorName, priceBarSize, Settings.Instance.Sector_Trend_Bar_Count);

            var usedSecurities = ApplyDefaultStaticFilters(securities).
                                 Where(x => x.Sector == sectorName);

            if (usedSecurities.Count() == 0)
            {
                return(trendIndex);
            }

            var currentDate = EarliestDate(usedSecurities, Settings.Instance.Sector_Trend_Bar_Size);
            var latestDate  = LatestDate(usedSecurities, Settings.Instance.Sector_Trend_Bar_Size);

            foreach (Security security in usedSecurities)
            {
                security.SetSwingPointsAndTrends(Settings.Instance.Sector_Trend_Bar_Count, Settings.Instance.Sector_Trend_Bar_Size);
            }

            while (currentDate <= latestDate)
            {
                TrendIndexDay indexDay = trendIndex.GetIndexDay(currentDate, true);

                var dailySecurityList = ApplyDefaultAsOfFilters(usedSecurities, currentDate);

                if (dailySecurityList.Count() > 0)
                {
                    foreach (TrendQualification trend in Enum.GetValues(typeof(TrendQualification)))
                    {
                        if (trend == TrendQualification.NotSet)
                        {
                            continue;
                        }

                        var totalTrending = dailySecurityList.Where(x => x.GetPriceBar(currentDate, Settings.Instance.Sector_Trend_Bar_Size).
                                                                    GetTrendType(Settings.Instance.Sector_Trend_Bar_Count) == trend).Count();

                        indexDay.AddTrendEntry(trend, totalTrending.ToDecimal() / dailySecurityList.Count().ToDecimal());
                    }

                    indexDay.SecurityCount = dailySecurityList.Count();
                }

                switch (Settings.Instance.Sector_Trend_Bar_Size)
                {
                case PriceBarSize.Daily:
                    currentDate = NextTradingDay(currentDate);
                    break;

                case PriceBarSize.Weekly:
                    currentDate = NextTradingWeekStart(currentDate);
                    break;

                case PriceBarSize.Monthly:
                    currentDate = NextTradingMonthStart(currentDate);
                    break;

                case PriceBarSize.Quarterly:
                    currentDate = NextTradingQuarterStart(currentDate);
                    break;
                }
            }

            return(trendIndex);
        }
Esempio n. 12
0
 public List <TrendIndex> GetAllTrendIndices(PriceBarSize priceBarSize)
 {
     return(SectorTrends.Where(x => x.TrendPriceBarSize == priceBarSize).
            Where(x => x.IndexSwingpointBarCount == Settings.Instance.DefaultSwingpointBarCount).ToList());
 }
Esempio n. 13
0
 public TrendIndex(string name, int indexSwingpointBarCount, PriceBarSize trendPriceBarSize)
 {
     IndexName = name;
     IndexSwingpointBarCount = indexSwingpointBarCount;
     TrendPriceBarSize       = trendPriceBarSize;
 }
Esempio n. 14
0
        public static List <NetChangeByTrendType> GetNetChangeByTrendType(this Security me, PriceBarSize priceBarSize, int barCount, DateTime?start = null, DateTime?end = null)
        {
            List <PriceBar> PriceBarsUsed;

            switch (priceBarSize)
            {
            case PriceBarSize.Weekly:
            {
                PriceBarsUsed = me.WeeklyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Weekly).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Weekly).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
                start = FirstTradingDayOfWeek(start.Value);
                end   = FirstTradingDayOfWeek(end.Value);
            }
            break;

            case PriceBarSize.Monthly:
            {
                PriceBarsUsed = me.MonthlyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Monthly).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Monthly).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
                start = FirstTradingDayOfMonth(start.Value);
                end   = FirstTradingDayOfMonth(end.Value);
            }
            break;

            case PriceBarSize.Quarterly:
            {
                PriceBarsUsed = me.QuarterlyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Quarterly).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Quarterly).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
                start = FirstTradingDayOfQuarter(start.Value);
                end   = FirstTradingDayOfQuarter(end.Value);
            }
            break;

            case PriceBarSize.Daily:
            default:
            {
                PriceBarsUsed = me.DailyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Daily).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Daily).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
            }
            break;
            }


            if (PriceBarsUsed.Count == 0)
            {
                return(null);
            }

            // Create a return list of all trends except NotSet
            var ret = new List <NetChangeByTrendType>();

            foreach (var trend in Enum.GetValues(typeof(TrendQualification)))
            {
                if ((TrendQualification)trend != TrendQualification.NotSet)
                {
                    ret.Add(new Analysis.NetChangeByTrendType((TrendQualification)trend));
                }
            }

            // Set swingpoints and trends if not done already
            me.SetSwingPointsAndTrends(barCount, priceBarSize);

            // Get the first bar, ignore the starting ambilavent trend
            PriceBar currentBar = me.GetPriceBar(start.Value, priceBarSize);

            // Track the current trend as we go through each bar
            TrendQualification currentTrend = currentBar.GetTrendType(barCount);

            while (currentBar != null && currentBar.GetTrendType(barCount) == currentTrend)
            {
                currentBar = currentBar.NextBar;
            }

            PriceBar firstBarOfTrend = null;
            PriceBar lastBarOfTrend  = null;

            while (currentBar != null)
            {
                // Since the trend change is based on the ending value for a bar, we track a trend from the second instance through the first bar of the next trend
                if (currentTrend != currentBar.GetTrendType(barCount))
                {
                    lastBarOfTrend = currentBar;
                    if (firstBarOfTrend != null)
                    {
                        var trendResult = ret.Find(x => x.TrendType == currentTrend);
                        trendResult.AddValues(firstBarOfTrend, lastBarOfTrend);
                    }

                    firstBarOfTrend = currentBar.NextBar;
                    currentTrend    = currentBar.GetTrendType(barCount);
                }
                currentBar = currentBar.NextBar;
            }

            return(ret);
        }