-
Notifications
You must be signed in to change notification settings - Fork 2
BATCH_STOCK_QUOTES
The batch stock quotes API enables the querying of multiple stock quotes with a single API request, updated realtime. It may serve as a lightweight alternative to our core stock time series APIs above (which have richer content but are symbol-specific). Given the specifications of our data provider (IEX), we currently only offer US stock quotes during US market hours through this API. If you would like to query stocks, ETFs, and mutual funds traded on major global exchanges, please refer to our core stock time series APIs (Intraday, Daily, Daily Ajudsted, Weekly, Weekly Adjusted, Monthly, and Monthly Adjusted). The related REST API documentation is here
The very first thing to do before diving into BATCH_STOCK_QUOTES calls is to include the right namespace.
using Avapi.AvapiBATCH_STOCK_QUOTES
The BATCH_STOCK_QUOTES object is retrieved from the Connection object.
The snippet below shows how to get the Connection object:
...
IAvapiConnection connection = AvapiConnection.Instance
connection.Connect("Your Alpha Vantage API Key !!!!");
...
Once you got the Connection object you can extract the BATCH_STOCK_QUOTES from it.
...
Int_BATCH_STOCK_QUOTES batchstockquotes =
connection.GetQueryObject_BATCH_STOCK_QUOTES();
To perform a BATCH_STOCK_QUOTES request you have 2 options:
- The request with constants:
IAvapiResponse_BATCH_STOCK_QUOTES Query(string symbols);
- The request without constants:
IAvapiResponse_BATCH_STOCK_QUOTES QueryPrimitive(string symbols);
To perform an BATCH_STOCK_QUOTES asynchronous request you have 2 options:
- The request with constants:
async Task<IAvapiResponse_BATCH_STOCK_QUOTES> QueryAsync(string symbols);
- The request without constants:
async Task<IAvapiResponse_BATCH_STOCK_QUOTES> QueryAsync(string symbols);
The parameters below are needed to perform the BATCH_STOCK_QUOTES request.
- symbols: The name of the equities
Please notice that the info above are copied from the official alphavantage documentation, that you can find here.
The request with constants implies the use of different enums:
The response of a BATCH_STOCK_QUOTES request is an object that implements the IAvapiResponse_BATCH_STOCK_QUOTES interface.
public interface IAvapiResponse_BATCH_STOCK_QUOTES
{
string RawData
{
get;
}
IAvapiResponse_BATCH_STOCK_QUOTES_Content Data
{
get;
}
}
The IAvapiResponse_BATCH_STOCK_QUOTES interface has two members: RawData and Data.
- RawData: represents the json response in string format.
- Data: It represents the parsed response in an object implementing the interface IAvapiResponse_BATCH_STOCK_QUOTES_Content.
Complete Example of a Console App: Display the result of a BATCH_STOCK_QUOTES request by using the method Query (synchronous request)
using System;
using System.IO;
using Avapi.AvapiBATCH_STOCK_QUOTES;
namespace Avapi
{
public class Example
{
static void Main()
{
// Creating the connection object
IAvapiConnection connection = AvapiConnection.Instance;
// Set up the connection and pass the API_KEY provided by alphavantage.co
connection.Connect("Your Alpha Vantage API Key !!!!");
// Get the BATCH_STOCK_QUOTES query object
Int_BATCH_STOCK_QUOTES batch_stock_quotes =
connection.GetQueryObject_BATCH_STOCK_QUOTES();
// Perform the BATCH_STOCK_QUOTES request and get the result
IAvapiResponse_BATCH_STOCK_QUOTES batch_stock_quotesResponse =
batch_stock_quotes.Query(
"MSFT,FB", null);
// Printout the results
Console.WriteLine("******** RAW DATA BATCH_STOCK_QUOTES ********");
Console.WriteLine(batch_stock_quotesResponse.RawData);
Console.WriteLine("******** STRUCTURED DATA BATCH_STOCK_QUOTES ********");
var data = batch_stock_quotesResponse.Data;
if (data.Error)
{
Console.WriteLine(data.ErrorMessage);
}
else
{
Console.WriteLine("Information: " + data.MetaData.Information);
Console.WriteLine("Notes: " + data.MetaData.Notes);
Console.WriteLine("TimeZone: " + data.MetaData.TimeZone);
Console.WriteLine("========================");
Console.WriteLine("========================");
foreach (var stockQuotes in data.StockQuotes)
{
Console.WriteLine("Symbol: " + stockQuotes.Symbol);
Console.WriteLine("Price: " + stockQuotes.Price);
Console.WriteLine("Volume: " + stockQuotes.Volume);
Console.WriteLine("TimeStamp: " + stockQuotes.TimeStamp);
Console.WriteLine("========================");
}
}
}
}
}
Complete Example of a Windows Form App: Display the result of a BATCH_STOCK_QUOTES request by using the method QueryAsync (asynchronous request)
using Avapi;
using Avapi.AvapiBATCH_STOCK_QUOTES
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private IAvapiConnection m_connection = AvapiConnection.Instance;
private Int_BATCH_STOCK_QUOTES m_batch_stock_quotes;
private IAvapiResponse_BATCH_STOCK_QUOTES m_batch_stock_quotesResponse;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
// Set up the connection and pass the API_KEY provided by alphavantage.co
m_connection.Connect("Your Alpha Vantage Key");
// Get the BATCH_STOCK_QUOTES query object
m_batch_stock_quotes = m_connection.GetQueryObject_BATCH_STOCK_QUOTES();
base.OnLoad(e);
}
private async void BATCH_STOCK_QUOTESAsyncButton_Click(object sender, EventArgs e)
{
// Perform the BATCH_STOCK_QUOTES request and get the result
m_batch_stock_quotesResponse =
await m_batch_stock_quotes.QueryAsync(
"MSFT,FB", null);
// Show the results
resultTextBox.AppendText("******** RAW DATA BATCH_STOCK_QUOTES ********" + "\n");
resultTextBox.AppendText(m_batch_stock_quotesResponse.RawData + "\n");
resultTextBox.AppendText("******** STRUCTURED DATA BATCH_STOCK_QUOTES ********" + "\n");
var data = m_batch_stock_quotesResponse.Data;
if (data.Error)
{
resultTextBox.AppendText(data.ErrorMessage + "\n");
}
else
{
resultTextBox.AppendText("Information: " + data.MetaData.Information + "\n");
resultTextBox.AppendText("Notes: " + data.MetaData.Notes + "\n");
resultTextBox.AppendText("TimeZone: " + data.MetaData.TimeZone + "\n");
resultTextBox.AppendText("========================" + "\n");
resultTextBox.AppendText("========================" + "\n");
foreach (var stockQuotes in data.StockQuotes)
{
resultTextBox.AppendText("Symbol: " + stockQuotes.Symbol + "\n");
resultTextBox.AppendText("Price: " + stockQuotes.Price + "\n");
resultTextBox.AppendText("Volume: " + stockQuotes.Volume + "\n");
resultTextBox.AppendText("TimeStamp: " + stockQuotes.TimeStamp + "\n");
resultTextBox.AppendText("========================" + "\n");
}
}
}
}
}