using BitgetAPI.Data; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static BitgetAPI.CommonProperty; using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip; namespace BitgetApi.IndicatorConditionSetup { public class SMABandConditionSetup { private readonly int rangeLookback = 10; private readonly double rangeThresholdPercent = 1.0; private readonly int bandLength = 12; private readonly int recentTouchLookback = 5; private int? sma4LongTouchBar = null; private int? sma4ShortTouchBar = null; public bool Calculate(List candles, SMABandType type) { var currentIndex = candles.Count - 1; var close = candles.Select(t => t.Close).ToList(); var high = candles.Select(t=>t.High).ToList(); var low = candles.Select(t => t.Low).ToList(); if (currentIndex < 72 || close.Count <= currentIndex || high.Count <= currentIndex || low.Count <= currentIndex) return false; // === SMA(4) 계산 === double sma4 = close.GetRange(currentIndex - 3, 4).Average(); double prevSma4 = new[] { close[currentIndex - 4], close[currentIndex - 1], close[currentIndex - 2], close[currentIndex - 3] }.Average(); double prev2Sma4 = new[] { close[currentIndex - 4], close[currentIndex - 5], close[currentIndex - 2], close[currentIndex - 3] }.Average(); // === SMA(4) 밴드 계산 === List sma4Series = new List(); for (int i = currentIndex - bandLength + 1; i <= currentIndex; i++) { var s = close.GetRange(i - 3, 4).Average(); sma4Series.Add(s); } double sma4High = sma4Series.Max(); double sma4Low = sma4Series.Min(); double sma4Middle = sma4Series.Average(); // 이전 SMA4 밴드 List prevSma4Series = new List(); for (int i = currentIndex - bandLength + 1; i <= currentIndex; i++) { var s = new[] { close[i - 4], close[i - 1], close[i - 2], close[i - 3] }.Average(); prevSma4Series.Add(s); } double prevSma4High = prevSma4Series.Max(); double prevSma4Low = prevSma4Series.Min(); double prevSma4Middle = prevSma4Series.Average(); List prev2Sma4Series = new List(); for (int i = currentIndex - bandLength + 1; i <= currentIndex; i++) { var s = new[] { close[i - 4], close[i - 5], close[i - 2], close[i - 3] }.Average(); prev2Sma4Series.Add(s); } double prev2Sma4Middle = prev2Sma4Series.Average(); double prev2Sma4High = prev2Sma4Series.Max(); double prev2Sma4Low = prev2Sma4Series.Min(); // === 횡보 구간 === double highestHigh = high.Skip(currentIndex - rangeLookback + 1).Take(rangeLookback).Max(); double lowestLow = low.Skip(currentIndex - rangeLookback + 1).Take(rangeLookback).Min(); double priceRange = highestHigh - lowestLow; bool isSideways = priceRange > (highestHigh * rangeThresholdPercent / 100.0); // === 최근 터치 === if (sma4 == sma4Low) sma4LongTouchBar = currentIndex; bool isLongRecentTouch = sma4LongTouchBar.HasValue && currentIndex - sma4LongTouchBar.Value >= recentTouchLookback; if (sma4 == sma4High) sma4ShortTouchBar = currentIndex; bool isShortRecentTouch = sma4ShortTouchBar.HasValue && currentIndex - sma4ShortTouchBar.Value >= recentTouchLookback; // === 시그널 계산 === if (type == SMABandType.LongBuy) { return isLongRecentTouch && isSideways && prevSma4 < sma4 && sma4High == sma4 && prevSma4High > prevSma4 && (prev2Sma4Middle - prevSma4Middle) > (prevSma4Middle - sma4Middle); } else if(type == SMABandType.ShortBuy) { return isShortRecentTouch && prevSma4 < sma4 && sma4High == sma4 && prevSma4High > prevSma4 && (prev2Sma4Middle - prevSma4Middle) > (prevSma4Middle - sma4Middle); } else if(type == SMABandType.LongSell) { return (!isSideways && prevSma4High > prevSma4) || (isSideways && prev2Sma4High > prev2Sma4 && prevSma4High > prevSma4); } else if(type == SMABandType.ShortSell) { return (!isSideways && prevSma4Low < prevSma4) || (isSideways && prev2Sma4Low < prev2Sma4 && prevSma4Low < prevSma4); } return false; } } }