Files
Million/Million Prototype/Form1.cs
2022-04-16 22:27:09 +09:00

426 lines
20 KiB
C#

using Million;
using Million.Data;
using Million.Data.Upbit;
using Million.DbManagement;
using Million.Indicators;
using Million.Manager;
using Million.WebMangement.Upbit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Million.CommonProperty;
namespace Million_Prototype
{
public partial class Form1 : Form
{
//public static string accessKey = "4jfMUL3lpwJ5GQSszm3rPp5M47tD8ssfIGew2dDb";
//public static string secretKey = "iAasVxQt8CyADjzXaAZiF5VZXTZPdElJOP4m97Bz";
//UpbitAPI uApi = new UpbitAPI(accessKey,secretKey);
Stopwatch sw = new Stopwatch();
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 1. 데이터 저장
/// 2. 조건식 검색
/// 3. 도출
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
sw.Restart();
var data = UpbitApi.GetMarketDatas(); //전체 목록
var krwList = data.FindAll(x => x.Payment.Contains("KRW")); //한화 목록
List<CandleDataList> dataList = new List<CandleDataList>();
setCandleDataList(dataList, krwList);
//최초 데이터 전체 저장
//4시간, 1분
var dateTime = DateTime.Now;
//4시간 가져오기
foreach (var krw in dataList)
{
krw.CandleDataLists_Im = UpbitApi.GetCandleStics(krw.ConGoodsData.CodeFullName, MCandleTimeType.H4, dateTime);
}
/*
//1분 가져오기
foreach (var krw in dataList)
{
var startDateTime = dateTime;
var endDateTime = krw.CandleDataLists_4H.Last().DateTime;
do
{
krw.CandleDataLists_Im.AddRange(uApi.GetCandleStics(krw.ConGoodsData.CodeFullName, MCandleTimeType.M1, dateTime));
dateTime = krw.CandleDataLists_Im.Last().DateTime;
}
while (endDateTime < dateTime);
}*/
//볼린저 계산
//일목균형표 계산
var test = krwList.Select(t => t.CodeFullName).ToList();
//var 현재가 = uApi.GetOrderBookList(test);GetTickerList
var 현재가 = UpbitApi.GetTickerList(test);
foreach (var krw in krwList)
{
//candledataList.Add(uApi.GetCandleStics(krw.CodeFullName, Million.CommonProperty.MCandleTimeType.D1, DateTime.Now, 50));
}
sw.Stop();
listBox1.Items.Add("시간 : " + sw.ElapsedMilliseconds);
현재가.ToString();
}
public void setCandleDataList(List<CandleDataList> dataList, List<MarketData> marketDataList)
{
foreach (var marketData in marketDataList)
{
dataList.Add(new CandleDataList()
{
ConGoodsData = marketData,
});
}
}
//DB데이터 불러오기
private void button2_Click(object sender, EventArgs e)
{
DbServiceFunctions.GetMarketTable_FromDB("Upbit");
var tableList = CommonProperty.selectedMarketOfPlaceList;
var selectTable = tableList.FirstOrDefault(t => t.CodeFullName == "KRW-BTC");
if (selectTable != null)
{
var startDate = new DateTime(2000, 1, 1);
var candleData = DbServiceFunctions.GetCandleData(selectTable, startDate, 100000, MCandleTimeType.M1);
Bollinger_Bands.AddMovingAndBollingerBands(candleData.CandleDataLists_Im, 40, 2);
Bollinger_Bands.AddMovingAndBollingerBands(candleData.CandleDataLists_Im, 40, 1, false);
Ichimoku_cloud.excute(candleData.CandleDataLists_Im);
var resultdata = candleData.CandleDataLists_Im.FindAll(x => x.IndicatorDic["Bol401"].overStatus == OverStatus.Over);
resultdata.ToString();
}
}
private void button3_Click(object sender, EventArgs e)
{
List<CandleDataList> list = null;
DataSettingManager realTimeMsg = null;
int firstFlag = 0;
for (; ; )
{
if (firstFlag == 0)
{
realTimeMsg = new DataSettingManager(null, PaymentType.KRW);
realTimeMsg.StartGetRealTimeTicker(null);
var dsm = new DataSettingManager(null, PaymentType.KRW);
list = dsm.GetRealTimeCandleData(MCandleTimeType.M1, 200);
firstFlag = 1;
}
else
{
if (firstFlag == 1)
{
firstFlag = 2;
realTimeMsg.EndRealTimeTicker();
list = realTimeMsg.ProcessRecentlyCandleDatas(list, realTimeMsg.tickerList, MCandleTimeType.M1);
realTimeMsg.StartGetRealTimeTicker(null);
}
else if (firstFlag == 2)
{
list = realTimeMsg.ProcessRecentlyCandleDatas(list, realTimeMsg.tickerList, MCandleTimeType.M1);
// 1분 간격
if (DateTime.Now.Second == 0)
{
realTimeMsg.StartGetRealTimeTicker(null);
}
Thread.Sleep(500);
}
}
}
}
private void button4_Click(object sender, EventArgs e)
{
DbServiceFunctions.GetMarketTable_FromDB("Upbit");
var tableList = CommonProperty.selectedMarketOfPlaceList;
var resultList = new List<SearchResult>();
var errorResultList = new List<SearchResult>();
var win = 0;
var lose = 0;
foreach (var marketData in tableList)
{
var buyCandleList = new List<CandleDataList>();
var sellList = new List<CandleData>();
if (marketData != null)
{
//사는 로직
var candleDataH1 = new CandleDataList();
var candleDataM1 = new CandleDataList();
var splitDataM = new List<List<CandleData>>();
var splitData = ValidationCheckManager.GetValidationCandleDatas(marketData,ref splitDataM, ref candleDataH1, ref candleDataM1, new DateTime(2022, 1, 1), DateTime.Now);
var buyData = new CandleDataList();
buyData.ConGoodsData = marketData;
buyData.CandleDataLists_Im = new List<CandleData>();
foreach (var split in splitData)
{
var overData = split.FindAll(x => (int)x.IndicatorDic["Bol401"].overStatus >= -2 && x.IndicatorDic["Bol401"].overStatus != OverStatus.None
&& ((IchimokData)x.IndicatorDic["Ich"]).value < x.StartValue && ((IchimokData)x.IndicatorDic["Ich"]).value2 < x.StartValue);
buyData = BuyLogicManager.GetBuyLogicByBol401Upper(overData, candleDataM1);
if (buyData.CandleDataLists_Im != null && buyData.CandleDataLists_Im.Count > 0)
buyCandleList.Add(buyData);
}
//파는 로직
foreach (var item in splitDataM)
{
var sell1 = SellLogicManager.GetSellLogicOverUnderIch(item);
var sell2 = SellLogicManager.GetSellLogicOVerUnderBol(item, 40, 1);
var sell3 = SellLogicManager.GetSellLogicOVerUnderBol(item, 40, 2);
sell1.AddRange(sell2);
sell1.AddRange(sell3);
var dateTimeList = sell1.Select(t => t.DateTime).Distinct().ToList();
foreach (var date in dateTimeList)
{
sellList.Add(sell1.FirstOrDefault(t => t.DateTime == date));
}
}
var result = new SearchResult();
var sellData = new CandleData();
//Result 계산
foreach (var item in buyCandleList)
{
result.BuyList.ConGoodsData = marketData;
if (item.CandleDataLists_Im.Count > 0)
{
sellData = sellList.FirstOrDefault(t => t.DateTime > item.CandleDataLists_Im[0].DateTime);
if (sellData == null) continue;
foreach (var buy in item.CandleDataLists_Im)
{
if (buy.DateTime < sellData.DateTime)
{
result.BuyList.CandleDataLists_Im.Add(buy);
}
else
{
var sDate = result.BuyList.CandleDataLists_Im[0].DateTime;
var eDate = sellData.DateTime;
//Result Data Setting
result.TermCandleDataList = candleDataM1.CandleDataLists_Im.Where(x => x.DateTime >= sDate && x.DateTime <= eDate).ToList();
result.CodeFullName = result.BuyList.ConGoodsData.CodeFullName;
result.StartDate = sDate;
result.EndDate = eDate;
result.SellData = sellData;
var sellLogicKeys = result.SellData.SellLogicFlag.Keys;
double sellValue = 0;
#region 파는 값 계산(파는 로직 중 제일 높은 값)
var key = Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.IchMok);
if (result.SellData.SellLogicFlag.ContainsKey(key))
{
if(sellValue < ((IchimokData) result.SellData.IndicatorDic["Ich"]).GetOverValue())
{
sellValue = ((IchimokData)result.SellData.IndicatorDic["Ich"]).GetOverValue();
}
}
key = Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.Bol) + "401";
if (result.SellData.SellLogicFlag.ContainsKey(key))
{
if (sellValue < result.SellData.IndicatorDic[key].overlapValue)
{
sellValue = result.SellData.IndicatorDic[key].overlapValue;
}
}
key = Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.Bol) + "402";
if (result.SellData.SellLogicFlag.ContainsKey(key))
{
if (sellValue < result.SellData.IndicatorDic[key].overlapValue)
{
sellValue = result.SellData.IndicatorDic[key].overlapValue;
}
}
result.SellValue = sellValue;
#endregion
#region Rating 계산
double totalVal = 0;
foreach (var x in result.BuyList.CandleDataLists_Im)
{
var dateTime = x.DateTime.AddMinutes(x.DateTime.Minute*-1);
var hCandle = candleDataH1.CandleDataLists_Im.FirstOrDefault(t => t.DateTime == dateTime);
var buyValue = hCandle.IndicatorDic[Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.Bol) + "401"].overlapValue;
var rate = ((sellValue - buyValue) / buyValue) * 100;
result.BuyEarningRate.Add(rate);
result.BuyValueList.Add(buyValue);
if (rate >= 0) win++;
else lose++;
totalVal += rate;
}
result.TotalEarningRate = totalVal / result.BuyValueList.Count;
#endregion
var errorList = ValidationCheckManager.ValidateCandleDataList(result.TermCandleDataList, "", MCandleTimeType.M1);
if (errorList.Count == 0)
{
result.SellData = sellData;
resultList.Add(result);
}
else
{
result.SellData = sellData;
errorResultList.Add(result);
}
result = new SearchResult();
result.BuyList.CandleDataLists_Im.Add(buy);
sellData = sellList.FirstOrDefault(t => t.DateTime > buy.DateTime);
}
}
}
}
//Sell 시점이 더 늦는 경우에 대한 처리(나중에 안에 있는 로직이랑 병합해서 한번에처리해야함
if (result.BuyList.CandleDataLists_Im.Count > 0)
{
var sDate = result.BuyList.CandleDataLists_Im[0].DateTime;
var eDate = sellData.DateTime;
//Result Data Setting
result.TermCandleDataList = candleDataM1.CandleDataLists_Im.Where(x => x.DateTime >= sDate && x.DateTime <= eDate).ToList();
result.CodeFullName = result.BuyList.ConGoodsData.CodeFullName;
result.StartDate = sDate;
result.EndDate = eDate;
result.SellData = sellData;
var sellLogicKeys = result.SellData.SellLogicFlag.Keys;
double sellValue = 0;
#region 파는 값 계산(파는 로직 중 제일 높은 값)
var key = Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.IchMok);
if (result.SellData.SellLogicFlag.ContainsKey(key))
{
if (sellValue < ((IchimokData)result.SellData.IndicatorDic["Ich"]).GetOverValue())
{
sellValue = ((IchimokData)result.SellData.IndicatorDic["Ich"]).GetOverValue();
}
}
key = Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.Bol) + "401";
if (result.SellData.SellLogicFlag.ContainsKey(key))
{
if (sellValue < result.SellData.IndicatorDic[key].overlapValue)
{
sellValue = result.SellData.IndicatorDic[key].overlapValue;
}
}
key = Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.Bol) + "402";
if (result.SellData.SellLogicFlag.ContainsKey(key))
{
if (sellValue < result.SellData.IndicatorDic[key].overlapValue)
{
sellValue = result.SellData.IndicatorDic[key].overlapValue;
}
}
result.SellValue = sellValue;
#endregion
#region Rating 계산
double totalVal = 0;
foreach (var x in result.BuyList.CandleDataLists_Im)
{
var dateTime = x.DateTime.AddMinutes(x.DateTime.Minute * -1);
var hCandle = candleDataH1.CandleDataLists_Im.FirstOrDefault(t => t.DateTime == dateTime);
var buyValue = hCandle.IndicatorDic[Enum.GetName(typeof(CommonProperty.SellLogicKeyList), CommonProperty.SellLogicKeyList.Bol) + "401"].overlapValue;
var rate = ((sellValue - buyValue) / buyValue) * 100;
result.BuyEarningRate.Add(rate);
result.BuyValueList.Add(buyValue);
if (rate >= 0) win++;
else lose++;
totalVal += rate;
}
result.TotalEarningRate = totalVal / result.BuyValueList.Count;
#endregion
var errorList = ValidationCheckManager.ValidateCandleDataList(result.TermCandleDataList, "", MCandleTimeType.M1);
if (errorList.Count == 0)
{
result.SellData = sellData;
resultList.Add(result);
}
else
{
result.SellData = sellData;
errorResultList.Add(result);
}
}
double total = 0;
foreach (var item in resultList)
{
Console.WriteLine(item.TotalEarningRate);
total += item.TotalEarningRate;
}
Console.WriteLine("Total 수익률 : " + total/resultList.Count);
Console.WriteLine("Win : " + win);
Console.WriteLine("Lose : " + lose);
Console.WriteLine("승률 : " + win * 100 / (win+lose) );
sellList.ToString();
}
}
resultList.ToString();
}
}
}