Saturday, 27 July 2013

32 - C# dynamic drawing

New form added which shows how C# could be used for drawing graphs and diagrams. This form uses panels, labels and lineshapes to represent stack market data and analysing tools: Japanese candlesticks, 2 and 13 days Force indexes plus MACD histogram. I hope form is not too crowdy with stock information and easy readable (small rectangles are hidden, p.visible=false, in production version. See below.). During daily trading form is updated with latest stock information and graph is after computation updated accordingly.


In production, analyzing Dow Jones Industrial Average for 120 days.




Form looks like this. Black rectangle remains from testing phase. All other elements (controls) are handled dynamically. Change of labels positions (top property) define new drawing area(s). During run-time labels are set to visible=false.



How to add Label control in C#?

First, this is method to add one panel.

 private void addHistPanel(double[] panelParametri) // dodaj hist panel
        {
            Panel p = new Panel();              // new panel
            p.BackColor = Color.Black;          // by default it is black
            p.Width = sirinaHistPanel;          // sted width
            p.Height = Convert.ToInt16(Math.Abs(panelParametri[1])*koefHistVisine());   // set height - po visini do maksimuma
            p.Left = lijevaGranicaHist + Convert.ToInt16(panelParametri[0]) * (sirinaHistPanel+razmakHist); // the most left panel
            p.BorderStyle = BorderStyle.FixedSingle;            // add some fancy line arrond panel
            if (panelParametri[1] > 0) { p.Top = middlePodrucjaHist - p.Height; p.BackColor = Color.Green;  // set its color if positive
            if (p.Top > topGranicaHist) {   // za iznad crte // above zero line
                if ( iznadGranice < p.Top - topGranicaHist){    // also, we are looking for heighest top
                    iznadGranice = p.Top - topGranicaHist;  // nadji naj iznad
                    }
            }  
            } // za ispod crte // bellow line - it means negative value
            else { p.Top = middlePodrucjaHist; p.BackColor = Color.Red;
                if (p.Top + p.Height > downGranicaHist)
                {
                    if (ispodGranice < Math.Abs(p.Top + p.Height - downGranicaHist)) {
                        ispodGranice = p.Top + p.Height - downGranicaHist;
                    }    // traži najispod also, we are looking for lowest top
                    }   
            }         
            Controls.Add(p);    // add panel
        }


This is method which adds all panels in the loop:

 private void dodajHistPanele(double[,] radni)       // two dimensional array has all panel information
         {
             double[] parametar = new double[2];
             for (int i = brStupaca; i >= 0; i--)       // for all columns
             {
                 parametar[0] = brStupaca - i;   // 0 positive or negative;
1 -panel.height                 
                 parametar[1] = radni[i, columnHistogram];  // input for add panel method is one dimensional array with two items
                 addHistPanel(parametar);                   // add panel passing array
             }
         }


Discrete values of Force indexes are shown with small panels. Panel.top(s) are connected with lines called LineShape.

How to add LineShape control in C#?

fi13Lines is two dimensional array which holds about points which should be connected. Actually, these are panel's tops of small rectangles saved in array during p.tops calculation.

void addLineFi13()
        {   //ShapeContainer canvaS;   // LineShape needs ShapeContainer which we'll call canvaS
            canvaS.Parent = this;                       // add canvaS to form
            for (int i = brStupaca; i > 0; i--)
            {
                LineShape ls = new Microsoft.VisualBasic.PowerPacks.LineShape();    // new LineShape
                ls.Parent = canvaS;         // belongs to canvaS
                ls.BorderWidth = 2; // set how much line is thick
                ls.BorderColor = Color.Purple;  // set color
                ls.StartPoint = new System.Drawing.Point(fi13Lines[i, 0], fi13Lines[i, 1]);     // line Star point
                ls.EndPoint = new System.Drawing.Point(fi13Lines[i - 1, 0], fi13Lines[i - 1, 1]);   // line End point
                ls.BringToFront();  // bring it to front - not needed basically
                canvaS.BringToFront(); // bring it to front - not needed basically 
            }
        } 

Monday, 1 October 2012

31. Japanese candlestiscks and C# - trading application analyze

This post is not about C# 4.0 programming itself but what have been done so far. Please keep in mind I'm IT guy  who is learning C# 4.0 (I'm experienced in VB and VBscript ) and how trading world works in parallel.
There is one more form accessible which (from Main in menu bar or Trader 2012 item in 11) covers trader's portfolio (probably the most important part of trading ;-) ). Data are stored in Access database and managed by SQL. Also, on this form hidden frame(at the moment) can be invoked  where trading transactions can be entered. So, this is only part of whole functionality but still enough to demonstrate C# 4.0 against Japanese candlesticks, trading volumes, three days flag and RSI. Development is in progress and new options will be added.



1. Table with history trading information which could be entered manually or pulled/filled from Internet. Also, if I click on certain date in the past, day analyze of that row date will happen. Candles will be redrawn and new findings will be placed in 3. It is useful to test trading strategies.

2. During trading day I can follow more shares which can be seen in 12. So, if I hit "timer Stopped" (it will change his label to "timer Started" and back color to red) button program will pull data (price) form Internet in a pace defined in field before "sec." label. Next field defines time of a day after which automatic update of trading information will stop.

3. Findings according to Japanese candlesticks and volumes for trading day. Back color is the same as for "Conclusion" field in 6.

4. Findings from previous trading day for easier trading decisions, since trading is field full of explosive devices which can blow out money.

5. During trading day field in green (it means present prices is bigger then previous) fills last row in table 1 in appropriate, in case I enter data manually.  Two next fields are prices to Sell/Buy with commission. Two fields bellow represents Bid and Ask prices. These fields are filled/calculated if automatic pulling is chosen.

6. Actually this puts this program to an expert system. It consider three (at the moment) parameters and suggest action. At the moment, he says "Do not buy" ("Do not sell", "Buy", "Sell" or "Unknown" are other options) since price suggest buying action. Back color is yellow and could be white, green or red depending on suggested action.

7. These are live transactions with zero and one per cent profit. If selected (by application according to actual price) it means it could be sold with profit. Otherwise, no profit.

8. These are transaction from the past so I can follow what I did in past. These are very useful if kept up to date. Frame which maintains data of 7. and 8. can be invoked by Menu-> Ledger or button "Ledg/stats".

9. RSI as described in previous chapter. It is automatically recalculated by change of green field in 5. It can be used to test RSI limits with no influence to actual data.

10. This is Japanese candlestick chart with dates and volume bars.

11. It is list with active forms (indexes) at the moment and if item is selected I jump to corresponding form/share to see full details of that index/share. I'm not in CZ but in HR and shares as well (of course DAX is German index). This is mistake from Internet but no influence on nothing of importance.

12. This is summary status for all active shares. It is updated automatically when green field (Price) is changed on any listed/active form, active or in background.

13.  This is reach text field for comments for specific share index.

14. This is list with different prices and indexes based on yesterday's average price. Selections are based on minimum and maximum prices for previous six days.

As mentioned, other functionality exists but since programming / learnig is in progress there will be time to present them.


Sunday, 30 September 2012

30. Japanese candlestiscks and C# - Two dimensional array

How to calculate RSI (relative strength index)?

Now, application analyses Japanese candlesticks, volumes and RSI to advise trading action. 

We'll use 2D array to calculate RSI value. If you are interested in theory of RSI please read this

http://en.wikipedia.org/wiki/Relative_strength_index

and related

http://en.wikipedia.org/wiki/Moving_average_(finance)#Simple_moving_average (SMA chapter)

Under RSI button is code which uses  values of these Excel calculation example

http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi

It is worth to have a look how it works in Excel, first.

So, now you can easier follow code. 2D table does not use all 8 columns but I did it so to be more easier to follow Excel example. Number of array rows is variable and type of data is double.

Under commented lines is code which is used for daily live trading analysis. Variable noDays is set to 14 as in Excel example. On your form you will need one button (any name) and text field rsItext. Of course, you can use string variable and change code to

string rsIstring== rsI.ToString(); // fill last value to string

and you'll need debugger to see result or you can use line such as this

 MessageBox.Show("RSI value", rsIstring)

Also, if you are interested in RSI and how it changes with different number of days (traders use different values according to their trading goals for example 2,..9,14,20)  then you can add text filed rsidayS to form and delete in bold line code( // also). With rsidayS filed change RSI will change, also.

noDays = 14; // noDays = Convert.ToInt16(rsidayS.Text);

Great example for debugger usage. :-)





And code:

    private void rsI_Click(object sender, EventArgs e)
        {   // how to calculate RSI - Relative strength index
            int noRows = 44;                            // number or days to look back
            double[,] rsiTable=new double[noRows,9];    // two dimensional array declaration with noRows rows
            rsiTable[1, 1] = 43.13; // 1 means today - table filled with test data from
            rsiTable[2, 1] = 42.66; // http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi
            rsiTable[3, 1] = 43.42;
            rsiTable[4, 1] = 44.57;
            rsiTable[5, 1] = 44.22;
            rsiTable[6, 1] = 44.18;
            rsiTable[7, 1] = 44.03;
            rsiTable[8, 1] = 45.35;
            rsiTable[9, 1] = 45.78;
            rsiTable[10, 1] = 46.45;
            rsiTable[11, 1] = 45.71;
            rsiTable[12, 1] = 46.25;
            rsiTable[13, 1] = 46.21;
            rsiTable[14, 1] = 45.64;
            rsiTable[15, 1] = 46.22;
            rsiTable[16, 1] = 46.41;
            rsiTable[17, 1] = 46.03;
            rsiTable[18, 1] = 46.00;
            rsiTable[19, 1] = 46.28;
            rsiTable[20, 1] = 46.28;
            rsiTable[21, 1] = 45.61;
            rsiTable[22, 1] = 46.03;
            rsiTable[23, 1] = 45.89;
            rsiTable[24, 1] = 46.08;
            rsiTable[25, 1] = 45.84;
            rsiTable[26, 1] = 45.42;
            rsiTable[27, 1] = 45.10;
            rsiTable[28, 1] = 44.83;
            rsiTable[29, 1] = 44.33;
            rsiTable[30, 1] = 43.61;
            rsiTable[31, 1] = 44.15;
            rsiTable[32, 1] = 44.09;
            rsiTable[33, 1] = 44.34;
     /*       for (int m = 0; m < noRows-1; m++)              // load from live data table for number of days
            {
                rsiTable[m + 1, 1] = historyArrayL[m, 2];
            }       */
            int noDays = 0;
            int firstDayForAverage = 19;
            int lastDayForAverage = 0;
            double averageGainSum=0;
            double averageLossSum=0;
            double gainAverage=0;
            double lossAverage=0;
            double rS=0;
            double rsI=0;
            double deltA = 0;
            noDays = 14; // noDays = Convert.ToInt16(rsidayS.Text);
            if (noDays * 2 + 2 > noRows) { MessageBox.Show("Too many days", "Maximum is 20"); goto kraj; }  // we have 44 days to examine
            for (int k=gloB.rowSelected+1;k<noRows-1;k++)   // calclulate loss
            {
                deltA=Math.Round(rsiTable[k,1]-rsiTable[k+1,1],2);    // close yesterday - close today
                if (deltA > 0) { rsiTable[k,3]=deltA; } // delta gain fill column 3
                if (deltA < 0) { rsiTable[k, 4] =  Math.Abs(deltA); }   // delta loss fill column for as positive value
            }
            firstDayForAverage=firstDayForAverage+gloB.rowSelected; // calculate first average day index
            lastDayForAverage=firstDayForAverage+noDays-1;      // calculate last average day index
            if (lastDayForAverage >= noRows) { MessageBox.Show("Too far in the past", "Maximum is 20"); goto kraj; } // if we are not in range
           
            for (int k = firstDayForAverage; k <= lastDayForAverage; k++)
            {
                averageGainSum = averageGainSum + rsiTable[k, 3]; averageLossSum = averageLossSum + rsiTable[k, 4]; // for numebr of days
            }
            gainAverage = Math.Round(averageGainSum / noDays,3);    //
            lossAverage = Math.Round(averageLossSum / noDays,3); // average of 20th day is calculated
            rsiTable[firstDayForAverage,5]=gainAverage;   // average gain column 5
            rsiTable[firstDayForAverage,6]=lossAverage; // average loss column 6
            if (lossAverage > 0) { rS = gainAverage / lossAverage; } else { rS = 100; }     // calc. loss avarege - value 100 is by definiton
                rsiTable[firstDayForAverage, 7] = Math.Round(rS,2);   // RS column 7
                if (rsiTable[firstDayForAverage, 6] == 0) { rsiTable[firstDayForAverage, 8] = 100; }  // calc. of first RSI value (column 8 of nth day)- value 100 is by definiton
                else{rsiTable[firstDayForAverage, 8] = Math.Round(100-(100/(1+rsiTable[firstDayForAverage, 7])) ,2);} // RSI column 8
           
            for (int k = firstDayForAverage -1; k > 0; k--)     // calculte other RSI valuse
            {
                gainAverage = Math.Round((rsiTable[k + 1, 5] * (noDays - 1) + rsiTable[k, 3]) / noDays, 3);  // previous and today  day
                lossAverage = Math.Round((rsiTable[k + 1, 6] * (noDays - 1) + rsiTable[k, 4]) / noDays,3);
                rsiTable[k, 5] = gainAverage;       // fill column 5
                rsiTable[k, 6] = lossAverage;
                rS = rsiTable[k, 5] / rsiTable[k, 6];
                if (rsiTable[k, 6] == 0) { rsI = 100; } else { rsI = Math.Round(100 - 100 / (1 + rS), 2); rsiTable[k, 8] = rsI; }   // calclulate RSI
            }

            rsItext.Text = rsI.ToString(); // fill last value to field rsIText
        kraj: ;
        }