Files
BitGet_API/BitgetApi/IndicatorConditionSetup/MovingAverageConditionSetup.cs
2024-08-19 10:44:43 +09:00

118 lines
4.0 KiB
C#

using BitgetApi.Data;
using BitgetAPI.Data;
using BitgetAPI.Data.IndicatorData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BitgetAPI.IndicatorConditionSetup
{
[Serializable]
public class MovingAverageConditionSetup
{
// 지표 기본 값 설정
// 이동 평균선 종류
public CommonProperty.MovingAverageType MovingAverageType { get; set; }
// 기간
public int CandleLegnth24 { get; set; } = 24;
public int CandleLegnth48 { get; set; } = 48;
// 기간
public int CandleLegnth4 { get; set; } = 4;
// 기간
public int CandleLegnth72 { get; set; } = 72;
// 기간
public int CandleLegnth12 { get; set; } = 12;
// 가격
public CommonProperty.CandlePriceType MACandlePriceType { get; set; }
// 추후 업데이트 된것을 계산 안하는거 고려 !!!!
public void Cal_YHK_Downpublic(List<CandleData> candleDatas, CandleData candleData)
{
// 현재 종가 인덱스
var endIndex = candleDatas.Count() - 1;
if (candleData == null) return;
// Moving 없으면 생성
if (candleData.movingAverageDataList == null)
{
candleData.movingAverageDataList = new Dictionary<int, MovingAverageData>();
candleData.movingAverageDataList.Add(CandleLegnth4, new MovingAverageData());
candleData.movingAverageDataList.Add(CandleLegnth12, new MovingAverageData());
candleData.movingAverageDataList.Add(CandleLegnth24, new MovingAverageData());
candleData.movingAverageDataList.Add(CandleLegnth48, new MovingAverageData());
candleData.movingAverageDataList.Add(CandleLegnth72, new MovingAverageData());
}
foreach(var item in candleData.movingAverageDataList.ToList())
{
// 전봉까지 만듬
var datas = candleDatas.FindAll(i =>
candleDatas.IndexOf(i) > endIndex - item.Key &&
candleDatas.IndexOf(i) <= endIndex);
candleData.movingAverageDataList[item.Key] = new MovingAverageData();
candleData.movingAverageDataList[item.Key].Value = datas.Sum(i => i.Close) / item.Key;
CalBolangerValue(datas, candleData.movingAverageDataList[item.Key], datas.Count - 1, item.Key);
}
}
//볼랜저 계산
public void CalBolangerValue(List<CandleData> data, MovingAverageData mvData, int i, int period)
{
double total_average = 0;
if (i < period - 1)
{
return;
}
for (int t = i - period + 1; t < i; t++)
{
total_average += data[t].Close;
}
total_average += data[i].Close;
double average = total_average / period;
double t_Close = 0;
//오버렙 값 구하기 위한 변수
double sum = 0;
double powSum = 0;
int j;
for (j = (i - period + 1); j < i; j++)
{
t_Close += Math.Pow(data[j].Close - average, 2);
sum += data[j].Close;
powSum += Math.Pow(data[j].Close, 2);
}
t_Close += Math.Pow(data[j].Close - average, 2);
//하향선
if (mvData.BolangerType == 0)
{
mvData.BolangerValue = average - mvData.BolangerSquared * Math.Sqrt(t_Close / period);
}
//상향선
else if (mvData.BolangerType == 1)
{
mvData.BolangerValue = average + mvData.BolangerSquared * Math.Sqrt(t_Close / period);
}
//기준선
else if (mvData.BolangerType == 2)
{
mvData.BolangerValue = average;
}
}
}
}