Files
BitGet_API/BitgetApi/IndicatorConditionSetup/SMABandConditionSetup.cs
2025-06-04 10:53:51 +09:00

231 lines
9.0 KiB
C#

using BitgetAPI.Data;
using BitgetAPI.Data.IndicatorData;
using DevExpress.XtraBars.Ribbon.Gallery;
using DevExpress.XtraBars.ViewInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BitgetAPI.CommonProperty;
using static DevExpress.Utils.Diagnostics.GUIResources;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
namespace BitgetApi.IndicatorConditionSetup
{
public class IchimokuResult
{
public DateTime Time { get; set; }
public double TenkanSen { get; set; } // 전환선
public double KijunSen { get; set; } // 기준선
public double SenkouSpanA { get; set; } // 선행스팬1
public double SenkouSpanB { get; set; } // 선행스팬2
public double ChikouSpan { get; set; } // 후행스팬
}
public class SMABandConditionSetup
{
private readonly int rangeLookback = 12;
private readonly double rangeThresholdPercent = 0.7;
/// <summary>
/// false 는 구매 전, true는 판매 후
/// </summary>
private bool firstCloudFlag = true;
public bool Calculate(List<CandleData> candles, SMABandType type)
{
var close = candles.Select(t => t.Close).ToList();
var high = candles.Select(t => t.High).ToList();
var low = candles.Select(t => t.Low).ToList();
var open = candles.Select(t => t.Open).ToList();
int i = close.Count - 1;
if (close.Count < 100 || high.Count < 100 || low.Count < 100 || open.Count < 100)
return false;
// SMA4 관련
double sma4 = Sma4(close, i);
double prevSma4 = Sma4(close, i - 1);
double prev2Sma4 = Sma4(close, i - 2);
double prev3Sma4 = Sma4(close, i - 3);
// 밴드 계산
double sma4High = Highest(close, i, 12);
double sma4Middle = SMA(close, i, 12);
double sma4Low = Lowest(close, i, 12);
double prevSma4High = Highest(close, i - 1, 12);
double prevSma4Middle = SMA(close, i - 1, 12);
double prevSma4Low = Lowest(close, i - 1, 12);
double prev2Sma4High = Highest(close, i - 2, 12);
double prev2Sma4Low = Lowest(close, i - 2, 12);
double prev2Sma4Middle = SMA(close, i - 2, 12);
double prev3Sma4High = Highest(close, i - 3, 12);
double prev3Sma4Low = Lowest(close, i - 3, 12);
double prev3Sma4Middle = SMA(close, i - 3, 12);
bool isSideways = IsSideways(high, low, i);
var ichmok = GetIchimokuCloud(candles, i);
var cloudTopNow = Math.Max(ichmok.SenkouSpanA, ichmok.SenkouSpanB);
var cloudBottomNow = Math.Min(ichmok.SenkouSpanA, ichmok.SenkouSpanB);
bool isSma4BelowCloud = sma4 < cloudBottomNow;
bool isSma4AboveCloud = sma4 > cloudTopNow;
bool cloudFlag = false;
if (!firstCloudFlag)
{
if(isSma4BelowCloud)
cloudFlag = true;
}
else
{
if (!isSma4BelowCloud)
firstCloudFlag = false;
}
bool greenCandle = open[i] < close[i];
bool redCandle = open[i] > close[i];
// 진입/청산 조건
bool rawNewLong = isSideways && isSma4AboveCloud && (sma4 == sma4High && prevSma4 <= prevSma4High && prev2Sma4 <= prev2Sma4High) && (sma4 >= prevSma4 && prevSma4 >= prev2Sma4 && prev2Sma4 >= prev3Sma4);
bool rawLongSell = (sma4 <= prevSma4 && prevSma4 <= prev2Sma4 && sma4 < sma4High);
bool rawNewShort = cloudFlag && isSideways && isSma4BelowCloud && (sma4 == sma4Low && prevSma4 >= prevSma4Low && prev2Sma4 >= prev2Sma4Low) && (sma4 <= prevSma4 && prevSma4 <= prev2Sma4 && prev2Sma4 <= prev3Sma4);
bool rawShortSell = (sma4 >= prevSma4 && prevSma4 >= prev2Sma4 && sma4 > sma4Middle);
// === 시그널 계산 ===
if (type == SMABandType.LongBuy)
{
return rawNewLong;
}
else if (type == SMABandType.ShortBuy)
{
return rawNewShort;
}
else if (type == SMABandType.LongSell)
{
return rawLongSell;
}
else if (type == SMABandType.ShortSell)
{
if (rawLongSell)
firstCloudFlag = true;
return rawShortSell;
}
return false;
}
private double Sma4(List<double> list, int index) =>
Avg(list[index], list[index - 1], list[index - 2], list[index - 3]);
private double Avg(params double[] values) =>
values.Average();
private double SMA(List<double> source, int currentIndex, int period)
{
return Enumerable.Range(currentIndex - period + 1, period)
.Select(i => Sma4(source, i))
.Average();
}
private double Highest(List<double> source, int currentIndex, int period)
{
return Enumerable.Range(currentIndex - period + 1, period)
.Select(i => Sma4(source, i))
.Max();
}
private double Lowest(List<double> source, int currentIndex, int period)
{
return Enumerable.Range(currentIndex - period + 1, period)
.Select(i => Sma4(source, i))
.Min();
}
private bool IsSideways(List<double> high, List<double> low, int index)
{
double highestHigh = high.Skip(index - rangeLookback).Take(rangeLookback).Max();
double lowestLow = low.Skip(index - rangeLookback).Take(rangeLookback).Min();
double range = highestHigh - lowestLow;
return range > (highestHigh * rangeThresholdPercent / 100.0);
}
private IchimokuResult GetIchimokuCloud(List<CandleData> candles, int i)
{
int lastIndex = i;
int tenkanPeriod = 9;
int kijunPeriod = 26;
int senkouBPeriod = 52;
int displacement = 26;
var result = new IchimokuResult();
// 전환선 계산 (현재봉 기준 9봉 범위)
if (lastIndex + 1 >= tenkanPeriod)
{
var tenkanHigh = candles.GetRange(lastIndex + 1 - tenkanPeriod, tenkanPeriod).Max(c => c.High);
var tenkanLow = candles.GetRange(lastIndex + 1 - tenkanPeriod, tenkanPeriod).Min(c => c.Low);
result.TenkanSen = (tenkanHigh + tenkanLow) / 2;
}
// 기준선 계산 (현재봉 기준 26봉 범위)
if (lastIndex + 1 >= kijunPeriod)
{
var kijunHigh = candles.GetRange(lastIndex + 1 - kijunPeriod, kijunPeriod).Max(c => c.High);
var kijunLow = candles.GetRange(lastIndex + 1 - kijunPeriod, kijunPeriod).Min(c => c.Low);
result.KijunSen = (kijunHigh + kijunLow) / 2;
}
// 선행스팬 A 계산: '현재봉 시점'에 표시하려면 26봉 전 값(미래 26봉이 아니라)으로 계산
if (lastIndex + 1 >= kijunPeriod && lastIndex + 1 >= tenkanPeriod && lastIndex - displacement >= 0)
{
// 26봉 전(과거) 봉 기준으로 전환선과 기준선 다시 계산
var spanIndex = lastIndex - displacement;
var spanTenkanHigh = candles.GetRange(spanIndex + 1 - tenkanPeriod, tenkanPeriod).Max(c => c.High);
var spanTenkanLow = candles.GetRange(spanIndex + 1 - tenkanPeriod, tenkanPeriod).Min(c => c.Low);
var spanTenkan = (spanTenkanHigh + spanTenkanLow) / 2;
var spanKijunHigh = candles.GetRange(spanIndex + 1 - kijunPeriod, kijunPeriod).Max(c => c.High);
var spanKijunLow = candles.GetRange(spanIndex + 1 - kijunPeriod, kijunPeriod).Min(c => c.Low);
var spanKijun = (spanKijunHigh + spanKijunLow) / 2;
result.SenkouSpanA = (spanTenkan + spanKijun) / 2;
}
// 선행스팬 B 계산 (26봉 전 기준 52봉 범위)
if (lastIndex + 1 >= senkouBPeriod && lastIndex - displacement >= senkouBPeriod - 1)
{
int spanIndex = lastIndex - displacement;
var spanBHigh = candles.GetRange(spanIndex + 1 - senkouBPeriod, senkouBPeriod).Max(c => c.High);
var spanBLow = candles.GetRange(spanIndex + 1 - senkouBPeriod, senkouBPeriod).Min(c => c.Low);
result.SenkouSpanB = (spanBHigh + spanBLow) / 2;
}
// 후행스팬은 26봉 전 종가 (시간 그대로)
if (lastIndex - displacement >= 0)
{
result.ChikouSpan = candles[lastIndex - displacement].Close;
}
return result;
}
}
}