using System; using System.Collections.Generic; using System.Linq; using TSLab.Script; using TSLab.Script.Handlers; namespace ATRcube { [HandlerCategory("RusAlgo")] [HandlerName("ATRCube")] [HandlerDecimals(3)] public class Class1 : IBar2DoubleHandler { DateTime count = new DateTime(); List dayATRs = new List(); List allATR = new List(); double ATR = 0; double minPrice = 0; double maxPrice = 0; public IList Execute(ISecurity sec) { count = sec.Bars[0].Date.Add(new TimeSpan(1, 0, 0, 0)); allATR.Add(0); maxPrice = sec.Bars[0].High; minPrice = sec.Bars[0].Low; for (int i = 1; i < sec.Bars.Count; i++) { if (sec.Bars[i].Date > count) { dayATRs.Add(Math.Abs(maxPrice - minPrice)); minPrice = sec.LowPrices[i]; maxPrice = sec.HighPrices[i]; if (dayATRs.Count > 5) { dayATRs.RemoveAt(0); } if (dayATRs.Count < 5) { count = count.Add(new TimeSpan(1, 0, 0, 0)); continue; } ATR = Math.Round(dayATRs.Sum() / dayATRs.Count, 0); count = count.Add(new TimeSpan(1, 0, 0, 0)); allATR.Add(ATR); } else { allATR.Add(allATR[allATR.Count - 1]); } if (sec.HighPrices[i] > maxPrice) { maxPrice = sec.HighPrices[i]; } if (sec.LowPrices[i] < minPrice) { minPrice = sec.LowPrices[i]; } } return allATR; } } }