111 lines
4.9 KiB
C#
111 lines
4.9 KiB
C#
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 = 12;
|
|
private readonly double rangeThresholdPercent = 0.7;
|
|
private readonly int bandLength = 12;
|
|
private readonly int recentTouchLookbackTypeLong = 5;
|
|
private readonly int recentTouchLookbackTypeShort = 15;
|
|
private int? sma4LongTouchBar = null;
|
|
private int? sma4ShortTouchBar = null;
|
|
|
|
public bool Calculate(List<CandleData> 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<double> sma4Series = new List<double>();
|
|
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<double> prevSma4Series = new List<double>();
|
|
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<double> prev2Sma4Series = new List<double>();
|
|
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 >= recentTouchLookbackTypeLong;
|
|
|
|
if (sma4 < sma4Middle)
|
|
sma4ShortTouchBar = currentIndex;
|
|
|
|
bool isShortRecentTouch = sma4ShortTouchBar.HasValue && currentIndex - sma4ShortTouchBar.Value >= recentTouchLookbackTypeShort;
|
|
|
|
// === 시그널 계산 ===
|
|
if (type == SMABandType.LongBuy)
|
|
{
|
|
return isLongRecentTouch && isSideways && prevSma4 < sma4 && sma4High == sma4 &&
|
|
prevSma4High > prevSma4 && (prev2Sma4Middle - prevSma4Middle) > (prevSma4Middle - sma4Middle);
|
|
}
|
|
else if (type == SMABandType.ShortBuy)
|
|
{
|
|
return isShortRecentTouch && prevSma4High > prevSma4 && prev2Sma4High == prev2Sma4 &&
|
|
prevSma4 > sma4;
|
|
}
|
|
else if (type == SMABandType.LongSell)
|
|
{
|
|
return (!isSideways && prevSma4High > prevSma4) || (isSideways && prev2Sma4High > prev2Sma4 && prevSma4High > prevSma4);
|
|
}
|
|
else if (type == SMABandType.ShortSell)
|
|
{
|
|
return (sma4High == sma4 && prevSma4High > prevSma4) || (!isSideways && prevSma4Low == prevSma4 && sma4Low < sma4) || (isSideways && prev2Sma4Low == prev2Sma4 && prevSma4Low < prevSma4 && sma4Low < sma4);
|
|
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|