Lot of code, so far. Here is whole project including item 22.
Sorry, Google does not allow file upload. :-(
C# 4.0 programming using Japanese candlesticks as an example. Calculation, drawing and trading decision management.
Wednesday, 30 May 2012
22. Trader - First drawing
Application can draw candles. Six days can be analysed. Gray one is for today and it is be drawn with current trading data. More details will come to graph but this is basic start.
Code needed:
// let's find trading limits
double priceMax=0;
double priceMin=1000000;
int j = 5; // number of days to draw
for (int i = 1; i <= j; i++)
{
if (historyArray[i, 3] > priceMax) priceMax = historyArray[i, 3]; // find max price for j last days
if (historyArray[i, 4] < priceMin) priceMin = historyArray[i, 4]; // find min price for j last days
}
double priceRange = priceMax - priceMin;
double pricePerPix = areaHeightMax / priceRange;
drawCandle(5, pricePerPix, priceMax);
drawCandle(4, pricePerPix, priceMax);
drawCandle(3, pricePerPix, priceMax);
drawCandle(2, pricePerPix, priceMax);
drawCandle(1, pricePerPix, priceMax);
then:
private void drawCandle(int noDay, double vPricePerPix, double vPriceMax)
{
double[] aR=new double[8];
for (int i = 0; i < 8; i++) { aR[i]=historyArray[noDay,i];}
aboutDay ad = new aboutDay(aR); // day 5*********************************
double candleHeiPix = ad.dayBodyLengthNum * vPricePerPix; // candle length in pix
double candleTaiPix = ad.dayTailLengthNum * vPricePerPix; // candle tail in pix
if (noDay == 5)
{
body5.Height = Convert.ToInt16(candleHeiPix);
body5.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail5.Height = Convert.ToInt16(candleTaiPix);
tail5.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail5.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body5.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body5.BackColor = Color.Black;
cLabel5.Text = Convert.ToString( aR[0]).Substring(6,2);
cLabel5.Top = body5.Top + body5.Height / 2;
}
if (noDay == 4)
{
body4.Height = Convert.ToInt16(candleHeiPix);
body4.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail4.Height = Convert.ToInt16(candleTaiPix);
tail4.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail4.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body4.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body4.BackColor = Color.Black;
} if (noDay == 3)
{
body3.Height = Convert.ToInt16(candleHeiPix);
body3.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail3.Height = Convert.ToInt16(candleTaiPix);
tail3.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail3.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body3.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body3.BackColor = Color.Black;
}
if (noDay == 2)
{
body2.Height = Convert.ToInt16(candleHeiPix);
body2.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail2.Height = Convert.ToInt16(candleTaiPix);
tail2.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail2.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body2.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body2.BackColor = Color.Black;
}
if (noDay == 1)
{
body1.Height = Convert.ToInt16(candleHeiPix);
body1.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail1.Height = Convert.ToInt16(candleTaiPix);
tail1.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail1.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body1.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body1.BackColor = Color.Black;
}
if (noDay == 0)
{
body0.Height = Convert.ToInt16(candleHeiPix);
body0.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail0.Height = Convert.ToInt16(candleTaiPix);
tail0.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail0.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body0.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body0.BackColor = Color.Black;
}
}
Code needed:
// let's find trading limits
double priceMax=0;
double priceMin=1000000;
int j = 5; // number of days to draw
for (int i = 1; i <= j; i++)
{
if (historyArray[i, 3] > priceMax) priceMax = historyArray[i, 3]; // find max price for j last days
if (historyArray[i, 4] < priceMin) priceMin = historyArray[i, 4]; // find min price for j last days
}
double priceRange = priceMax - priceMin;
double pricePerPix = areaHeightMax / priceRange;
drawCandle(5, pricePerPix, priceMax);
drawCandle(4, pricePerPix, priceMax);
drawCandle(3, pricePerPix, priceMax);
drawCandle(2, pricePerPix, priceMax);
drawCandle(1, pricePerPix, priceMax);
then:
private void drawCandle(int noDay, double vPricePerPix, double vPriceMax)
{
double[] aR=new double[8];
for (int i = 0; i < 8; i++) { aR[i]=historyArray[noDay,i];}
aboutDay ad = new aboutDay(aR); // day 5*********************************
double candleHeiPix = ad.dayBodyLengthNum * vPricePerPix; // candle length in pix
double candleTaiPix = ad.dayTailLengthNum * vPricePerPix; // candle tail in pix
if (noDay == 5)
{
body5.Height = Convert.ToInt16(candleHeiPix);
body5.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail5.Height = Convert.ToInt16(candleTaiPix);
tail5.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail5.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body5.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body5.BackColor = Color.Black;
cLabel5.Text = Convert.ToString( aR[0]).Substring(6,2);
cLabel5.Top = body5.Top + body5.Height / 2;
}
if (noDay == 4)
{
body4.Height = Convert.ToInt16(candleHeiPix);
body4.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail4.Height = Convert.ToInt16(candleTaiPix);
tail4.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail4.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body4.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body4.BackColor = Color.Black;
} if (noDay == 3)
{
body3.Height = Convert.ToInt16(candleHeiPix);
body3.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail3.Height = Convert.ToInt16(candleTaiPix);
tail3.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail3.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body3.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body3.BackColor = Color.Black;
}
if (noDay == 2)
{
body2.Height = Convert.ToInt16(candleHeiPix);
body2.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail2.Height = Convert.ToInt16(candleTaiPix);
tail2.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail2.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body2.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body2.BackColor = Color.Black;
}
if (noDay == 1)
{
body1.Height = Convert.ToInt16(candleHeiPix);
body1.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail1.Height = Convert.ToInt16(candleTaiPix);
tail1.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail1.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body1.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body1.BackColor = Color.Black;
}
if (noDay == 0)
{
body0.Height = Convert.ToInt16(candleHeiPix);
body0.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBodyBigger) * vPricePerPix);
tail0.Height = Convert.ToInt16(candleTaiPix);
tail0.Top = Convert.ToInt16(areaTopPointMin + (vPriceMax - ad.dayBiggest) * vPricePerPix);
tail0.Height = Convert.ToInt16(ad.dayTailLengthNum * vPricePerPix);
if (ad.dayColor == Globals.whitE) body0.BackColor = Color.White;
if (ad.dayColor == Globals.blacK) body0.BackColor = Color.Black;
}
}
Tuesday, 29 May 2012
21. Trader - New navigation among forms
There is new navigation in place. Now, once activated forms, stayed alive in background and can be invoked by click in Active form list (it is listBox ListActiveForms). Active form can be hidden and still be activated. List item is added with form name after new indice is chosen and form was created. There is checking of only one form per indice can be created.
Code is changed under Menu item "Patterns":
private void patternsToolStripMenuItem_Click(object sender, EventArgs e)
{
FormCollection fC = Application.OpenForms;
string ait = activeIndice.Text;
if (ait == "") { goto kraj; }
int i = Application.OpenForms.Count;
if (i == 1)
{
fPatterns ff = new fPatterns();
ff.Text = ait;
ff.Name = ait;
ff.Show();
goto kraj;
}
foreach (Form f in fC)
{
if (f.Text == ait) {
goto kraj; }
}
{
fPatterns ff = new fPatterns();
ff.Text = ait;
ff.Name = ait;
ff.Show();
}
kraj: ;
listActiveForms.Items.Clear();
foreach (Form f in fC) { listActiveForms.Items.Add(f.Name); }
}
This code is activated on click (selection) on ListBox labeled "Active form list":
private void listActiveForms_SelectedIndexChanged(object sender, EventArgs e)
{
if (listActiveForms.SelectedItem.ToString() == "") { goto kraj; }
string fS = listActiveForms.SelectedItem.ToString(); // Value.ToString(); // form selected
Form form = Application.OpenForms[fS];
form.Visible = true;
form.Activate();
kraj: ;
}
Code under menu item Hide is:
private void hideToolStripMenuItem_Click(object sender, EventArgs e)
{
fPatterns.ActiveForm.Visible=false;
}
Code is changed under Menu item "Patterns":
private void patternsToolStripMenuItem_Click(object sender, EventArgs e)
{
FormCollection fC = Application.OpenForms;
string ait = activeIndice.Text;
if (ait == "") { goto kraj; }
int i = Application.OpenForms.Count;
if (i == 1)
{
fPatterns ff = new fPatterns();
ff.Text = ait;
ff.Name = ait;
ff.Show();
goto kraj;
}
foreach (Form f in fC)
{
if (f.Text == ait) {
goto kraj; }
}
{
fPatterns ff = new fPatterns();
ff.Text = ait;
ff.Name = ait;
ff.Show();
}
kraj: ;
listActiveForms.Items.Clear();
foreach (Form f in fC) { listActiveForms.Items.Add(f.Name); }
}
This code is activated on click (selection) on ListBox labeled "Active form list":
private void listActiveForms_SelectedIndexChanged(object sender, EventArgs e)
{
if (listActiveForms.SelectedItem.ToString() == "") { goto kraj; }
string fS = listActiveForms.SelectedItem.ToString(); // Value.ToString(); // form selected
Form form = Application.OpenForms[fS];
form.Visible = true;
form.Activate();
kraj: ;
}
Code under menu item Hide is:
private void hideToolStripMenuItem_Click(object sender, EventArgs e)
{
fPatterns.ActiveForm.Visible=false;
}
Monday, 28 May 2012
20. Trader - Candles for patterns drawing
As said, we'll use five days to analyze from dataGridView i.e. historyGrid.
So, we need five candles and we'll use new form Patterns for candle drawing. This is default screen before actual drawing. I used Panel control to represent candles and their tails.
Code for Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace Stocker
{
// "Copyright © Mladen Zupancic 2012")
public partial class Form1 : Form
{
// DataTable historyTable = new DataTable(); // histroyTable declaration
public Form1()
{
InitializeComponent();
}
public string workingPath = @"P:\Private\Analizator\"; // constant is working directory on disk P
public string[] arrDay0 = new string[8]; // working array - today
public string[] arrDay1 = new string[8]; // working array - yesterday
public string[] arrDay2 = new string[8]; // working array - day before yesterday
public string[] arrDay3 = new string[8]; // working array - 3 days before
public string[] arrDay4 = new string[8]; // working array - 4 days before
public double[] arrDay0d = new double[8]; // working array - today
public double[] arrDay1d = new double[8]; // working array - yesterday
public double[] arrDay2d = new double[8]; // working array - day before yesterday
public double[] arrDay3d = new double[8]; // working array - 3 days before
public double[] arrDay4d = new double[8]; // working array - 4 days before
public double sum2; // auxiliary variable
public double sum1; // auxiliary variable
private void Form1_DoubleClick(object sender, EventArgs e)
{
Environment.Exit(0); // double click on form to end application- just for quick exit when testing
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Stocker2012"; // put title on form bar
this.Width = 1000; // set form width
this.Height = 500; // set form height
this.Top = 0; // set position of form
this.Left = 0;
activeOnly.Checked = true; // by default we would like to work only with active indicies
DataTable historyTable = new DataTable(); // histroyTable declaration
historyTable.Columns.Add(new DataColumn("Date", typeof(string)));
historyTable.Columns.Add(new DataColumn("Open", typeof(string)));
historyTable.Columns.Add(new DataColumn("Close", typeof(string)));
historyTable.Columns.Add(new DataColumn("High", typeof(string)));
historyTable.Columns.Add(new DataColumn("Low", typeof(string)));
historyTable.Columns.Add(new DataColumn("Volume", typeof(string)));
historyTable.Columns.Add(new DataColumn("Avg.Vol.", typeof(string))); // avgDays days Volume
historyTable.Columns.Add(new DataColumn("Avg.Len.", typeof(string))); // avgDays days Length
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns(); // justify columns acording to header
if (Globals.ghistoryArray[0, 0] == null)
{ }
else
{
for (int i = 0; i <= Convert.ToInt16(Globals.ghtDays); i++)
{
historyTable.Rows.Add(Globals.ghistoryArray[i, 0], Globals.ghistoryArray[i, 1], Globals.ghistoryArray[i, 2], Globals.ghistoryArray[i, 3], Globals.ghistoryArray[i, 4],
Globals.ghistoryArray[i, 5], Globals.ghistoryArray[i, 6], Globals.ghistoryArray[i, 7]);
}
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns();
// historyGrid.CurrentCell = historyGrid[0, 1];
}
}
public void getIndicesL()
{
string aPath; // working string to make path
if (activeOnly.Checked == true) // upon a checkbox state
aPath = workingPath + "ActiveIndices.txt"; // if checked - this file
else aPath = workingPath + "NonActiveIndices.txt"; // if not - other file
listIndices.Items.Clear(); // clear listbox before filling
FileStream fs = File.OpenRead(aPath); // prepare filestream
TextReader reader = new StreamReader(fs); // use textreader
while (reader.Peek() > -1) // read while end of file is reached
listIndices.Items.Add(reader.ReadLine()); // add line to listbox
listIndices.Sorted = true;
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0); // quit application
}
private void getIndicies_Click(object sender, EventArgs e)
{
getIndicesL();
}
private void listIndices_SelectedIndexChanged(object sender, EventArgs e)
{
if (listIndices.SelectedItem.ToString() == "") { goto kra; }
string part1bloom = "http://www.internet.com/apps/data?pid=webpxta&Securities="; // first part of Internet
string part2bloom = "&TimePeriod=5Y&Outfields=HDATE,PR006-H,PR007-H,PR008-H,PR005-H,PR013-H,&rnd=421"; // second part of internet url histroy string
urlHistory.Text = part1bloom + listIndices.SelectedItem.ToString()+ part2bloom; // construct whole url string
Int16 j = Convert.ToInt16( fillHistoryTableDays.Text); // convert textbox text to int16
fillHistoryT(j); // getting data from local disk // invoke table filling
fillWorkingArrays(0,8); // fill daily working arrays
zeroDay.Text = historyGrid[0, 0].Value.ToString(); // show zero day to analyze
urlHistory.BackColor = Color.White; // restore back color
kra: ;
}
private void getHistory_Click(object sender, EventArgs e)
{
if (urlHistory.Text=="") // is url entered?
{MessageBox.Show("URL field is empty.", "No infomation"); // url is not enetred
goto end1;} // no action. goto end
WebRequest req = WebRequest.Create(urlHistory.Text); // to use WebRequest we need to add "using System.Net;" row
req.Proxy = null; // i do not use proxy
string selectedIndices = listIndices.SelectedItem.ToString(); // get selected indice
selectedIndices=selectedIndices.Replace(":", "-"); // file name can not contain ":"
selectedIndices = workingPath + selectedIndices + ".txt"; // make full path,file name and add .txt extension as it is text file
WebResponse res = req.GetResponse(); // get response from internet
Stream s = res.GetResponseStream(); // make a stream
StreamReader sr = new StreamReader(s);
File.WriteAllText(selectedIndices, sr.ReadToEnd()); // save stream to file
urlHistory.BackColor = Color.LightGreen; // if file was created make BackColor LightGreen
// System.Diagnostics.Process.Start(selectedIndices); // remove comment and result can be seen in text editor
end1: ;
}
private void fillHistoryT(int j)
{
if (listIndices.SelectedItem == null) //formal control if any indice is selected
{
MessageBox.Show("Indice is not selected.", "No infomation"); // no
goto end1; // no action
}
string selectedIndices = listIndices.SelectedItem.ToString(); // get selected indice
selectedIndices = selectedIndices.Replace(":", "-"); // file name can not contain ":"
selectedIndices = workingPath + selectedIndices + ".txt"; // make full path,file name and add .txt extension as it is text file
if (File.Exists(selectedIndices))
{
List<string> fileLines = File.ReadAllLines(selectedIndices).ToList(); // read file to list filelines
int fL = fileLines.Count - 2; // newest date is two rows before end of file
DataTable historyTable = new DataTable(); // histroyTable declaration
historyTable.Columns.Add(new DataColumn("Date", typeof(string)));
historyTable.Columns.Add(new DataColumn("Open", typeof(string)));
historyTable.Columns.Add(new DataColumn("Close", typeof(string)));
historyTable.Columns.Add(new DataColumn("High", typeof(string)));
historyTable.Columns.Add(new DataColumn("Low", typeof(string)));
historyTable.Columns.Add(new DataColumn("Volume", typeof(string)));
historyTable.Columns.Add(new DataColumn("Avg.Vol.", typeof(string))); // avgDays days Volume
historyTable.Columns.Add(new DataColumn("Avg.Len.", typeof(string))); // avgDays days Length
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns(); // justify columns acording to header
for (int i=0; i<=j; i++) // be in loop for number of days asked (j)
{
string row = fileLines[fL-i]; // decrement file line number
string[] words = row.Split('"'); // words are splitted by " delimiter
historyTable.Rows.Add(words[0], words[1].ToString(), words[4].ToString(), words[2].ToString(),
words[3].ToString(), words[5].ToString()); // fill table accordingly to header line
Globals.ghistoryArray[i+1, 0] = words[0];
Globals.ghistoryArray[i + 1, 1] = words[1];
Globals.ghistoryArray[i + 1, 2] = words[2];
Globals.ghistoryArray[i + 1, 3] = words[3];
Globals.ghistoryArray[i + 1, 4] = words[4];
Globals.ghistoryArray[i + 1, 5] = words[5]; // volumen
Globals.ghtDays = Convert.ToString( i);
}
computeAverage(); // compute some auxiliary values
historyTable.Clear(); // prepare to refill with avarage data
historyTable.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0"); // empty row in data table
for (int i = 0; i < 8; i++) { Globals.ghistoryArray[0, i] = "0"; } // zero row in global array
for (int i = 1; i <= Convert.ToInt16(Globals.ghtDays); i++)
{
historyTable.Rows.Add(Globals.ghistoryArray[i, 0], Globals.ghistoryArray[i, 1], Globals.ghistoryArray[i, 2], Globals.ghistoryArray[i, 3], Globals.ghistoryArray[i, 4],
Globals.ghistoryArray[i, 5], Globals.ghistoryArray[i, 6], Globals.ghistoryArray[i, 7]);
}
historyGrid.DataSource = historyTable; // refresh grid content
historyGrid.AutoResizeColumns(); // adjust column width
}
else MessageBox.Show(selectedIndices + " file does not exist","Download needed");
end1: ;
}
private void fillHistoryTable_Click(object sender, EventArgs e)
{
fillHistoryT(Convert.ToInt16(fillHistoryTableDays.Text));
}
private void activeOnly_CheckedChanged(object sender, EventArgs e)
{
if (activeOnly.Checked == true) // make take appropriate to state
activeOnly.Text = "activeOnly"; // if checked
else activeOnly.Text = "all"; // in not checked
getIndicesL();
}
private void fillWorkingArrays(int k, int kC)
{
for (int m = 0; m <kC ; m++) // copy data of kC columns to array
{
if (historyGrid.RowCount <= k + kC-1) { // we need five more rows from selected
MessageBox.Show("No rows","Selection failed" );
goto end1; // cause to exit for loop
}
arrDay0[m] = historyGrid[m, k].Value.ToString(); // copy days in their arrays
arrDay1[m] = historyGrid[m, k+1].Value.ToString();
arrDay2[m] = historyGrid[m, k + 2].Value.ToString();
arrDay3[m] = historyGrid[m, k+3].Value.ToString();
arrDay4[m] = historyGrid[m, k+4].Value.ToString();
calC cc0 = new calC("1", Globals.ghistoryArray[k, m]); // convert string value to double i.e. arrays are declared as dobule
arrDay0d[m] = cc0.producT;
calC cc1 = new calC("1", Globals.ghistoryArray[k + 1, m]);
arrDay1d[m] = cc1.producT;
calC cc2 = new calC("1", Globals.ghistoryArray[k + 2, m]);
arrDay2d[m] = cc2.producT;
calC cc3 = new calC("1", Globals.ghistoryArray[k + 3, m]);
arrDay3d[m] = cc3.producT;
calC cc4 = new calC("1", Globals.ghistoryArray[k + 4, m]);
arrDay4d[m] = cc4.producT;
}
end1: ;
}
private void historyGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int r = e.RowIndex; // get a row index
zeroDay.Text = historyGrid[0, r].Value.ToString()+ " " + e.RowIndex.ToString() + "."; // put date in zeroDay field
fillWorkingArrays(r,8); // selected raw index will be 0 day -> "today". this is for lates market analyse.
analyseResults.Items.Clear(); // clear listbox
}
private string dayColor( string[] aR)
{
string mE = "Day color : "; // name of routine
string coloR = "Color is not calculated - ERROR" ; // color of the day, by defalt ERROR message which should be changed later
calC cC = new calC(aR[1], aR[2]); // class calC computes two strings and returns result as double
if (cC.diffFmS > 0) { coloR = Globals.blacK; } // if difference first minus second is bigger then zero then it is white day
if (cC.diffFmS < 0) { coloR = Globals.whitE; }
if (cC.diffFmS == 0) { coloR = Globals.nO; }
if (showDetails.Checked) { analyseResults.Items.Add(mE +aR[0] + " -> " + coloR); } // show result if checked
return coloR;
}
private void analyseDay_Click(object sender, EventArgs e)
{ // routine for testing all patterns
mBullishEngulfing(); // bullish pattern
}
private void computeAverage() // compute average volume, day length
{
int i; // auxiliary variable
int j1; // auxiliary variable
string aux; // auxiliary variable
string aux1; // auxiliary variable
int j = historyGrid.RowCount - Globals.avgDays; // last row to calculate
for (int k = 0; k < j; k++) // =<
{
j1 = k; // j1 is working row
sum2 = 0; // make sum zero
sum1 = 0; // make sum zero
for (i = 1; i <= Globals.avgDays && j1 <= j; i++) // make loop avgDays times
{
aux = historyGrid[Globals.volumeColumn, j1 + 1].Value.ToString(); // extract volume
calC cC = new calC(aux,"1"); // class calC computes two strings and returns result as double
sum2 = sum2 + cC.producT; // convert to double
aux = historyGrid[1, j1+1].Value.ToString(); // extract open price
aux1 = historyGrid[2, j1+1].Value.ToString(); // extract close price
calC cC1 = new calC(aux, aux1); // class calC computes two strings and returns result as double
sum1 = sum1 + cC1.abS; // sum
j1++; // increment
}
if ( k +1 < j) // after last loop no need to calculate
{
sum2 = Math.Round((sum2 / Globals.avgDays), 0); // compute averige based on avgDays (3)
sum1 = Math.Round((sum1 / Globals.avgDays), 2); // compute averige based on avgDays (3)
// historyTable.Rows[k][Globals.avgVolumeColumn] = sum2.ToString(); // add to history table
// historyTable.Rows[k][Globals.avgDayLengthColumn] = sum1.ToString(); // add to history table
Globals.ghistoryArray[k + 1, Globals.avgVolumeColumn] = sum2.ToString();
Globals.ghistoryArray[k + 1, Globals.avgDayLengthColumn] = sum1.ToString();
}
}
}
private void mBullishEngulfing() // bullish pattern Medium strong
{
string mE = " 111-M-Bullish Engulfing "; // name of the pattern
string directioN = Globals.growtH; // expected direction
mE = mE + directioN; // make whole name and direction
int sumA = 0; // init score
if (dayColor(arrDay0) == Globals.whitE) { sumA++; } // today is white day
if (dayColor(arrDay1) == Globals.blacK) { sumA++; } // yesterday is black day
biggeRsmaller bgR = new biggeRsmaller(arrDay1[2].ToString(), arrDay0[1].ToString());
if (bgR.bseN == Globals.fBts) { sumA++; } // close yesterday should be lower then open today
biggeRsmaller bgR1 = new biggeRsmaller(arrDay0[2].ToString(), arrDay1[1].ToString());
if (bgR1.bseN == Globals.fBts) { sumA++; } // close yesterday should be lower then open today
int critNo = 4; // number of criteria to meet
if (critNo==sumA) // if met
{ analyseResults.Items.Add(arrDay0[0] + mE); } // fill listbox
}
public void switchTo() {
new fPatterns().Show(); // prepare showing of form named fPatterns
}
private void patternsToolStripMenuItem_Click(object sender, EventArgs e)
{
switchTo(); // to switch to form named fPatterns
this.Hide(); // hide source form
}
private void button1_Click(object sender, EventArgs e)
{
// listIndices.SetSelected(2,true);
aboutDay aD = new aboutDay(arrDay0d);
if (aD.dayColor == Globals.whitE) { }
}
}
}
Code for form Patterns:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Stocker
{
public partial class fPatterns : Form
{
public int maxAreaTopPoint; // working array - today
public int maxAreaHeight;
DataTable historyTable = new DataTable(); // histroyTable declaration
public const int tailHeightDef = 90;
public const int tailWidhtDef = 2;
public const int bodyWidthDef = 20;
public const int bodyHeightDef = 50;
public const int bodyGap = 50;
public fPatterns()
{
InitializeComponent();
}
private void fPatterns_Load(object sender, EventArgs e)
{
this.BackColor = Color.LightGray; //FromName( "red");
historyTable.Columns.Add(new DataColumn("Date", typeof(string)));
historyTable.Columns.Add(new DataColumn("Open", typeof(string)));
historyTable.Columns.Add(new DataColumn("Close", typeof(string)));
historyTable.Columns.Add(new DataColumn("High", typeof(string)));
historyTable.Columns.Add(new DataColumn("Low", typeof(string)));
historyTable.Columns.Add(new DataColumn("Volume", typeof(string)));
historyTable.Columns.Add(new DataColumn("Avg.Vol.", typeof(string))); // avgDays days Volume
historyTable.Columns.Add(new DataColumn("Avg.Len.", typeof(string))); // avgDays days Length
historyTable.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0"); // empty row in data table
for (int i = 0; i < 8; i++) { Globals.ghistoryArray[0, i] = "0"; } // zero row in global array
for (int i = 1; i <= Convert.ToInt16(Globals.ghtDays); i++) // copy data from global array to local historyTable
{
historyTable.Rows.Add(Globals.ghistoryArray[i, 0], Globals.ghistoryArray[i, 1], Globals.ghistoryArray[i, 2],
Globals.ghistoryArray[i, 3], Globals.ghistoryArray[i, 4],
Globals.ghistoryArray[i, 5], Globals.ghistoryArray[i, 6], Globals.ghistoryArray[i, 7]);
}
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns();
// calculate drawing area below historyGrid
int fW = Screen.PrimaryScreen.WorkingArea.Width; // screen max width
int fH = Screen.PrimaryScreen.WorkingArea.Height; // screen max height
int dgH = historyGrid.Height + historyGrid.Top; // historyGrid is above graphics
int maxCdH = fH - dgH - 2 * MainMenuStrip.Height; // max drawing area height
int maxTop = dgH; // top point of drawing area - upper right corner is 0,0
maxAreaHeight = maxCdH;
maxAreaTopPoint = maxTop;
// candeles
tail4.Width = tailWidhtDef; //oldest
tail4.Height = tailHeightDef;
body4.Width = bodyWidthDef;
body4.Height = bodyHeightDef;
body4.Left = historyGrid.Left;
tail4.Left = body4.Left + (body4.Width / 2 - tail4.Width / 2);
tail3.Width = tailWidhtDef;
tail3.Height = tailHeightDef;
body3.Width = bodyWidthDef;
body3.Height = bodyHeightDef;
body3.Left = historyGrid.Left + bodyGap * 1;
tail3.Left = body3.Left + (body3.Width / 2 - tail3.Width / 2);
tail2.Width = tailWidhtDef;
tail2.Height = tailHeightDef;
body2.Width = bodyWidthDef;
body2.Height = bodyHeightDef;
body2.Left = historyGrid.Left + bodyGap * 2;
tail2.Left = body2.Left + (body2.Width / 2 - tail2.Width / 2);
tail1.Width = tailWidhtDef;
tail1.Height = tailHeightDef;
body1.Width = bodyWidthDef;
body1.Height = bodyHeightDef;
body1.Left = historyGrid.Left + bodyGap * 3;
tail1.Left = body1.Left + (body1.Width / 2 - tail1.Width / 2);
tail0.Width = tailWidhtDef; // candle for today
tail0.Height = tailHeightDef;
body0.Width = bodyWidthDef;
body0.Height = bodyHeightDef;
body0.Left = historyGrid.Left+bodyGap*4;
tail0.Left = body0.Left + (body0.Width / 2 - tail0.Width / 2);
}
public void switchTo() { new Form1().Show(); }
private void mainToolStripMenuItem_Click(object sender, EventArgs e)
{
switchTo();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0); // quit application
}
private void button3_Click_1(object sender, EventArgs e)
{
int a = mainToolStripMenuItem.Height;
body0.BackColor = Color.Black;
body0.Width = Globals.candleW;
body0.Top = 0 + a;
body0.Left = Convert.ToInt16(candleWidth.Text);
}
private void button4_Click(object sender, EventArgs e)
{
}
private void body0_Paint(object sender, PaintEventArgs e)
{
}
}
}
and Globals.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stocker
{
public static class Globals
{
public const string blacK = "Black"; // black constant
public const string whitE = "White"; // white constant
public const string yeS = "Yes"; // yes constant
public const string nO = "No"; // no constant
public const string fiRst = "First"; // First constant
public const string seCond = "Second"; // Second constant
public const string eqUal = "Equal"; // Equel constant
public const string neaR = "Near"; // NearEquel constant
public const int avgDays = 3; // number of days to count average values
public const int avgVolumeColumn = 6; // average volume column in history table
public const int avgDayLengthColumn = 7; // average day column in history table
public const int volumeColumn = 5; // volume column index
public const int candleW = 20;
public const double almostTheSame = 0.001; // if ratio is less then this, we consider them the same
public const string fEts = "fEts"; // first equal to second
public const string fBts = "fBts"; // first bigger then second
public const string fSts = "fSts"; // first smaller then second
public const string fNts = "fNts"; // first near to second
public const string falL = "FALL"; // fall constant
public const string growtH = "GROW"; // grow constant
public const string commaCountry=yeS; // if your country uses decimal comma change yeS to nO below
static string _strTest1;
public static string strTest1 // global variable
{ get { return _strTest1; } set { _strTest1 = value; } } // access routine for global variable static string _strTest1; static string _strTest1;
static string _ghtDays; // number of days in history table
public static string ghtDays // global variable
{ get { return _ghtDays; } set { _ghtDays = value; } } //access routine for global variable static string _ghtDays;
static string[,] _ghistoryArray=new string[90,8]; // global variable
public static string[,] ghistoryArray
{
get { return _ghistoryArray; } set { _ghistoryArray = value; } } // access routine for global variable
}
class makeHeader
{
public makeHeader()
{
this.succesS=Globals.yeS;
}
public string succesS {get;private set;}
}
class aboutDay
{
public aboutDay(double[] arra)
{
this.dayColor = ""; // Globals.whitE;
this.dayLengthDesc = "";
this.dayLengthNum = 0;
compareTwoNumbers ctn = new compareTwoNumbers(arra[1], arra[2]);
if (ctn.biggeRs==Globals.seCond) this.dayColor = Globals.whitE;
if (ctn.smalleRs==Globals.fiRst) this.dayColor = Globals.blacK;
if (ctn.equalS==Globals.yeS | ctn.equalFNearS==Globals.yeS) this.dayColor = Globals.nO;
}
public string dayColor { get; private set; }
public string dayLengthDesc { get; private set; }
public double dayLengthNum { get; private set; }
}
class stringToDouble
{
public stringToDouble(string valuE) // coverts string to double having in mind decimal sign
{
double mla; // init variable
mla = Convert.ToDouble(valuE); // convert with default function
if (valuE.Contains(".")) // check for decimal point
{
if (Globals.commaCountry == Globals.yeS) // check if we use decimal comma
{ mla = Convert.ToDouble(valuE.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mla = Convert.ToDouble(valuE); } // convert string to double
this.doublE = mla; // return values
}
public double doublE { get; private set; }
}
class calC
{
public calC(string firsT, string seconD) // calC class - input are two numbers as string - string to double for countries
{ // using decimal comma
double mlaF; // auxiliary number for the first day
double mlaS; // auxiliary number for the second day
if (firsT == null | seconD == null) goto kraj;
if (firsT.Contains(".")) // if string contains point
{
if (Globals.commaCountry == Globals.yeS) // if country uses decimal comma
{ mlaF = Convert.ToDouble(firsT.Replace(".", ",")); } // and if you use decimal comma we need fix
}
else
{ mlaF = Convert.ToDouble(firsT); } // convert string to doubleif (firsT.Contains("."))
if (seconD.Contains(".")) // the same as above for first number
{
if (Globals.commaCountry == Globals.yeS)
{ mlaS = Convert.ToDouble(seconD.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mlaS = Convert.ToDouble(seconD); } // convert string to doubleif (firsT.Contains("."))
double mlaPro = Math.Round( mlaF * mlaS,2); // product rounded to two decimal places
double mlaDiffFirstMinSecond = Math.Round(mlaF - mlaS,2); // difference first minus second rounded to two decimal places
double mlaSum = Math.Round(mlaF + mlaS,2); // first plus second rounded to two decimal places
double mlaabS = Math.Round (Math.Abs(mlaF - mlaS),2) ; // difference first minus second rounded to two decimal places
biggeRsmaller bgR = new biggeRsmaller(firsT, seconD);
double c = Math.Abs((mlaF - mlaS) / bgR.biggeR);
this.producT = mlaPro; // define properties
this.diffFmS = mlaDiffFirstMinSecond;
this.sumFS = mlaSum;
this.abS = mlaabS;
kraj: ;
}
public double producT { get; private set; }
public double diffFmS { get; private set; }
public double sumFS { get; private set; }
public double abS { get; private set; }
}
class biggeRsmaller // compares two numbers as string
{
public biggeRsmaller(string f, string s) // (f)irst and (s)econd
{
stringToDouble ca = new stringToDouble(f); // convert string to number having in mind decimal comma
double a = ca.doublE; // a is first result
stringToDouble cb = new stringToDouble(s);
double b = cb.doublE; // b is second result
double sM = 0; // init variable
if (a < b) { sM = b; this.biggeR = b; this.biggeRs = s; this.smalleR = a; this.smalleRs = f; this.bseN = Globals.fSts; }
if (a > b) { sM = a; this.smalleR = b; this.smalleRs = s; this.biggeR = a; this.biggeRs = f; this.bseN = Globals.fBts; }
if (this.biggeR == 0) { this.biggeR = a; }
double c = Math.Abs((a - b) / this.biggeR);
if (c <= Globals.almostTheSame) { this.equalNearS = Globals.yeS; } // first near to second i.e. almost the same
if (a == b) { sM = b; this.equalS = Globals.yeS; }
}
public double biggeR { get; private set; }
public double smalleR { get; private set; }
public string biggeRs { get; private set; }
public string smalleRs { get; private set; }
public string equalS { get; private set; }
public string equalNearS { get; private set; }
public string bseN { get; private set; }
}
class compareTwoNumbers // compares two numbers as string
{
public compareTwoNumbers(double f, double s) // (f)irst and (s)econd
{
double a = f;
double b = s;
if (a < b) { this.biggeR = b; this.biggeRs = Globals.seCond; this.smalleR = a; this.smalleRs = Globals.fiRst; this.equalFNearS = Globals.nO; this.equalS = Globals.nO; }
if (a > b) { this.smalleR = b; this.smalleRs = Globals.seCond; this.biggeR = a; this.biggeRs = Globals.fiRst; this.equalFNearS = Globals.nO; this.equalS = Globals.nO; }
// if (this.biggeR == 0) { this.biggeR = a; }
double c = Math.Abs((a - b) / this.biggeR);
if (c <= Globals.almostTheSame) { this.equalFNearS = Globals.yeS; this.equalS = Globals.nO; } // first near to second i.e. almost the same
if (a == b) { this.equalS = Globals.nO; this.equalFNearS = Globals.yeS; this.biggeRs = Globals.nO; this.smalleRs = Globals.nO; }
}
public double biggeR { get; private set; }
public double smalleR { get; private set; }
public string biggeRs { get; private set; }
public string smalleRs { get; private set; }
public string equalS { get; private set; }
public string equalFNearS { get; private set; }
}
}
So, we need five candles and we'll use new form Patterns for candle drawing. This is default screen before actual drawing. I used Panel control to represent candles and their tails.
Code for Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace Stocker
{
// "Copyright © Mladen Zupancic 2012")
public partial class Form1 : Form
{
// DataTable historyTable = new DataTable(); // histroyTable declaration
public Form1()
{
InitializeComponent();
}
public string workingPath = @"P:\Private\Analizator\"; // constant is working directory on disk P
public string[] arrDay0 = new string[8]; // working array - today
public string[] arrDay1 = new string[8]; // working array - yesterday
public string[] arrDay2 = new string[8]; // working array - day before yesterday
public string[] arrDay3 = new string[8]; // working array - 3 days before
public string[] arrDay4 = new string[8]; // working array - 4 days before
public double[] arrDay0d = new double[8]; // working array - today
public double[] arrDay1d = new double[8]; // working array - yesterday
public double[] arrDay2d = new double[8]; // working array - day before yesterday
public double[] arrDay3d = new double[8]; // working array - 3 days before
public double[] arrDay4d = new double[8]; // working array - 4 days before
public double sum2; // auxiliary variable
public double sum1; // auxiliary variable
private void Form1_DoubleClick(object sender, EventArgs e)
{
Environment.Exit(0); // double click on form to end application- just for quick exit when testing
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Stocker2012"; // put title on form bar
this.Width = 1000; // set form width
this.Height = 500; // set form height
this.Top = 0; // set position of form
this.Left = 0;
activeOnly.Checked = true; // by default we would like to work only with active indicies
DataTable historyTable = new DataTable(); // histroyTable declaration
historyTable.Columns.Add(new DataColumn("Date", typeof(string)));
historyTable.Columns.Add(new DataColumn("Open", typeof(string)));
historyTable.Columns.Add(new DataColumn("Close", typeof(string)));
historyTable.Columns.Add(new DataColumn("High", typeof(string)));
historyTable.Columns.Add(new DataColumn("Low", typeof(string)));
historyTable.Columns.Add(new DataColumn("Volume", typeof(string)));
historyTable.Columns.Add(new DataColumn("Avg.Vol.", typeof(string))); // avgDays days Volume
historyTable.Columns.Add(new DataColumn("Avg.Len.", typeof(string))); // avgDays days Length
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns(); // justify columns acording to header
if (Globals.ghistoryArray[0, 0] == null)
{ }
else
{
for (int i = 0; i <= Convert.ToInt16(Globals.ghtDays); i++)
{
historyTable.Rows.Add(Globals.ghistoryArray[i, 0], Globals.ghistoryArray[i, 1], Globals.ghistoryArray[i, 2], Globals.ghistoryArray[i, 3], Globals.ghistoryArray[i, 4],
Globals.ghistoryArray[i, 5], Globals.ghistoryArray[i, 6], Globals.ghistoryArray[i, 7]);
}
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns();
// historyGrid.CurrentCell = historyGrid[0, 1];
}
}
public void getIndicesL()
{
string aPath; // working string to make path
if (activeOnly.Checked == true) // upon a checkbox state
aPath = workingPath + "ActiveIndices.txt"; // if checked - this file
else aPath = workingPath + "NonActiveIndices.txt"; // if not - other file
listIndices.Items.Clear(); // clear listbox before filling
FileStream fs = File.OpenRead(aPath); // prepare filestream
TextReader reader = new StreamReader(fs); // use textreader
while (reader.Peek() > -1) // read while end of file is reached
listIndices.Items.Add(reader.ReadLine()); // add line to listbox
listIndices.Sorted = true;
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0); // quit application
}
private void getIndicies_Click(object sender, EventArgs e)
{
getIndicesL();
}
private void listIndices_SelectedIndexChanged(object sender, EventArgs e)
{
if (listIndices.SelectedItem.ToString() == "") { goto kra; }
string part1bloom = "http://www.internet.com/apps/data?pid=webpxta&Securities="; // first part of Internet
string part2bloom = "&TimePeriod=5Y&Outfields=HDATE,PR006-H,PR007-H,PR008-H,PR005-H,PR013-H,&rnd=421"; // second part of internet url histroy string
urlHistory.Text = part1bloom + listIndices.SelectedItem.ToString()+ part2bloom; // construct whole url string
Int16 j = Convert.ToInt16( fillHistoryTableDays.Text); // convert textbox text to int16
fillHistoryT(j); // getting data from local disk // invoke table filling
fillWorkingArrays(0,8); // fill daily working arrays
zeroDay.Text = historyGrid[0, 0].Value.ToString(); // show zero day to analyze
urlHistory.BackColor = Color.White; // restore back color
kra: ;
}
private void getHistory_Click(object sender, EventArgs e)
{
if (urlHistory.Text=="") // is url entered?
{MessageBox.Show("URL field is empty.", "No infomation"); // url is not enetred
goto end1;} // no action. goto end
WebRequest req = WebRequest.Create(urlHistory.Text); // to use WebRequest we need to add "using System.Net;" row
req.Proxy = null; // i do not use proxy
string selectedIndices = listIndices.SelectedItem.ToString(); // get selected indice
selectedIndices=selectedIndices.Replace(":", "-"); // file name can not contain ":"
selectedIndices = workingPath + selectedIndices + ".txt"; // make full path,file name and add .txt extension as it is text file
WebResponse res = req.GetResponse(); // get response from internet
Stream s = res.GetResponseStream(); // make a stream
StreamReader sr = new StreamReader(s);
File.WriteAllText(selectedIndices, sr.ReadToEnd()); // save stream to file
urlHistory.BackColor = Color.LightGreen; // if file was created make BackColor LightGreen
// System.Diagnostics.Process.Start(selectedIndices); // remove comment and result can be seen in text editor
end1: ;
}
private void fillHistoryT(int j)
{
if (listIndices.SelectedItem == null) //formal control if any indice is selected
{
MessageBox.Show("Indice is not selected.", "No infomation"); // no
goto end1; // no action
}
string selectedIndices = listIndices.SelectedItem.ToString(); // get selected indice
selectedIndices = selectedIndices.Replace(":", "-"); // file name can not contain ":"
selectedIndices = workingPath + selectedIndices + ".txt"; // make full path,file name and add .txt extension as it is text file
if (File.Exists(selectedIndices))
{
List<string> fileLines = File.ReadAllLines(selectedIndices).ToList(); // read file to list filelines
int fL = fileLines.Count - 2; // newest date is two rows before end of file
DataTable historyTable = new DataTable(); // histroyTable declaration
historyTable.Columns.Add(new DataColumn("Date", typeof(string)));
historyTable.Columns.Add(new DataColumn("Open", typeof(string)));
historyTable.Columns.Add(new DataColumn("Close", typeof(string)));
historyTable.Columns.Add(new DataColumn("High", typeof(string)));
historyTable.Columns.Add(new DataColumn("Low", typeof(string)));
historyTable.Columns.Add(new DataColumn("Volume", typeof(string)));
historyTable.Columns.Add(new DataColumn("Avg.Vol.", typeof(string))); // avgDays days Volume
historyTable.Columns.Add(new DataColumn("Avg.Len.", typeof(string))); // avgDays days Length
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns(); // justify columns acording to header
for (int i=0; i<=j; i++) // be in loop for number of days asked (j)
{
string row = fileLines[fL-i]; // decrement file line number
string[] words = row.Split('"'); // words are splitted by " delimiter
historyTable.Rows.Add(words[0], words[1].ToString(), words[4].ToString(), words[2].ToString(),
words[3].ToString(), words[5].ToString()); // fill table accordingly to header line
Globals.ghistoryArray[i+1, 0] = words[0];
Globals.ghistoryArray[i + 1, 1] = words[1];
Globals.ghistoryArray[i + 1, 2] = words[2];
Globals.ghistoryArray[i + 1, 3] = words[3];
Globals.ghistoryArray[i + 1, 4] = words[4];
Globals.ghistoryArray[i + 1, 5] = words[5]; // volumen
Globals.ghtDays = Convert.ToString( i);
}
computeAverage(); // compute some auxiliary values
historyTable.Clear(); // prepare to refill with avarage data
historyTable.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0"); // empty row in data table
for (int i = 0; i < 8; i++) { Globals.ghistoryArray[0, i] = "0"; } // zero row in global array
for (int i = 1; i <= Convert.ToInt16(Globals.ghtDays); i++)
{
historyTable.Rows.Add(Globals.ghistoryArray[i, 0], Globals.ghistoryArray[i, 1], Globals.ghistoryArray[i, 2], Globals.ghistoryArray[i, 3], Globals.ghistoryArray[i, 4],
Globals.ghistoryArray[i, 5], Globals.ghistoryArray[i, 6], Globals.ghistoryArray[i, 7]);
}
historyGrid.DataSource = historyTable; // refresh grid content
historyGrid.AutoResizeColumns(); // adjust column width
}
else MessageBox.Show(selectedIndices + " file does not exist","Download needed");
end1: ;
}
private void fillHistoryTable_Click(object sender, EventArgs e)
{
fillHistoryT(Convert.ToInt16(fillHistoryTableDays.Text));
}
private void activeOnly_CheckedChanged(object sender, EventArgs e)
{
if (activeOnly.Checked == true) // make take appropriate to state
activeOnly.Text = "activeOnly"; // if checked
else activeOnly.Text = "all"; // in not checked
getIndicesL();
}
private void fillWorkingArrays(int k, int kC)
{
for (int m = 0; m <kC ; m++) // copy data of kC columns to array
{
if (historyGrid.RowCount <= k + kC-1) { // we need five more rows from selected
MessageBox.Show("No rows","Selection failed" );
goto end1; // cause to exit for loop
}
arrDay0[m] = historyGrid[m, k].Value.ToString(); // copy days in their arrays
arrDay1[m] = historyGrid[m, k+1].Value.ToString();
arrDay2[m] = historyGrid[m, k + 2].Value.ToString();
arrDay3[m] = historyGrid[m, k+3].Value.ToString();
arrDay4[m] = historyGrid[m, k+4].Value.ToString();
calC cc0 = new calC("1", Globals.ghistoryArray[k, m]); // convert string value to double i.e. arrays are declared as dobule
arrDay0d[m] = cc0.producT;
calC cc1 = new calC("1", Globals.ghistoryArray[k + 1, m]);
arrDay1d[m] = cc1.producT;
calC cc2 = new calC("1", Globals.ghistoryArray[k + 2, m]);
arrDay2d[m] = cc2.producT;
calC cc3 = new calC("1", Globals.ghistoryArray[k + 3, m]);
arrDay3d[m] = cc3.producT;
calC cc4 = new calC("1", Globals.ghistoryArray[k + 4, m]);
arrDay4d[m] = cc4.producT;
}
end1: ;
}
private void historyGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int r = e.RowIndex; // get a row index
zeroDay.Text = historyGrid[0, r].Value.ToString()+ " " + e.RowIndex.ToString() + "."; // put date in zeroDay field
fillWorkingArrays(r,8); // selected raw index will be 0 day -> "today". this is for lates market analyse.
analyseResults.Items.Clear(); // clear listbox
}
private string dayColor( string[] aR)
{
string mE = "Day color : "; // name of routine
string coloR = "Color is not calculated - ERROR" ; // color of the day, by defalt ERROR message which should be changed later
calC cC = new calC(aR[1], aR[2]); // class calC computes two strings and returns result as double
if (cC.diffFmS > 0) { coloR = Globals.blacK; } // if difference first minus second is bigger then zero then it is white day
if (cC.diffFmS < 0) { coloR = Globals.whitE; }
if (cC.diffFmS == 0) { coloR = Globals.nO; }
if (showDetails.Checked) { analyseResults.Items.Add(mE +aR[0] + " -> " + coloR); } // show result if checked
return coloR;
}
private void analyseDay_Click(object sender, EventArgs e)
{ // routine for testing all patterns
mBullishEngulfing(); // bullish pattern
}
private void computeAverage() // compute average volume, day length
{
int i; // auxiliary variable
int j1; // auxiliary variable
string aux; // auxiliary variable
string aux1; // auxiliary variable
int j = historyGrid.RowCount - Globals.avgDays; // last row to calculate
for (int k = 0; k < j; k++) // =<
{
j1 = k; // j1 is working row
sum2 = 0; // make sum zero
sum1 = 0; // make sum zero
for (i = 1; i <= Globals.avgDays && j1 <= j; i++) // make loop avgDays times
{
aux = historyGrid[Globals.volumeColumn, j1 + 1].Value.ToString(); // extract volume
calC cC = new calC(aux,"1"); // class calC computes two strings and returns result as double
sum2 = sum2 + cC.producT; // convert to double
aux = historyGrid[1, j1+1].Value.ToString(); // extract open price
aux1 = historyGrid[2, j1+1].Value.ToString(); // extract close price
calC cC1 = new calC(aux, aux1); // class calC computes two strings and returns result as double
sum1 = sum1 + cC1.abS; // sum
j1++; // increment
}
if ( k +1 < j) // after last loop no need to calculate
{
sum2 = Math.Round((sum2 / Globals.avgDays), 0); // compute averige based on avgDays (3)
sum1 = Math.Round((sum1 / Globals.avgDays), 2); // compute averige based on avgDays (3)
// historyTable.Rows[k][Globals.avgVolumeColumn] = sum2.ToString(); // add to history table
// historyTable.Rows[k][Globals.avgDayLengthColumn] = sum1.ToString(); // add to history table
Globals.ghistoryArray[k + 1, Globals.avgVolumeColumn] = sum2.ToString();
Globals.ghistoryArray[k + 1, Globals.avgDayLengthColumn] = sum1.ToString();
}
}
}
private void mBullishEngulfing() // bullish pattern Medium strong
{
string mE = " 111-M-Bullish Engulfing "; // name of the pattern
string directioN = Globals.growtH; // expected direction
mE = mE + directioN; // make whole name and direction
int sumA = 0; // init score
if (dayColor(arrDay0) == Globals.whitE) { sumA++; } // today is white day
if (dayColor(arrDay1) == Globals.blacK) { sumA++; } // yesterday is black day
biggeRsmaller bgR = new biggeRsmaller(arrDay1[2].ToString(), arrDay0[1].ToString());
if (bgR.bseN == Globals.fBts) { sumA++; } // close yesterday should be lower then open today
biggeRsmaller bgR1 = new biggeRsmaller(arrDay0[2].ToString(), arrDay1[1].ToString());
if (bgR1.bseN == Globals.fBts) { sumA++; } // close yesterday should be lower then open today
int critNo = 4; // number of criteria to meet
if (critNo==sumA) // if met
{ analyseResults.Items.Add(arrDay0[0] + mE); } // fill listbox
}
public void switchTo() {
new fPatterns().Show(); // prepare showing of form named fPatterns
}
private void patternsToolStripMenuItem_Click(object sender, EventArgs e)
{
switchTo(); // to switch to form named fPatterns
this.Hide(); // hide source form
}
private void button1_Click(object sender, EventArgs e)
{
// listIndices.SetSelected(2,true);
aboutDay aD = new aboutDay(arrDay0d);
if (aD.dayColor == Globals.whitE) { }
}
}
}
Code for form Patterns:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Stocker
{
public partial class fPatterns : Form
{
public int maxAreaTopPoint; // working array - today
public int maxAreaHeight;
DataTable historyTable = new DataTable(); // histroyTable declaration
public const int tailHeightDef = 90;
public const int tailWidhtDef = 2;
public const int bodyWidthDef = 20;
public const int bodyHeightDef = 50;
public const int bodyGap = 50;
public fPatterns()
{
InitializeComponent();
}
private void fPatterns_Load(object sender, EventArgs e)
{
this.BackColor = Color.LightGray; //FromName( "red");
historyTable.Columns.Add(new DataColumn("Date", typeof(string)));
historyTable.Columns.Add(new DataColumn("Open", typeof(string)));
historyTable.Columns.Add(new DataColumn("Close", typeof(string)));
historyTable.Columns.Add(new DataColumn("High", typeof(string)));
historyTable.Columns.Add(new DataColumn("Low", typeof(string)));
historyTable.Columns.Add(new DataColumn("Volume", typeof(string)));
historyTable.Columns.Add(new DataColumn("Avg.Vol.", typeof(string))); // avgDays days Volume
historyTable.Columns.Add(new DataColumn("Avg.Len.", typeof(string))); // avgDays days Length
historyTable.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0"); // empty row in data table
for (int i = 0; i < 8; i++) { Globals.ghistoryArray[0, i] = "0"; } // zero row in global array
for (int i = 1; i <= Convert.ToInt16(Globals.ghtDays); i++) // copy data from global array to local historyTable
{
historyTable.Rows.Add(Globals.ghistoryArray[i, 0], Globals.ghistoryArray[i, 1], Globals.ghistoryArray[i, 2],
Globals.ghistoryArray[i, 3], Globals.ghistoryArray[i, 4],
Globals.ghistoryArray[i, 5], Globals.ghistoryArray[i, 6], Globals.ghistoryArray[i, 7]);
}
historyGrid.DataSource = historyTable;
historyGrid.AutoResizeColumns();
// calculate drawing area below historyGrid
int fW = Screen.PrimaryScreen.WorkingArea.Width; // screen max width
int fH = Screen.PrimaryScreen.WorkingArea.Height; // screen max height
int dgH = historyGrid.Height + historyGrid.Top; // historyGrid is above graphics
int maxCdH = fH - dgH - 2 * MainMenuStrip.Height; // max drawing area height
int maxTop = dgH; // top point of drawing area - upper right corner is 0,0
maxAreaHeight = maxCdH;
maxAreaTopPoint = maxTop;
// candeles
tail4.Width = tailWidhtDef; //oldest
tail4.Height = tailHeightDef;
body4.Width = bodyWidthDef;
body4.Height = bodyHeightDef;
body4.Left = historyGrid.Left;
tail4.Left = body4.Left + (body4.Width / 2 - tail4.Width / 2);
tail3.Width = tailWidhtDef;
tail3.Height = tailHeightDef;
body3.Width = bodyWidthDef;
body3.Height = bodyHeightDef;
body3.Left = historyGrid.Left + bodyGap * 1;
tail3.Left = body3.Left + (body3.Width / 2 - tail3.Width / 2);
tail2.Width = tailWidhtDef;
tail2.Height = tailHeightDef;
body2.Width = bodyWidthDef;
body2.Height = bodyHeightDef;
body2.Left = historyGrid.Left + bodyGap * 2;
tail2.Left = body2.Left + (body2.Width / 2 - tail2.Width / 2);
tail1.Width = tailWidhtDef;
tail1.Height = tailHeightDef;
body1.Width = bodyWidthDef;
body1.Height = bodyHeightDef;
body1.Left = historyGrid.Left + bodyGap * 3;
tail1.Left = body1.Left + (body1.Width / 2 - tail1.Width / 2);
tail0.Width = tailWidhtDef; // candle for today
tail0.Height = tailHeightDef;
body0.Width = bodyWidthDef;
body0.Height = bodyHeightDef;
body0.Left = historyGrid.Left+bodyGap*4;
tail0.Left = body0.Left + (body0.Width / 2 - tail0.Width / 2);
}
public void switchTo() { new Form1().Show(); }
private void mainToolStripMenuItem_Click(object sender, EventArgs e)
{
switchTo();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0); // quit application
}
private void button3_Click_1(object sender, EventArgs e)
{
int a = mainToolStripMenuItem.Height;
body0.BackColor = Color.Black;
body0.Width = Globals.candleW;
body0.Top = 0 + a;
body0.Left = Convert.ToInt16(candleWidth.Text);
}
private void button4_Click(object sender, EventArgs e)
{
}
private void body0_Paint(object sender, PaintEventArgs e)
{
}
}
}
and Globals.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stocker
{
public static class Globals
{
public const string blacK = "Black"; // black constant
public const string whitE = "White"; // white constant
public const string yeS = "Yes"; // yes constant
public const string nO = "No"; // no constant
public const string fiRst = "First"; // First constant
public const string seCond = "Second"; // Second constant
public const string eqUal = "Equal"; // Equel constant
public const string neaR = "Near"; // NearEquel constant
public const int avgDays = 3; // number of days to count average values
public const int avgVolumeColumn = 6; // average volume column in history table
public const int avgDayLengthColumn = 7; // average day column in history table
public const int volumeColumn = 5; // volume column index
public const int candleW = 20;
public const double almostTheSame = 0.001; // if ratio is less then this, we consider them the same
public const string fEts = "fEts"; // first equal to second
public const string fBts = "fBts"; // first bigger then second
public const string fSts = "fSts"; // first smaller then second
public const string fNts = "fNts"; // first near to second
public const string falL = "FALL"; // fall constant
public const string growtH = "GROW"; // grow constant
public const string commaCountry=yeS; // if your country uses decimal comma change yeS to nO below
static string _strTest1;
public static string strTest1 // global variable
{ get { return _strTest1; } set { _strTest1 = value; } } // access routine for global variable static string _strTest1; static string _strTest1;
static string _ghtDays; // number of days in history table
public static string ghtDays // global variable
{ get { return _ghtDays; } set { _ghtDays = value; } } //access routine for global variable static string _ghtDays;
static string[,] _ghistoryArray=new string[90,8]; // global variable
public static string[,] ghistoryArray
{
get { return _ghistoryArray; } set { _ghistoryArray = value; } } // access routine for global variable
}
class makeHeader
{
public makeHeader()
{
this.succesS=Globals.yeS;
}
public string succesS {get;private set;}
}
class aboutDay
{
public aboutDay(double[] arra)
{
this.dayColor = ""; // Globals.whitE;
this.dayLengthDesc = "";
this.dayLengthNum = 0;
compareTwoNumbers ctn = new compareTwoNumbers(arra[1], arra[2]);
if (ctn.biggeRs==Globals.seCond) this.dayColor = Globals.whitE;
if (ctn.smalleRs==Globals.fiRst) this.dayColor = Globals.blacK;
if (ctn.equalS==Globals.yeS | ctn.equalFNearS==Globals.yeS) this.dayColor = Globals.nO;
}
public string dayColor { get; private set; }
public string dayLengthDesc { get; private set; }
public double dayLengthNum { get; private set; }
}
class stringToDouble
{
public stringToDouble(string valuE) // coverts string to double having in mind decimal sign
{
double mla; // init variable
mla = Convert.ToDouble(valuE); // convert with default function
if (valuE.Contains(".")) // check for decimal point
{
if (Globals.commaCountry == Globals.yeS) // check if we use decimal comma
{ mla = Convert.ToDouble(valuE.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mla = Convert.ToDouble(valuE); } // convert string to double
this.doublE = mla; // return values
}
public double doublE { get; private set; }
}
class calC
{
public calC(string firsT, string seconD) // calC class - input are two numbers as string - string to double for countries
{ // using decimal comma
double mlaF; // auxiliary number for the first day
double mlaS; // auxiliary number for the second day
if (firsT == null | seconD == null) goto kraj;
if (firsT.Contains(".")) // if string contains point
{
if (Globals.commaCountry == Globals.yeS) // if country uses decimal comma
{ mlaF = Convert.ToDouble(firsT.Replace(".", ",")); } // and if you use decimal comma we need fix
}
else
{ mlaF = Convert.ToDouble(firsT); } // convert string to doubleif (firsT.Contains("."))
if (seconD.Contains(".")) // the same as above for first number
{
if (Globals.commaCountry == Globals.yeS)
{ mlaS = Convert.ToDouble(seconD.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mlaS = Convert.ToDouble(seconD); } // convert string to doubleif (firsT.Contains("."))
double mlaPro = Math.Round( mlaF * mlaS,2); // product rounded to two decimal places
double mlaDiffFirstMinSecond = Math.Round(mlaF - mlaS,2); // difference first minus second rounded to two decimal places
double mlaSum = Math.Round(mlaF + mlaS,2); // first plus second rounded to two decimal places
double mlaabS = Math.Round (Math.Abs(mlaF - mlaS),2) ; // difference first minus second rounded to two decimal places
biggeRsmaller bgR = new biggeRsmaller(firsT, seconD);
double c = Math.Abs((mlaF - mlaS) / bgR.biggeR);
this.producT = mlaPro; // define properties
this.diffFmS = mlaDiffFirstMinSecond;
this.sumFS = mlaSum;
this.abS = mlaabS;
kraj: ;
}
public double producT { get; private set; }
public double diffFmS { get; private set; }
public double sumFS { get; private set; }
public double abS { get; private set; }
}
class biggeRsmaller // compares two numbers as string
{
public biggeRsmaller(string f, string s) // (f)irst and (s)econd
{
stringToDouble ca = new stringToDouble(f); // convert string to number having in mind decimal comma
double a = ca.doublE; // a is first result
stringToDouble cb = new stringToDouble(s);
double b = cb.doublE; // b is second result
double sM = 0; // init variable
if (a < b) { sM = b; this.biggeR = b; this.biggeRs = s; this.smalleR = a; this.smalleRs = f; this.bseN = Globals.fSts; }
if (a > b) { sM = a; this.smalleR = b; this.smalleRs = s; this.biggeR = a; this.biggeRs = f; this.bseN = Globals.fBts; }
if (this.biggeR == 0) { this.biggeR = a; }
double c = Math.Abs((a - b) / this.biggeR);
if (c <= Globals.almostTheSame) { this.equalNearS = Globals.yeS; } // first near to second i.e. almost the same
if (a == b) { sM = b; this.equalS = Globals.yeS; }
}
public double biggeR { get; private set; }
public double smalleR { get; private set; }
public string biggeRs { get; private set; }
public string smalleRs { get; private set; }
public string equalS { get; private set; }
public string equalNearS { get; private set; }
public string bseN { get; private set; }
}
class compareTwoNumbers // compares two numbers as string
{
public compareTwoNumbers(double f, double s) // (f)irst and (s)econd
{
double a = f;
double b = s;
if (a < b) { this.biggeR = b; this.biggeRs = Globals.seCond; this.smalleR = a; this.smalleRs = Globals.fiRst; this.equalFNearS = Globals.nO; this.equalS = Globals.nO; }
if (a > b) { this.smalleR = b; this.smalleRs = Globals.seCond; this.biggeR = a; this.biggeRs = Globals.fiRst; this.equalFNearS = Globals.nO; this.equalS = Globals.nO; }
// if (this.biggeR == 0) { this.biggeR = a; }
double c = Math.Abs((a - b) / this.biggeR);
if (c <= Globals.almostTheSame) { this.equalFNearS = Globals.yeS; this.equalS = Globals.nO; } // first near to second i.e. almost the same
if (a == b) { this.equalS = Globals.nO; this.equalFNearS = Globals.yeS; this.biggeRs = Globals.nO; this.smalleRs = Globals.nO; }
}
public double biggeR { get; private set; }
public double smalleR { get; private set; }
public string biggeRs { get; private set; }
public string smalleRs { get; private set; }
public string equalS { get; private set; }
public string equalFNearS { get; private set; }
}
}
Friday, 25 May 2012
19. Trader - How to draw rectangle?
Soon, we'll need rectangle as part of candle construction. Here is the simple way of how to draw rectangle.
Code under button1:
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics(); // invoke graphics
SolidBrush solidBrush = new SolidBrush(Color.Pink); // choose drawing tool in pink color
g.FillRectangle(solidBrush, 100, 100, 100, 200); // draw rectangle with top coordinates and dimensions
}
Code under button1:
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics(); // invoke graphics
SolidBrush solidBrush = new SolidBrush(Color.Pink); // choose drawing tool in pink color
g.FillRectangle(solidBrush, 100, 100, 100, 200); // draw rectangle with top coordinates and dimensions
}
Subscribe to:
Posts (Atom)