- Simulation 기초
This commit is contained in:
289
SimulationHelper.cs
Normal file
289
SimulationHelper.cs
Normal file
@@ -0,0 +1,289 @@
|
||||
using CPSYSDIBLib;
|
||||
using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AutoSellerNS
|
||||
{
|
||||
class SimulationHelper
|
||||
{
|
||||
CybosHelper m_CybosHelper = null;
|
||||
class SIMUL_ITEM
|
||||
{
|
||||
}
|
||||
|
||||
public SimulationHelper(CybosHelper CybosHelper)
|
||||
{
|
||||
m_CybosHelper = CybosHelper;
|
||||
}
|
||||
|
||||
public void LoadTxt()
|
||||
{
|
||||
List<string> aItems = new List<string>();
|
||||
StockChart CPStockChart = new StockChart();
|
||||
|
||||
foreach(string strLine in File.ReadLines(Util.GetSimulationPath()+"/0-input.txt", Encoding.UTF8))
|
||||
{
|
||||
var tokens = strLine.Split('\t');
|
||||
|
||||
var strDate = tokens[0];
|
||||
var strNewTime = tokens[1];
|
||||
var strSimulationTime = tokens[2];
|
||||
var strCodeName = tokens[3];
|
||||
var strCode = tokens[4];
|
||||
var strStartPrice = tokens[5];
|
||||
|
||||
DateTime StartDT;
|
||||
DateTime.TryParse(strDate+" "+strSimulationTime, out StartDT);
|
||||
uint uiStartDate;
|
||||
uint.TryParse(StartDT.ToString("yyyyMMdd"), out uiStartDate);
|
||||
|
||||
string strOutFileName = Util.GetSimulationPath() + "/" + strCodeName + "-" + StartDT.ToString("yyyy-MM-dd") + ".txt";
|
||||
if(File.Exists(strOutFileName) == true)
|
||||
continue;
|
||||
|
||||
CPStockChart.SetInputValue(0, "A"+strCode);
|
||||
CPStockChart.SetInputValue(1, '1');
|
||||
CPStockChart.SetInputValue(2, uiStartDate);
|
||||
CPStockChart.SetInputValue(3, uiStartDate);
|
||||
CPStockChart.SetInputValue(4, 100000);
|
||||
CPStockChart.SetInputValue(5, new int[] { 0, 1, 5, 8, 9, 10, 11 });
|
||||
CPStockChart.SetInputValue(6, 'T');
|
||||
CPStockChart.BlockRequest2(0);
|
||||
|
||||
int iFieldCnt = CPStockChart.GetHeaderValue(1);
|
||||
string[] astrFieldName = CPStockChart.GetHeaderValue(2);
|
||||
|
||||
string strOutLine = "";
|
||||
for(int j = 0; j<iFieldCnt; j++)
|
||||
{
|
||||
strOutLine += astrFieldName[j];
|
||||
strOutLine += '\t';
|
||||
}
|
||||
File.AppendAllText(strOutFileName, strOutLine+Environment.NewLine, new UTF8Encoding(true));
|
||||
strOutLine = "";
|
||||
|
||||
bool bContinue = true;
|
||||
while(bContinue == true)
|
||||
{
|
||||
int iCnt = CPStockChart.GetHeaderValue(3);
|
||||
for(int i = 0; i<iCnt; i++)
|
||||
{
|
||||
for(int j = 0; j<iFieldCnt; j++)
|
||||
{
|
||||
strOutLine += CPStockChart.GetDataValue(j, i);
|
||||
strOutLine += '\t';
|
||||
}
|
||||
|
||||
strOutLine += Environment.NewLine;
|
||||
|
||||
if(i % 1000 == 999)
|
||||
{
|
||||
File.AppendAllText(strOutFileName, strOutLine, new UTF8Encoding(true));
|
||||
strOutLine = "";
|
||||
}
|
||||
}
|
||||
|
||||
bContinue = (CPStockChart.Continue==1);
|
||||
if(bContinue == true)
|
||||
CPStockChart.BlockRequest2(0);
|
||||
|
||||
Console.WriteLine(string.Format("RequestRQ({0})", m_CybosHelper.GetLimitRemainCountRQ()));
|
||||
}
|
||||
|
||||
if(strOutLine.Length > 0)
|
||||
{
|
||||
File.AppendAllText(strOutFileName, strOutLine, new UTF8Encoding(true));
|
||||
strOutLine = "";
|
||||
}
|
||||
|
||||
Console.WriteLine("[Load] " + strOutFileName + " End");
|
||||
}
|
||||
|
||||
Console.WriteLine("[Load] All End");
|
||||
}
|
||||
|
||||
public void LoadExcel()
|
||||
{
|
||||
List<string> aItems = new List<string>();
|
||||
StockChart CPStockChart = new StockChart();
|
||||
ExcelHandler Excel = null;
|
||||
|
||||
foreach(string strLine in File.ReadLines(Util.GetSimulationPath()+"/0-input.txt", Encoding.UTF8))
|
||||
{
|
||||
var tokens = strLine.Split('\t');
|
||||
|
||||
var strDate = tokens[0];
|
||||
var strNewTime = tokens[1];
|
||||
var strSimulationTime = tokens[2];
|
||||
var strCodeName = tokens[3];
|
||||
var strCode = tokens[4];
|
||||
var strStartPrice = tokens[5];
|
||||
|
||||
DateTime StartDT;
|
||||
DateTime.TryParse(strDate+" "+strSimulationTime, out StartDT);
|
||||
|
||||
string strOutFileName = Util.GetSimulationPath() + "/" + strCodeName + "-" + StartDT.ToString("yyyy-MM-dd") + ".xlsx";
|
||||
if(File.Exists(strOutFileName) == true)
|
||||
continue;
|
||||
|
||||
CPStockChart.SetInputValue(0, "A"+strCode);
|
||||
CPStockChart.SetInputValue(1, '1');
|
||||
CPStockChart.SetInputValue(2, StartDT.ToString("yyyyMMdd"));
|
||||
CPStockChart.SetInputValue(3, StartDT.ToString("yyyyMMdd"));
|
||||
CPStockChart.SetInputValue(5, new int[] { 0, 1, 5, 8, 9, 10, 11 });
|
||||
CPStockChart.SetInputValue(6, 'T');
|
||||
CPStockChart.BlockRequest2(0);
|
||||
|
||||
int iFieldCnt = CPStockChart.GetHeaderValue(1);
|
||||
string[] astrFieldName = CPStockChart.GetHeaderValue(2);
|
||||
List<object> aRow = new List<object>();
|
||||
List<object[]> aRows = new List<object[]>();
|
||||
|
||||
Excel = new ExcelHandler(strOutFileName, astrFieldName);
|
||||
|
||||
bool bContinue = true;
|
||||
while(bContinue == true)
|
||||
{
|
||||
int iCnt = CPStockChart.GetHeaderValue(3);
|
||||
for(int i = 0; i<iCnt; i++)
|
||||
{
|
||||
for(int j = 0; j<iFieldCnt; j++)
|
||||
aRow.Add(CPStockChart.GetDataValue(j, i));
|
||||
aRows.Add(aRow.ToArray());
|
||||
aRow.Clear();
|
||||
}
|
||||
|
||||
bContinue = (CPStockChart.Continue==1);
|
||||
if(bContinue == true)
|
||||
CPStockChart.BlockRequest2(0);
|
||||
|
||||
Console.WriteLine(string.Format("RequestRQ({0})", m_CybosHelper.GetLimitRemainCountRQ()));
|
||||
}
|
||||
|
||||
aRows.Reverse();
|
||||
Excel.AddRows(aRows.ToArray());
|
||||
|
||||
Console.WriteLine("[Load] " + strOutFileName + " End");
|
||||
}
|
||||
|
||||
Console.WriteLine("[Load] All End");
|
||||
}
|
||||
|
||||
public void StartSimuation()
|
||||
{
|
||||
int iTotalProfit = 0;
|
||||
int iTotalBid = 0;
|
||||
|
||||
foreach(string strLine in File.ReadLines(Util.GetSimulationPath()+"/0-input.txt", Encoding.UTF8))
|
||||
{
|
||||
var tokens = strLine.Split('\t');
|
||||
|
||||
var strDate = tokens[0];
|
||||
var strNewTime = tokens[1];
|
||||
var strSimulationTime = tokens[2];
|
||||
var strCodeName = tokens[3];
|
||||
var strCode = tokens[4];
|
||||
var strStartPrice = tokens[5];
|
||||
|
||||
DateTime StartDT;
|
||||
DateTime.TryParse(strDate+" "+strSimulationTime, out StartDT);
|
||||
|
||||
int iStartPrice;
|
||||
int.TryParse(strStartPrice, out iStartPrice);
|
||||
|
||||
string strFileName = Util.GetSimulationPath() + "/" + strCodeName + "-" + StartDT.ToString("yyyy-MM-dd") + ".xlsx";
|
||||
|
||||
|
||||
FileInfo newFile = new FileInfo(strFileName);
|
||||
newFile.IsReadOnly = true;
|
||||
ExcelPackage package = new ExcelPackage(newFile);
|
||||
ExcelWorksheet Sheet = package.Workbook.Worksheets["sheet"];
|
||||
|
||||
int iMaxRow = Sheet.Dimension.Rows;
|
||||
|
||||
DateTime RowDT;
|
||||
bool bStart = false;
|
||||
int iTotalAskCount = 0;
|
||||
int iTotalBidCount = 0;
|
||||
int iPrevAskCount = 0;
|
||||
int iPrevBidCount = 0;
|
||||
bool bBid = false;
|
||||
int iAskCount = 0;
|
||||
int iTrailingCount = 0;
|
||||
int iMaxPrice = int.MinValue;
|
||||
int iStockCount = 0;
|
||||
for(int iRow = 2; iRow<=iMaxRow; iRow++)
|
||||
{
|
||||
int iTime = (int)(double)Sheet.Cells[iRow, 2].Value;
|
||||
int iPrice = (int)(double)Sheet.Cells[iRow, 3].Value;
|
||||
iTotalAskCount = (int)(double)Sheet.Cells[iRow, 6].Value;
|
||||
iTotalBidCount = (int)(double)Sheet.Cells[iRow, 7].Value;
|
||||
|
||||
bBid = (iTotalAskCount == iPrevAskCount);
|
||||
DateTime.TryParse(strDate+" "+string.Format("{0}:{1}:{2}", iTime/100, iTime%100, 59), out RowDT);
|
||||
|
||||
iStockCount = 1000000/iStartPrice;
|
||||
|
||||
if(bStart == false)
|
||||
{
|
||||
if(RowDT >= StartDT && iPrice==iStartPrice)
|
||||
bStart = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(bBid == false)
|
||||
iAskCount++;
|
||||
else
|
||||
iAskCount = 0;
|
||||
|
||||
if(iAskCount >= Config.GetBidCount())
|
||||
{
|
||||
Console.WriteLine("[{0}] 시뮬레이션 매도 {1} {2} {3} ({4} : {5:n2}%)",
|
||||
strCodeName,
|
||||
RowDT.ToString("yyyy-MM-dd HH:mm:00"),
|
||||
iRow,
|
||||
iPrice,
|
||||
iPrice-iStartPrice,
|
||||
(iPrice-iStartPrice)*100/(float)iStartPrice
|
||||
);
|
||||
iTotalProfit += (iPrice-iStartPrice)*iStockCount;
|
||||
iTotalBid += iStartPrice*iStockCount;
|
||||
break;
|
||||
}
|
||||
|
||||
if(iPrice > iMaxPrice)
|
||||
iTrailingCount = 0;
|
||||
else if(iPrice <= iMaxPrice*(100-Config.GetTrailingRate())/100)
|
||||
iTrailingCount++;
|
||||
iMaxPrice = Math.Max(iPrice, iMaxPrice);
|
||||
|
||||
if(iTrailingCount >= Config.GetTrailingCnt())
|
||||
{
|
||||
Console.WriteLine("[{0}] 트레일링 매도 {1} {2} {3} ({4} : {5:n2}%)",
|
||||
strCodeName,
|
||||
RowDT.ToString("yyyy-MM-dd HH:mm:00"),
|
||||
iRow,
|
||||
iPrice,
|
||||
iPrice-iStartPrice,
|
||||
(iPrice-iStartPrice)*100/(float)iStartPrice
|
||||
);
|
||||
iTotalProfit += (iPrice-iStartPrice)*iStockCount;
|
||||
iTotalBid += iStartPrice*iStockCount;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
iPrevAskCount = iTotalAskCount;
|
||||
iPrevBidCount = iTotalBidCount;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("[시뮬레이션 종료] 총수익 : {0} : {1:n2}%", iTotalProfit, iTotalProfit*100/iTotalBid);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user