public void CalculateIndicators() { pivotPointsIndicator.CalculateLows(security, 6, 5); pivotPointsIndicator.CalculateHighs(security, 5, 4); ema = Series.EMA(sec.ClosePrices, 100); atr = Series.AverageTrueRange(sec.Bars, 20); }
public static IList <double> generateATR(IContext ctx, IList <Bar> data, int period) { return(ctx.GetData( "ATR" + period.ToString(), new[] { period.ToString() }, delegate { return Series.AverageTrueRange(data, period); } )); }
public static IList <double> generateATR(IContext ctx, ISecurity source, int period) { return(ctx.GetData( "ATR" + period.ToString(), new[] { period.ToString() }, delegate { return Series.AverageTrueRange(source.Bars, period); } )); }
public virtual IList <double> Execute(ISecurity sec) { var sec2 = sec.CompressTo(Interval); var atrs = Context.GetData("ATR", new[] { Period.ToString(), Interval.ToString() }, () => Series.AverageTrueRange(sec2.Bars, Period, Context)); return(Context.GetData("NRTR", new[] { Period.ToString(), Coefficient.ToString(CultureInfo.InvariantCulture), Interval.ToString() }, () => sec2.Decompress(NRTR(Context, sec2, atrs, Coefficient)))); }
public IList <bool> Execute(ISecurity source) { IReadOnlyList <IDataBar> bars = source.Bars; int count = bars.Count; IList <double> doubleList = Series.AverageTrueRange(bars, this.ParPeriod, (IMemoryContext)this.Context); IList <bool> boolList = (IList <bool>)(this.Context?.GetArray <bool>(count) ?? new bool[count]); int paRi1 = this.PARi; for (int paRi2 = this.PARi; paRi2 < count; ++paRi2) { boolList[paRi2] = this.ParBol ? doubleList[paRi2] > doubleList[paRi2 - paRi1] : doubleList[paRi2] < doubleList[paRi2 - paRi1]; } return(boolList); }
public IList <double> Execute(ISecurity source) { return(Series.AverageTrueRange(source.Bars, Period, Context)); }
public IList <bool> Execute(ISecurity source) { ISecurity dailySec = source.CompressTo(Interval.D1, 0, 1440, 600); IList <double> highPrices = dailySec.GetHighPrices(this.Context); IList <double> lowPrices = dailySec.GetLowPrices(this.Context); IList <double> closePrices = dailySec.GetClosePrices(this.Context); int count1 = source.Bars.Count; int count2 = dailySec.Bars.Count; IList <double> doubleList1 = (IList <double>) new double[count2]; IList <double> doubleList2 = (IList <double>) new double[count2]; IList <double> candles1 = (IList <double>) new double[count2]; IList <double> candles2 = (IList <double>) new double[count2]; IList <double> candles3 = (IList <double>) new double[count2]; IList <double> data1 = this.Context.GetData("ATR", new string[1] { this.ParPeriod.ToString() }, (CacheObjectMaker <IList <double> >)(() => Series.AverageTrueRange(dailySec.Bars, this.ParPeriod))); IList <double> data2 = this.Context.GetData("EMA", new string[1] { this.ParPeriod.ToString() }, (CacheObjectMaker <IList <double> >)(() => Series.EMA(dailySec.ClosePrices, this.ParPeriod))); IList <double> doubleList3 = (IList <double>) new double[count1]; IList <double> doubleList4 = (IList <double>) new double[count1]; IList <double> doubleList5 = (IList <double>) new double[count1]; IList <bool> boolList1 = (IList <bool>) new bool[count1]; IList <bool> boolList2 = (IList <bool>) new bool[count1]; IList <bool> boolList3 = (IList <bool>) new bool[count1]; for (int index = 1; index < count2; ++index) { candles1[index] = 0.0; candles2[index] = 0.0; if (highPrices[index] > highPrices[index - 1]) { candles1[index] = highPrices[index] - highPrices[index - 1]; } if (lowPrices[index] < lowPrices[index - 1]) { candles2[index] = lowPrices[index - 1] - lowPrices[index]; } if (candles1[index] > candles2[index]) { candles2[index] = 0.0; } else if (candles2[index] > candles1[index]) { candles1[index] = 0.0; } else if (candles1[index] == candles2[index]) { candles1[index] = 0.0; candles2[index] = 0.0; } } IList <double> doubleList6 = Series.EMA(candles1, this.ParPeriod); IList <double> doubleList7 = Series.EMA(candles2, this.ParPeriod); for (int index = 0; index < count2; ++index) { doubleList6[index] = data1[index] == 0.0 ? 0.0 : doubleList6[index] / data1[index]; doubleList7[index] = data1[index] == 0.0 ? 0.0 : doubleList7[index] / data1[index]; candles3[index] = doubleList6[index] != 0.0 || doubleList7[index] != 0.0 ? Math.Abs(doubleList6[index] - doubleList7[index]) / (doubleList6[index] + doubleList7[index]) * 100.0 : 0.0; } IList <double> doubleList8 = Series.EMA(candles3, this.ParPeriod); int index1 = 0; for (int index2 = 0; index2 < count1; ++index2) { DateTime date1 = source.Bars[index2].Date; DateTime date2 = date1.Date; boolList1[index2] = false; boolList2[index2] = false; while (true) { int num; if (index1 < count2) { date1 = dailySec.Bars[index1].Date; num = date1.Date < date2 ? 1 : 0; } else { num = 0; } if (num != 0) { doubleList3[index2] = doubleList8[index1]; doubleList4[index2] = data2[index1]; doubleList5[index2] = closePrices[index1]; if ((uint)index1 > 0U) { if (doubleList8[index1] > doubleList8[index1 - 1]) { boolList1[index2] = true; } else if (doubleList8[index1] < doubleList8[index1 - 1]) { boolList2[index2] = true; } } else { boolList1[index2] = false; boolList2[index2] = false; } ++index1; } else { break; } } int num1; if (index2 + 1 < count1) { date1 = source.Bars[index2 + 1].Date; if (date1.Date == date2) { num1 = 1; goto label_33; } } num1 = index1 == count2 ? 1 : 0; label_33: if (num1 != 0) { --index1; } if (index1 <= 0) { index1 = 0; doubleList3[index2] = doubleList8[index1]; doubleList4[index2] = data2[index1]; doubleList5[index2] = closePrices[index1]; boolList1[index2] = false; boolList2[index2] = false; } } for (int index2 = 0; index2 < count1; ++index2) { if (this.ParTrendUp && !this.ParFlet && !this.ParTrendDn) { bool ResADX = doubleList5[index2] > doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; int num = this.ParBolTrendUp ? 0 : (!this.ParMenTrendUp ? 1 : 0); boolList3[index2] = num == 0 ? AdvFazyRynka2.GetResADX(this.ParBolTrendUp, this.ParMenTrendUp, ResADX, boolList1[index2], boolList2[index2]) : ResADX; } else if (!this.ParTrendUp && this.ParFlet && !this.ParTrendDn) { bool ResADX = doubleList3[index2] < this.KoefADXD1; int num = this.ParBolFlet ? 0 : (!this.ParMenFlet ? 1 : 0); boolList3[index2] = num == 0 ? AdvFazyRynka2.GetResADX(this.ParBolFlet, this.ParMenFlet, ResADX, boolList1[index2], boolList2[index2]) : ResADX; } else if (!this.ParTrendUp && !this.ParFlet && this.ParTrendDn) { bool ResADX = doubleList5[index2] < doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; int num = this.ParBolTrendDn ? 0 : (!this.ParMenTrendDn ? 1 : 0); boolList3[index2] = num == 0 ? AdvFazyRynka2.GetResADX(this.ParBolTrendDn, this.ParMenTrendDn, ResADX, boolList1[index2], boolList2[index2]) : ResADX; } else if (this.ParTrendUp && this.ParFlet && !this.ParTrendDn) { bool ResADX1 = doubleList5[index2] > doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; bool flag1 = this.ParBolTrendUp || this.ParMenTrendUp ? AdvFazyRynka2.GetResADX(this.ParBolTrendUp, this.ParMenTrendUp, ResADX1, boolList1[index2], boolList2[index2]) : ResADX1; bool ResADX2 = doubleList3[index2] < this.KoefADXD1; bool flag2 = this.ParBolFlet || this.ParMenFlet ? AdvFazyRynka2.GetResADX(this.ParBolFlet, this.ParMenFlet, ResADX2, boolList1[index2], boolList2[index2]) : ResADX2; boolList3[index2] = flag1 || flag2; } else if (this.ParTrendUp && !this.ParFlet && this.ParTrendDn) { bool ResADX1 = doubleList5[index2] > doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; bool flag1 = this.ParBolTrendUp || this.ParMenTrendUp ? AdvFazyRynka2.GetResADX(this.ParBolTrendUp, this.ParMenTrendUp, ResADX1, boolList1[index2], boolList2[index2]) : ResADX1; bool ResADX2 = doubleList5[index2] < doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; bool flag2 = this.ParBolTrendDn || this.ParMenTrendDn ? AdvFazyRynka2.GetResADX(this.ParBolTrendDn, this.ParMenTrendDn, ResADX2, boolList1[index2], boolList2[index2]) : ResADX2; boolList3[index2] = flag1 || flag2; } else if (!this.ParTrendUp && this.ParFlet && this.ParTrendDn) { bool ResADX1 = doubleList3[index2] < this.KoefADXD1; bool flag1 = this.ParBolFlet || this.ParMenFlet ? AdvFazyRynka2.GetResADX(this.ParBolFlet, this.ParMenFlet, ResADX1, boolList1[index2], boolList2[index2]) : ResADX1; bool ResADX2 = doubleList5[index2] < doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; bool flag2 = this.ParBolTrendDn || this.ParMenTrendDn ? AdvFazyRynka2.GetResADX(this.ParBolTrendDn, this.ParMenTrendDn, ResADX2, boolList1[index2], boolList2[index2]) : ResADX2; boolList3[index2] = flag1 || flag2; } else if (this.ParTrendUp && this.ParFlet && this.ParTrendDn) { bool ResADX1 = doubleList5[index2] > doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; bool flag1 = this.ParBolTrendUp || this.ParMenTrendUp ? AdvFazyRynka2.GetResADX(this.ParBolTrendUp, this.ParMenTrendUp, ResADX1, boolList1[index2], boolList2[index2]) : ResADX1; bool ResADX2 = doubleList3[index2] < this.KoefADXD1; bool flag2 = this.ParBolFlet || this.ParMenFlet ? AdvFazyRynka2.GetResADX(this.ParBolFlet, this.ParMenFlet, ResADX2, boolList1[index2], boolList2[index2]) : ResADX2; bool ResADX3 = doubleList5[index2] < doubleList4[index2] && doubleList3[index2] > this.KoefADXD1; bool flag3 = this.ParBolTrendDn || this.ParMenTrendDn ? AdvFazyRynka2.GetResADX(this.ParBolTrendDn, this.ParMenTrendDn, ResADX3, boolList1[index2], boolList2[index2]) : ResADX3; boolList3[index2] = flag1 || flag2 || flag3; } else { boolList3[index2] = false; } } return(boolList3); }
private void MakeAtrs(ISecurity security) { m_atrs = Context.GetData("ATRTrailStop$ATR", new[] { ATRPeriod.ToString(), security.Symbol, security.Interval.ToString() }, () => Series.AverageTrueRange(security.Bars, ATRPeriod, Context)); }
/// <summary> /// Calculate ATRM indicator for bars list /// </summary> public static IList <double> CalcATRM(IContext context, IReadOnlyList <IDataBar> bars, int period, Interval roundInterval, bool positive) { var count = bars.Count; var atrs = context.GetArray <double>(count) ?? new double[count]; if (count == 0) { return(atrs); } double high = 0, low = 0; double kr = 0, no = 0; var date = roundInterval.AlignDate(bars[0].Date); for (var i = 0; i < period; i++) { atrs[i] = 0; } double prevATR = 0; var first = true; for (var i = period; i < count; i++) { prevATR = Series.AverageTrueRange(bars, i, period, prevATR); var kirPer = prevATR; prevATR = kirPer; if (DoubleUtil.IsZero(kirPer)) { kirPer = 1e-10; } var date2 = roundInterval.AlignDate(bars[i].Date); var cur = bars[i].Close; if (first || date < date2) { first = false; date = date2; high = low = cur; kr = no = 0; } if (cur > high + kirPer) { var kk = Math.Floor((cur - (high + kirPer)) / kirPer) + 1; high = cur; low = cur - kirPer; kr = kr + kk; no = 0; } if (cur < low - kirPer) { var kn = Math.Floor((low - kirPer - cur) / kirPer) + 1; low = cur; high = cur + kirPer; no = no + kn; kr = 0; } var lowValue = -no; var highValue = kr; var value = highValue + lowValue; if (positive && value < 0) { value = 0; } if (!positive && value > 0) { value = 0; } atrs[i] = value; } return(atrs); }