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
        }

Tuesday, 22 May 2012

18. Trader - How convert Method to Class?

How convert Method to Class in C#? Application will be better organised if same type of work is grouped. stringToDouble method is in Form, but it is needed to be used by public classes in Globals.cs. It could be moved, only, if it is converted to Class. After conversion to Class, stringToDouble is accessible from whole application.

As Method:
 private double stringToDouble(string valuE)
        {
            double mla;
            mla = Convert.ToDouble(valuE);
            if (valuE.Contains("."))
            {
                if (Globals.commaCountry == Globals.yeS)
                { mla = Convert.ToDouble(valuE.Replace(".", ",")); }  // but if you use decimal comma we need fix
            }
            else
            { mla = Convert.ToDouble(valuE); } // convert string to double
            return mla;
        } 


As Class:
  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; }
    }


How to use as Class:
       stringToDouble ca = new stringToDouble(f);
            double a = ca.doublE;
   

Monday, 21 May 2012

17. Trader - New class calC

It seems that some additional tuning is needed before continue to program. The issue is that Croatia uses decimal comma and conversion string to double using Convert.ToDouble does not work fine since decimal comma is part of string. Solution with using System.Globalization; did not provide satisfactory results when converting to Double (much faster then Decimal). So, commaCountry constant string was introduced to define if we use comma as decimal sign.
After, conversion string to double is done, some additional math is done: product, sum and difference. It was an excellent opportunity to use class and its properties to return results. Bellow is difference between old and new version of routine dayColor.
Class calC is part of Globals.


 class calC
    {
        public calC(string firsT, string seconD)        // calC class - input 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.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);               // product rounded to two decimal places
            double mlaDiffFirstMinSecond = Math.Round(mlaF - mlaS); // difference first minus second rounded to two decimal places
            double mlaSum = Math.Round(mlaF + mlaS); //  first plus second rounded to two decimal places
            this.producT = mlaPro;                      // define properties
            this.diffFmS = mlaDiffFirstMinSecond;
            this.sumFS = mlaSum;
        }
        public double producT { get; private set; }
        public double diffFmS { get; private set; }
        public double sumFS { get; private set; }
    }


How is called;

old:
   private string dayColor( string[] aR)
        {
            string mE = "Day color : ";              // name of routine
            string coloR;                           // color of the day
            double openP;                           // opening price
            double closeR;                          // closing price
            openP = stringToDouble(aR[1]);          // covert string to double
            closeR=stringToDouble(aR[2]);
            double closeMinusOpen = closeR - openP; // covert string to double
            if (closeMinusOpen > 0)                 // is close price bigger then oper
            { coloR = Globals.whitE; }                                // yes, white day
            else if (closeMinusOpen < 0) { coloR = Globals.blacK; } // no, black day
            else { coloR = Globals.nO; }                            // both prices are the same -> no color
            if (showDetails.Checked) { analyseResults.Items.Add(mE  +aR[0] + " -> " + coloR); }
            return coloR;
        }

new:

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]);           // calC class computes two strings and returns double
            if (cC.diffFmS > 0) { coloR = Globals.blacK; }
            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;
        }