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;
}
///
/// Width
///
///Gets/Sets the width of the output image
public int Width{get;set;}
///
/// Height
///
///Gets/Sets the height of the output image
public int Height{get;set;}
///
/// PenWidth
///
///Gets/Sets the width of the drawing pen
public float PenWidth{get;set;}
///
/// FillBrush
///
///Gets/Sets the background brush
public Brush FillBrush{get;set;}
///
/// DrawingBrush
///
///Gets/Sets the drawing brush brush
public Brush DrawingBrush{get;set;}
///
/// DrawBlack
///
///Sets up the canvas to do black drawing
public void DrawBlack()
{
DrawingBrush=new SolidBrush(Color.Black);
FillBrush=new SolidBrush(Color.White);
}
///
/// IncludePrice
///
///Indicates whether we want to include price data in the graph
public bool DrawPrice{get; set;}
///
/// UseGrayScale
///
///Indicates whether we want to use grayscale.. default is B&W
public bool UseGrayScale{get; set;}
///
/// DrawWhite
///
///Sets up the canvas to do white drawing
public void DrawWhite()
{
DrawingBrush=new SolidBrush(Color.White);
FillBrush=new SolidBrush(Color.Black);
}
///
/// MovingAverageArray
///
///Gets/Sets the moving average array
public int[] MovingAverageArray{get; set; }
///
/// PenWidthArray
///
///Gets/Sets thepen width array
public float[] PenWidthArray{get; set; }
///
/// NoiseArray
///
///Gets/Sets the noise array
public double[] NoiseArray{get; set; }
///
/// NoiseColor
///
///Gets/Sets the noise color
public Color NoiseColor{get; set; }
///
/// UseNoise
///
///The seed value
///The increment
///The size
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
/// SetOutputFolderPath
///
///The test cases
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
/// ProcessData item
///
///The test cases
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;penIndexbufferDays)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
/// ProcessBollingerBandData item
///
///Symbol
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
/// ProcessPriceData item
///
///TestCase
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