Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9112907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:50:52+00:00 2026-06-17T03:50:52+00:00

I don’t think this little piece of code I’m working on has any practical

  • 0

I don’t think this little piece of code I’m working on has any practical application, but I’m trying to wrap my head around async and seem to be struggling a bit. Let’s say I want to pull historical stock price data from Yahoo, save all the data to a single csv file, and then load it into SQL Server using bulk copy. I’m not too worried about loading the data into SQL Server, but I’m wondering how to write the data to a new csv file. Can/should it be done asynchronously?

As far as I know there is no way to grab the ticker as part of the stream when grabbing the historical data, so I am grabbing the stream and mapping it to a new list with the ticker appended to the front of each item. From time to time when I run a test I’ll get a record without a ticker and there will be a record with multiple tickers (eg, “MSFT, YHOO”).

So, my question is, how can I dump this data into a single csv file without causing issues? Secondarily, when I split the data I’m getting an empty tail item. What’s the best method for dropping that?

Like I said, I don’t know that this has any practical application, but I’m trying to learn, so I hope you’ll pardon my ignorance. Thanks for any help, I really appreciate it. Here’s what I have:

open System
open System.IO
open System.Web
open System.Net

let fromDate = new DateTime(2013, 1, 1)

let getTickers = 
    "MSFT" :: "YHOO" :: []

let getData (ticker : string) = 
    async {
        let url = System.String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&g=d&ignore=.csv&a={1}&b={2}&c={3}", ticker, fromDate.Month - 1, fromDate.Day, fromDate.Year)

        Console.WriteLine(url)

        let req = WebRequest.Create(url)
        let! rsp = req.AsyncGetResponse()
        use stream = rsp.GetResponseStream()
        use reader = new StreamReader(stream)

        let lines = 
            reader.ReadToEnd().Split('\n')
            |> Seq.skip 1 // skip header
            |> Seq.map (fun line-> (String.Format("{0}, {1}", ticker, line.ToString())))

        Seq.iter (fun x->printfn "%s" (x.ToString())) lines
        ()
    }

let z = 
    getTickers 
    |> List.map getData
    |> Async.Parallel
    |> Async.RunSynchronously
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:50:53+00:00Added an answer on June 17, 2026 at 3:50 am

    IMO, this is overdone, but hopefully it demonstrates what you want to know:

    open System
    open System.IO
    open System.Net
    
    let tickers = 
      [ 
        "MSFT"
        "YHOO"
      ]
    
    let getData (writer: TextWriter) ticker =
      async {
        let url = sprintf "http://ichart.finance.yahoo.com/table.csv?s=%s&g=d&ignore=.csv" ticker
        let req = WebRequest.Create(url)
        let! resp = req.GetResponseAsync() |> Async.AwaitTask
        use stream = resp.GetResponseStream()
        use reader = new StreamReader(stream)
        let! lines = reader.ReadToEndAsync() |> Async.AwaitTask
        let lines = 
          lines.Split('\n')
            |> Seq.skip 1
            |> Seq.filter ((<>) "") //skip empty lines
        for line in lines do
          do! writer.WriteLineAsync(String.Format("{0}, {1}", ticker, line)).ContinueWith(ignore) |> Async.AwaitTask
      }
    
    let writeAllToFile path =
      use writer = new StreamWriter(path=path)
      tickers
        |> Seq.map (getData writer)
        |> Async.Parallel
        |> Async.RunSynchronously
        |> ignore
    
    writeAllToFile @"C:\quotes.csv"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know why, but this code worked for me a month ago... maybe
Don't think my virtualhost is working correctly. This is what I have inside of
Don't know why, but sometimes LocationManager is still working also after closing application. I
Don't know if this is possible, but I have some code like this: val
Don't know if I'm over-thinking this or not.. but I'm trying to be able
don't know better title for this, but here's my code. I have class user
Don't know how to frase this but I found this code wich works as
(Don't know if this is strictly on-topic, but I don't see any better Stack
don't know if the title describes anything about what I'm trying to say but
Don't ask why, but is there any way to suppress a failed linking error?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.