private void WriteThresholdHeader(CriteriaSetObj criteriaSet)
        {
            // TODO: create constants
            criteriaThresholdWriter.WriteLine("Set," + criteriaSet.criteriaSetName);
            criteriaThresholdWriter.WriteLine("Date," + DateTime.Today.ToString("MM/dd/yyyy"));

            if (criteriaSet.aggSpecsObj.stockCode)
            {
                criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.STOCK_CODE);
            }
            if (criteriaSet.aggSpecsObj.stockType)
            {
                criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.STOCK_TYPE);
            }
            if (criteriaSet.aggSpecsObj.holderId)
            {
                criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.HOLDER_ID);
            }
            if (criteriaSet.aggSpecsObj.direction)
            {
                criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.DIRECTION);
            }

            criteriaThresholdWriter.WriteLine();
            criteriaThresholdWriter.WriteLine("Agg Key,Column Value,Threshold Crossed");
        }
        public override void Execute(ref bool destroyAfterExecute)
        {
            int            outstadindChunkContexts;
            string         key;
            CriteriaSetObj workingCriteria = Global.criteriaSets[criteriaIndex];
            RawDataChunk   workingData     = Global.parsedRawData[dataIndex];
            PreAggSpecs    myPreSpecs      = workingCriteria.preAggObj;
            AggSpecs       myAggSpecs      = workingCriteria.aggSpecsObj;
            PostAggSpecs   myPostSpecs     = workingCriteria.postAggObj;
            uint           rawDataRowId;

            for (int i = 0; i < workingData.chunkDataParsed.Count; i++)
            {
                StockDataEntry currStockEntry = workingData.chunkDataParsed[i];
                if (DataEntryWithinFilter(myPreSpecs, currStockEntry))
                {
                    rawDataRowId = ((uint)dataIndex << 16) | ((uint)i);
                    key          = myAggSpecs.CreateAggKeyWithAggValues(currStockEntry);
                    workingCriteria.AddValueToDictionary(yesterdayData, key, currStockEntry.precentageSharesHeld, currStockEntry.sharesHeld, currStockEntry.value, rawDataRowId);
                }
            }

            outstadindChunkContexts = Interlocked.Decrement(ref chunkCount);
            if (outstadindChunkContexts == 0)
            {
                Global.mainThreadWait.Set();
            }
        }
        public override void Execute(ref bool destory)
        {
            string              correctedCriteriaName;
            int                 outstandingPostAggContext;
            CriteriaSetObj      aggCriteriaObj   = Global.criteriaSets[m_CriteriaIndex];
            ThresholdComparison typeOfComparison = aggCriteriaObj.postAggObj.comparison;

            correctedCriteriaName = Regex.Replace(aggCriteriaObj.criteriaSetName, "[^a-zA-Z0-9]", "");
            writeDirectory        = Path.Combine(Global.outputDirectory, correctedCriteriaName);
            Directory.CreateDirectory(writeDirectory);
            criteriaFilterDetailsWriter = new StreamWriter(Path.Combine(writeDirectory, CRITERIA_FILTER_DETAILS_FILE_NAME));
            criteriaFilterWriter        = new StreamWriter(Path.Combine(writeDirectory, CRITERIA_FILTER_FILE_NAME));
            criteriaThresholdWriter     = new StreamWriter(Path.Combine(writeDirectory, CRITERA_THRESHOLD_FILE_NAME));

            criteriaFilterWriter.WriteLine(FILTER_FILE_HEADING);
            // TODO: write header lines for details and threshold files

            switch (typeOfComparison)
            {
            case ThresholdComparison.Undefined:
            {
                // shouldn't EVER come in here, checks before this in CriteriaObj should prevent this.
                break;
            }

            case ThresholdComparison.Max:
            {
                // TODO: call associated method.
                break;
            }

            case ThresholdComparison.Crosses:
            {
                CrossesYesterdayValueComparison(aggCriteriaObj, typeOfComparison);
                break;
            }

            case ThresholdComparison.GreaterThan:
            case ThresholdComparison.GreaterThanOrEqualTo:
            case ThresholdComparison.LessThan:
            case ThresholdComparison.LessThanOrEqualTo:
            {
                ValueOfColumnComparison(aggCriteriaObj, typeOfComparison);
                break;
            }

            default:
            {
                // TODO: check criteria set text file he gave us with all criteria sets.
                break;
            }
            }

            outstandingPostAggContext = Interlocked.Decrement(ref postAggContextCount);
            if (outstandingPostAggContext == 0)
            {
                Global.mainThreadWait.Set();
            }
        }
        private void CrossesYesterdayValueComparison(CriteriaSetObj criteriaObj, ThresholdComparison compare)
        {
            int    rawDataChunkIndex;
            int    stockDataIndex;
            double columnValue = 0.0, thresholdCrossed = 0.0;
            ThresholdComparison    comparison = compare;
            CriteriaSetObj         criteria = criteriaObj;
            ThresholdColumn        crossesColumn = criteria.postAggObj.thresholdColumn;
            IReadOnlyList <double> thresholds = criteria.postAggObj.GetRefToThresholdValues();
            IReadOnlyDictionary <string, AggValues> todaysParsedDataDictionary = criteria.GetRefToAggResults();
            IReadOnlyDictionary <string, AggValues> yesterdaysParsedDataDictionary = criteria.GetRefToYesterdayAggResults();
            AggValues todaysAgg;
            AggValues yestedayAgg;
            bool      keyExistsYesterday, todayGreaterYesterday;

            if (crossesColumn == ThresholdColumn.Undefiend)
            {
                // TODO: do something else
                return;
            }

            WriteThresholdHeader(criteria);

            foreach (string key in todaysParsedDataDictionary.Keys)
            {
                todaysParsedDataDictionary.TryGetValue(key, out todaysAgg); // should always find something
                keyExistsYesterday = yesterdaysParsedDataDictionary.TryGetValue(key, out yestedayAgg);
                if (!keyExistsYesterday)
                {
                    yestedayAgg = new AggValues(); // all values are 0 then.
                }

                switch (crossesColumn)
                {
                case ThresholdColumn.PrecentageSharesHeld:
                {
                    columnValue = todaysAgg.percentageSharesHeld;
                    if (columnValue > yestedayAgg.percentageSharesHeld)
                    {
                        todayGreaterYesterday = true;
                    }
                    else
                    {
                        todayGreaterYesterday = false;
                    }
                    thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
                    break;
                }

                case ThresholdColumn.SharesHeld:
                {
                    columnValue = todaysAgg.sharesHeld;
                    if (columnValue > yestedayAgg.sharesHeld)
                    {
                        todayGreaterYesterday = true;
                    }
                    else
                    {
                        todayGreaterYesterday = false;
                    }
                    thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
                    break;
                }

                case ThresholdColumn.Value:
                {
                    columnValue = todaysAgg.value;
                    if (columnValue > yestedayAgg.value)
                    {
                        todayGreaterYesterday = true;
                    }
                    else
                    {
                        todayGreaterYesterday = false;
                    }
                    thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
                    break;
                }

                default:
                {
                    break;
                }
                }

                // TODO: write to files
                criteriaFilterWriter.WriteLine(GetAggValuesCommaSeparatedString(key, todaysAgg));

                IReadOnlyList <uint> unaggregatedDictEntry = todaysAgg.GetRefToRowsInChunk();
                for (int i = 0; i < unaggregatedDictEntry.Count; i++)
                {
                    rawDataChunkIndex = (int)unaggregatedDictEntry[i] >> 16;
                    stockDataIndex    = (int)unaggregatedDictEntry[i] & 0xffff;
                    StockDataEntry sde = Global.parsedRawData[rawDataChunkIndex].chunkDataParsed[stockDataIndex];
                    criteriaFilterDetailsWriter.WriteLine(GetCommaSeparatedStockDataEntryWithKey(sde, key));
                }

                criteriaThresholdWriter.WriteLine(GetCommaSeparatedThresholdEntry(key, columnValue, thresholdCrossed));
            }

            criteriaFilterWriter.Dispose();
            criteriaFilterDetailsWriter.Dispose();
            criteriaThresholdWriter.Dispose();
        }
        private void ValueOfColumnComparison(CriteriaSetObj criteriaObj, ThresholdComparison compare)
        {
            int rawDataChunkIndex;
            int stockDataIndex;
            ThresholdComparison comparison = compare;
            CriteriaSetObj      criteria   = criteriaObj;
            ThresholdColumn     column     = criteria.postAggObj.thresholdColumn;
            IReadOnlyDictionary <string, AggValues> readOnlyAggResults = criteria.GetRefToAggResults();
            AggValues aggValue;
            IReadOnlyList <double> thresholdValues = criteria.postAggObj.GetRefToThresholdValues(); // should only contain 1 value.
            double thresholdValue = thresholdValues[0];                                             // should be only 1 value here'
            double columnValue    = 0.0;
            bool   thresholdCheck = false;

            if (column == ThresholdColumn.Undefiend)
            {
                // TODO: Do something if it is undefined, shouldn't be.
                return;
            }

            WriteThresholdHeader(criteria);

            foreach (string key in readOnlyAggResults.Keys)
            {
                readOnlyAggResults.TryGetValue(key, out aggValue);

                switch (column)
                {
                case ThresholdColumn.PrecentageSharesHeld:
                {
                    columnValue    = aggValue.percentageSharesHeld;
                    thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
                    break;
                }

                case ThresholdColumn.SharesHeld:
                {
                    columnValue    = aggValue.sharesHeld;
                    thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
                    break;
                }

                case ThresholdColumn.Value:
                {
                    columnValue    = aggValue.value;
                    thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
                    break;
                }

                default:
                {
                    break;
                }
                }

                if (thresholdCheck)
                {
                    criteriaFilterWriter.WriteLine(GetAggValuesCommaSeparatedString(key, aggValue));

                    IReadOnlyList <uint> unaggregatedDictEntry = aggValue.GetRefToRowsInChunk();
                    for (int i = 0; i < unaggregatedDictEntry.Count; i++)
                    {
                        rawDataChunkIndex = (int)unaggregatedDictEntry[i] >> 16;
                        stockDataIndex    = (int)unaggregatedDictEntry[i] & 0xffff;
                        StockDataEntry sde = Global.parsedRawData[rawDataChunkIndex].chunkDataParsed[stockDataIndex];
                        criteriaFilterDetailsWriter.WriteLine(GetCommaSeparatedStockDataEntryWithKey(sde, key));
                    }
                    criteriaThresholdWriter.WriteLine(GetCommaSeparatedThresholdEntry(key, columnValue, thresholdValue));
                }
            }

            criteriaFilterWriter.Dispose();
            criteriaFilterDetailsWriter.Dispose();
            criteriaThresholdWriter.Dispose();
        }
Пример #6
0
        // This execute method parses the entire CriteriaSet.txt in one go. Since it is a small file, one thread should be able to do it.
        public override void Execute(ref bool destory)
        {
            bool           criteriaObjCheck;
            string         criteriaLine;
            string         lineSpecifier;
            CriteriaSetObj newCriteria = new CriteriaSetObj();

            // TODO: set the completedParsing field to true when done with a criteria set.


            using (criteriaSetReader = new StreamReader(criteriaSetFilePath))
            {
                sizeOfCriteriaFile = criteriaSetReader.BaseStream.Length;

                while ((criteriaLine = criteriaSetReader.ReadLine()) != null)
                {
                    if (criteriaLine.Length == 0) // Empty line
                    {
                        if (newCriteria.ValidateParsingCompletion())
                        {
                            Global.AddCriteriaSet(newCriteria);
                            newCriteria = new CriteriaSetObj();
                            state       = WorkingState.PreAgg;
                        }
                    }
                    else if (string.Compare(COMMENT_LINE, criteriaLine.Substring(0, 2)) != 0) // Skip if line is a comment
                    {
                        lineSpecifier = criteriaLine.Substring(0, 1);
                        switch (lineSpecifier)
                        {
                        case NEW_CRITERIA_SET_START:
                        {
                            criteriaObjCheck = newCriteria.SetCriteriaName(criteriaLine.Substring(1));
                            if (!criteriaObjCheck)
                            {
                                Program.WriteToConsole("CriteriaSetParser: More than one name associated with CriteriaSet.");
                            }
                            break;
                        }

                        case PRE_AGG_COLUMN:
                        {
                            newCriteria.preAggObj.ChangeCurrentAssignee(criteriaLine.Substring(1));
                            break;
                        }

                        case COMPARISON_INDICATOR:
                        {
                            newCriteria.preAggObj.AddComparisonType(criteriaLine.Substring(1));
                            break;
                        }

                        case COMPARISON_VALUE_IDENTIFIER:
                        {
                            if (state == WorkingState.PreAgg)
                            {
                                newCriteria.preAggObj.AddComparisonValue(criteriaLine.Substring(1));
                            }
                            else if (state == WorkingState.PostAgg)
                            {
                                // TODO: check this, if it removes commas and converts to double.
                                string temp = criteriaLine.Substring(1).Replace(",", "");
                                newCriteria.postAggObj.AddThresholdValue(Convert.ToDouble(temp));
                            }
                            else
                            {
                                // TODO: State did not change from Aggregation. File error.
                            }
                            break;
                        }

                        case AGG_KEY_COLUMN:
                        {
                            if (state == WorkingState.PreAgg)
                            {
                                state = WorkingState.Aggregation;
                            }
                            newCriteria.aggSpecsObj.SetAggregationKey(criteriaLine.Substring(1));
                            break;
                        }

                        case AGG_SUM_COLUMN:
                        {
                            newCriteria.aggSpecsObj.SetColumnSummations(criteriaLine.Substring(1));
                            break;
                        }

                        case POST_AGG_COMPARISON_SPECIFIER:
                        {
                            if (state == WorkingState.Aggregation)
                            {
                                state = WorkingState.PostAgg;
                            }
                            newCriteria.postAggObj.SetThresholdColumn(criteriaLine.Substring(1));
                            break;
                        }

                        case COMPARISON_SPECIFIER:
                        {
                            if (CriteriaConstants.CROSSES_THRESHOLD_SPECIFIER.Equals(criteriaLine.Substring(1)))
                            {
                                crossesCount++;
                            }
                            newCriteria.postAggObj.SetThresholdComparison(criteriaLine.Substring(1));
                            break;
                        }

                        default:
                        {
                            break;
                        }
                        }
                    }
                }

                if (newCriteria.ValidateParsingCompletion())
                {
                    Global.AddCriteriaSet(newCriteria);
                }
            }
        }
Пример #7
0
 internal static void AddCriteriaSet(CriteriaSetObj cso)
 {
     criteriaSets.Add(cso);
 }