public void Initialise(StockSerie serie) { this.stockSerie = serie; this.lowSerie = stockSerie.GetSerie(StockDataType.LOW); this.highSerie = stockSerie.GetSerie(StockDataType.HIGH); this.closeSerie = stockSerie.GetSerie(StockDataType.CLOSE); }
public override void ApplyTo(StockSerie stockSerie) { this.CreateEventSeries(stockSerie.Count); FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE).CalculateEMA((int)this.parameters[1]); FloatSerie highSerie = stockSerie.GetSerie(StockDataType.HIGH); FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW); FloatSerie hlSerie= new FloatSerie(stockSerie.Count, this.SerieNames[0]); this.Series[0] = hlSerie; int period = (int)this.parameters[0]; float min, max; for (int i = 0; i < period; i++) { min = lowSerie.GetMin(0, i); max = highSerie.GetMax(0, i); hlSerie[i] = (min + max)/2f; } for (int i = period; i < stockSerie.Count; i++) { min = lowSerie.GetMin(i-period, i); max = highSerie.GetMax(i - period, i); hlSerie[i] = (min + max)/2f; } // Detecting events this.CreateEventSeries(stockSerie.Count); for (int i = 2; i < hlSerie.Count; i++) { this.eventSeries[0][i] = (hlSerie[i - 2] > hlSerie[i - 1] && hlSerie[i - 1] < hlSerie[i]); this.eventSeries[1][i] = (hlSerie[i - 2] < hlSerie[i - 1] && hlSerie[i - 1] > hlSerie[i]); this.eventSeries[2][i] = closeSerie[i - 1] < hlSerie[i - 1] && closeSerie[i] > hlSerie[i]; this.eventSeries[3][i] = closeSerie[i - 1] > hlSerie[i - 1] && closeSerie[i] < hlSerie[i]; this.eventSeries[4][i] = lowSerie[i] > hlSerie[i] && lowSerie[i - 1] < hlSerie[i - 1]; this.eventSeries[5][i] = highSerie[i] < hlSerie[i] && highSerie[i - 1] > hlSerie[i - 1]; this.eventSeries[6][i] = lowSerie[i] > hlSerie[i] && closeSerie[i - 1] < closeSerie[i]; this.eventSeries[7][i] = highSerie[i] < hlSerie[i] && closeSerie[i - 1] > closeSerie[i]; if (this.eventSeries[8][i - 1]) { // Check if BullRun Persists this.eventSeries[8][i] = !this.eventSeries[5][i]; } else { // Check if BullRun Starts this.eventSeries[8][i] = this.eventSeries[4][i]; } if (this.eventSeries[9][i - 1]) { // Check if BearRun Persists this.eventSeries[9][i] = !this.eventSeries[4][i]; } else { // Check if BearRun Starts this.eventSeries[9][i] = this.eventSeries[5][i]; } } }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { base.Initialise(stockSerie, lastBuyOrder, supportShortSelling); this.oscSerie = null; this.trailStop = StockTrailStopManager.CreateTrailStop("TRAILHL(3)"); this.trailStop.ApplyTo(stockSerie); }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { base.Initialise(stockSerie, lastBuyOrder, supportShortSelling); IStockIndicator indicator = TriggerIndicator as IStockIndicator; middleUpBandSerie = indicator.Series[1]; middleBandSerie = indicator.Series[2]; middleDownBandSerie = indicator.Series[3]; lowSerie = stockSerie.GetSerie(StockDataType.LOW); highSerie = stockSerie.GetSerie(StockDataType.HIGH); closeSerie = stockSerie.GetSerie(StockDataType.CLOSE); }
public override StockOrder TryToBuy(StockDailyValue dailyValue, int index, float amount, ref float benchmark) { benchmark = dailyValue.CLOSE; if (this.TriggerIndicator == null) { return null; } if (this.oscSerie == null) this.oscSerie = (this.TriggerIndicator as IStockIndicator).Series[0]; if (this.oscSerie[index - 1] < 0 && this.oscSerie[index] > 0) { trailActivated = false; return StockOrder.CreateBuyAtMarketOpenStockOrder(dailyValue.NAME, dailyValue.DATE, dailyValue.DATE.AddDays(30), amount, false); } return null; }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { this.Serie = stockSerie; this.LastBuyOrder = lastBuyOrder; this.SupportShortSelling = supportShortSelling; if (StockDictionary.StockDictionarySingleton.ContainsKey("TB." + stockSerie.StockName)) { StockSerie breadthSerie = StockDictionary.StockDictionarySingleton["TB." + stockSerie.StockName]; if (breadthSerie.Initialise()) { IStockIndicator OSCIndicator = breadthSerie.GetIndicator(((IStockIndicator)TriggerIndicator).Name); if (OSCIndicator != null) { OSCSerie = OSCIndicator.Series[0]; } } } }
public FloatSerie CalculateHLTrail(int period) { FloatSerie trailSerie = new FloatSerie(this.Count, "TRAILHL"); trailSerie[0] = this[0]; // Initialise until period is reached bool upTrend = this[period - 1] > this[0]; float min = float.MaxValue, max = float.MinValue; this.GetMinMax(0, period - 1, ref min, ref max); for (int i = 1; i < period; i++) { trailSerie[i] = upTrend ? min : max; } for (int i = period; i < this.Count; i++) { if (upTrend) { if (this[i] < trailSerie[i - 1]) // upTrend broken { upTrend = false; trailSerie[i] = this.GetMax(i - period, i - 1); } else // Uptrend continues { if (this[i] > this[i - 1]) { trailSerie[i] = Math.Max(trailSerie[i - 1], this.GetMin(i - period, i - 1)); } else { trailSerie[i] = trailSerie[i - 1]; } } } else { if (this[i] > trailSerie[i - 1]) // downTrend broken { upTrend = true; trailSerie[i] = this.GetMin(i - period, i - 1); } else // downTrend continues { if (this[i] < this[i - 1]) { trailSerie[i] = Math.Min(trailSerie[i - 1], this.GetMax(i - period, i - 1)); } else { trailSerie[i] = trailSerie[i - 1]; } } } } return trailSerie; }
public override void ApplyTo(StockSerie stockSerie) { int period = (int)this.Parameters[0]; int smoothing = (int)this.Parameters[1]; FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW).CalculateEMA(smoothing); FloatSerie highSerie = stockSerie.GetSerie(StockDataType.HIGH).CalculateEMA(smoothing); FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE).CalculateEMA(smoothing); FloatSerie pivotSerie = new FloatSerie(stockSerie.Count, "PIVOT"); FloatSerie s1Serie = new FloatSerie(stockSerie.Count, "S1"); FloatSerie s2Serie = new FloatSerie(stockSerie.Count, "S2"); FloatSerie s3Serie = new FloatSerie(stockSerie.Count, "S3"); FloatSerie r1Serie = new FloatSerie(stockSerie.Count, "R1"); FloatSerie r2Serie = new FloatSerie(stockSerie.Count, "R2"); FloatSerie r3Serie = new FloatSerie(stockSerie.Count, "R3"); this.Series[0] = pivotSerie; this.Series[1] = s1Serie; this.Series[2] = s2Serie; this.Series[3] = s3Serie; this.Series[4] = r1Serie; this.Series[5] = r2Serie; this.Series[6] = r3Serie; // Detecting events this.CreateEventSeries(stockSerie.Count); if (stockSerie.StockName.StartsWith("INT_")) { List<StockDailyValue> dailyValues = stockSerie.GenerateDailyFromIntraday(); IEnumerator<StockDailyValue> dailyEnumerator = dailyValues.GetEnumerator(); dailyEnumerator.Reset(); dailyEnumerator.MoveNext(); StockDailyValue dailyValue = dailyEnumerator.Current; float pivot; float s1; float r1; float r2; float s2; float r3; float s3; dailyValue.CalculatePivot(out pivot, out s1, out r1, out r2, out s2, out r3, out s3); DateTime intradayBarDate = stockSerie.Values.First().DATE.Date; int count = 0; bool first = true; foreach (StockDailyValue intradayValue in stockSerie.Values) { if (intradayBarDate != intradayValue.DATE.Date) { if (first) { first = false; } else { dailyEnumerator.MoveNext(); dailyEnumerator.Current.CalculatePivot(out pivot, out s1, out r1, out r2, out s2, out r3, out s3); } intradayBarDate = intradayValue.DATE.Date; } pivotSerie[count] = pivot; r1Serie[count] = r1; s1Serie[count] = s1; r2Serie[count] = r2; s2Serie[count] = s2; r3Serie[count] = r3; s3Serie[count] = s3; count++; } } else { for (int i = 0; i <= period; i++) { s1Serie[i] = closeSerie[i]; s2Serie[i] = closeSerie[i]; s3Serie[i] = closeSerie[i]; r1Serie[i] = closeSerie[i]; r2Serie[i] = closeSerie[i]; r3Serie[i] = closeSerie[i]; } for (int i = period + 1; i < stockSerie.Count; i++) { float low = lowSerie.GetMin(i - period - 1, i - 1); float high = highSerie.GetMax(i - period - 1, i - 1); float pivot = (low + high + closeSerie[i - 1]) / 3; pivotSerie[i] = pivot; r1Serie[i] = (2 * pivot) - low; s1Serie[i] = (2 * pivot) - high; r2Serie[i] = (pivot - s1Serie[i]) + r1Serie[i]; s2Serie[i] = pivot - (r1Serie[i] - s1Serie[i]); r3Serie[i] = (pivot - s2Serie[i]) + r2Serie[i]; s3Serie[i] = pivot - (r2Serie[i] - s2Serie[i]); } } }
public FloatSerie CalculateCorrelation(FloatSerie otherSerie, int period) { if (this.Count != otherSerie.Count) { return null; } FloatSerie correlationSerie = new FloatSerie(this.Count, "CORREL"); float[] x = this.Values; float[] y = otherSerie.Values; float[] mx = this.CalculateEMA(period).Values; float[] my = otherSerie.CalculateEMA(period).Values; double sum1 = 0, sum2 = 0, sum3 = 0, xmx = 0, ymy = 0; correlationSerie[0] = 0; for (int i = 1; i < this.Count; i++) { sum1 = 0; sum2 = 0; sum3 = 0; for (int j = Math.Max(0, i - period); j <= i; j++) { xmx = x[j] - mx[j]; ymy = y[j] - my[j]; sum1 += xmx * ymy; sum2 += xmx * xmx; sum3 += ymy * ymy; } correlationSerie[i] = (float)(sum1 / (Math.Sqrt(sum2) * Math.Sqrt(sum3))); } return correlationSerie; }
public override void ApplyTo(StockSerie stockSerie) { StockSerie seasonSerie = stockSerie.CalculateSeasonality(); FloatSerie seasonCloseSerie = new FloatSerie(stockSerie.Count); int i = 0; float previousClose = stockSerie.Values.First().CLOSE; FloatSerie seasonClose = seasonSerie.GetSerie(StockDataType.CLOSE); int indexOfDate; int previousIndexOfDate = 0; foreach (StockDailyValue dailyValue in stockSerie.Values) { indexOfDate = seasonSerie.IndexOf(new DateTime(2000, dailyValue.DATE.Month, dailyValue.DATE.Day)); if (indexOfDate != -1) { if (indexOfDate >= previousIndexOfDate) { seasonCloseSerie[i] = previousClose * (1 + (seasonClose[indexOfDate] - seasonClose[previousIndexOfDate]) / seasonClose[previousIndexOfDate]); } previousIndexOfDate = indexOfDate; } else { seasonCloseSerie[i] = previousClose; } previousClose = seasonCloseSerie[i]; i++; } this.series[0] = stockSerie.GetSerie(StockDataType.CLOSE).CalculateCorrelation(seasonCloseSerie, (int)parameters[0]); this.Series[0].Name = this.Name; }
public FloatSerie CalculateCorrelation(int period, FloatSerie serie) { if (this.Count != serie.Count) throw new ArgumentOutOfRangeException("Cannot Covariance on series with different size"); FloatSerie ma1 = this.CalculateMA(period); FloatSerie ma2 = serie.CalculateMA(period); // Formula is there http://www.socscistatistics.com/tests/pearson/ FloatSerie correl = new FloatSerie(this.Count, "CORREL"); for (int i = period; i < this.Count; i++) { double sum1 = 0.0; double sum2 = 0.0; double sum3 = 0.0; for (int j = i - period; j <= i; j++) { double diff1 = this[j] - ma1[i]; double diff2 = serie[j] - ma2[i]; sum1 += diff1 * diff2; sum2 += diff1 * diff1; sum3 += diff2 * diff2; } correl[i] = (float)(sum1 / (Math.Sqrt(sum2) * Math.Sqrt(sum3))); } return correl; }
public override void ApplyTo(StockSerie stockSerie) { // Set HLine value float trendThreshold = (float)this.Parameters[1]; int period = (int)this.Parameters[0]; FloatSerie atrSerie = stockSerie.GetIndicator("ATR(" + period + ")").Series[0]; FloatSerie pDM = new FloatSerie(stockSerie.Count); FloatSerie mDM = new FloatSerie(stockSerie.Count); FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW).CalculateEMA((int)this.Parameters[2]); FloatSerie higherie = stockSerie.GetSerie(StockDataType.HIGH).CalculateEMA((int)this.Parameters[2]); // Calculate +DM and -DM for (int i = 1; i < stockSerie.Count; i++) { float rangeUp = higherie[i] - higherie[i - 1]; float rangeDown = lowSerie[i - 1] - lowSerie[i]; if (rangeUp > rangeDown) { pDM[i] = Math.Max(0, rangeUp); } else { mDM[i] = Math.Max(0, rangeDown); } } // Calclate +DI and -DI FloatSerie pDI = pDM.CalculateEMA(period).Div(atrSerie).Mult(100); FloatSerie mDI = mDM.CalculateEMA(period).Div(atrSerie).Mult(100); FloatSerie ADX = ((pDI - mDI).Abs() / (pDI + mDI)).CalculateEMA(period).Mult(100); ADX.Name = this.SerieNames[0]; pDI.Name = this.SerieNames[1]; mDI.Name = this.SerieNames[2]; this.Series[0] = ADX; this.Series[1] = pDI; this.Series[2] = mDI; // Manage events this.CreateEventSeries(stockSerie.Count); for (int i = period; i < stockSerie.Count; i++) { this.eventSeries[0][i] = ADX[i] > trendThreshold && ADX[i] > ADX[i - 1] && pDI[i] > mDI[i]; this.eventSeries[1][i] = ADX[i] > trendThreshold && ADX[i] > ADX[i - 1] && pDI[i] < mDI[i]; } }
public FloatSerie CalculateCovariance(int period, FloatSerie serie) { if (this.Count != serie.Count) throw new ArgumentOutOfRangeException("Cannot Covariance on series with different size"); FloatSerie ma1 = this.CalculateMA(period); FloatSerie ma2 = serie.CalculateMA(period); FloatSerie variance = new FloatSerie(this.Count, "COVAR"); float avg1, avg2; float sum; float spread1, spread2; int count; for (int i = period; i < this.Count; i++) { count = 0; sum = 0.0f; avg1 = ma1[i]; avg2 = ma2[i]; for (int j = i - period; j <= i; j++) { count++; spread1 = this.Values[j] - avg1; spread2 = serie.Values[j] - avg2; sum += spread1 * spread2; } variance[i] = sum / (float)count; } return variance; }
public override void ApplyTo(StockSerie stockSerie) { using (MethodLogger ml = new MethodLogger(this)) { FloatSerie fastMom = stockSerie.CalculateBuySellMomemtum((int)this.parameters[0], (bool)this.parameters[1]); this.series[0] = fastMom; this.Series[0].Name = this.Name; if (this.series[0] != null && this.Series[0].Count > 0) { this.CreateEventSeries(stockSerie.Count); FloatSerie upExLimit = new FloatSerie(stockSerie.Count, this.SerieNames[1]); FloatSerie downExLimit = new FloatSerie(stockSerie.Count, this.SerieNames[2]); FloatSerie highSerie = stockSerie.GetSerie(StockDataType.HIGH); FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW); for (int i = 1; i < this.SeriesCount; i++) { this.Series[i] = new FloatSerie(stockSerie.Count, this.SerieNames[i]); } FloatSerie indicatorToDecorate = this.Series[0]; float exhaustionSellLimit = indicatorToDecorate[0]; float exhaustionBuyLimit = indicatorToDecorate[0]; float exhaustionBuyPrice = highSerie[0]; float exhaustionSellPrice = lowSerie[0]; float exFadeOut = (100.0f - (float)this.parameters[2]) / 100.0f; float previousValue = indicatorToDecorate[0]; float currentValue; for (int i = 1; i < indicatorToDecorate.Count - 1; i++) { currentValue = indicatorToDecorate[i]; if (currentValue < previousValue) { if (indicatorToDecorate.IsBottom(i)) { if (currentValue <= exhaustionSellLimit) { // This is an exhaustion selling exhaustionSellPrice = lowSerie[i]; exhaustionSellLimit = currentValue; } else { exhaustionSellLimit *= exFadeOut; } exhaustionBuyLimit *= exFadeOut; } else { // trail exhaustion limit down exhaustionSellLimit = Math.Min(currentValue, exhaustionSellLimit); exhaustionBuyLimit *= exFadeOut; } } else if (currentValue > previousValue) { if (indicatorToDecorate.IsTop(i)) { if (currentValue >= exhaustionBuyLimit) { // This is an exhaustion selling exhaustionBuyPrice = highSerie[i]; exhaustionBuyLimit = currentValue; } else { exhaustionSellLimit *= exFadeOut; } exhaustionBuyLimit *= exFadeOut; } else { // trail exhaustion limit up exhaustionBuyLimit = Math.Max(currentValue, exhaustionBuyLimit); exhaustionSellLimit *= exFadeOut; } } else { exhaustionSellLimit *= exFadeOut; exhaustionBuyLimit *= exFadeOut; } previousValue = currentValue; upExLimit[i] = exhaustionBuyLimit; downExLimit[i] = exhaustionSellLimit; } upExLimit[indicatorToDecorate.Count - 1] = exhaustionBuyLimit; downExLimit[indicatorToDecorate.Count - 1] = exhaustionSellLimit; this.series[1] = upExLimit; this.series[2] = downExLimit; for (int i = 5; i < indicatorToDecorate.Count - 1; i++) { this.eventSeries[0][i] = fastMom[i - 1] == upExLimit[i - 1] && fastMom[i] < fastMom[i - 1]; this.eventSeries[1][i] = fastMom[i - 1] == downExLimit[i - 1] && fastMom[i] > fastMom[i - 1]; } } else { for (int i = 0; i < this.SeriesCount; i++) { this.Series[i] = new FloatSerie(0, this.SerieNames[i]); } } } }
public override void ApplyTo(StockSerie stockSerie) { FloatSerie pukeSerie = new FloatSerie(stockSerie.Count); int period = ((int)this.parameters[0]); int smoothing = ((int)this.parameters[1]); int signalSmoothing = ((int)this.parameters[3]); int inputSmoothing = ((int)this.parameters[2]); FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE).CalculateEMA(inputSmoothing); var values = stockSerie.ValueArray; // Calculate PUKE for (int i = period; i < stockSerie.Count; i++) { int puke = 0; for (int j = i - period+1; j <= i; j++) { StockDailyValue dv = values[j]; if (closeSerie[j] - closeSerie[j-1] >= 0) puke++; else puke--; } pukeSerie[i] = puke; } pukeSerie = pukeSerie.CalculateEMA(smoothing); this.Series[0] = pukeSerie; this.series[0].Name = this.SerieNames[0]; FloatSerie signalSerie = pukeSerie.CalculateEMA(signalSmoothing); this.series[1] = signalSerie; this.series[1].Name = this.SerieNames[1]; // Detecting events this.CreateEventSeries(stockSerie.Count); for (int i = period; i < stockSerie.Count; i++) { this.eventSeries[0][i] = (signalSerie[i - 1] > pukeSerie[i - 1] && signalSerie[i] < pukeSerie[i]); this.eventSeries[1][i] = (signalSerie[i - 1] < pukeSerie[i - 1] && signalSerie[i] > pukeSerie[i]); this.eventSeries[2][i] = pukeSerie[i] > signalSerie[i]; this.eventSeries[3][i] = pukeSerie[i] < signalSerie[i]; } }
public FloatSerie CalculateER(int period) { float volatility = 0; float direction; FloatSerie erSerie = new FloatSerie(this.Count, "ER"); int i = 0; for (i = 1; i <= period; i++) { volatility += Math.Abs(this[i] - this[i - 1]); } for (i = period + 1; i < this.Count; i++) { volatility += Math.Abs(this[i] - this[i - 1]) - Math.Abs(this[i - period] - this[i - period - 1]); if (volatility == 0) { erSerie[i] = 0; } else { direction = this[i] - this[i - period]; erSerie[i] = direction / volatility; } } return erSerie; }
public FloatSerie CalculateEMATrailStop(int period, int inputSmoothing) { float alpha = 2.0f / (float)(period + 1); // shortStopSerie[i] = shortStopSerie[i - 1] + alpha * (closeEMASerie[i] - shortStopSerie[i - 1]); // longStopSerie[i] = longStopSerie[i - 1] + alpha * (closeEMASerie[i] - longStopSerie[i - 1]); FloatSerie trailSerie = new FloatSerie(this.Count, "TRAILEMA"); FloatSerie EMASerie = this.CalculateEMA(inputSmoothing); bool upTrend = EMASerie[1] > EMASerie[0]; int i = 1; float extremum = EMASerie[0]; trailSerie[0] = EMASerie[0]; extremum = EMASerie[0]; foreach (float currentValue in this.Values.Skip(1)) { if (i > inputSmoothing) { if (upTrend) { if (EMASerie[i] < trailSerie[i - 1]) { // Trailing stop has been broken => reverse trend upTrend = false; trailSerie[i] = extremum; extremum = EMASerie[i]; } else { // Trail the stop trailSerie[i] = trailSerie[i - 1] + alpha * (EMASerie[i] - trailSerie[i - 1]); extremum = Math.Max(extremum, EMASerie[i]); } } else { if (EMASerie[i] > trailSerie[i - 1]) { // Trailing stop has been broken => reverse trend upTrend = true; trailSerie[i] = extremum; extremum = EMASerie[i]; } else { // Trail the stop trailSerie[i] = trailSerie[i - 1] + alpha * (EMASerie[i] - trailSerie[i - 1]); extremum = Math.Min(extremum, EMASerie[i]); } } } i++; } return trailSerie; }
public FloatSerie CalculateEMA(int emaPeriod) { FloatSerie serie = new FloatSerie(Values.Count()); if (emaPeriod <= 1) { for (int i = 0; i < this.Count; i++) { serie[i] = this[i]; } } else { float alpha = 2.0f / (float)(emaPeriod + 1); serie[0] = Values[0]; for (int i = 1; i < Values.Count(); i++) { serie[i] = serie[i - 1] + alpha * (Values[i] - serie[i - 1]); } } serie.Name = "EMA_" + emaPeriod.ToString(); return serie; }
public FloatSerie CalculateEC(int emaPeriod, float gain) { FloatSerie serie = new FloatSerie(Values.Count(), "EMA_" + emaPeriod.ToString()); if (emaPeriod <= 1) { for (int i = 0; i < this.Count; i++) { serie[i] = this[i]; } } else { float alpha = 2.0f / (float)(emaPeriod + 1); // Tradestation code: EC = a*(Price + gain*(Price – EC[1])) + (1 – a)*EC[1]; serie[0] = Values[0]; for (int i = 1; i < Values.Count(); i++) { serie[i] = alpha * (Values[i] + gain * (Values[i] - serie[i - 1])) + (1 - alpha) * serie[i - 1]; } } return serie; }
public FloatSerie CalculateEA(int period) { FloatSerie serie = new FloatSerie(Values.Count()); if (period <= 1) { for (int i = 0; i < this.Count; i++) { serie[i] = this[i]; } } else { float α = 2.0f / (float)(period + 1); float α2 = α * α; float c1 = α - (α / 2) * (α / 2); float c2 = α2 / 2; float c3 = α - 3 * α2 / 4; float c4 = 2 * (1 - α); float c5 = (1 - α) * (1 - α); serie[0] = Values[0]; serie[1] = (Values[1] + Values[0]) / 2.0f; // InstTrend = (α - (α/2)2) * Price + (α2/2) * Price[1] - (α - 3α2/4) * Price[2]) // + 2 * (1 - α) * InstTrend[1] - (1 - α)2 * InstTrend[2]; for (int i = 2; i < Values.Count(); i++) { serie[i] = c1 * Values[i] + c2 * Values[i - 1] - c3 * Values[i - 2] + c4 * serie[i - 1] - c5 * serie[i - 2]; } } serie.Name = "EA_" + period.ToString(); return serie; }
public override void ApplyTo(StockSerie stockSerie) { Queue<float> resistanceQueue = new Queue<float>(new float[] { float.MinValue, float.MinValue }); Queue<float> supportQueue = new Queue<float>(new float[] { float.MaxValue, float.MaxValue }); FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW); FloatSerie highSerie = stockSerie.GetSerie(StockDataType.HIGH); FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE); int period = (int)this.Parameters[0]; IStockTrailStop trailStop = stockSerie.GetTrailStop("TRAILHL(" + period + ")"); FloatSerie longStopSerie = trailStop.Series[0]; FloatSerie shortStopSerie = trailStop.Series[1]; BoolSerie brokenUpSerie = trailStop.Events[2]; BoolSerie brokenDownSerie = trailStop.Events[3]; FloatSerie supportSerie = new FloatSerie(stockSerie.Count, "TRAILHL.S"); supportSerie.Reset(float.NaN); FloatSerie resistanceSerie = new FloatSerie(stockSerie.Count, "TRAILHL.R"); resistanceSerie.Reset(float.NaN); this.Series[0] = supportSerie; this.Series[1] = resistanceSerie; // Detecting events this.CreateEventSeries(stockSerie.Count); this.Events[0] = brokenUpSerie; this.Events[1] = brokenDownSerie; // Begin Sequence // Calculate Support/Resistance float extremum = lowSerie[0]; bool waitingForEndOfTrend = false; int i = 0; for (; i < stockSerie.Count && (!brokenUpSerie[i] && !brokenDownSerie[i]); i++) { //if (float.IsNaN(longStopSerie[i])) //{ // this.UpDownState[i] = StockSerie.Trend.DownTrend; // Down trend // supportSerie[i] = float.NaN; // resistanceSerie[i] = highSerie.GetMax(0, i); // resistanceQueue.Dequeue(); // resistanceQueue.Enqueue(resistanceSerie[i]); // extremum = highSerie.GetMax(0, i); //} //else //{ // this.UpDownState[i] = StockSerie.Trend.UpTrend; // Up trend // supportSerie[i] = lowSerie.GetMin(0, i); // supportQueue.Dequeue(); // supportQueue.Enqueue(supportSerie[i]); // resistanceSerie[i] = float.NaN; // extremum = lowSerie.GetMin(0, i); //} } if (i < stockSerie.Count) { if (brokenUpSerie[i]) { this.UpDownState[i] = StockSerie.Trend.UpTrend; extremum = lowSerie.GetMin(0, i); } if (brokenDownSerie[i]) { this.UpDownState[i] = StockSerie.Trend.DownTrend; extremum = highSerie.GetMax(0, i); } } for (; i < stockSerie.Count; i++) { bool upSwing = float.IsNaN(shortStopSerie[i]); this.UpDownState[i] = StockUpDownIndicatorBase.BoolToTrend(upSwing); this.Events[8][i] = upSwing; this.Events[9][i] = !upSwing; if (brokenUpSerie[i]) { supportSerie[i] = extremum; supportQueue.Dequeue(); supportQueue.Enqueue(extremum); resistanceSerie[i] = float.NaN; if (waitingForEndOfTrend) {// Detect EndOfUptrend waitingForEndOfTrend = false; this.Events[3][i] = true; } else if (extremum > resistanceQueue.ElementAt(0)) {// Detect if pullback in uptrend this.Events[2][i] = true; waitingForEndOfTrend = true; } if (extremum > supportQueue.ElementAt(0)) { // Higher Low detected this.Events[4][i] = true; } else { // Lower Low this.Events[11][i] = true; } extremum = highSerie[i]; } else if (brokenDownSerie[i]) { supportSerie[i] = float.NaN; resistanceSerie[i] = extremum; resistanceQueue.Dequeue(); resistanceQueue.Enqueue(extremum); if (waitingForEndOfTrend) {// Detect EndOfUptrend waitingForEndOfTrend = false; this.Events[3][i] = true; } else if (extremum < supportQueue.ElementAt(0)) {// Detect if pullback in downTrend this.Events[2][i] = true; waitingForEndOfTrend = true; } if (extremum < resistanceQueue.ElementAt(0)) { // Lower high detected this.Events[5][i] = true; } else { // Higher high detected this.Events[10][i] = true; } extremum = lowSerie[i]; } else { supportSerie[i] = supportSerie[i - 1]; resistanceSerie[i] = resistanceSerie[i - 1]; if (float.IsNaN(supportSerie[i])) // Down trend { extremum = Math.Min(extremum, lowSerie[i]); if (closeSerie[i - 1] >= supportQueue.ElementAt(1) && closeSerie[i] < supportQueue.ElementAt(1)) { // Previous support broken this.Events[7][i] = true; } } else { extremum = Math.Max(extremum, highSerie[i]); if (closeSerie[i - 1] <= resistanceQueue.ElementAt(1) && closeSerie[i] > resistanceQueue.ElementAt(1)) { // Previous resistance broken this.Events[6][i] = true; } } } } }
public FloatSerie CalculateKEMA(int fastPeriod, int slowPeriod) { FloatSerie serie = new FloatSerie(Values.Count()); FloatSerie erSerie = CalculateER((fastPeriod + slowPeriod) / 2); erSerie = erSerie.Abs(); // Calculate square serie[0] = Values[0]; for (int i = 1; i < Values.Count(); i++) { int period = (int)((erSerie[i]) * (slowPeriod - fastPeriod)) + fastPeriod; float alpha = 2.0f / (float)(period + 1); serie[i] = serie[i - 1] + alpha * (Values[i] - serie[i - 1]); } serie.Name = "KEMA_" + slowPeriod + "_" + fastPeriod; return serie; }
public override void ApplyTo(StockSerie stockSerie) { FloatSerie varSerie = stockSerie.GetSerie(StockDataType.VARIATION); FloatSerie emaSerie = varSerie.CalculateEMA( (int)this.parameters[0]); FloatSerie cumulSerie = new FloatSerie(emaSerie.Count); float cumul = 0f; for (int i = 0; i < emaSerie.Count; i++) { cumulSerie[i] = cumul += emaSerie[i]; } this.series[0] = cumulSerie; this.Series[0].Name = this.Name; }
public FloatSerie CalculateMyAutoCovariance(int period, int lag) { FloatSerie ma1 = this.CalculateMA(period); FloatSerie variance = new FloatSerie(this.Count, "AUTOCOVAR"); float avg1, avg2; float sum; float spread1, spread2; int count; int step; for (int i = period + lag; i < this.Count; i++) { count = 0; step = 0; sum = 0.0f; avg1 = ma1[i]; avg2 = ma1[i - lag]; for (int j = i - period; j <= i; j++) { count += ++step; spread1 = this.Values[j] * step; spread2 = this.Values[j - lag] * step; sum += spread1 * spread2; } variance[i] = sum / (float)count; } return variance; }
public override void ApplyTo(StockSerie stockSerie) { FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE).CalculateEMA((int)this.parameters[3]); FloatSerie volumeSerie = stockSerie.GetSerie(StockDataType.VOLUME); FloatSerie mfiSerie = new FloatSerie(stockSerie.Count); int period = (int)this.parameters[0]; for (int i = period + 1; i < stockSerie.Count; i++) { float upFlow = 0f, downFlow = 0f; for (int j = 0; j < period; j++) { if (closeSerie[i - j - 1] < closeSerie[i - j]) { upFlow += volumeSerie[i - j] * closeSerie[i - j]; } else { downFlow += volumeSerie[i - j] * closeSerie[i - j]; } } if (downFlow == 0) { mfiSerie[i] = 100f; } else { float ratio = upFlow / downFlow; mfiSerie[i] = 100f - 100f / (1f + ratio); } } this.series[0] = mfiSerie; this.series[0].Name = this.Name; // Detecting events this.CreateEventSeries(stockSerie.Count); float overbought = (float)this.parameters[1]; float oversold = (float)this.parameters[2]; for (int i = 2; i < mfiSerie.Count; i++) { this.eventSeries[0][i] = (mfiSerie[i - 2] < mfiSerie[i - 1] && mfiSerie[i - 1] > mfiSerie[i]); this.eventSeries[1][i] = (mfiSerie[i - 2] > mfiSerie[i - 1] && mfiSerie[i - 1] < mfiSerie[i]); this.eventSeries[2][i] = mfiSerie[i] >= overbought; this.eventSeries[3][i] = mfiSerie[i] <= oversold; this.eventSeries[4][i] = this.eventSeries[2][i - 1] && !this.eventSeries[2][i]; this.eventSeries[5][i] = this.eventSeries[3][i - 1] && !this.eventSeries[3][i]; } }
public void CalculateBBEX(FloatSerie referenceAverage, int bbTimePeriod, float BBUpCoef, float BBDownCoef, ref FloatSerie bbUpSerie, ref FloatSerie bbDownSerie) { float squareSum = 0.0f; float tmp = 0.0f; float upBB = 0.0f; float downBB = 0.0f; float referenceAverageVal = 0.0f; bbUpSerie = new FloatSerie(this.Values.Count()); bbUpSerie.Name = "BBUp"; bbDownSerie = new FloatSerie(this.Values.Count()); bbDownSerie.Name = "BBDown"; for (int i = 0; i < this.Values.Count(); i++) { referenceAverageVal = referenceAverage.Values[i]; if (i < bbTimePeriod) { // Calculate BB if (i == 0) { upBB = 0.0f; downBB = 0.0f; } else { squareSum = 0.0f; for (int j = 0; j <= i; j++) { tmp = this.Values[j] - referenceAverageVal; squareSum += tmp * tmp; } tmp = (float)Math.Sqrt(squareSum / (double)(i + 1)); upBB = BBUpCoef * tmp; downBB = BBDownCoef * tmp; } } else { squareSum = 0.0f; for (int j = i - bbTimePeriod + 1; j <= i; j++) { tmp = this.Values[j] - referenceAverageVal; squareSum += tmp * tmp; } tmp = (float)Math.Sqrt(squareSum / (double)bbTimePeriod); float tt = tmp / referenceAverageVal; upBB = ((referenceAverageVal * (1.0f + tt)) - referenceAverageVal) * BBUpCoef; downBB = -((referenceAverageVal / (1.0f + tt)) - referenceAverageVal) * BBDownCoef; } if (bbUpSerie != null) { bbUpSerie.Values[i] = referenceAverageVal + upBB; } if (bbDownSerie != null) { bbDownSerie.Values[i] = referenceAverageVal + downBB; } } }
public override void ApplyTo(StockSerie stockSerie) { FloatSerie emaSerie = stockSerie.GetSerie(StockDataType.CLOSE).CalculateEMA((int)this.Parameters[0]); FloatSerie indexSerie = new FloatSerie(stockSerie.Count); for (int i = 1; i < stockSerie.Count; i++) { int count = 0; for (int j = i - 1; j >= 0; j--) { if (emaSerie[i] > emaSerie[j]) { count++; } else { break; } } indexSerie[i] = count; } this.series[0] = indexSerie; this.Series[0].Name = this.Name; }
public override void ApplyTo(StockSerie stockSerie) { using (MethodLogger ml = new MethodLogger(this)) { List<StockDailyValue> dailyValues = stockSerie.GenerateHeikinAshiBarFromDaily(stockSerie.Values.ToList()); FloatSerie upVolume = new FloatSerie(stockSerie.Count); FloatSerie downVolume = new FloatSerie(stockSerie.Count); int i = -1; foreach (StockDailyValue dailyValue in dailyValues) { i++; float R = dailyValue.HIGH - dailyValue.LOW; // Bar range float R2 = Math.Abs(dailyValue.CLOSE - dailyValue.OPEN); // Body range if (R == 0 || R2 == 0) { upVolume[i] = downVolume[i] = dailyValue.VOLUME / 2L; continue; } float R1 = dailyValue.HIGH - Math.Max(dailyValue.CLOSE, dailyValue.OPEN); // Higher shade range float R3 = Math.Min(dailyValue.CLOSE, dailyValue.OPEN) - dailyValue.LOW; // Lower shade range float V = dailyValue.VOLUME; float V1 = V * (R1 / R); float V2 = V * (R2 / R); float V3 = V * (R3 / R); if (dailyValue.CLOSE > dailyValue.OPEN) // UpBar { upVolume[i] = V2 + (V1 + V3) / 2.0f; downVolume[i] = V - upVolume[i]; } else // DownBar { downVolume[i] = V2 + (V1 + V3) / 2.0f; upVolume[i] = V - downVolume[i]; } //V = V(R1/R) + V(R2/R) + V(R3/R); } //FloatSerie upVolume = stockSerie.GetSerie(StockDataType.UPVOLUME).Sqrt(); //FloatSerie downVolume = stockSerie.GetSerie(StockDataType.DOWNVOLUME).Sqrt(); FloatSerie cumulVolume = (upVolume - downVolume).Cumul(); FloatSerie diffVolume = (cumulVolume - cumulVolume.CalculateEMA((int)this.parameters[0])).CalculateEMA((int)this.parameters[1]); FloatSerie fastSerie = diffVolume; FloatSerie fastMom = fastSerie; this.series[0] = fastMom; this.Series[0].Name = this.Name; FloatSerie signalSerie = fastMom.CalculateEMA(((int)this.parameters[2])); this.series[1] = signalSerie; this.Series[1].Name = this.SerieNames[1]; if (this.series[0] != null && this.Series[0].Count > 0) { this.CreateEventSeries(stockSerie.Count); FloatSerie upExLimit = new FloatSerie(stockSerie.Count, this.SerieNames[1]); FloatSerie downExLimit = new FloatSerie(stockSerie.Count, this.SerieNames[2]); FloatSerie highSerie = stockSerie.GetSerie(StockDataType.HIGH); FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW); FloatSerie indicatorToDecorate = this.Series[0]; float exhaustionSellLimit = indicatorToDecorate[0]; float exhaustionBuyLimit = indicatorToDecorate[0]; float exhaustionBuyPrice = highSerie[0]; float exhaustionSellPrice = lowSerie[0]; float exFadeOut = (100.0f - (float)this.parameters[3]) / 100.0f; float previousValue = indicatorToDecorate[0]; float currentValue; for (i = 1; i < indicatorToDecorate.Count - 1; i++) { currentValue = indicatorToDecorate[i]; if (currentValue < previousValue) { if (indicatorToDecorate.IsBottom(i)) { if (currentValue <= exhaustionSellLimit) { // This is an exhaustion selling exhaustionSellPrice = lowSerie[i]; exhaustionSellLimit = currentValue; } else { exhaustionSellLimit *= exFadeOut; } exhaustionBuyLimit *= exFadeOut; } else { // trail exhaustion limit down exhaustionSellLimit = Math.Min(currentValue, exhaustionSellLimit); exhaustionBuyLimit *= exFadeOut; } } else if (currentValue > previousValue) { if (indicatorToDecorate.IsTop(i)) { if (currentValue >= exhaustionBuyLimit) { // This is an exhaustion selling exhaustionBuyPrice = highSerie[i]; exhaustionBuyLimit = currentValue; } else { exhaustionSellLimit *= exFadeOut; } exhaustionBuyLimit *= exFadeOut; } else { // trail exhaustion limit up exhaustionBuyLimit = Math.Max(currentValue, exhaustionBuyLimit); exhaustionSellLimit *= exFadeOut; } } else { exhaustionSellLimit *= exFadeOut; exhaustionBuyLimit *= exFadeOut; } previousValue = currentValue; upExLimit[i] = exhaustionBuyLimit; downExLimit[i] = exhaustionSellLimit; } upExLimit[indicatorToDecorate.Count - 1] = exhaustionBuyLimit; downExLimit[indicatorToDecorate.Count - 1] = exhaustionSellLimit; this.series[2] = upExLimit; this.series[3] = downExLimit; //for ( i = 5; i < indicatorToDecorate.Count - 1; i++) //{ // this.eventSeries[0][i] = fastMom[i - 1] == upExLimit[i - 1] && fastMom[i] < fastMom[i - 1]; // this.eventSeries[1][i] = fastMom[i - 1] == downExLimit[i - 1] && fastMom[i] > fastMom[i - 1]; //} } else { for (i = 0; i < this.SeriesCount; i++) { this.Series[i] = new FloatSerie(0, this.SerieNames[i]); } } for (i = 0; i < stockSerie.Count; i++) { this.eventSeries[0][i] = fastMom[i] >= signalSerie[i]; this.eventSeries[1][i] = fastMom[i] < signalSerie[i]; } } }
public override void ApplyTo(StockSerie stockSerie) { FloatSerie highSerie = stockSerie.GetSerie(StockDataType.HIGH).CalculateEMA((int)this.Parameters[1]); FloatSerie lowSerie = stockSerie.GetSerie(StockDataType.LOW).CalculateEMA((int)this.Parameters[1]); FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE).CalculateEMA((int)this.Parameters[1]); FloatSerie atrSerie = new FloatSerie(stockSerie.Count); for (int i = 1; i < stockSerie.Count; i++) { atrSerie[i] = 100f*(highSerie[i] - lowSerie[i]) / closeSerie[i - 1]; } this.series[0] = atrSerie.CalculateEMA((int)this.Parameters[0]); this.Series[0].Name = this.Name; }
public void CalculateBBOSC(FloatSerie referenceAverage, FloatSerie widthSerie, float BBUpCoef, float BBDownCoef, ref FloatSerie bbUpSerie, ref FloatSerie bbDownSerie) { bbUpSerie = new FloatSerie(this.Values.Count()); bbUpSerie.Name = "BBOSCUp"; bbDownSerie = new FloatSerie(this.Values.Count()); bbDownSerie.Name = "BBOSCDown"; for (int i = 0; i < this.Values.Count(); i++) { float absWidth = Math.Abs(widthSerie[i]); bbDownSerie[i] = referenceAverage[i] + BBDownCoef * absWidth; bbUpSerie[i] = referenceAverage[i] + BBUpCoef * absWidth; } }