private void TestExtremum(T value, int id, ExtremumType extremumType) { Collection <DataFieldInfo> dataFields = DataObjectInfo <T> .GetDataFields(DataFieldMask.Data); // Set the value foreach (DataFieldInfo field in dataFields) { Type fieldType = field.Field.FieldType; if (fieldType.IsValueType && fieldType.IsPrimitive) { DataObjectInfo <T> .SetProperty(field, value, Extremum(fieldType, extremumType)); } } // Save the object DataObjectFactory <T> .WriteObject(value); value = DataObjectFactory <T> .CreateObject(id); // Verify the value foreach (DataFieldInfo field in dataFields) { Type fieldType = field.Field.FieldType; if (fieldType.IsValueType && fieldType.IsPrimitive) { object storedValue = field.Field.GetValue(value); Assert.AreEqual(Extremum(fieldType, extremumType), storedValue, string.Format("#17 - {0}: Maximum value should store correctly.", FormatFieldName(field))); } } }
/// <summary> /// Finds local extremum of the function on a segment /// </summary> /// <param name="x">Argument values, defining segment points</param> /// <param name="y">Function values on the segment</param> /// <param name="extremum">Type of extremum (maximum or minimum)</param> /// <param name="epsilon">Expected accuracy</param> /// <param name="x0">Output value of X-coordinate of the extremum</param> /// <param name="y0">Output value of Y-coordinate of the extremum</param> private static void FindExtremum(double[] x, double[] y, ExtremumType extremum, double epsilon, out double x0, out double y0) { if (x.Length != y.Length) { throw new ArgumentException("Array sizes do not match."); } if (x.Length < 2) { throw new ArgumentException("Arrays must contain at least 2 points."); } // X-coordinate of extremum point x0 = 0; // left edge of the segment double a = x[0]; // right edge of the segment double b = x[x.Length - 1]; // golden ratio double ratio = 1.61803399; do { double x1 = b - (b - a) / ratio; double x2 = a + (b - a) / ratio; double y1 = Lagrange(x, y, x1); double y2 = Lagrange(x, y, x2); if ((extremum == ExtremumType.Max && y1 <= y2) || (extremum == ExtremumType.Min && y1 >= y2)) { a = x1; } else { b = x2; } x0 = (a + b) / 2; }while (Math.Abs(b - a) > epsilon); y0 = Lagrange(x, y, x0); }
protected bool CheckForExtremum(DataItem item, ExtremumType type, bool fromScratch) { Extremum extremum; if (!fromScratch) { //Jeżeli analiza nie jest przeprowadzana od początku, sprawdzane jest czy dla tego DataItemu //przypisany jest obieket ExtremumCalculator danego typu. Jeżeli nie, oznacza to, że już //wcześniej został zdyskwalifikowany i nie ma sensu go sprawdzać. extremum = item.Price.GetExtremumObject(type); if (extremum == null) return false; if (!extremum.IsOpen) return false; //Sprawdź czy notowania późniejsze względem tego pozwalają uznać je za ekstremum. var laterCounter = CountSerie(item.Index, type.IsPeak(), type.ByClose(), false); if (laterCounter < MinRange && laterCounter < (analyzer.getDataItemsLength() - 1 - item.Index)) { extremum.Cancelled = true; item.Price.ApplyExtremumValue(type, null); item.Price.IsUpdated = true; return true; } else { extremum.LaterCounter = laterCounter; } } else { //Wartości oparte na wcześniejszych notowaniach obliczane są tylko, jeżeli analiza wykonywana jest od zera. var earlierCounter = CountSerie(item.Index, type.IsPeak(), type.ByClose(), true); var laterCounter = CountSerie(item.Index, type.IsPeak(), type.ByClose(), false); //Jeżeli liczba wcześniejszych lub późniejszych notowań gorszych od tego notowania nie osiągnęła //minimalnego poziomu, to notowanie jest dyskwalifikowane jako ekstremum i nie ma sensu go dalej sprawdzać. if (earlierCounter < MinRange) return false; if (laterCounter < MinRange && laterCounter < (analyzer.getDataItemsLength() - 1 - item.Index)) return false; extremum = new Extremum(item.Date, atf, type.IsPeak(), type.ByClose()); extremum.EarlierCounter = earlierCounter; extremum.LaterCounter = laterCounter; extremum.EarlierAmplitude = FindEarlierPriceAmplitude(type, item, getCurrentExtremum(type)); extremum.EarlierChange1 = GetPriceChange(item, extremum, true, 1); extremum.EarlierChange2 = GetPriceChange(item, extremum, true, 2); extremum.EarlierChange3 = GetPriceChange(item, extremum, true, 3); extremum.EarlierChange5 = GetPriceChange(item, extremum, true, 5); extremum.EarlierChange10 = GetPriceChange(item, extremum, true, 10); extremum.Volatility = item.Quotation.Volatility(); //Calculate [LaterAmplitude] for previous extremum. var prevExtremumDataItem = getCurrentExtremum(type); if (prevExtremumDataItem != null) { var prevExtremum = prevExtremumDataItem.Extremum(type); if (prevExtremum != null) { var laterAmplitude = FindLaterPriceAmplitude(type, prevExtremumDataItem, item); prevExtremum.LaterAmplitude = laterAmplitude; prevExtremumDataItem.Price.IsUpdated = true; } } } //Właściwie, to już wcześniej zostało zapewnione, że do tego miejsca wykonanie programu dotrze //tylko, jeżeli extremum nie jest puste, ale mimo to kompilator nie przepuszcza bez takiego warunku tutaj. if (extremum != null) { //extremum.LaterAmplitude = FindPriceAmplitude(item, extremum, false); if (extremum.LaterChange1 == null) extremum.LaterChange1 = GetPriceChange(item, extremum, false, 1); if (extremum.LaterChange2 == null) extremum.LaterChange2 = GetPriceChange(item, extremum, false, 2); if (extremum.LaterChange3 == null) extremum.LaterChange3 = GetPriceChange(item, extremum, false, 3); if (extremum.LaterChange5 == null) extremum.LaterChange5 = GetPriceChange(item, extremum, false, 5); if (extremum.LaterChange10 == null) extremum.LaterChange10 = GetPriceChange(item, extremum, false, 10); extremum.IsOpen = (item.Index + extremum.LaterCounter == analyzer.getDataItemsLength() - 1) || quotationsLeft(item) < 10; if (extremum.IsConfirmed()) { setCurrentExtremum(type, item); } item.Price.ApplyExtremumValue(type, extremum); item.Price.IsUpdated = true; } return true; }
private void setCurrentExtremum(ExtremumType type, DataItem item) { currentExtrema[type] = item; }
private DataItem getCurrentExtremum(ExtremumType type) { DataItem item; if (currentExtrema.TryGetValue(type, out item)) { return item; } return null; }
protected double FindLaterPriceAmplitude(ExtremumType type, DataItem item, DataItem nextExtremum) { int startIndex = item.Index + 1; int endIndex = nextExtremum.Index - 1; var itemsRange = analyzer.getDataItems().Where(i => i.Index >= startIndex && i.Index <= endIndex); if (itemsRange.Count() == 0) return 0; double oppositeValue = (type.IsPeak() ? itemsRange.Min(i => i.Quotation.Low) : itemsRange.Max(i => i.Quotation.High)); double baseValue = item.Quotation.ProperValue(type); return Math.Abs(oppositeValue - baseValue) / Math.Max(oppositeValue, baseValue); }
protected double FindEarlierPriceAmplitude(ExtremumType type, DataItem item, DataItem prevExtremum) { int startIndex; int endIndex; startIndex = (prevExtremum == null ? 0 : prevExtremum.Index); endIndex = item.Index - 1; IEnumerable<DataItem> items = analyzer.getDataItems(); var itemsRange = items.Where(i => i.Index >= startIndex && i.Index <= endIndex); if (itemsRange.Count() == 0) return 0; double oppositeValue = (type.IsPeak() ? itemsRange.Min(i => i.Quotation.Low) : itemsRange.Max(i => i.Quotation.High)); double baseValue = item.Quotation.ProperValue(type); return Math.Abs(oppositeValue - baseValue) / Math.Max(oppositeValue, baseValue); }
public Extremum GetExtremumObject(ExtremumType type) { switch (type) { case ExtremumType.PeakByClose: return PeakByCloseExtremum; case ExtremumType.PeakByHigh: return PeakByHighExtremum; case ExtremumType.TroughByClose: return TroughByCloseExtremum; case ExtremumType.TroughByLow: return TroughByLowExtremum; default: return null; } }
public void ApplyExtremumValue(ExtremumType type, Extremum extremum) { switch (type) { case ExtremumType.PeakByClose: PeakByCloseExtremum = extremum; PeakByClose = extremum == null ? 0 : extremum.Evaluate(); break; case ExtremumType.PeakByHigh: PeakByHighExtremum= extremum; PeakByHigh = extremum == null ? 0 : extremum.Evaluate(); break; case ExtremumType.TroughByClose: TroughByCloseExtremum = extremum; TroughByClose = extremum == null ? 0 : extremum.Evaluate(); break; case ExtremumType.TroughByLow: TroughByLowExtremum = extremum; TroughByLow = extremum == null ? 0 : extremum.Evaluate(); break; } }
private object Extremum(Type dataType, ExtremumType extremumType) { if (dataType == typeof(bool)) { switch (extremumType) { case ExtremumType.Maximum: return(true); case ExtremumType.Minimum: return(false); } } if (dataType == typeof(byte)) { switch (extremumType) { case ExtremumType.Maximum: return(byte.MaxValue); case ExtremumType.Minimum: return(byte.MinValue); } } else if (dataType == typeof(short)) { switch (extremumType) { case ExtremumType.Maximum: return(short.MaxValue); case ExtremumType.Minimum: return(short.MinValue); } } else if (dataType == typeof(int)) { switch (extremumType) { case ExtremumType.Maximum: return(int.MaxValue); case ExtremumType.Minimum: return(int.MinValue); } } else if (dataType == typeof(long)) { switch (extremumType) { case ExtremumType.Maximum: return(long.MaxValue); case ExtremumType.Minimum: return(long.MinValue); } } else if (dataType == typeof(float)) { switch (extremumType) { case ExtremumType.Maximum: return(float.MaxValue); case ExtremumType.Minimum: return(float.MinValue); } } else if (dataType == typeof(double)) { switch (extremumType) { case ExtremumType.Maximum: return(double.MaxValue); case ExtremumType.Minimum: return(double.MinValue); } } else if (dataType == typeof(decimal)) { switch (extremumType) { case ExtremumType.Maximum: return(decimal.MaxValue); case ExtremumType.Minimum: return(decimal.MinValue); } } throw new NotSupportedException(); }
public double ProperValue(ExtremumType extremumType) { switch (extremumType) { case ExtremumType.PeakByClose: case ExtremumType.TroughByClose: return Close; case ExtremumType.PeakByHigh: return High; case ExtremumType.TroughByLow: return Low; default: return Close; } }
public static bool IsOpposite(this ExtremumType type, ExtremumType tested) { if (type == ExtremumType.PeakByClose || type == ExtremumType.PeakByHigh) { return (tested == ExtremumType.TroughByClose || tested == ExtremumType.TroughByLow); } else { return (tested == ExtremumType.PeakByClose || tested == ExtremumType.PeakByHigh); } }
public Extremum Extremum(ExtremumType type) { if (Price == null) return null; Extremum extremum = Price.GetExtremumObject(type); if (extremum == null || extremum.Cancelled) return null; return extremum; }