init
This commit is contained in:
45
MarketDataLib/CNNProcessing/BitmapExtensions.cs
Normal file
45
MarketDataLib/CNNProcessing/BitmapExtensions.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using MarketData.Utils;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Drawing;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public static class BitmapExtensions
|
||||
{
|
||||
// This call is used to generate the training, test, and validation bitmaps.
|
||||
// I noticed a large difference in the accuracy or the model when the quality parameter is 100.
|
||||
// I therefore intentionally not using EncoderParameters to specify quality but instead taking the default quality.
|
||||
public static void SaveJPG100(this Bitmap bmp, string filename)
|
||||
{
|
||||
// bmp.Save(filename,ImageFormat.Jpeg);
|
||||
EncoderParameters encoderParameters = new EncoderParameters(1);
|
||||
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
|
||||
bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
|
||||
}
|
||||
|
||||
public static void SaveJPG100(this Bitmap bmp, Stream stream)
|
||||
{
|
||||
EncoderParameters encoderParameters = new EncoderParameters(1);
|
||||
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
|
||||
bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
|
||||
}
|
||||
|
||||
private static ImageCodecInfo GetEncoder(ImageFormat format)
|
||||
{
|
||||
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
|
||||
|
||||
foreach (ImageCodecInfo codec in codecs)
|
||||
{
|
||||
if (codec.FormatID == format.Guid)
|
||||
{
|
||||
return codec;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
98
MarketDataLib/CNNProcessing/CNNClient.cs
Normal file
98
MarketDataLib/CNNProcessing/CNNClient.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class CNNClient
|
||||
{
|
||||
public enum Model{resnet50,inception,vgg16,lenet5,ping};
|
||||
private static readonly string Alive="Alive";
|
||||
private readonly HttpClient client = new HttpClient();
|
||||
private string baseUrl;
|
||||
private static int REQUEST_TIMEOUT=15000;
|
||||
|
||||
public CNNClient(String baseUrl)
|
||||
{
|
||||
client.Timeout=new TimeSpan(0,0,0,0,REQUEST_TIMEOUT);
|
||||
if(!baseUrl.StartsWith("http://") && !baseUrl.StartsWith("HTTP://"))baseUrl="http://"+baseUrl;
|
||||
this.baseUrl=baseUrl;
|
||||
}
|
||||
|
||||
private String GetModelUrl(CNNClient.Model model)
|
||||
{
|
||||
return baseUrl+"/predict_"+model.ToString();
|
||||
}
|
||||
|
||||
public String Predict(CNNClient.Model model,Stream stream)
|
||||
{
|
||||
try
|
||||
{
|
||||
string responsePayload = Upload(GetModelUrl(model),stream).GetAwaiter().GetResult();
|
||||
return responsePayload;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
return exception.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Ping()
|
||||
{
|
||||
try
|
||||
{
|
||||
string responsePayload = Upload(baseUrl+"/ping").GetAwaiter().GetResult();
|
||||
if(null==responsePayload)return false;
|
||||
return responsePayload.Equals(Alive);
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception encountered: {0}",exception.ToString()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private async Task<String> Upload(String url,Stream stream)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
|
||||
{
|
||||
int streamEnd = Convert.ToInt32(stream.Length);
|
||||
byte[] byteArray = new byte[streamEnd];
|
||||
stream.Read(byteArray, 0, streamEnd);
|
||||
request.Content=new ByteArrayContent(byteArray);
|
||||
using (HttpResponseMessage response = await client.SendAsync(request))
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception encountered: {0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private async Task<String> Upload(String url)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url))
|
||||
{
|
||||
HttpResponseMessage response = await client.SendAsync(request);
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception encountered: {0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
204
MarketDataLib/CNNProcessing/CNNProcessor.cs
Normal file
204
MarketDataLib/CNNProcessing/CNNProcessor.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using MarketData.DataAccess;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Numerical;
|
||||
using System.Drawing;
|
||||
using System.Collections.Generic;
|
||||
using MarketData.Utils;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class CNNProcessor
|
||||
{
|
||||
private CNNProcessor()
|
||||
{
|
||||
}
|
||||
public static void GenerateTraining()
|
||||
{
|
||||
TestCases testCases=new TestCases();
|
||||
DataProcessor dataProcessor=new DataProcessor();
|
||||
dataProcessor.Width=128;
|
||||
dataProcessor.Height=128;
|
||||
int dayCount=270;
|
||||
// dataProcessor.UseGrayScale=true;
|
||||
// dataProcessor.PenWidth=1;
|
||||
|
||||
dataProcessor.PenWidthArray=new float[]{.50f,.75f,1.00f,1.12f,1.25f,1.31f,1.37f,1.50f,1.56f,1.62f,1.75f,1.87f,2.00f};
|
||||
// dataProcessor.PenWidthArray=new float[]{.50f,.625f,.75f,.875f,1.00f,1.06f,1.12f,1.185f,1.25f,1.28f,1.31f,1.37f,1.435f,1.50f,1.56f,1.59f,1.62f,1.685f,1.75f,1.87f,1.935f,2.00f};
|
||||
// dataProcessor.PenWidthArray=new float[]{0.550000f,0.600000f,0.650000f,0.700000f,0.750000f,0.800000f,0.850000f,0.900000f,0.950000f,1.000000f,1.050000f,1.100000f,1.150000f,1.200000f,1.250000f,1.300000f,1.350000f,1.400000f,1.450000f,1.500000f,1.550000f,1.600000f,1.650000f,1.700000f,1.750000f,1.800000f,1.850000f,1.900000f,1.950000f,2.000000f}
|
||||
// dataProcessor.PenWidthArray=new float[]{0.515000f,0.530000f,0.545000f,0.560000f,0.575000f,0.590000f,0.605000f,0.620000f,0.635000f,0.650000f,0.665000f,0.680000f,0.695000f,0.710000f,0.725000f,0.740000f,0.755000f,0.770000f,0.785000f,0.800000f,0.815000f,0.830000f,0.845000f,0.860000f,0.875000f,0.890000f,0.905000f,0.920000f,0.935000f,0.950000f,0.965000f,0.980000f,0.995000f,1.010000f,1.025000f,1.040000f,1.055000f,1.070000f,1.085000f,1.100000f,1.115000f,1.130000f,1.145000f,1.160000f,1.175000f,1.190000f,1.205000f,1.220000f,1.235000f,1.250000f,1.265000f,1.280000f,1.295000f,1.310000f,1.325000f,1.340000f,1.355000f,1.370000f,1.385000f,1.400000f,1.415000f,1.430000f,1.445000f,1.460000f,1.475000f,1.490000f,1.505000f,1.520000f,1.535000f,1.550000f,1.565000f,1.580000f,1.595000f,1.610000f,1.625000f,1.640000f,1.655000f,1.670000f,1.685000f,1.700000f,1.715000f,1.730000f,1.745000f,1.760000f,1.775000f,1.790000f,1.805000f,1.820000f,1.835000f,1.850000f,1.865000f,1.880000f,1.895000f,1.910000f,1.925000f,1.940000f,1.955000f,1.970000f,1.985000f,2.000000f};
|
||||
|
||||
// [0] Data
|
||||
testCases.Add(new TestCase("ICPT",DateTime.Parse("12/31/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("AMD",DateTime.Parse("09/28/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("AVAV",DateTime.Parse("09/28/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("ULH",DateTime.Parse("08/31/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("ADT",DateTime.Parse("11/29/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("VC",DateTime.Parse("11/29/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("VIVO",DateTime.Parse("07/31/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("AKS",DateTime.Parse("12/30/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("PRO",DateTime.Parse("07/31/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("RCKT",DateTime.Parse("01/31/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("ENPH",DateTime.Parse("01/29/2021"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("ACLS",DateTime.Parse("11/30/2017"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("KURA",DateTime.Parse("11/30/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("PTLA",DateTime.Parse("03/29/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("SSRI",DateTime.Parse("07/29/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("CORE",DateTime.Parse("09/28/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("OKTA",DateTime.Parse("06/28/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("CTMX",DateTime.Parse("03/29/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("TDOC",DateTime.Parse("08/31/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("WRD",DateTime.Parse("05/31/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("FTCH",DateTime.Parse("12/31/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("COKE",DateTime.Parse("10/30/2015"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
|
||||
dataProcessor.SetOutputFolderPath(@"C:\boneyard\DeepLearning\ModelInputData\0");
|
||||
dataProcessor.ProcessData(testCases);
|
||||
testCases.Clear();
|
||||
|
||||
// [1] Data
|
||||
testCases.Add(new TestCase("AG",DateTime.Parse("03/31/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("NKTR",DateTime.Parse("11/30/2017"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("ABX",DateTime.Parse("03/31/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("PTON",DateTime.Parse("09/30/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("CLVS",DateTime.Parse("03/31/2017"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("TREE",DateTime.Parse("06/30/2017"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("BTG",DateTime.Parse("04/29/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("SIG",DateTime.Parse("03/31/2021"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("COOP",DateTime.Parse("09/30/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("THC",DateTime.Parse("03/29/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("ARWR",DateTime.Parse("07/31/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("BAND",DateTime.Parse("06/30/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("TSLA",DateTime.Parse("07/31/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("VEDL",DateTime.Parse("08/31/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("GTHX",DateTime.Parse("04/30/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("PDD",DateTime.Parse("05/29/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("KRO",DateTime.Parse("01/31/2017"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("CPRI",DateTime.Parse("01/29/2021"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("SQ",DateTime.Parse("08/31/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("EHTH",DateTime.Parse("02/28/2019"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("NVCR",DateTime.Parse("10/30/2020"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("FRPT",DateTime.Parse("07/31/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("TLRD",DateTime.Parse("01/31/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("BVN",DateTime.Parse("05/31/2016"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
testCases.Add(new TestCase("DNR",DateTime.Parse("06/29/2018"),dayCount,TestCase.CaseType.Training,TestCase.GenerateType.BollingerBand));
|
||||
|
||||
dataProcessor.SetOutputFolderPath(@"C:\boneyard\DeepLearning\ModelInputData\1");
|
||||
dataProcessor.ProcessData(testCases);
|
||||
}
|
||||
|
||||
public static void GenerateGeneralizedData()
|
||||
{
|
||||
TestCases testCases=new TestCases();
|
||||
DataProcessor dataProcessor=new DataProcessor();
|
||||
dataProcessor.Width=128;
|
||||
dataProcessor.Height=128;
|
||||
dataProcessor.PenWidth=1;
|
||||
int dayCount=270;
|
||||
|
||||
string[,] s=new string[,]{{"CWH","01/31/2020"},{"DISCA","02/26/2021"},{"ANAB","02/28/2018"},{"SPWR","07/31/2017"},{"DNLI","12/31/2020"},{"IDTI","11/30/2015"},{"PRO","07/31/2019"},{"ACLS","11/30/2017"},{"TDOC","08/31/2018"},{"ATHM","08/31/2017"},{"SIG","10/30/2020"},{"DOCU","05/29/2020"},{"MTDR","03/31/2021"},{"NTLA","12/31/2020"},{"SE","06/30/2020"},{"BAND","06/30/2020"},{"PDD","05/29/2020"},{"CRSP","03/29/2018"},{"DAC","04/30/2021"}};
|
||||
for(int index=0;index<s.Length/2;index++)
|
||||
{
|
||||
String symbol=s[index,0];
|
||||
DateTime purchaseDate=DateTime.Parse(s[index,1]);
|
||||
TestCase testCase=new TestCase(symbol,purchaseDate,dayCount,TestCase.CaseType.Test,TestCase.GenerateType.BollingerBand);
|
||||
testCases.Add(testCase);
|
||||
}
|
||||
dataProcessor.SetOutputFolderPath(@"C:\boneyard\DeepLearning\IndividualValidationCases");
|
||||
dataProcessor.ProcessData(testCases);
|
||||
}
|
||||
public static String Predict()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void TestPredictAPI()
|
||||
{
|
||||
// String strPathOutputFile=@"C:\boneyard\DeepLearning\TestPredictAPIOutput\outputcases_europa.csv";
|
||||
// CNNClient cnnClient=new CNNClient("http://127.0.0.1:5000");
|
||||
|
||||
String strPathOutputFile=@"C:\boneyard\DeepLearning\TestPredictAPIOutput\outputcases_ganymede.csv";
|
||||
CNNClient cnnClient=new CNNClient("http://192.168.0.151:5000");
|
||||
|
||||
if(!cnnClient.Ping())
|
||||
{
|
||||
Console.WriteLine("Model server is not reachable.");
|
||||
return;
|
||||
}
|
||||
DataProcessor dataProcessor=new DataProcessor();
|
||||
dataProcessor.Width=128;
|
||||
dataProcessor.Height=128;
|
||||
dataProcessor.PenWidth=1;
|
||||
int dayCount=270;
|
||||
|
||||
if(File.Exists(strPathOutputFile))
|
||||
{
|
||||
try{File.Delete(strPathOutputFile);}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(String.Format("Exception:{0}",exception.ToString()));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
StreamWriter streamWriter=new StreamWriter(new FileStream(strPathOutputFile,FileMode.CreateNew));
|
||||
string[,] s=new string[,]{{"CWH","01/31/2020"},{"DISCA","02/26/2021"},{"ANAB","02/28/2018"},{"SPWR","07/31/2017"},{"DNLI","12/31/2020"},{"IDTI","11/30/2015"},{"PRO","07/31/2019"},{"ACLS","11/30/2017"},{"TDOC","08/31/2018"},{"ATHM","08/31/2017"},{"SIG","10/30/2020"},{"DOCU","05/29/2020"},{"MTDR","03/31/2021"},{"NTLA","12/31/2020"},{"SE","06/30/2020"},{"BAND","06/30/2020"},{"PDD","05/29/2020"},{"CRSP","03/29/2018"},{"DAC","04/30/2021"}};
|
||||
streamWriter.WriteLine("Symbol,Date,Response,Response,Raw Response");
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Symbol,Date,Response,Response,Raw Response");
|
||||
for(int index=0;index<s.Length/2;index++)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
String symbol=s[index,0];
|
||||
DateTime purchaseDate=DateTime.Parse(s[index,1]);
|
||||
TestCase testCase=new TestCase(symbol,purchaseDate,dayCount,TestCase.CaseType.Test,TestCase.GenerateType.BollingerBand,TestCase.OutputType.OutputStream);
|
||||
dataProcessor.ProcessData(testCase);
|
||||
// testCase.Responses.Add(cnnClient.Predict(CNNClient.Model.lenet5,testCase.LastStream));
|
||||
testCase.Responses.Add(cnnClient.Predict(CNNClient.Model.resnet50,testCase.LastStream));
|
||||
String strPredictionResponse=testCase.LastResponse;
|
||||
|
||||
String strPredictionResult=strPredictionResponse.Substring(strPredictionResponse.IndexOf("-->")+3);
|
||||
int result=int.Parse(Utility.BetweenString(strPredictionResult,"[[","]"));
|
||||
|
||||
String strPredictionValue=strPredictionResponse.Substring(0,strPredictionResponse.IndexOf("-->"));
|
||||
double value=double.Parse(Utility.BetweenString(strPredictionValue,"[[","]"));
|
||||
sb.Append(symbol);
|
||||
sb.Append(",");
|
||||
sb.Append(Utility.AddQuotes(Utility.DateTimeToStringMMSDDSYYYY(purchaseDate)));
|
||||
sb.Append(",");
|
||||
sb.Append(Utility.AddQuotes(Utility.FormatNumber(value,8,false))).Append(",").Append(Utility.AddQuotes(result.ToString()));
|
||||
sb.Append(",").Append(Utility.AddQuotes(strPredictionResponse));
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,sb.ToString());
|
||||
streamWriter.WriteLine(sb.ToString());
|
||||
}
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
streamWriter.Dispose();
|
||||
|
||||
}
|
||||
public static void divide(double divisions,double seedMin,double seedMax)
|
||||
{
|
||||
double skip=(seedMax-seedMin)/divisions;
|
||||
List<double> array=new List<double>();
|
||||
for(double seed=seedMax;seed>=seedMin;seed-=skip)
|
||||
{
|
||||
if(array.Count==divisions)break;
|
||||
array.Add(seed);
|
||||
}
|
||||
|
||||
for(int index=array.Count-1;index>=0;index--)
|
||||
{
|
||||
Console.Write(String.Format("{0}f,",Utility.FormatNumber(array[index],6)));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
}
|
||||
}
|
||||
//TestCases minerviniCases=new TestCases();
|
||||
//minerviniCases.Add(new TestCase("SNAP",DateTime.Parse("07-08-2020"),DateTime.Parse("09-13-2020")));
|
||||
//minerviniCases.Add(new TestCase("TAL",DateTime.Parse("03-05-2020"),DateTime.Parse("06-01-2020")));
|
||||
//minerviniCases.Add(new TestCase("TSM",DateTime.Parse("02-25-2020"),DateTime.Parse("07-01-2020")));
|
||||
//minerviniCases.Add(new TestCase("TGT",DateTime.Parse("05-15-2020"),DateTime.Parse("08-03-2020")));
|
||||
//dataProcessor.ProcessData(minerviniCases);
|
||||
|
||||
}
|
||||
439
MarketDataLib/CNNProcessing/DataProcessor.cs
Normal file
439
MarketDataLib/CNNProcessing/DataProcessor.cs
Normal file
@@ -0,0 +1,439 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using MarketData.Numerical;
|
||||
using MarketData.DataAccess;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Generator;
|
||||
using MarketData.Utils;
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class DataProcessor
|
||||
{
|
||||
private String strFolderPath=@"c:\1\";
|
||||
public DataProcessor(int width=640,int height=480)
|
||||
{
|
||||
Width=width;
|
||||
Height=height;
|
||||
PenWidth=2f;
|
||||
DrawingBrush=new SolidBrush(Color.Black);
|
||||
FillBrush=new SolidBrush(Color.White);
|
||||
DrawPrice=true;
|
||||
UseGrayScale=false;
|
||||
NoiseColor=Color.White;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Width
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the width of the output image</param>
|
||||
public int Width{get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Height
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the height of the output image</param>
|
||||
public int Height{get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// PenWidth
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the width of the drawing pen</param>
|
||||
public float PenWidth{get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// FillBrush
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the background brush</param>
|
||||
public Brush FillBrush{get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// DrawingBrush
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the drawing brush brush</param>
|
||||
public Brush DrawingBrush{get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// DrawBlack
|
||||
/// </summary>
|
||||
///<param name="value">Sets up the canvas to do black drawing</param>
|
||||
public void DrawBlack()
|
||||
{
|
||||
DrawingBrush=new SolidBrush(Color.Black);
|
||||
FillBrush=new SolidBrush(Color.White);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// IncludePrice
|
||||
/// </summary>
|
||||
///<param name="value">Indicates whether we want to include price data in the graph</param>
|
||||
public bool DrawPrice{get; set;}
|
||||
|
||||
/// <summary>
|
||||
/// UseGrayScale
|
||||
/// </summary>
|
||||
///<param name="value">Indicates whether we want to use grayscale.. default is B&W</param>
|
||||
public bool UseGrayScale{get; set;}
|
||||
|
||||
/// <summary>
|
||||
/// DrawWhite
|
||||
/// </summary>
|
||||
///<param name="value">Sets up the canvas to do white drawing</param>
|
||||
public void DrawWhite()
|
||||
{
|
||||
DrawingBrush=new SolidBrush(Color.White);
|
||||
FillBrush=new SolidBrush(Color.Black);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MovingAverageArray
|
||||
/// </summary>
|
||||
///<param name="MovingAverageArray">Gets/Sets the moving average array</param>
|
||||
public int[] MovingAverageArray{get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// PenWidthArray
|
||||
/// </summary>
|
||||
///<param name="PenWidthArray">Gets/Sets thepen width array</param>
|
||||
public float[] PenWidthArray{get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// NoiseArray
|
||||
/// </summary>
|
||||
///<param name="NoiseArray">Gets/Sets the noise array</param>
|
||||
public double[] NoiseArray{get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// NoiseColor
|
||||
/// </summary>
|
||||
///<param name="NoiseColor">Gets/Sets the noise color</param>
|
||||
public Color NoiseColor{get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// UseNoise
|
||||
/// </summary>
|
||||
///<param name="seed">The seed value</param>
|
||||
///<param name="increment">The increment</param>
|
||||
///<param name="length">The size</param>
|
||||
public void UseNoise(double seed, double increment, int length, Color noiseColor)
|
||||
{
|
||||
NoiseColor=noiseColor;
|
||||
NoiseArray=new double[length+1];
|
||||
NoiseArray[0]=0.00;
|
||||
for(int index=1;index<length;index++)
|
||||
{
|
||||
NoiseArray[index]=seed;
|
||||
seed+=increment;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SetOutputFolderPath
|
||||
/// </summary>
|
||||
///<param name="testCases">The test cases</param>
|
||||
public void SetOutputFolderPath(String strFolderPath)
|
||||
{
|
||||
this.strFolderPath=strFolderPath;
|
||||
if(!this.strFolderPath.EndsWith(@"\"))this.strFolderPath=this.strFolderPath+@"\";
|
||||
}
|
||||
public void ProcessData(TestCases testCases)
|
||||
{
|
||||
for(int index=0;index<testCases.Count;index++)
|
||||
{
|
||||
TestCase testCase=testCases[index];
|
||||
ProcessData(testCase,index);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// ProcessData item
|
||||
/// </summary>
|
||||
///<param name="testCases">The test cases</param>
|
||||
public void ProcessData(TestCase testCase,int index=0)
|
||||
{
|
||||
if(null==PenWidthArray)PenWidthArray=new float[]{PenWidth};
|
||||
if(null==NoiseArray)NoiseArray=new double[]{0.00};
|
||||
if(testCase.TypeGenerate.Equals(TestCase.GenerateType.Price))
|
||||
{
|
||||
for(int penIndex=0;penIndex<PenWidthArray.Length;penIndex++)
|
||||
{
|
||||
float penWidth=PenWidthArray[penIndex];
|
||||
for(int noiseIndex=0;noiseIndex<NoiseArray.Length;noiseIndex++)
|
||||
{
|
||||
double noise=NoiseArray[noiseIndex];
|
||||
String strPathFileName=String.Format("{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}.jpg",strFolderPath,testCase.Symbol,index,penIndex,noiseIndex,testCase.TypeCase.ToString(),testCase.TypeGenerate.ToString(),Utility.DateToLong(testCase.PurchaseDate));
|
||||
testCase.PathFileNames.Add(strPathFileName);
|
||||
ProcessPriceData(testCase,penWidth,noise);
|
||||
}
|
||||
}
|
||||
}
|
||||
else // Bollinger bands
|
||||
{
|
||||
if(null==MovingAverageArray)
|
||||
{
|
||||
for(int penIndex=0;penIndex<PenWidthArray.Length;penIndex++)
|
||||
{
|
||||
float penWidth=PenWidthArray[penIndex];
|
||||
for(int noiseIndex=0;noiseIndex<NoiseArray.Length;noiseIndex++)
|
||||
{
|
||||
double noise=NoiseArray[noiseIndex];
|
||||
String strPathFileName=String.Format("{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}.jpg",strFolderPath,testCase.Symbol,index,penIndex,noiseIndex,testCase.TypeCase.ToString(),testCase.TypeGenerate.ToString(),Utility.DateToLong(testCase.PurchaseDate));
|
||||
testCase.PathFileNames.Add(strPathFileName);
|
||||
ProcessBollingerBandData(testCase,penWidth,noise);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int avgIndex=0;avgIndex<MovingAverageArray.Length;avgIndex++)
|
||||
{
|
||||
int movingAverage=MovingAverageArray[avgIndex];
|
||||
|
||||
for(int penIndex=0;penIndex<PenWidthArray.Length;penIndex++)
|
||||
{
|
||||
float penWidth=PenWidthArray[penIndex];
|
||||
for(int noiseIndex=0;noiseIndex<NoiseArray.Length;noiseIndex++)
|
||||
{
|
||||
double noise=NoiseArray[noiseIndex];
|
||||
String strPathFileName=String.Format("{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}.jpg",strFolderPath,testCase.Symbol,index,penIndex,noiseIndex,testCase.TypeCase.ToString(),testCase.TypeGenerate.ToString(),Utility.DateToLong(testCase.PurchaseDate));
|
||||
testCase.PathFileNames.Add(strPathFileName);
|
||||
ProcessBollingerBandData(testCase,movingAverage,penWidth,noise);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessBollingerBandData(TestCase testCase,int movingAverageDays,float penWidth,double noise)
|
||||
{
|
||||
int bufferDays=60;
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
|
||||
int daysInPeriod=dateGenerator.DaysBetweenActual(testCase.PurchaseDate,testCase.HistDate);
|
||||
if(movingAverageDays>bufferDays)daysInPeriod=movingAverageDays*2;
|
||||
else daysInPeriod+=60;
|
||||
Prices prices=PricingDA.GetPrices(testCase.Symbol,testCase.PurchaseDate,daysInPeriod);
|
||||
BollingerBands bollingerBands=BollingerBandGenerator.GenerateBollingerBands(prices); // we want to grab K, L, and Close
|
||||
bollingerBands=new BollingerBands(bollingerBands.Where(x=>x.Date>=testCase.HistDate).ToList());
|
||||
float[] k=new float[bollingerBands.Count];
|
||||
float[] l=new float[bollingerBands.Count];
|
||||
float[] close=new float[bollingerBands.Count];
|
||||
|
||||
DMAPrices dmaPrices=MovingAverageGenerator.GenerateMovingAverage(prices, movingAverageDays);
|
||||
DMAPricesByDate dmaPricesByDate=dmaPrices.GetDMAPricesByDate();
|
||||
|
||||
// populate the arrays in reverse order so that we have the most historical date in the lowest index
|
||||
for(int index=bollingerBands.Count-1;index>=0;index--)
|
||||
{
|
||||
BollingerBandElement bollingerBandElement=bollingerBands[index];
|
||||
if(!dmaPricesByDate.ContainsKey(bollingerBandElement.Date))continue;
|
||||
k[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.K)*1000.00f;
|
||||
l[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.L)*1000.00f;
|
||||
double movingAverageClose=dmaPricesByDate[bollingerBandElement.Date].AVGPrice;
|
||||
close[bollingerBands.Count-index-1]=(float)Math.Log(movingAverageClose)*1000.00f;
|
||||
}
|
||||
Numerics.ZeroForNaN(ref k);
|
||||
Numerics.ZeroForNaN(ref l);
|
||||
Numerics.ZeroForNaN(ref close);
|
||||
float maxY=Math.Max(Numerics.Max(ref l),Math.Max(Numerics.Max(ref close),Numerics.Max(ref k)));
|
||||
float minY=Math.Min(Numerics.Min(ref l),Math.Min(Numerics.Min(ref close),Numerics.Min(ref k)))-5f;
|
||||
float maxX=close.Length;
|
||||
float minX=0.00f;
|
||||
|
||||
Pen pen=new Pen(DrawingBrush,penWidth);
|
||||
ImageHelper imageHelper=new ImageHelper();
|
||||
|
||||
PointMapping pointMapping=new PointMapping(Width,Height,maxX,minX,maxY,minY);
|
||||
imageHelper.CreateImage(Width,Height,pointMapping);
|
||||
imageHelper.Fill(FillBrush);
|
||||
|
||||
LineSegments lineSegments=new LineSegments();
|
||||
for(int index=0;index<close.Length && DrawPrice;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)close[index-1]);
|
||||
Point p2=new Point(index,(int)close[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
|
||||
lineSegments.Clear();
|
||||
for(int index=0;index<k.Length;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)k[index-1]);
|
||||
Point p2=new Point(index,(int)k[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
|
||||
lineSegments.Clear();
|
||||
for(int index=0;index<l.Length;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)l[index-1]);
|
||||
Point p2=new Point(index,(int)l[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
|
||||
if(0.00!=noise)imageHelper.AddNoise(NoiseColor,noise);
|
||||
|
||||
if(testCase.TypeOutput.Equals(TestCase.OutputType.OutputFile))
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Writing {0}",testCase.ToString()));
|
||||
if(File.Exists(testCase.LastPathFileName))File.Delete(testCase.LastPathFileName);
|
||||
if(UseGrayScale)imageHelper.SaveGrayScaleJPG(testCase.LastPathFileName);
|
||||
else imageHelper.SaveBlackAndWhiteJPG(testCase.LastPathFileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Streaming {0}",testCase.ToString()));
|
||||
testCase.Streams.Add(imageHelper.SaveBlackAndWhiteJPG());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ProcessBollingerBandData item
|
||||
/// </summary>
|
||||
///<param name="testCase">Symbol</param>
|
||||
private void ProcessBollingerBandData(TestCase testCase,float penWidth,double noise)
|
||||
{
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
|
||||
int daysInPeriod=dateGenerator.DaysBetweenActual(testCase.PurchaseDate,testCase.HistDate);
|
||||
daysInPeriod+=60;
|
||||
Prices prices=PricingDA.GetPrices(testCase.Symbol,testCase.PurchaseDate,daysInPeriod);
|
||||
BollingerBands bollingerBands=BollingerBandGenerator.GenerateBollingerBands(prices); // we want to grab K, L, and Close
|
||||
bollingerBands=new BollingerBands(bollingerBands.Where(x=>x.Date>=testCase.HistDate).ToList());
|
||||
float[] k=new float[bollingerBands.Count];
|
||||
float[] l=new float[bollingerBands.Count];
|
||||
float[] close=new float[bollingerBands.Count];
|
||||
|
||||
// populate the arrays in reverse order so that we have the most historical date in the lowest index
|
||||
for(int index=bollingerBands.Count-1;index>=0;index--)
|
||||
{
|
||||
BollingerBandElement bollingerBandElement=bollingerBands[index];
|
||||
k[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.K)*1000.00f;
|
||||
l[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.L)*1000.00f;
|
||||
close[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.Close)*1000.00f;
|
||||
}
|
||||
Numerics.ZeroForNaN(ref k);
|
||||
Numerics.ZeroForNaN(ref l);
|
||||
Numerics.ZeroForNaN(ref close);
|
||||
float maxY=Math.Max(Numerics.Max(ref l),Math.Max(Numerics.Max(ref close),Numerics.Max(ref k)));
|
||||
float minY=Math.Min(Numerics.Min(ref l),Math.Min(Numerics.Min(ref close),Numerics.Min(ref k)))-5f;
|
||||
float maxX=close.Length;
|
||||
float minX=0.00f;
|
||||
|
||||
Pen pen=new Pen(DrawingBrush,penWidth);
|
||||
ImageHelper imageHelper=new ImageHelper();
|
||||
|
||||
PointMapping pointMapping=new PointMapping(Width,Height,maxX,minX,maxY,minY);
|
||||
imageHelper.CreateImage(Width,Height,pointMapping);
|
||||
imageHelper.Fill(FillBrush);
|
||||
|
||||
LineSegments lineSegments=new LineSegments();
|
||||
for(int index=0;index<close.Length && DrawPrice;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)close[index-1]);
|
||||
Point p2=new Point(index,(int)close[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
|
||||
lineSegments.Clear();
|
||||
for(int index=0;index<k.Length;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)k[index-1]);
|
||||
Point p2=new Point(index,(int)k[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
|
||||
lineSegments.Clear();
|
||||
for(int index=0;index<l.Length;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)l[index-1]);
|
||||
Point p2=new Point(index,(int)l[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
|
||||
if(0.00!=noise)imageHelper.AddNoise(NoiseColor,noise);
|
||||
|
||||
if(testCase.TypeOutput.Equals(TestCase.OutputType.OutputFile))
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Writing {0}",testCase.ToString()));
|
||||
if(File.Exists(testCase.LastPathFileName))File.Delete(testCase.LastPathFileName);
|
||||
if(UseGrayScale)imageHelper.SaveGrayScaleJPG(testCase.LastPathFileName);
|
||||
else imageHelper.SaveBlackAndWhiteJPG(testCase.LastPathFileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Streaming {0}",testCase.ToString()));
|
||||
testCase.Streams.Add(imageHelper.SaveBlackAndWhiteJPG());
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// ProcessPriceData item
|
||||
/// </summary>
|
||||
///<param name="testCase">TestCase</param>
|
||||
private void ProcessPriceData(TestCase testCase,float penWidth,double noise)
|
||||
{
|
||||
Prices prices=PricingDA.GetPrices(testCase.Symbol,testCase.PurchaseDate,testCase.HistDate);
|
||||
prices.Reverse(); // get the most historical date into the lowest index
|
||||
float[] priceArray=prices.GetPrices();
|
||||
for(int index=0;index<priceArray.Length;index++)
|
||||
{
|
||||
priceArray[index]=(float)Math.Log((double)priceArray[index])*1000.00f;
|
||||
}
|
||||
Numerics.ZeroForNaN(ref priceArray);
|
||||
|
||||
float maxY=Numerics.Max(ref priceArray);
|
||||
float minY=Numerics.Min(ref priceArray)-5f;
|
||||
float maxX=priceArray.Length;
|
||||
float minX=0.00f;
|
||||
|
||||
Pen pen=new Pen(DrawingBrush,penWidth);
|
||||
ImageHelper imageHelper=new ImageHelper();
|
||||
|
||||
PointMapping pointMapping=new PointMapping(Width,Height,maxX,minX,maxY,minY);
|
||||
imageHelper.CreateImage(Width,Height,pointMapping);
|
||||
imageHelper.Fill(FillBrush);
|
||||
|
||||
LineSegments lineSegments=new LineSegments();
|
||||
for(int index=0;index<priceArray.Length;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)priceArray[index-1]);
|
||||
Point p2=new Point(index,(int)priceArray[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(pen,lineSegments);
|
||||
if(0.00!=noise)imageHelper.AddNoise(NoiseColor,noise);
|
||||
|
||||
if(testCase.TypeOutput.Equals(TestCase.OutputType.OutputFile))
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Writing {0}",testCase.ToString()));
|
||||
if(File.Exists(testCase.LastPathFileName))File.Delete(testCase.LastPathFileName);
|
||||
if(UseGrayScale)imageHelper.SaveGrayScaleJPG(testCase.LastPathFileName);
|
||||
else imageHelper.SaveBlackAndWhiteJPG(testCase.LastPathFileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Streaming {0}",testCase.ToString()));
|
||||
testCase.Streams.Add(imageHelper.SaveBlackAndWhiteJPG());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
198
MarketDataLib/CNNProcessing/ImageHelper.cs
Normal file
198
MarketDataLib/CNNProcessing/ImageHelper.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using System.Drawing;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using MarketData.Utils;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class ImageHelper : IDisposable
|
||||
{
|
||||
private Bitmap bitmap = null;
|
||||
private Graphics graphics=null;
|
||||
private PointMapping pointMapping;
|
||||
private int width;
|
||||
private int height;
|
||||
public ImageHelper()
|
||||
{
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if(null!=bitmap)
|
||||
{
|
||||
bitmap.Dispose();
|
||||
bitmap=null;
|
||||
}
|
||||
if(null!=graphics)
|
||||
{
|
||||
graphics.Dispose();
|
||||
graphics=null;
|
||||
}
|
||||
}
|
||||
public void CreateImage(int width, int height,PointMapping pointMapping)
|
||||
{
|
||||
this.width=width;
|
||||
this.height=height;
|
||||
this.pointMapping=pointMapping;
|
||||
bitmap=new Bitmap(width,height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
||||
Validate();
|
||||
graphics=Graphics.FromImage(bitmap);
|
||||
}
|
||||
public Stream SaveBlackAndWhiteJPG()
|
||||
{
|
||||
Validate();
|
||||
Bitmap bwBitmap=ToBlackAndWhite(bitmap);
|
||||
Stream memoryStream=new MemoryStream();
|
||||
BitmapExtensions.SaveJPG100(bwBitmap,memoryStream);
|
||||
memoryStream.Position=0;
|
||||
bwBitmap.Dispose();
|
||||
return memoryStream;
|
||||
}
|
||||
public void SaveGrayScaleJPG(String pathFileName)
|
||||
{
|
||||
Validate();
|
||||
Bitmap bwBitmap=ToBlackAndWhite(bitmap);
|
||||
Save(pathFileName,bwBitmap);
|
||||
bwBitmap.Dispose();
|
||||
}
|
||||
public void SaveBlackAndWhiteJPG(String pathFileName)
|
||||
{
|
||||
Validate();
|
||||
Bitmap bwBitmap=ToBlackAndWhite(bitmap);
|
||||
Save(pathFileName,bwBitmap);
|
||||
bwBitmap.Dispose();
|
||||
}
|
||||
private void Save(String pathFileName,Bitmap altBitmap=null)
|
||||
{
|
||||
Validate();
|
||||
if(null==altBitmap)BitmapExtensions.SaveJPG100(bitmap,pathFileName);
|
||||
else BitmapExtensions.SaveJPG100(altBitmap,pathFileName);
|
||||
}
|
||||
// Convert to 8 bits per pixel black and white
|
||||
private Bitmap ToBlackAndWhite(Bitmap bmp)
|
||||
{
|
||||
var result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
|
||||
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
|
||||
byte[] bytes = new byte[data.Height * data.Stride];
|
||||
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
|
||||
for (int y = 0; y < bmp.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < bmp.Width; x++)
|
||||
{
|
||||
Color color = bmp.GetPixel(x, y);
|
||||
byte rgb = (byte)((color.R + color.G + color.B) / 3);
|
||||
if(rgb>0)rgb=255;
|
||||
bytes[y * data.Stride + x] = rgb;
|
||||
}
|
||||
}
|
||||
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
|
||||
result.UnlockBits(data);
|
||||
return result;
|
||||
}
|
||||
// Convert to 8 bits per pixel gray scale
|
||||
private Bitmap ToGrayScale(Bitmap bmp)
|
||||
{
|
||||
var result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
|
||||
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
|
||||
byte[] bytes = new byte[data.Height * data.Stride];
|
||||
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
|
||||
for (int y = 0; y < bmp.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < bmp.Width; x++)
|
||||
{
|
||||
Color color = bmp.GetPixel(x, y);
|
||||
byte rgb = (byte)((color.R + color.G + color.B) / 3);
|
||||
bytes[y * data.Stride + x] = rgb;
|
||||
}
|
||||
}
|
||||
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
|
||||
result.UnlockBits(data);
|
||||
return result;
|
||||
}
|
||||
public Color GetPixel(int x,int y)
|
||||
{
|
||||
return bitmap.GetPixel(x,y);
|
||||
}
|
||||
public void SetPixel(int x, int y,Color color)
|
||||
{
|
||||
bitmap.SetPixel(x,y,color);
|
||||
}
|
||||
public Point TranslatePoint(Point point)
|
||||
{
|
||||
return pointMapping.TranslatePoint(point);
|
||||
}
|
||||
public void Validate()
|
||||
{
|
||||
if(null==bitmap)throw new InvalidDataException("The image has not been initialized");
|
||||
}
|
||||
public int Width
|
||||
{
|
||||
get{return width;}
|
||||
}
|
||||
public int Height
|
||||
{
|
||||
get{return height;}
|
||||
}
|
||||
public void Fill(Brush brush)
|
||||
{
|
||||
Validate();
|
||||
Region region=new Region(new Rectangle(0,0,bitmap.Width,bitmap.Height));
|
||||
graphics.FillRegion(brush,region);
|
||||
}
|
||||
|
||||
public void DrawPoint(Pen pen,Point drawPoint)
|
||||
{
|
||||
Validate();
|
||||
Point txPoint=pointMapping.MapPoint(drawPoint);
|
||||
graphics.DrawImage(bitmap,txPoint);
|
||||
}
|
||||
public void AddNoise(Color color,double percentDecimal)
|
||||
{
|
||||
Random random=new Random();
|
||||
int amount=(int)(percentDecimal*(Width*Height));
|
||||
for(int index=0;index<amount;index++)
|
||||
{
|
||||
int row=random.Next(Height-1);
|
||||
int col=random.Next(Width-1);
|
||||
SetPixel(row,col,color);
|
||||
}
|
||||
}
|
||||
public void DrawLine(Pen pen,Point srcPoint,Point dstPoint)
|
||||
{
|
||||
Validate();
|
||||
Point txSrcPoint=pointMapping.MapPoint(srcPoint);
|
||||
Point txDstPoint=pointMapping.MapPoint(dstPoint);
|
||||
graphics.DrawLine(pen,txSrcPoint,txDstPoint);
|
||||
}
|
||||
public void DrawPath(Pen pen,LineSegments lineSegments)
|
||||
{
|
||||
GraphicsPath graphicsPath=new GraphicsPath();
|
||||
foreach(LineSegment lineSegment in lineSegments)
|
||||
{
|
||||
graphicsPath.AddLine(pointMapping.MapPoint(lineSegment.P1),pointMapping.MapPoint(lineSegment.P2));
|
||||
}
|
||||
graphics.DrawPath(pen,graphicsPath);
|
||||
}
|
||||
public void DrawCircle(Brush brush,Point pt,float size=1.00f)
|
||||
{
|
||||
Validate();
|
||||
Point txPoint=pointMapping.MapPoint(pt);
|
||||
txPoint.X-=(int)(size/2f);
|
||||
txPoint.Y-=(int)(size/2F);
|
||||
Rectangle boundingRectangle=new Rectangle();
|
||||
boundingRectangle.X=txPoint.X;
|
||||
boundingRectangle.Y=txPoint.Y;
|
||||
boundingRectangle.Width=(int)size;
|
||||
boundingRectangle.Height=(int)size;
|
||||
graphics.FillEllipse(brush,boundingRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
MarketDataLib/CNNProcessing/LineSegment.cs
Normal file
34
MarketDataLib/CNNProcessing/LineSegment.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class LineSegment
|
||||
{
|
||||
public LineSegment()
|
||||
{
|
||||
}
|
||||
public LineSegment(Point p1,Point p2)
|
||||
{
|
||||
P1=p1;
|
||||
P2=p2;
|
||||
}
|
||||
public Point P1{get;set;}
|
||||
public Point P2{get;set;}
|
||||
}
|
||||
|
||||
public class LineSegments : List<LineSegment>
|
||||
{
|
||||
public LineSegments()
|
||||
{
|
||||
}
|
||||
public void Add(Point p1,Point p2)
|
||||
{
|
||||
Add(new LineSegment(p1,p2));
|
||||
}
|
||||
}
|
||||
}
|
||||
158
MarketDataLib/CNNProcessing/Matrix.cs
Normal file
158
MarketDataLib/CNNProcessing/Matrix.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Drawing;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace MarketData.CNNProcessing
|
||||
//{
|
||||
// public class Matrix<T>
|
||||
// {
|
||||
// private T[,] elements;
|
||||
// private int rows;
|
||||
// private int cols;
|
||||
|
||||
// public Matrix(int rows, int cols)
|
||||
// {
|
||||
// this.rows=rows;
|
||||
// this.cols=cols;
|
||||
// elements=new T[rows, cols];
|
||||
// }
|
||||
// public T this[int row, int col]
|
||||
// {
|
||||
// get { return elements[row,col]; }
|
||||
// set { elements[row,col] = value; }
|
||||
// }
|
||||
// public int Rows
|
||||
// {
|
||||
// get{return rows;}
|
||||
// }
|
||||
// public int Cols
|
||||
// {
|
||||
// get{return cols;}
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public class Kernel : Matrix<double>
|
||||
// {
|
||||
// public Kernel(int rows,int cols)
|
||||
// : base(rows,cols)
|
||||
// {
|
||||
// }
|
||||
// public double GetMultiplier(int row,int col)
|
||||
// {
|
||||
// if(row>Rows+1 || col>Cols+1)throw new Exception("Index out of range.");
|
||||
// return this[row,col];
|
||||
// }
|
||||
// public virtual void Process(ImageHelper imageHelper)
|
||||
// {
|
||||
// for(int rowIndex=0;rowIndex<imageHelper.Height;rowIndex++)
|
||||
// {
|
||||
// for(int colIndex=0;colIndex<imageHelper.Height;colIndex++)
|
||||
// {
|
||||
// Process(rowIndex,colIndex,imageHelper);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// private void Process(int imageRow,int imageCol, ImageHelper imageHelper)
|
||||
// {
|
||||
// double redAccumulator=0.00;
|
||||
// double greenAccumulator=0.00;
|
||||
// double blueAccumulator=0.00;
|
||||
// int kernelCenterRow=(int)Rows/2;
|
||||
// int kernelCenterCol=(int)Cols/2;
|
||||
|
||||
// for(int kernelRow=0;kernelRow<Rows;kernelRow++)
|
||||
// {
|
||||
// for(int kernelCol=0;kernelCol<Cols;kernelCol++)
|
||||
// {
|
||||
// Point imagePoint=new Point(imageRow,imageCol);
|
||||
// imagePoint=imageHelper.TranslatePoint(imagePoint);
|
||||
// int imageSourceRow=(int)imagePoint.X+(kernelRow-kernelCenterRow);
|
||||
// int imageSourceCol=(int)imagePoint.Y+(kernelCol-kernelCenterCol);
|
||||
// if(imageSourceRow<0)imageSourceRow=0;
|
||||
// if(imageSourceCol<0)imageSourceCol=0;
|
||||
// if(imageSourceRow>=imageHelper.Height)imageSourceRow=imageHelper.Height-1;
|
||||
// if(imageSourceCol>=imageHelper.Width)imageSourceCol=imageHelper.Width-1;
|
||||
// Color color=imageHelper.GetPixel(imageSourceRow,imageSourceCol);
|
||||
// double multiplier=GetMultiplier(kernelRow,kernelCol);
|
||||
// redAccumulator=redAccumulator+(color.R * multiplier);
|
||||
// greenAccumulator=greenAccumulator+(color.G * multiplier);
|
||||
// blueAccumulator=blueAccumulator+(color.B * multiplier);
|
||||
// }
|
||||
// }
|
||||
// if(redAccumulator>255)redAccumulator=redAccumulator%255;
|
||||
// if(greenAccumulator>255)greenAccumulator=greenAccumulator%255;
|
||||
// if(blueAccumulator>255)blueAccumulator=blueAccumulator%255;
|
||||
// Color newColor=Color.FromArgb((int)redAccumulator,(int)greenAccumulator,(int)blueAccumulator);
|
||||
// Point setPoint=new Point(imageRow,imageCol);
|
||||
// setPoint=imageHelper.TranslatePoint(setPoint);
|
||||
// imageHelper.SetPixel(setPoint.X, setPoint.Y, newColor);
|
||||
// }
|
||||
// }
|
||||
|
||||
// public class GaussianBlurKernelV1 : Kernel
|
||||
// {
|
||||
// public GaussianBlurKernelV1()
|
||||
// : base(7,7)
|
||||
// {
|
||||
// this[1,2]=.01;
|
||||
// this[1,3]=.01;
|
||||
// this[1,4]=.01;
|
||||
|
||||
// this[2,1]=.01;
|
||||
// this[2,2]=.05;
|
||||
// this[2,3]=.11;
|
||||
// this[2,4]=.05;
|
||||
// this[2,5]=.01;
|
||||
|
||||
// this[3,1]=.01;
|
||||
// this[3,2]=.11;
|
||||
// this[3,3]=.25;
|
||||
// this[3,4]=.11;
|
||||
// this[3,5]=.01;
|
||||
|
||||
// this[4,1]=.01;
|
||||
// this[4,2]=.05;
|
||||
// this[4,3]=.11;
|
||||
// this[4,4]=.05;
|
||||
// this[4,5]=.01;
|
||||
|
||||
// this[5,2]=.01;
|
||||
// this[5,3]=.01;
|
||||
// this[5,4]=.01;
|
||||
// }
|
||||
// }
|
||||
|
||||
// public class GaussianIdentityKernel : Kernel
|
||||
// {
|
||||
// public GaussianIdentityKernel()
|
||||
// : base(3,3)
|
||||
// {
|
||||
// this[1,1]=1;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public class BoxFilteringKernel : Kernel
|
||||
// {
|
||||
// public BoxFilteringKernel()
|
||||
// : base(3,3)
|
||||
// {
|
||||
// this[0,0]=1.0/9.0;
|
||||
// this[0,1]=1.0/9.0;
|
||||
// this[0,2]=1.0/9.0;
|
||||
|
||||
// this[1,0]=1.0/9.0;
|
||||
// this[1,1]=1.0/9.0;
|
||||
// this[1,2]=1.0/9.0;
|
||||
|
||||
// this[2,0]=1.0/9.0;
|
||||
// this[2,1]=1.0/9.0;
|
||||
// this[2,2]=1.0/9.0;
|
||||
// }
|
||||
// }
|
||||
|
||||
//}
|
||||
52
MarketDataLib/CNNProcessing/PointMapping.cs
Normal file
52
MarketDataLib/CNNProcessing/PointMapping.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class PointMapping
|
||||
{
|
||||
public PointMapping(double width,double height,double xDataExtent,double xDataExtentMin,double yDataExtent,double yDataExtentMin,double xMarginExtent=0.00,double yMarginExtent=0.00)
|
||||
{
|
||||
Width=width;
|
||||
Height=height;
|
||||
XMargin=xMarginExtent;
|
||||
YMargin=yMarginExtent;
|
||||
XDataExtent=xDataExtent;
|
||||
XDataExtentMin=xDataExtentMin;
|
||||
YDataExtent=yDataExtent;
|
||||
YDataExtentMin=yDataExtentMin;
|
||||
XScalingFactor=(Width-(XMargin*2.00))/(XDataExtent-XDataExtentMin);
|
||||
YScalingFactor=(Height-(YMargin*2.00))/(YDataExtent-YDataExtentMin);
|
||||
}
|
||||
// MapPoint will both scale the given point and translate the given point from an upper left origin system to a bottom left origin system
|
||||
public Point MapPoint(Point sourcePoint)
|
||||
{
|
||||
Point mappedPoint=new Point((int)((sourcePoint.X-XDataExtentMin)*XScalingFactor),(int)(Height-((sourcePoint.Y-YDataExtentMin)*YScalingFactor)));
|
||||
mappedPoint.X+=(int)XMargin; // offset by the xMargin
|
||||
mappedPoint.Y-=(int)YMargin; // offset by the yMargin
|
||||
return mappedPoint;
|
||||
}
|
||||
// TranslatePoint will only translate the given point from an upper left origin system to a bottom left origin system
|
||||
public Point TranslatePoint(Point sourcePoint)
|
||||
{
|
||||
Point mappedPoint=new Point((int)sourcePoint.X,(int)(Height-sourcePoint.Y-1));
|
||||
return mappedPoint;
|
||||
}
|
||||
public double Width{get;private set;}
|
||||
public double Height{get;private set;}
|
||||
public double XDataExtent{get;private set;}
|
||||
public double XDataExtentMin{get;private set;}
|
||||
public double XRange{get{return XDataExtent-XDataExtentMin;}}
|
||||
public double YDataExtent{get;private set;}
|
||||
public double YDataExtentMin{get;private set;}
|
||||
public double YRange{get{return YDataExtent-YDataExtentMin;}}
|
||||
public double XMargin{get;private set;}
|
||||
public double YMargin{get;private set;}
|
||||
public double XScalingFactor{get;private set;}
|
||||
public double YScalingFactor{get;private set;}
|
||||
}
|
||||
}
|
||||
59
MarketDataLib/CNNProcessing/TestCase.cs
Normal file
59
MarketDataLib/CNNProcessing/TestCase.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MarketData.Utils;
|
||||
using System.IO;
|
||||
|
||||
namespace MarketData.CNNProcessing
|
||||
{
|
||||
public class TestCases : List<TestCase>
|
||||
{
|
||||
}
|
||||
public class TestCase
|
||||
{
|
||||
public enum CaseType{Training,Test,Validation};
|
||||
public enum GenerateType{Price,BollingerBand};
|
||||
public enum OutputType{OutputFile,OutputStream}
|
||||
private readonly List<Stream> streams=new List<Stream>();
|
||||
private readonly List<String> pathFileNames=new List<String>();
|
||||
private readonly List<String> responses=new List<String>();
|
||||
/// <summary>
|
||||
/// ProcessData item
|
||||
/// </summary>
|
||||
///<param name="symbol">Symbol</param>
|
||||
///<param name="startDate">The start datee.</param>
|
||||
///<param name="purchaseDate">The purchasedate.</param>
|
||||
public TestCase(String symbol,DateTime purchaseDate, int dayCount,TestCase.CaseType caseType,TestCase.GenerateType generateType=TestCase.GenerateType.Price,OutputType outputType=OutputType.OutputFile)
|
||||
{
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
this.Symbol=symbol;
|
||||
this.HistDate=dateGenerator.GenerateHistoricalDate(purchaseDate,dayCount);
|
||||
this.PurchaseDate=dateGenerator.GetNextBusinessDay(purchaseDate);
|
||||
this.TypeCase=caseType;
|
||||
TypeGenerate=generateType;
|
||||
TypeOutput=outputType;
|
||||
}
|
||||
|
||||
public String Symbol{get;set;}
|
||||
public DateTime PurchaseDate{get;set;}
|
||||
public DateTime HistDate{get;set;}
|
||||
public CaseType TypeCase{get;set;}
|
||||
public GenerateType TypeGenerate{get;set;}
|
||||
public OutputType TypeOutput{get;set;}
|
||||
public List<Stream> Streams{get{return streams;}} // This will get set automatically depending on OutputType
|
||||
public List<String> PathFileNames{get{return pathFileNames;}} // This will get set automaticall depending on OutputType
|
||||
public List<String> Responses{get{return responses;}}
|
||||
public Stream LastStream{get{return Streams.Count>0?Streams[Streams.Count-1]:null;}}
|
||||
public String LastPathFileName{get{return PathFileNames.Count>0?PathFileNames[PathFileNames.Count-1]:null;}}
|
||||
public String LastResponse{get{return Responses.Count>0?Responses[Responses.Count-1]:null;}}
|
||||
public override String ToString()
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if(OutputType.OutputFile.Equals(TypeOutput))return LastPathFileName;
|
||||
sb.Append(String.Format("{0}:P:{1}->H:{2}",Symbol,PurchaseDate.ToShortDateString(),HistDate.ToShortDateString()));
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user