Click here to Skip to main content
15,886,857 members
Home / Discussions / C#
   

C#

 
AnswerRe: Synchronization csv file to SQL Server 2008 R2 Database Pin
Richard MacCutchan9-Jul-13 21:34
mveRichard MacCutchan9-Jul-13 21:34 
AnswerRe: Synchronization csv file to SQL Server 2008 R2 Database Pin
Mycroft Holmes9-Jul-13 22:49
professionalMycroft Holmes9-Jul-13 22:49 
QuestionTransactionScope in a sub class? Pin
Dominik9-Jul-13 10:36
Dominik9-Jul-13 10:36 
AnswerRe: TransactionScope in a sub class? Pin
Eddy Vluggen10-Jul-13 1:03
professionalEddy Vluggen10-Jul-13 1:03 
GeneralRe: TransactionScope in a sub class? Pin
Dominik11-Jul-13 3:04
Dominik11-Jul-13 3:04 
Questionadd yesterdays value to todays empty value Pin
lordoftrades9-Jul-13 9:53
lordoftrades9-Jul-13 9:53 
AnswerRe: add yesterdays value to todays empty value Pin
Eddy Vluggen10-Jul-13 1:03
professionalEddy Vluggen10-Jul-13 1:03 
GeneralRe: add yesterdays value to todays empty value Pin
lordoftrades10-Jul-13 1:24
lordoftrades10-Jul-13 1:24 
Thanks Eddy, I do not have much experience in programming in C#, so I'm on thin ice here. But I do not have any SQL related to this. The case is that I'm importing a file from Yahoo Finance, it's a stock, where I have set start-date and end-date.

My problem is that I also need to fill in all the missing days, like holidays and weekends etc. And I need to "copy" the last recorded value to the missing days that follows.

As for now I've just added "0" to those, because I should be able to create a formula later on that will detect this, but it would be much more clean if I could add quotes instead of just a zero.

This is my code (I guess it's pretty messy...):

public override void OnShutdown()
        {
       	string[] row = new string[7];
       	double Close;
       	string currentLine;
       	var dates_in_IQB = new List<DateTime>();
       	int i = 0;
       
       	DateTime endDate =new DateTime(DateTimeEnd());
    	DateTime startDate = new DateTime (DateTimeStart());
                                   
           for (var x = startDate; x <= endDate; x = x.AddDays(1)){
           //This loop to get a complete list of dates and values 
                       	i = i + 1;
                       	dates_in_IQB.Add(x);
                       	double equity = AccountHistoryEquity(0, i);
                                               
                                   
OutputWriteLine(String.Format("Date {0}, Equity {1}",x ,equity)); //Complete list of 365 days pr year
									   
if(Directory.Exists(_filePath)){  //Checking if the path exists...
string lines =  String.Format("{0}, {1}", x, equity); //Writing the daily equity changes for all accounts...
// Write the string to a file.                                                                                                 System.IO.StreamWriter file = new System.IO.StreamWriter(_filePath +"\\ref1.txt", true);{
file.WriteLine(lines, i);
file.Close();
}
}
   }
                                           
           DataTable dt = new DataTable();
           dt.Columns.Add(new DataColumn("date", typeof(DateTime)));
           dt.Columns.Add(new DataColumn("close", typeof(double)));
               
           string theURL = @"http://ichart.finance.yahoo.com/table.csv?s=msft" 
                + @"&a=" + (startDate.Month - 1).ToString() 
                + @"&b=" + startDate.Day.ToString() 
                + @"&c=" + startDate.Year.ToString() 
                + @"&d=" + (endDate.Month - 1).ToString() 
                + @"&e=" + endDate.Day.ToString() 
                + @"&f=" + endDate.Year.ToString() 
                + @"&g=d&ignore=.csv";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(theURL);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream resStream = response.GetResponseStream();
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];
            string tempString = null;
            int count = 0;

            do
            {
                count = resStream.Read(buf, 0, buf.Length);
                if (count != 0)
                {
                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                    sb.Append(tempString);
                }
            }
            while (count > 0);

            string data = sb.ToString();
            String[] data2 = data.Split('\n');
            int iteration = 0;
            double[] open;
            double[] high;
            double[] low;
            double[] close;
            double[] volume;
            double[] adjClose;

            DateTime[] dates = new DateTime[data2.Length - 2];
            open = new double[data2.Length - 2];
            high = new double[data2.Length - 2];
            low = new double[data2.Length - 2];
            close = new double[data2.Length - 2];
            volume = new double[data2.Length - 2];
            adjClose = new double[data2.Length - 2];

            foreach (string strLine in data2)
            {
                String[] entries = strLine.Split(',');
                if ((iteration != 0) && (entries[0] != ""))
                {
                    dates[iteration - 1] = System.Convert.ToDateTime(entries[0]);
                    open[iteration - 1] = System.Convert.ToDouble(entries[1]);
                    high[iteration - 1] = System.Convert.ToDouble(entries[2]);
                    low[iteration - 1] = System.Convert.ToDouble(entries[3]);
                    close[iteration - 1] = System.Convert.ToDouble(entries[4]);
                    volume[iteration - 1] = System.Convert.ToDouble(entries[5]);
                    adjClose[iteration - 1] = System.Convert.ToDouble(entries[6]);
                                                           
    dt.Rows.Add(new object[] {dates[iteration - 1], close[iteration - 1]}); //Writing from yahoo to datatable
					

// OutputWriteLine(String.Format("Date {0}, Close {1}", dates[iteration - 1], close[iteration - 1]));
                }
  iteration = iteration + 1;
            }
                                   
        // Code to lookup missing dates in the datas from Yahoo, and make it ready for comparisement with IQB's data
        DateTime minDT = dt.Rows.Cast<DataRow>().Min(row1 => (DateTime)row1["date"]);
        DateTime maxDT = dt.Rows.Cast<DataRow>().Max(row1 => (DateTime)row1["date"]);
		

        // Create all the dates that should be in table
        List<DateTime> dts = new List<DateTime>();
        DateTime DT = minDT;
        while (DT <= maxDT)
        {
            dts.Add(DT);
            DT = DT.AddDays(1);
        }
            // Find the dates that should be in table but aren't
        var DTsNotInTable = dts.Except(dt.Rows.Cast<DataRow>().Select(row1 => (DateTime)row1["date"]));

	
        foreach (DateTime dateTime in DTsNotInTable)
		
        dt.Rows.Add(dateTime, 0); //ADDING A ZERO TO THE STOCK QUOTE WHEN A MISSING DAY IS ADDED
		
        // Order the results collection
        var ordered = dt.Rows.Cast<DataRow>().OrderBy(row1 => (DateTime)row1["date"]);

        // Create a DataTable object where missing dates are filled in together with close-values = 0
        DataTable dt2 = ordered.CopyToDataTable();
                        
                        
                                   foreach (DataRow row1 in dt2.Rows) // Loop over the rows.
                                   {
OutputWriteLine(String.Join(",", row1.ItemArray));
									   
string lines =  (String.Join(",", row1.ItemArray)); //Writing the daily equity changes for all accounts...
// Write the string to a file.                                                                                                   
System.IO.StreamWriter file = new System.IO.StreamWriter(_filePath +"\\ref1.txt", true);{
file.WriteLine(lines);
file.Close();
 }
					//***************************************************************************************
								   }
								   
        }

Kind regards
Espen

GeneralRe: add yesterdays value to todays empty value Pin
Eddy Vluggen10-Jul-13 6:40
professionalEddy Vluggen10-Jul-13 6:40 
GeneralRe: add yesterdays value to todays empty value Pin
lordoftrades12-Jul-13 2:23
lordoftrades12-Jul-13 2:23 
QuestionLPARAM value of the usb device Pin
Blubbo9-Jul-13 6:41
Blubbo9-Jul-13 6:41 
AnswerRe: LPARAM value of the usb device Pin
Richard MacCutchan9-Jul-13 10:13
mveRichard MacCutchan9-Jul-13 10:13 
GeneralRe: LPARAM value of the usb device Pin
Blubbo9-Jul-13 10:27
Blubbo9-Jul-13 10:27 
GeneralRe: LPARAM value of the usb device Pin
Richard MacCutchan9-Jul-13 10:50
mveRichard MacCutchan9-Jul-13 10:50 
AnswerRe: LPARAM value of the usb device Pin
Alan N10-Jul-13 4:06
Alan N10-Jul-13 4:06 
QuestionSqlLocalDB in Visual Studio2010 &2012 Pin
dharmaa.m9-Jul-13 2:35
dharmaa.m9-Jul-13 2:35 
AnswerRe: SqlLocalDB in Visual Studio2010 &2012 Pin
Dave Kreskowiak9-Jul-13 4:06
mveDave Kreskowiak9-Jul-13 4:06 
QuestionReason for no display of data in the pdf. Pin
Mausam Bharati9-Jul-13 1:41
Mausam Bharati9-Jul-13 1:41 
AnswerRe: Reason for no display of data in the pdf. Pin
Manfred Rudolf Bihy9-Jul-13 2:25
professionalManfred Rudolf Bihy9-Jul-13 2:25 
GeneralRe: Reason for no display of data in the pdf. Pin
Mausam Bharati9-Jul-13 20:58
Mausam Bharati9-Jul-13 20:58 
Questionlistview Pin
Member 99613129-Jul-13 0:56
Member 99613129-Jul-13 0:56 
AnswerRe: listview Pin
Eddy Vluggen9-Jul-13 3:07
professionalEddy Vluggen9-Jul-13 3:07 
AnswerRe: listview Pin
Jay Nardev11-Jul-13 21:01
Jay Nardev11-Jul-13 21:01 
AnswerRe: listview Pin
Kevin Bewley15-Jul-13 5:41
Kevin Bewley15-Jul-13 5:41 
QuestionMVC Grid Pin
_BrijeshSingh8-Jul-13 23:37
_BrijeshSingh8-Jul-13 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.