Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. Public Function GetQuoteRange(ByVal SymbolName As String, ByVal StartDate As Date, ByVal EndDate As Date, Optional ByVal AdjustSplits As Boolean = True) As Quote()
  2.  
  3.  
  4.  
  5. Dim Data(), DataQuote() As String
  6. Dim tempquote() As Quote
  7. Dim i As Integer
  8. Dim NoQuotes As Boolean = False
  9. Dim MainQuoteHolder() As Quote
  10.  
  11. Try
  12. '-----------------------------------------------------------
  13. Dim myReq As System.Net.HttpWebRequest = System.Net.WebRequest.Create("http://google.com/finance/historical?q=tadawul:" & SymbolName & "&enddate=" & Microsoft.VisualBasic.DateAndTime.MonthName(EndDate.Month, True) & "+" + CStr(EndDate.Day) + "+" + CStr(EndDate.Year) + "&startdate=" & Microsoft.VisualBasic.DateAndTime.MonthName(StartDate.Month, True) & "+" & CStr(StartDate.Day) & "+" + CStr(StartDate.Year) + "&output=csv")
  14. 'Dim myReq As System.Net.HttpWebRequest = System.Net.WebRequest.Create("http://google.com/finance/historical?q=tadawul:" & SymbolName & "&output=csv")
  15. Dim wres As System.Net.HttpWebResponse = myReq.GetResponse
  16. Dim sr As New IO.StreamReader(wres.GetResponseStream)
  17. Dim str As String = sr.ReadToEnd
  18. wres.Close()
  19. Debug.Write("Data:")
  20. Debug.WriteLine(str)
  21. Dim j As Integer
  22. j = 1
  23. Data = Split(Replace(str, Chr(13), "", 1, -1, CompareMethod.Binary), Chr(10))
  24. If ((Data.Length - 3) > 0) Then
  25. ReDim tempquote(Data.Length - 3)
  26. For i = 1 To Data.Length - 2 Step 1 'start on the second row, skip the header
  27. DataQuote = Split(Data(i), ",")
  28. If (Replace(FixDateMod(DataQuote(0)), "/0", "/", , , CompareMethod.Binary) <> StartDate) Then 'skip the quote if its the same day as StartDate to avoid duplicate quotes
  29. If AdjustSplits = True Then
  30.  
  31.  
  32.  
  33.  
  34. tempquote(j - 1) = New Quote(FixDateMod(DataQuote(0)), DataQuote(1), DataQuote(2), DataQuote(3), DataQuote(4), 0, DataQuote(5))
  35.  
  36.  
  37.  
  38.  
  39.  
  40. Else
  41. tempquote(j - 1) = New Quote(FixDateMod(DataQuote(0)), DataQuote(1), DataQuote(2), DataQuote(3), DataQuote(4), 0, DataQuote(5))
  42. End If
  43. j = j + 1
  44. Else
  45. If (tempquote.Length > 0) Then
  46. ReDim Preserve tempquote(tempquote.Length - 2)
  47. End If
  48. End If
  49. Next
  50. End If
  51.  
  52. If (Not (tempquote Is Nothing)) Then
  53. If (Not (MainQuoteHolder Is Nothing)) Then
  54.  
  55. Dim p As Single
  56. For p = 0 To MainQuoteHolder.Length - 1 Step 1
  57. If (tempquote.Length > 1) Then
  58. If (tempquote(1).QDate = MainQuoteHolder(p).QDate) Then
  59. NoQuotes = True
  60. End If
  61. Else
  62. NoQuotes = True
  63. Exit For
  64. End If
  65. Next
  66. End If
  67. If (NoQuotes = False) Then
  68. If (MainQuoteHolder Is Nothing) Then
  69. ReDim MainQuoteHolder(tempquote.Length - 1)
  70. System.Array.Copy(tempquote, MainQuoteHolder, tempquote.Length)
  71. Else
  72. Dim tempquote2() As Quote
  73. ReDim tempquote2(MainQuoteHolder.Length - 1)
  74. tempquote2 = MainQuoteHolder
  75. ReDim MainQuoteHolder(tempquote.Length - 1 + MainQuoteHolder.Length - 1) 'Reduce the size of the array is a line is skipped
  76. Dim k As Integer
  77. For k = 0 To tempquote2.Length - 1 Step 1
  78. MainQuoteHolder(k) = tempquote2(k)
  79. Next
  80. For k = 1 To tempquote.Length - 1 Step 1
  81. MainQuoteHolder(k + tempquote2.Length - 1) = tempquote(k)
  82. Next
  83.  
  84. End If
  85. End If
  86.  
  87. EndDate = MainQuoteHolder(MainQuoteHolder.Length - 1).QDate
  88. Else
  89. NoQuotes = True
  90. End If
  91. 'End While
  92. GetQuoteRange = MainQuoteHolder
  93. Catch ex As Net.WebException
  94. Debug.WriteLine(ex.Message)
  95. Return Nothing
  96.  
  97. Catch ex As Exception
  98. End Try
  99. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement