Push MarketData Changes.
This commit is contained in:
@@ -24,6 +24,7 @@ namespace MarketData.CNNProcessing
|
||||
Height=height;
|
||||
PenWidth=2f;
|
||||
DrawingBrush=new SolidBrush(Color.Black);
|
||||
DrawingBrushRed=new SolidBrush(Color.Red);
|
||||
FillBrush=new SolidBrush(Color.White);
|
||||
DrawPrice=true;
|
||||
UseGrayScale=false;
|
||||
@@ -59,6 +60,11 @@ namespace MarketData.CNNProcessing
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the drawing brush brush</param>
|
||||
public Brush DrawingBrush{get;set;}
|
||||
/// <summary>
|
||||
/// DrawingBrush
|
||||
/// </summary>
|
||||
///<param name="value">Gets/Sets the drawing brush brush</param>
|
||||
public Brush DrawingBrushRed{get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// DrawBlack
|
||||
@@ -143,6 +149,29 @@ namespace MarketData.CNNProcessing
|
||||
this.strFolderPath=strFolderPath;
|
||||
if(!this.strFolderPath.EndsWith(@"\"))this.strFolderPath=this.strFolderPath+@"\";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ClearFolderPath
|
||||
/// </summary>
|
||||
///<param name="testCases">The test cases</param>
|
||||
public void ClearFolderPath()
|
||||
{
|
||||
if(String.IsNullOrEmpty(strFolderPath))throw new InvalidDataException($"{nameof(strFolderPath)} cannot be null");
|
||||
if(!Directory.Exists(strFolderPath))
|
||||
{
|
||||
Directory.CreateDirectory(strFolderPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
String[] pathFileNames = Directory.GetFiles(strFolderPath);
|
||||
Console.WriteLine($"Deleting {pathFileNames.Length} files from {strFolderPath}");
|
||||
foreach(String file in pathFileNames)
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ProcessData(TestCases testCases)
|
||||
{
|
||||
for(int index=0;index<testCases.Count;index++)
|
||||
@@ -173,7 +202,7 @@ namespace MarketData.CNNProcessing
|
||||
}
|
||||
}
|
||||
}
|
||||
else // Bollinger bands
|
||||
else if(testCase.TypeGenerate.Equals(TestCase.GenerateType.BollingerBand))// Bollinger bands
|
||||
{
|
||||
if(null==MovingAverageArray)
|
||||
{
|
||||
@@ -194,7 +223,6 @@ namespace MarketData.CNNProcessing
|
||||
for(int avgIndex=0;avgIndex<MovingAverageArray.Length;avgIndex++)
|
||||
{
|
||||
int movingAverage=MovingAverageArray[avgIndex];
|
||||
|
||||
for(int penIndex=0;penIndex<PenWidthArray.Length;penIndex++)
|
||||
{
|
||||
float penWidth=PenWidthArray[penIndex];
|
||||
@@ -208,7 +236,22 @@ namespace MarketData.CNNProcessing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // Bollinger Bands
|
||||
else if(testCase.TypeGenerate.Equals(TestCase.GenerateType.BollingerBandWithVIX))
|
||||
{
|
||||
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 = CreateFileName(strFolderPath, testCase.Symbol, testCase.DayCount, index, penIndex, noiseIndex, testCase.TypeCase, testCase.TypeGenerate, testCase.PurchaseDate);
|
||||
testCase.PathFileNames.Add(strPathFileName);
|
||||
ProcessBollingerBandDataWithVolatility(testCase, penWidth, noise);
|
||||
}
|
||||
}
|
||||
} // Bollinger Bands with ~VIX
|
||||
else throw new InvalidDataException("Unknown option");
|
||||
}
|
||||
|
||||
private String CreateFileName(String strFolderPath,String symbol,int dayCount,int index,int penIndex,int noiseIndex,TestCase.CaseType caseType,TestCase.GenerateType generateType,DateTime purchaseDate)
|
||||
@@ -216,6 +259,132 @@ namespace MarketData.CNNProcessing
|
||||
return String.Format("{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}d.jpg",strFolderPath,symbol,index,penIndex,noiseIndex,caseType.ToString(),generateType.ToString(),Utility.DateToLong(purchaseDate),dayCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ProcessBollingerBandData item - Draws Price, K, L and Volatility
|
||||
/// </summary>
|
||||
///<param name="testCase">Symbol</param>
|
||||
private void ProcessBollingerBandDataWithVolatility(TestCase testCase,float penWidth,double noise)
|
||||
{
|
||||
String symbolVolatility="^VIX";
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
|
||||
int daysInPeriod=dateGenerator.DaysBetweenActual(testCase.PurchaseDate,testCase.HistDate);
|
||||
daysInPeriod+=60;
|
||||
Prices prices=PricingDA.GetPrices(testCase.Symbol,testCase.PurchaseDate,daysInPeriod);
|
||||
Prices volatilityPrices=PricingDA.GetPrices(symbolVolatility,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];
|
||||
|
||||
// Line up volatility dates with bollinger bands
|
||||
DateTime minDate = bollingerBands.Min(x=>x.Date);
|
||||
DateTime maxDate = bollingerBands.Max(x=>x.Date);
|
||||
volatilityPrices = new Prices(volatilityPrices.Where(x=>x.Date<=maxDate && x.Date>=minDate).OrderBy(x=>x.Date).ToList()); // most historical date in lowest index
|
||||
float[] v=volatilityPrices.GetPrices();
|
||||
float minV=Numerics.Min(ref v); // get the minimum volatility value
|
||||
double minP=bollingerBands.Min(x=>x.Close); // get minimum price
|
||||
double factor=minP/minV; // determine scaling factor
|
||||
for(int index=0;index<v.Length;index++)
|
||||
{
|
||||
double item = v[index];
|
||||
item*=factor;
|
||||
v[index]=(float)Math.Log(item)*1000.00f;
|
||||
}
|
||||
|
||||
// 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; // put the data in log form
|
||||
l[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.L)*1000.00f; // put the data in log form
|
||||
close[bollingerBands.Count-index-1]=(float)Math.Log(bollingerBandElement.Close)*1000.00f; // put the data in log form
|
||||
}
|
||||
Numerics.ZeroForNaNOrInfinity(ref k);
|
||||
Numerics.ZeroForNaNOrInfinity(ref l);
|
||||
Numerics.ZeroForNaNOrInfinity(ref close);
|
||||
Numerics.ZeroForNaNOrInfinity(ref v);
|
||||
float maxY=Math.Max(Math.Max(Numerics.Max(ref l),Math.Max(Numerics.Max(ref close),Numerics.Max(ref k))),Numerics.Max(ref v));
|
||||
float minY=Math.Min(Math.Min(Numerics.Min(ref l),Math.Min(Numerics.Min(ref close),Numerics.Min(ref k))),Numerics.Min(ref v))-5f;
|
||||
float maxX=close.Length;
|
||||
float minX=0.00f;
|
||||
|
||||
Pen pen=new Pen(DrawingBrush,penWidth);
|
||||
Pen redPen=new Pen(DrawingBrushRed,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();
|
||||
// draw volatility
|
||||
for(int index=0;index<v.Length;index++)
|
||||
{
|
||||
if(0==index)continue;
|
||||
Point p1=new Point(index-1,(int)v[index-1]);
|
||||
Point p2=new Point(index,(int)v[index]);
|
||||
lineSegments.Add(p1,p2);
|
||||
}
|
||||
imageHelper.DrawPath(redPen,lineSegments);
|
||||
|
||||
// draw prices
|
||||
lineSegments.Clear();
|
||||
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);
|
||||
// draw k
|
||||
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);
|
||||
|
||||
// draw l
|
||||
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,$"Writing {testCase.LastPathFileName}");
|
||||
if(File.Exists(testCase.LastPathFileName))File.Delete(testCase.LastPathFileName);
|
||||
if(UseGrayScale)imageHelper.SaveGrayScaleJPG(testCase.LastPathFileName);
|
||||
else imageHelper.Save(testCase.LastPathFileName);
|
||||
// else imageHelper.SaveBlackAndWhiteJPG(testCase.LastPathFileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
testCase.Streams.Add(imageHelper.ToStream());
|
||||
// testCase.Streams.Add(imageHelper.SaveBlackAndWhiteJPG());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate Bollinger Band Data
|
||||
/// </summary>
|
||||
/// <param name="testCase"></param>
|
||||
/// <param name="movingAverageDays"></param>
|
||||
/// <param name="penWidth"></param>
|
||||
/// <param name="noise"></param>
|
||||
private void ProcessBollingerBandData(TestCase testCase,int movingAverageDays,float penWidth,double noise)
|
||||
{
|
||||
int bufferDays=60;
|
||||
@@ -377,6 +546,7 @@ namespace MarketData.CNNProcessing
|
||||
|
||||
if(testCase.TypeOutput.Equals(TestCase.OutputType.OutputFile))
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,$"Writing {testCase.LastPathFileName}");
|
||||
if(File.Exists(testCase.LastPathFileName))File.Delete(testCase.LastPathFileName);
|
||||
if(UseGrayScale)imageHelper.SaveGrayScaleJPG(testCase.LastPathFileName);
|
||||
else imageHelper.SaveBlackAndWhiteJPG(testCase.LastPathFileName);
|
||||
@@ -426,6 +596,7 @@ namespace MarketData.CNNProcessing
|
||||
|
||||
if(testCase.TypeOutput.Equals(TestCase.OutputType.OutputFile))
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,$"Writing {testCase.LastPathFileName}");
|
||||
if(File.Exists(testCase.LastPathFileName))File.Delete(testCase.LastPathFileName);
|
||||
if(UseGrayScale)imageHelper.SaveGrayScaleJPG(testCase.LastPathFileName);
|
||||
else imageHelper.SaveBlackAndWhiteJPG(testCase.LastPathFileName);
|
||||
|
||||
Reference in New Issue
Block a user