Steves_Code/Websites/SharePrices/SharePrices/SharePrices.aspx.vb
2025-01-05 18:33:39 +00:00

307 lines
18 KiB
VB.net

Imports System.Web.Services
Imports SharePrices.SharePricesUtils
Public Class SharePrices
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Debug.Print("SharePrices - Page load - " + Now().ToString("yyyy-MM-dd HH:mm:ss") + ", IP: " + Request.UserHostAddress + ", HostName: " + Request.UserHostName)
End Sub
<WebMethod()>
Public Shared Sub GetInstruments()
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetInstruments" + ", IP: " + HttpContext.Current.Request.UserHostAddress + ", HostName: " + HttpContext.Current.Request.UserHostName)
Dim responseText As String = DataAccessLayer.GetInstruments()
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub AddInstrument(Symbol As String, FullName As String)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.AddInstrument webmethod: " + Symbol + ", " + FullName)
Dim responseText As String = DataAccessLayer.AddInstrument(Symbol, FullName)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
'<WebMethod()>
'Public Shared Sub UpdateInstrument(Symbol As String, GMTOffset As Integer, Currency As String, CurrentPrice As Double, InstrumentType As String)
' Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.UpdateInstrument webmethod: " + Symbol)
' DataAccessLayer.UpdateInstrument(Symbol, GMTOffset, Currency, CurrentPrice, InstrumentType)
'End Sub
<WebMethod()>
Public Shared Sub AddHolding(AccountName As String, Symbol As String, NoUnits As Double, PurchasePricePerUnit As Double, PurchaseDate As Int64)
'Public Shared Sub AddHolding(AccountName As String, Symbol As String, NoUnits As Integer, PurchasePricePerUnit As Double, PurchaseDate As Int64)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.AddHolding webmethod: " + AccountName + ", " + Symbol)
Dim responseText As String = DataAccessLayer.AddHolding(AccountName, Symbol, NoUnits, PurchasePricePerUnit, PurchaseDate)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub GetAllDailyData(startDate As Int64, endDate As Int64)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetAllDailyData webmethod: """ + FromEpochTime(startDate).ToString("yyyy-MM-dd HH:mm:ss") + """, """ + FromEpochTime(endDate).ToString("yyyy-MM-dd HH:mm:ss") + """")
Dim responseText As String = DataAccessLayer.GetAllDailyData(FromEpochTime(startDate), FromEpochTime(endDate))
If responseText = "" Then responseText = "[]"
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetAllDailyData finished")
End Sub
<WebMethod()>
Public Shared Sub GetDailyData(Symbol As String, startDate As Int64, endDate As Int64)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetDailyData webmethod: """ + Symbol + """, """ + FromEpochTime(startDate).ToString("yyyy-MM-dd HH:mm:ss") + """, """ + FromEpochTime(endDate).ToString("yyyy-MM-dd HH:mm:ss") + """")
Dim responseText As String = DataAccessLayer.GetDailyData(Symbol, FromEpochTime(startDate), FromEpochTime(endDate))
If responseText = "" Then responseText = "[]"
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetDailyData finished")
End Sub
<WebMethod()>
Public Shared Sub GetAllIntradayData(startDate As Int64, endDate As Int64)
Dim startDT As Date = Now()
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetAllIntradayData webmethod: """ + FromEpochTime(startDate).ToString("yyyy-MM-dd HH:mm:ss") + """, """ + FromEpochTime(endDate).ToString("yyyy-MM-dd HH:mm:ss") + """")
Dim responseText As String = DataAccessLayer.GetAllIntradayData(FromEpochTime(startDate), FromEpochTime(endDate))
If responseText = "" Then responseText = "[]"
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Dim endDT As Date = Now()
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetAllIntradayData completed. Duration = " + DateDiff(DateInterval.Second, startDT, endDT).ToString())
End Sub
<WebMethod()>
Public Shared Sub GetIntradayData(Symbol As String, startDate As Int64, endDate As Int64)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetIntradayData webmethod: """ + Symbol + """, """ + FromEpochTime(startDate).ToString("yyyy-MM-dd HH:mm:ss") + """, """ + FromEpochTime(endDate).ToString("yyyy-MM-dd HH:mm:ss") + """")
Dim responseText As String = DataAccessLayer.GetIntradayData(Symbol, FromEpochTime(startDate), FromEpochTime(endDate))
If responseText = "" Then responseText = "[]"
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub SubmitDailyData(Symbol As String, DailyPrices As List(Of DataAccessLayer.PriceData))
Dim startDT As Date = Now()
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SubmitDailyData webmethod: " + Symbol)
Try
Dim responseText As String = DataAccessLayer.InsertDailyData(Symbol, DailyPrices)
If responseText = "" Then responseText = "[]"
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
'If Symbol = "RYA.L" Then
' For Each pd As DataAccessLayer.PriceData In DailyPrices
' Debug.Print(" " + FromEpochTime(pd.DT))
' Next
' Debug.Print(" Response: " + responseText)
'End If
Catch ex As Exception
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", "{""Result"":""Error""}")
End Try
Dim endDT As Date = Now()
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SubmitDailyData(" + Symbol + ") completed. Duration = " + DateDiff(DateInterval.Second, startDT, endDT).ToString())
End Sub
<WebMethod()>
Public Shared Sub SubmitIntradayData(Symbol As String, DailyPrices As List(Of DataAccessLayer.PriceData), GMTOffset As Integer, Currency As String, CurrentPrice As Double, InstrumentType As String, TradeDayStart As Long, TradeDayEnd As Long)
'Public Shared Sub SubmitIntradayData(Symbol As String, DailyPrices As List(Of DataAccessLayer.PriceData))
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SubmitIntradayData webmethod: " + Symbol)
DataAccessLayer.UpdateInstrument(Symbol, GMTOffset, Currency, CurrentPrice, InstrumentType, FromEpochTime(TradeDayStart), FromEpochTime(TradeDayEnd))
Dim responseText As String = DataAccessLayer.InsertIntradayData(Symbol, DailyPrices)
If responseText = "" Then responseText = "[]"
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub SwapInstrumentDisplayOrders(Symbol1 As String, Symbol2 As String)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SwapInstrumentDisplayOrders webmethod: " + Symbol1 + ", " + Symbol2)
Dim responseText As String = DataAccessLayer.SwapInstrumentDisplayOrders(Symbol1, Symbol2)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
'<WebMethod()>
'Public Shared Sub DeleteHolding(HoldingID As Integer)
' 'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.DeleteHolding webmethod: " + HoldingID.ToString())
' Dim responseText As String = DataAccessLayer.DeleteHolding(HoldingID)
' SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
'End Sub
<WebMethod()>
Public Shared Sub SoldHolding(HoldingID As Integer, SoldDate As Int64, SoldCurrency As String, SoldProceeds As Double)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SoldHolding webmethod: " + HoldingID.ToString() + ". " + SoldDate.TYoString())
Dim responseText As String = DataAccessLayer.SoldHolding(HoldingID, FromEpochTime(SoldDate), SoldCurrency, SoldProceeds)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub SetAccountCashValue(AccountName As String, Currency As String, Value As Double)
Dim responseText As String = DataAccessLayer.SetAccountCashValue(AccountName, Currency, Value)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub FetchYahooFinanceIntraday(Symbol As String)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.FetchYahooFinanceIntraday webmethod: " + Symbol)
Dim webClient As New System.Net.WebClient
Dim responseText As String = webClient.DownloadString("https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1h&range=1mo&.tsrc=finance")
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub FetchYahooFinanceIntraday(Symbol As String, MinIntradayDT As Long)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.FetchYahooFinanceIntraday webmethod: " + Symbol + ", " + MinIntradayDT.ToString() + " (" + FromEpochTime(MinIntradayDT).ToString("yyyy-MM-dd HH:mm") + ")")
'Dim webClient As New System.Net.WebClient
'Dim responseText As String = webClient.DownloadString("https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1h&range=1mo&.tsrc=finance")
'SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Try
Dim queryString As String
'If Symbol = "BTC-USD" Or Symbol = "ARB.L" Then 'Request 15min interval data for Bitcoin/USD and Argo Blockchain
If MinIntradayDT <= ToEpochTime(Now().AddDays(-59)) Then 'Yahoo finance only provides 15min data for previous 60 days
queryString = "https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=15m&range=1mo&.tsrc=finance"
Else
Dim dateTo As Long
If MinIntradayDT > 9999999999 Then
dateTo = Math.Ceiling(MinIntradayDT / 1000)
Else
dateTo = MinIntradayDT
End If
Dim dateFrom As Long = dateTo - (15 * (60 * 60 * 24)) '15 days
'Need to make sure dateFrom is no more than 60 days ago
Dim minAllowableDate = Math.Floor(ToEpochTime(Now().AddDays(-59)) / 1000) - 60
If dateFrom < minAllowableDate Then
dateFrom = minAllowableDate
End If
queryString = "https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?period1=" + dateFrom.ToString() + "&period2=" + dateTo.ToString() + "&interval=15m&includePrePost=true&events=div|split|earn"
End If
'Else
'If MinIntradayDT <= ToEpochTime(Now().AddYears(-1)) Then
' queryString = "https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1d&range=6mo&.tsrc=finance"
'Else
' queryString = "https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1d&range=2y&.tsrc=finance"
'End If
'End If
Debug.Print(Strings.StrDup(22, " ") + queryString)
Dim responseText As String = New System.Net.WebClient().DownloadString(queryString)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Catch ex As Exception
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - ERROR - SharePrices.FetchYahooFinanceIntraday webmethod: " + Symbol + ", " + MinIntradayDT.ToString() + " (" + FromEpochTime(MinIntradayDT).ToString("yyyy-MM-dd") + "): " + ex.Message)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", "{""Result"":""Error"", ""MSG"":""" + ex.Message.Replace("""", "'") + """}")
End Try
End Sub
<WebMethod()>
Public Shared Sub FetchYahooFinanceDaily(Symbol As String)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.FetchYahooFinanceDaily webmethod: " + Symbol)
'If Symbol = "RYA.L" Then
' Debug.Print(" FetchYahooFinanceDaily")
'End If
Try
Dim webClient As New System.Net.WebClient
Dim responseText As String = webClient.DownloadString("https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1d&range=6mo&.tsrc=finance")
'Dim responseText As String = webClient.DownloadString("https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1d&range=2y&.tsrc=finance")
'If Symbol = "RYA.L" Then
' Debug.Print(" FetchYahooFinanceDaily result: " + responseText)
'End If
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Catch ex As Exception
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", "{""Result"":""Error""}")
End Try
End Sub
<WebMethod()>
Public Shared Sub FetchYahooFinanceDaily(Symbol As String, MinDailyDT As Long)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.FetchYahooFinanceDaily webmethod: " + Symbol + ", " + MinDailyDT.ToString() + " (" + FromEpochTime(MinDailyDT).ToString("yyyy-MM-dd") + ")")
Try
Dim queryString As String
If MinDailyDT <= ToEpochTime(Now().AddYears(-2)) Then
queryString = "https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1d&range=6mo&.tsrc=finance"
Else
queryString = "https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=1d&range=2y&.tsrc=finance"
End If
Dim responseText As String = New System.Net.WebClient().DownloadString(queryString)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Catch ex As Exception
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - ERROR - SharePrices.FetchYahooFinanceDaily webmethod: " + Symbol + ", " + MinDailyDT.ToString() + " (" + FromEpochTime(MinDailyDT).ToString("yyyy-MM-dd") + "): " + ex.Message)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", "{""Result"":""Error"", ""MSG"":""" + ex.Message.Replace("""", "'") + """}")
End Try
End Sub
<WebMethod()>
Public Shared Sub FetchYahooFinanceSingleDay(Symbol As String)
'Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.FetchYahooFinanceSingleDay webmethod: " + Symbol)
Try
Dim webClient As New System.Net.WebClient
Dim responseText As String = webClient.DownloadString("https://query1.finance.yahoo.com/v8/finance/chart/" + Symbol + "?region=GB&lang=en-GB&includePrePost=false&interval=2m&range=1d&corsDomain=uk.finance.yahoo.com&.tsrc=finance")
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
Catch ex As Exception
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - ERROR - SharePrices.FetchYahooFinanceSingleDay webmethod: " + Symbol + "): " + ex.Message)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", "{""Result"":""Error"", ""MSG"":""" + ex.Message.Replace("""", "'") + """}")
End Try
End Sub
<WebMethod()>
Public Shared Sub SearchYahooFinanceShares(SearchString As String)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SearchYahooFinanceShares webmethod: " + SearchString)
Dim webClient As New System.Net.WebClient
Dim responseText As String = webClient.DownloadString("https://query1.finance.yahoo.com/v1/finance/search?q=" + SearchString + "&quotesCount=6&newsCount=0&enableFuzzyQuery=false&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&newsQueryId=news_ss_symbols&enableCb=false&enableNavLinks=false&vespaNewsTimeoutMs=600")
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub SetTotalHoldingsValue(TotalValue As Double)
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.SetTotalHoldingsValue webmethod: " + CStr(TotalValue))
Dim responseText As String = DataAccessLayer.SetTotalHoldingsValues(TotalValue)
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub GetTotalHoldingsHistory()
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.GetTotalHoldingsHistory")
Dim responseText As String = DataAccessLayer.GetTotalHoldingsHistory()
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", responseText)
End Sub
<WebMethod()>
Public Shared Sub TestWebMethod(Symbol As String, DailyPrices As List(Of DataAccessLayer.PriceData))
Dim startDT As Date = Now()
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.TestWebMethod: " + Symbol)
While startDT.AddSeconds(1) > Now()
System.Threading.Thread.Sleep(100)
End While
SetResponseAndCompleteRequest(HttpContext.Current, "application/json", "{""Result"": ""OK - " + Symbol + """}")
Dim endDT As Date = Now()
Debug.Print(Now().ToString("yyyy-MM-dd HH:mm:ss") + " - SharePrices.TestWebMethod(" + Symbol + ") completed. Duration = " + DateDiff(DateInterval.Second, startDT, endDT).ToString())
End Sub
End Class