Click here to Skip to main content
15,893,790 members
Articles / Mobile Apps

Yahoo! Managed

Rate me:
Please Sign up or sign in to vote.
4.87/5 (56 votes)
8 Jan 2015Apache12 min read 528.2K   25.5K   262  
Download financial data, managing online portfolio or using Search BOSS from Yahoo! with .NET
' ******************************************************************************
' ** 
' **  Yahoo Finance Managed
' **  Written by Marius Häusler 2010
' **  It would be pleasant, if you contact me when you are using this code.
' **  Contact: YahooFinanceManaged@gmail.com
' **  Project Home: http://code.google.com/p/yahoo-finance-managed/
' **  
' ******************************************************************************
' **  
' **  Copyright 2010 Marius Häusler
' **  
' **  Licensed under the Apache License, Version 2.0 (the "License");
' **  you may not use this file except in compliance with the License.
' **  You may obtain a copy of the License at
' **  
' **    http://www.apache.org/licenses/LICENSE-2.0
' **  
' **  Unless required by applicable law or agreed to in writing, software
' **  distributed under the License is distributed on an "AS IS" BASIS,
' **  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' **  See the License for the specific language governing permissions and
' **  limitations under the License.
' ** 
' ******************************************************************************


Namespace Finance.API

    ''' <summary>
    ''' Provides methods for downloading technical analysing chart images.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ChartDownload
        Inherits Base.Download

        ''' <summary>
        ''' Raises if an asynchronous download of an technical analysing chart image completes.
        ''' </summary>
        ''' <param name="sender">The event raising object.</param>
        ''' <param name="ea">The results of the asynchronous download.</param>
        ''' <remarks></remarks>
        Public Event AsyncDownloadCompleted(ByVal sender As Base.Download, ByVal ea As ChartDownloadCompletedEventArgs)

        Friend ReadOnly mFinanceHelper As New FinanceHelper
        Private mOptions As New ChartDownloadOptions

        ''' <summary>
        ''' Gets or sets the chart image download options.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>By setting null/Nothing, a default instance will be setted and used for downloading.</remarks>
        Public Property Options() As ChartDownloadOptions
            Get
                Return mOptions
            End Get
            Set(ByVal value As ChartDownloadOptions)
                mOptions = value
            End Set
        End Property

        ''' <summary>
        ''' Downloads a chart image.
        ''' </summary>
        ''' <param name="managedID">The managed ID of the stock</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Function Download(ByVal managedID As IID) As Base.StreamResponse
            If managedID Is Nothing Then Throw New ArgumentNullException("managedID", "The passed ID is null.")
            Return Me.Download(managedID.ID)
        End Function
        ''' <summary>
        ''' Downloads a chart image.
        ''' </summary>
        ''' <param name="unmanagedID">The unmanaged ID of the stock</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Function Download(ByVal unmanagedID As String) As Base.StreamResponse
            If unmanagedID = String.Empty Then Throw New ArgumentNullException("unmanagedID", "The passed ID is empty.")
            Return MyBase.Download(Me.DownloadURL(unmanagedID, New ChartDownloadOptions(mOptions)))
        End Function

        ''' <summary>
        ''' Starts an asynchronous download of an chart image.
        ''' </summary>
        ''' <param name="managedID">The managed ID of the stock</param>
        ''' <param name="userArgs">Individual user argument</param>
        ''' <remarks></remarks>
        Public Overloads Sub DownloadAsync(ByVal managedID As IID, Optional ByVal userArgs As Object = Nothing)
            If managedID Is Nothing Then Throw New ArgumentNullException("managedID", "The passed ID is null.")
            Me.DownloadAsync(managedID.ID, userArgs)
        End Sub
        ''' <summary>
        ''' Starts an asynchronous download of an chart image.
        ''' </summary>
        ''' <param name="unmanagedID">The unmanaged ID of the stock</param>
        ''' <param name="userArgs">Individual user argument</param>
        ''' <remarks></remarks>
        Public Overloads Sub DownloadAsync(ByVal unmanagedID As String, Optional ByVal userArgs As Object = Nothing)
            If unmanagedID = String.Empty Then Throw New ArgumentNullException("unmanagedID", "The passed ID is empty.")
            Dim args As New AsyncDownloadArgs(New ChartDownloadOptions(mOptions), unmanagedID, userArgs)
            MyBase.DownloadAsync(Me.DownloadURL(args.ID, args.Options), args)
        End Sub

        ''' <summary>
        ''' Default constructor
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        Private Sub DownloadAsync_Completed(ByVal sender As Base.Download, ByVal ba As Base.StreamDownloadCompletedEventArgs) Handles MyBase.AsyncStreamDownloadCompleted
            If ba IsNot Nothing AndAlso ba.UserArgs IsNot Nothing AndAlso TypeOf ba.UserArgs Is AsyncDownloadArgs Then
                Dim dlArgs As AsyncDownloadArgs = DirectCast(ba.UserArgs, AsyncDownloadArgs)
                Dim args As New ChartDownloadCompletedEventArgs(dlArgs.UserArgs, ba.Response, dlArgs.ID, dlArgs.Options)
                RaiseEvent AsyncDownloadCompleted(Me, args)
            End If
        End Sub

        Private Function DownloadURL(ByVal id As String, ByVal options As ChartDownloadOptions) As String
            Dim url As New Text.StringBuilder
            url.Append("http://")
            With options
                If .EuropeanServer Then
                    url.Append("ichart.europe.")
                Else
                    Select Case .Server
                        Case Server.France, Server.Germany, Server.Italy, Server.Spain, Server.UK : url.Append(mHelper.ServerString(.Server))
                    End Select
                    url.Append("ichart.")
                End If
                url.Append("yahoo.com/")
                If .ImageSize = ChartImageSize.Small Then : url.Append("t")
                Else : url.Append("z"c)
                End If
                url.Append("?s=")
                url.Append(mHelper.CleanYqlParam(id).Replace("@", ""))
                url.Append("&t=")
                url.Append(mFinanceHelper.GetChartTimeSpan(.TimeSpan))
                url.Append("&q=")
                url.Append(mFinanceHelper.GetChartType(.Type))
                url.Append("&l=")
                url.Append(mFinanceHelper.GetChartScale(.Scale))
                If .ImageSize <> ChartImageSize.Small Then
                    url.Append("&z=")
                    url.Append(mFinanceHelper.GetChartImageSize(.ImageSize))
                    If .MovingAverages.Count > 0 Or .ExponentialMovingAverages.Count > 0 Or .TechnicalIndicators.Count > 0 Then
                        url.Append("&p=")
                        For Each ma As MovingAverageInterval In .MovingAverages
                            url.Append("m"c)
                            url.Append(mFinanceHelper.GetMovingAverageInterval(ma))
                            url.Append(","c)
                        Next
                        For Each ma As MovingAverageInterval In .ExponentialMovingAverages
                            url.Append("e"c)
                            url.Append(mFinanceHelper.GetMovingAverageInterval(ma))
                            url.Append(","c)
                        Next
                        For Each ti As TechnicalIndicator In .TechnicalIndicators
                            url.Append(mFinanceHelper.GetTechnicalIndicatorsI(ti))
                        Next
                    End If
                    If .TechnicalIndicators.Count > 0 Then
                        url.Append("&a=")
                        For Each ti As TechnicalIndicator In .TechnicalIndicators
                            url.Append(mFinanceHelper.GetTechnicalIndicatorsII(ti))
                        Next
                    End If
                    If .ComparingIDs.Count > 0 Then
                        url.Append("&c=")
                        For Each csid As String In .ComparingIDs
                            url.Append(mHelper.CleanYqlParam(csid.Replace("@", "")))
                            url.Append(","c)
                        Next
                    End If
                End If
            End With
            Return url.ToString
        End Function

        Private Class AsyncDownloadArgs
            Inherits Base.DownloadEventArgs
            Public ID As String
            Public Options As ChartDownloadOptions
            Public Sub New(ByVal opt As ChartDownloadOptions, ByVal id As String, ByVal userArgs As Object)
                MyBase.New(userArgs)
                Me.ID = id
                Me.Options = opt
            End Sub
        End Class

    End Class

    ''' <summary>
    ''' Provides information and response of an asynchronous chart image download.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ChartDownloadCompletedEventArgs
        Inherits Base.StreamDownloadCompletedEventArgs

        Private mID As String = String.Empty
        Private mOptions As ChartDownloadOptions

        ''' <summary>
        ''' Gets the ID of the chart.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property ID() As String
            Get
                Return mID
            End Get
        End Property
        ''' <summary>
        ''' Gets the used chart download options.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Options() As ChartDownloadOptions
            Get
                Return mOptions
            End Get
        End Property

        Friend Sub New(ByVal userArgs As Object, ByVal resp As Base.StreamResponse, ByVal id As String, ByVal opt As ChartDownloadOptions)
            MyBase.New(userArgs, resp)
            mID = id
            mOptions = opt
        End Sub

    End Class

    ''' <summary>
    ''' Provides properties for setting options of chart download.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ChartDownloadOptions

        Private mEuropeanServer As Boolean = False
        Private mServer As Server = Server.USA
        Private mImageSize As ChartImageSize = ChartImageSize.Middle
        Private mTimeSpan As ChartTimeSpan = ChartTimeSpan.c1D
        Private mType As ChartType = ChartType.Line
        Private mScale As ChartScale = ChartScale.Logarithmic
        Private mMovingAverages As New List(Of MovingAverageInterval)
        Private mEMovingAverages As New List(Of MovingAverageInterval)
        Private mTechnicalIndicators As New List(Of TechnicalIndicator)
        Private mComparingIDs As New List(Of String)

        ''' <summary>
        ''' Gets the alternative server for downloading
        ''' </summary>
        ''' <value></value>
        ''' <returns>If True the download will be accomplish over the european Yahoo-server and will ignore the "Server"-property. If False the used server is setted by the "Server"-property</returns>
        ''' <remarks></remarks>
        Public Property EuropeanServer() As Boolean
            Get
                Return mEuropeanServer
            End Get
            Set(ByVal value As Boolean)
                mEuropeanServer = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the used server for downloading.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Server() As Server
            Get
                Return mServer
            End Get
            Set(ByVal value As Server)
                mServer = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the size of the image.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ImageSize() As ChartImageSize
            Get
                Return mImageSize
            End Get
            Set(ByVal value As ChartImageSize)
                mImageSize = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the span of the reviewed period.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TimeSpan() As ChartTimeSpan
            Get
                Return mTimeSpan
            End Get
            Set(ByVal value As ChartTimeSpan)
                mTimeSpan = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the chart type of the image.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Type() As ChartType
            Get
                Return mType
            End Get
            Set(ByVal value As ChartType)
                mType = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the scaling of the image.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Scale() As ChartScale
            Get
                Return mScale
            End Get
            Set(ByVal value As ChartScale)
                mScale = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the list of moving average indicators.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property MovingAverages() As List(Of MovingAverageInterval)
            Get
                Return mMovingAverages
            End Get
            Set(ByVal value As List(Of MovingAverageInterval))
                mMovingAverages = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the list of exponential moving average indicators.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ExponentialMovingAverages() As List(Of MovingAverageInterval)
            Get
                Return mEMovingAverages
            End Get
            Set(ByVal value As List(Of MovingAverageInterval))
                mEMovingAverages = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the list of technical indicators.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TechnicalIndicators() As List(Of TechnicalIndicator)
            Get
                Return mTechnicalIndicators
            End Get
            Set(ByVal value As List(Of TechnicalIndicator))
                mTechnicalIndicators = value
            End Set
        End Property
        ''' <summary>
        ''' Gets the ID list of all compared stocks/indices.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ComparingIDs() As List(Of String)
            Get
                Return mComparingIDs
            End Get
            Set(ByVal value As List(Of String))
                mComparingIDs = value
            End Set
        End Property

        ''' <summary>
        ''' Default constructor
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub
        Friend Sub New(ByVal original As ChartDownloadOptions)
            If original IsNot Nothing Then
                With original
                    mEuropeanServer = .EuropeanServer
                    mServer = .Server
                    mImageSize = .ImageSize
                    mTimeSpan = .TimeSpan
                    mType = .Type
                    mScale = .Scale
                    For Each ma In .MovingAverages
                        mMovingAverages.Add(ma)
                    Next
                    For Each ema In .ExponentialMovingAverages
                        mEMovingAverages.Add(ema)
                    Next
                    For Each ti In .TechnicalIndicators
                        mTechnicalIndicators.Add(ti)
                    Next
                    For Each id In .ComparingIDs
                        mComparingIDs.Add(id)
                    Next
                End With
            End If
        End Sub

    End Class

End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions