using BitgetAPI.Data;
using BitgetAPI.Data.IndicatorData;
using DevExpress.Utils.Extensions;
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 System.Windows.Media.Media3D;
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 = 2;
///
/// false 는 구매 전, true는 판매 후
///
private bool firstCloudFlag = true;
public bool Calculate(List candles, SMABandType type, bool flag = false)
{
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();
if (flag)
{
close.RemoveAt(close.Count - 1);
high.RemoveAt(high.Count - 1);
low.RemoveAt(low.Count - 1);
open.RemoveAt(open.Count - 1);
}
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);
var ichmok = GetIchimokuCloud(candles, i - 25);
var cloudTopNow = Math.Max(ichmok.SenkouSpanA, ichmok.SenkouSpanB);
var cloudBottomNow = Math.Min(ichmok.SenkouSpanA, ichmok.SenkouSpanB);
bool isSma4BelowCloud = low[i] < cloudBottomNow;
bool isSma4AboveCloud = high[i] > 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 = sma4 > cloudBottomNow && IsSideways(high, low, i, true) && isSma4AboveCloud && (sma4 == sma4High && prevSma4 <= prevSma4High && prev2Sma4 <= prev2Sma4High) && (sma4 >= prevSma4 && prevSma4 >= prev2Sma4 && prev2Sma4 >= prev3Sma4);
bool rawLongSell = (sma4 <= prevSma4 && prevSma4 <= prev2Sma4 && sma4 < sma4High);
//cloudFlag &&
bool rawNewShort = IsSideways(high, low, i, false) && 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 (rawShortSell)
// firstCloudFlag = true;
return rawShortSell;
}
return false;
}
private double Sma4(List 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 source, int currentIndex, int period)
{
return Enumerable.Range(currentIndex - period + 1, period)
.Select(i => Sma4(source, i))
.Average();
}
private double Highest(List source, int currentIndex, int period)
{
return Enumerable.Range(currentIndex - period + 1, period)
.Select(i => Sma4(source, i))
.Max();
}
private double Lowest(List source, int currentIndex, int period)
{
return Enumerable.Range(currentIndex - period + 1, period)
.Select(i => Sma4(source, i))
.Min();
}
private bool IsSideways(List high, List low, int index, bool type)
{
double highestHigh = high.Skip(index - rangeLookback).Take(rangeLookback + 1).Max();
double lowestLow = low.Skip(index - rangeLookback).Take(rangeLookback + 1).Min();
// long
if (type)
{
double range = high[index] - lowestLow;
return range > (high[index] * rangeThresholdPercent / 100.0) && highestHigh == high[index];
}
//short
else
{
double range = highestHigh - low[index];
return range > (low[index] * rangeThresholdPercent / 100.0) && lowestLow == low[index];
}
}
private IchimokuResult GetIchimokuCloud(List candles, int i)
{
var highs = candles.Select(t => t.High).ToList();
var lows = candles.Select(t => t.Low).ToList();
var closes = candles.Select(t => t.Close).ToList();
var result = new IchimokuResult();
double tenkan = 0;
double kijun = 0;
double senkouA = 0;
double senkouB = 0;
double chikou = 0;
// Tenkan-sen (9)
if (i >= 8)
{
double max9 = highs.GetRange(i - 8, 9).Max();
double min9 = lows.GetRange(i - 8, 9).Min();
tenkan = (max9 + min9) / 2.0;
}
// Kijun-sen (26)
if (i >= 25)
{
double max26 = highs.GetRange(i - 25, 26).Max();
double min26 = lows.GetRange(i - 25, 26).Min();
kijun = (max26 + min26) / 2.0;
}
// Senkou Span A (현재 시점에서 계산만, 시프트는 X)
if (tenkan != 0 && kijun != 0)
{
senkouA = (tenkan + kijun) / 2.0;
}
// Senkou Span B (52)
if (i >= 51)
{
double max52 = highs.GetRange(i - 51, 52).Max();
double min52 = lows.GetRange(i - 51, 52).Min();
senkouB = (max52 + min52) / 2.0;
}
return new IchimokuResult
{
TenkanSen = tenkan,
KijunSen = kijun,
SenkouSpanA = senkouA,
SenkouSpanB = senkouB,
ChikouSpan = chikou
};
}
}
}