private void CalculateMovingAverageConvergenceDivergence()
        {
            var data = IndicatorService.GetData(Code, TargetDate, "Kapanis", NumberOfData);

            calculateMACD(data);
            calculateTrigger();
        }
        private void CalculateStochastics()
        {
            var data = IndicatorService.GetData(Code, TargetDate, new string[] { "Kapanis", "Dusuk", "Yuksek" }, FastKPeriod + NumberOfData - 1);

            if (data.Count < FastKPeriod)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            CalculateFastK(data);
            CalculateFastD();
            CalculateSlowD();
        }
        private void CalculateStochasticsMR()
        {
            int dataCount = IndicatorService.DataCount(Code, TargetDate);

            if (dataCount < FastKPeriod)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            CalculateFastK_MR(dataCount);
            CalculateFastD();
            CalculateSlowD();
        }
Example #4
0
        public static double[] Exponential(string code, DateTime targetDate, int period = 14, int numberOfData = 1)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Period must be positive.");
            }

            List <BsonDocument> data = IndicatorService.GetData(code, targetDate, "Kapanis", numberOfData);

            if (data.Count < period)
            {
                period = data.Count;
            }

            double[] avg = calculateEMA(period, numberOfData, data);

            return(avg);
        }
        public static double[] Wsr(string code, DateTime targetDate, int period = 14, int numberOfData = 1)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Periyot pozitif sayı olmalıdır.");
            }
            else if (numberOfData <= 0)
            {
                throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır.");
            }

            var data = IndicatorService.GetData(code, targetDate, new string[] { "Kapanis", "Dusuk", "Yuksek" }, period + numberOfData - 1);

            if (data.Count < period)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            return(calculateWsr(period, numberOfData, data));
        }
Example #6
0
        public static double[] Weighted(string code, DateTime targetDate, int period = 14, int numberOfData = 1)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Periyot pozitif sayı olmalıdır.");
            }
            else if (numberOfData <= 0)
            {
                throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır.");
            }

            List <BsonDocument> data = IndicatorService.GetData(code, targetDate, "Kapanis", numberOfData + period - 1);

            if (data.Count < period)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            double[] avg = calculateWMA(period, numberOfData, data);
            return(avg);
        }
        private void CalculateFastK_MR(int dataCount)
        {
            if (dataCount < NumberOfData + FastKPeriod - 1)
            {
                NumberOfData = dataCount - FastKPeriod + 1;
            }

            var limit  = FastKPeriod + NumberOfData - 1;
            var filter = new BsonDocument {
                { "Kod", Code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(TargetDate)) }
            };
            var sort       = new BsonDocument("Tarih", -1);
            var projection = new BsonDocument {
                { "_id", 0 }, { "Tarih", 1 }
            };

            BsonJavaScript mapper = new BsonJavaScript(@"
                function() {
	                if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate)
		                return;
	                else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate)
		                return;
	
	                var dateIndex = binarySearch(dates, this.Tarih);

	                if (dateIndex == -1)
		                return;
	
	                for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) {
		                if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate))
			                emit(dates[dateIndex], {close: this.Kapanis, low: this.Dusuk, high: this.Yuksek});
	                }
                }
            ");

            BsonJavaScript reducer = new BsonJavaScript(@"
                function(key, values) {
                    var result = {close: values[0].close, low: values[0].low, high: values[0].high};
                    for (var i = 1; i < values.length; i++) {
                        result.low = values[i].low < result.low ? values[i].low : result.low;
                        result.high = values[i].high > result.high ? values[i].high : result.high;
                    }
                    return result;
                }
            ");

            BsonJavaScript finalizer = new BsonJavaScript(@"
                function(key, reducedValue) {
                    reducedValue = 100 * (reducedValue.close - reducedValue.low) / (reducedValue.high - reducedValue.low);
                    return reducedValue;
                }
            ");

            var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, NumberOfData - 1).GetElement(0).Value.ToLocalTime();
            var lastDate     = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime();
            var limitDate    = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime();
            var dates        = IndicatorService.GetData(Code, TargetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray();
            var scope        = new BsonDocument {
                { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", FastKPeriod }, { "numberOfData", NumberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) }
            };

            var options = new MapReduceOptions <BsonDocument, BsonDocument>()
            {
                Filter        = filter,
                Sort          = sort,
                Scope         = scope,
                OutputOptions = MapReduceOutputOptions.Replace("stochasticsOut", "financialData", false),
                Finalize      = finalizer
            };

            FastK = new double[NumberOfData];
            var resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options);

            for (int i = 0, j = resultSet.Count - 1; i < NumberOfData; i++, j--)
            {
                FastK[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble();
            }
        }
        public static double[] WsrMR(string code, DateTime targetDate, int period = 14, int numberOfData = 1)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Periyot pozitif sayı olmalıdır.");
            }
            else if (numberOfData <= 0)
            {
                throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır.");
            }

            int dataCount = IndicatorService.DataCount(code, targetDate);

            if (dataCount < period)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            if (dataCount < numberOfData + period - 1)
            {
                numberOfData = dataCount - period + 1;
            }

            var limit  = period + numberOfData - 1;
            var filter = new BsonDocument {
                { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) }
            };
            var sort       = new BsonDocument("Tarih", -1);
            var projection = new BsonDocument {
                { "_id", 0 }, { "Tarih", 1 }
            };

            BsonJavaScript mapper = new BsonJavaScript(@"
                function() {
	                if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate)
		                return;
	                else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate)
		                return;
	
	                var dateIndex = binarySearch(dates, this.Tarih);

	                if (dateIndex == -1)
		                return;
	
	                for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) {
		                if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate))
			                emit(dates[dateIndex], {close: this.Kapanis, low: this.Dusuk, high: this.Yuksek});
	                }
                }
            ");

            BsonJavaScript reducer = new BsonJavaScript(@"
                function(key, values) {
                    var result = {close: values[0].close, low: values[0].low, high: values[0].high};
                    for (var i = 1; i < values.length; i++) {
                        result.low = values[i].low < result.low ? values[i].low : result.low;
                        result.high = values[i].high > result.high ? values[i].high : result.high;
                    }
                    return result;
                }
            ");

            BsonJavaScript finalizer = new BsonJavaScript(@"
                function(key, reducedValue) {
                    reducedValue = -100 * (reducedValue.high - reducedValue.close) / (reducedValue.high - reducedValue.low);
                    return reducedValue;
                }
            ");

            var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime();
            var lastDate     = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime();
            var limitDate    = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime();
            var dates        = IndicatorService.GetData(code, targetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray();
            var scope        = new BsonDocument {
                { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", period }, { "numberOfData", numberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) }
            };

            var options = new MapReduceOptions <BsonDocument, BsonDocument>()
            {
                Filter        = filter,
                Sort          = sort,
                Scope         = scope,
                OutputOptions = MapReduceOutputOptions.Replace("wsrOut", "financialData", false),
                Finalize      = finalizer
            };

            double[] wsr       = new double[numberOfData];
            var      resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options);

            for (int i = 0, j = resultSet.Count - 1; i < numberOfData; i++, j--)
            {
                wsr[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble();
            }

            return(wsr);
        }
        public static double[] RsiMR(string code, DateTime targetDate, int period = 14, int numberOfData = 1)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Periyot pozitif sayı olmalıdır.");
            }
            else if (numberOfData <= 0)
            {
                throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır.");
            }

            int dataCount = IndicatorService.DataCount(code, targetDate);

            if (dataCount < period)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            if (dataCount < numberOfData + period - 1)
            {
                numberOfData = dataCount - period + 1;
            }

            int limit = numberOfData + period - 1;

            var filter = new BsonDocument {
                { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) }
            };
            var sort = new BsonDocument("Tarih", -1);

            var projection = new BsonDocument {
                { "_id", 0 }, { "Tarih", 1 }
            };
            var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime();
            var lastDate     = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime();
            var limitDate    = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime();
            var dates        = IndicatorService.GetData(code, targetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray();

            MapReduceOptions <BsonDocument, BsonDocument> options = new MapReduceOptions <BsonDocument, BsonDocument>
            {
                Filter = new BsonDocument {
                    { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) }
                },
                Sort  = new BsonDocument("Tarih", -1),
                Scope = new BsonDocument {
                    { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", period }, { "numberOfData", numberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) }
                },
                OutputOptions = MapReduceOutputOptions.Replace("rsiOut", "financialData", false)
            };

            double[]       avg;
            BsonJavaScript mapper = new BsonJavaScript(@"
                function() {
                    if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate)
                        return;
                    else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate)
                        return;

                    var dateIndex;

                    dateIndex = binarySearch(dates, this.Tarih);
                    if (dateIndex == -1)
                        return;

                    for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) {
                        if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate))
                            emit(dates[dateIndex], {date: this.Tarih, headClose : this.Kapanis, tailClose : this.Kapanis, gain: 0 , loss: 0});
                    }
                }
            ");

            BsonJavaScript reducer = new BsonJavaScript(@"
                function(key, values) {
	                values.sort(function(a, b) {
		                return a.date < b.date;
	                });

                    sumGain = values[0].gain; 
                    sumLoss = values[0].loss;
                    var diff;
    
                    for(var i = 0; i < values.length - 1; i++) {
                        sumGain += values[i+1].gain;   
                        sumLoss += values[i+1].loss;
                        diff = values[i].tailClose - values[i+1].headClose;
                        if(diff > 0) 
                            sumGain += diff;
                        else 
                            sumLoss -= diff;
                    }

                    return {date: values[0].date, headClose: values[0].headClose, tailClose: values[values.length-1].tailClose, gain: sumGain, loss: sumLoss};
                }
            ");

            BsonJavaScript finalizer = new BsonJavaScript(@"
                function(key, reducedValue) {
                    if (reducedValue.loss == 0) return 100;
                    reducedValue = 100 - 100 / (1 + (reducedValue.gain / reducedValue.loss));
                    return reducedValue;
                }
            ");

            options.Finalize = finalizer;

            List <BsonDocument> resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options);

            avg = new double[numberOfData];

            for (int i = 0, j = numberOfData - 1; i < numberOfData; i++, j--)
            {
                avg[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble();
            }

            return(avg);
        }
Example #10
0
        public static double[] ExponentialMR(string code, DateTime targetDate, int period = 14, int numberOfData = 30)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Period must be positive.");
            }

            int dataCount = IndicatorService.DataCount(code, targetDate);

            if (dataCount < numberOfData)
            {
                numberOfData = dataCount;
            }

            double alpha = 2.0 / (1 + period);
            double beta  = 1 - alpha;
            double minimumCoefficient = 0.001;

            BsonJavaScript mapper = new BsonJavaScript(@"
                function() {
	                if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate)
		                return;
	                else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate)
		                return;

	                var dateIndex = binarySearch(dates, this.Tarih);
	                if (dateIndex == -1)
		                return;
	                
                    var epoch = Math.log(minimumCoefficient / (alpha * this.Kapanis)) / Math.log(beta);
                    var value;
	                if (dateIndex == numberOfData-1) {
		                value = this.Kapanis;
		                for (var i = dateIndex, j = 0; j < epoch && i >= 0; i--, j++) {
			                emit(dates[i], value);
			                value = value * beta;
		                }
	                } else {
		                value = this.Kapanis * alpha;
		                for (var i = dateIndex, j = 0; j < epoch && i >= 0; i--, j++) {
			                emit(dates[i], value);
			                value = value * beta;
		                }
	                }
                }
            ");

            BsonJavaScript reducer = new BsonJavaScript(@"
                function(key,values){
                    return Array.sum(values);
                }
            ");

            var filter = new BsonDocument {
                { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) }
            };
            var sort         = new BsonDocument("Tarih", -1);
            var mongoService = MongoDBService.GetService();

            var projection = new BsonDocument {
                { "_id", 0 }, { "Tarih", 1 }
            };
            var lastDate  = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime();
            var limitDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime();
            var dates     = IndicatorService.GetData(code, targetDate, "Tarih", numberOfData).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray();
            var scope     = new BsonDocument {
                { "numberOfData", numberOfData }, { "minimumCoefficient", minimumCoefficient }, { "alpha", alpha }, { "beta", beta }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "dates", new BsonArray(dates) }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }
            };

            MapReduceOptions <BsonDocument, BsonDocument> options = new MapReduceOptions <BsonDocument, BsonDocument>
            {
                Filter        = filter,
                Sort          = sort,
                Scope         = scope,
                OutputOptions = MapReduceOutputOptions.Replace("emaOut", "financialData", false)
            };

            double[] values = MongoDBService.GetService().MapReduceMany(mapper, reducer, options).Select(p => p.GetElement("value").Value.ToDouble()).ToArray();
            Array.Reverse(values);
            return(values);
        }
Example #11
0
        public static double[] WeightedMR(string code, DateTime targetDate, int period = 14, int numberOfData = 30)
        {
            if (period <= 0)
            {
                throw new IndicatorException("Periyot pozitif sayı olmalıdır.");
            }
            else if (numberOfData <= 0)
            {
                throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır.");
            }

            int dataCount = IndicatorService.DataCount(code, targetDate);

            if (dataCount < period)
            {
                throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE);
            }

            if (dataCount < numberOfData + period - 1)
            {
                numberOfData = dataCount - period + 1;
            }

            int    limit       = numberOfData + period - 1;
            double denominator = (period * (period + 1)) >> 1;

            var filter = new BsonDocument {
                { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) }
            };
            var sort = new BsonDocument("Tarih", -1);

            var projection = new BsonDocument {
                { "_id", 0 }, { "Tarih", 1 }
            };
            var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime();
            var lastDate     = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime();
            var limitDate    = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime();
            var dates        = IndicatorService.GetData(code, targetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray();

            MapReduceOptions <BsonDocument, BsonDocument> options = new MapReduceOptions <BsonDocument, BsonDocument>
            {
                Filter = new BsonDocument {
                    { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) }
                },
                Sort          = new BsonDocument("Tarih", -1),
                OutputOptions = MapReduceOutputOptions.Replace("wmaOut", "financialData", false),
                Scope         = new BsonDocument {
                    { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", period }, { "denominator", denominator }, { "numberOfData", numberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) }
                }
            };

            double[]       avg;
            BsonJavaScript mapper = new BsonJavaScript(@"
                function() {
	                if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate)
		                return;
	                else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate)
		                return;
	
	                var dateIndex;
	
	                dateIndex = binarySearch(dates, this.Tarih);
	                if (dateIndex == -1)
		                return;
	
	                var factor = period;

	                for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) {
		                if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate))
			                emit(dates[dateIndex], this.Kapanis * factor);
		                factor--;
	                }
                }
            ");

            BsonJavaScript reducer = new BsonJavaScript(@"
                function(key, values) {
	                return Array.sum(values);
                }
            ");

            BsonJavaScript finalizer = new BsonJavaScript(@"
                function(key, reducedValue) {
	                return reducedValue / denominator;
                }
            ");

            options.Finalize = finalizer;

            List <BsonDocument> resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options);

            avg = new double[numberOfData];

            for (int i = 0, j = numberOfData - 1; i < numberOfData; i++, j--)
            {
                avg[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble();
            }

            return(avg);
        }