-
Notifications
You must be signed in to change notification settings - Fork 2
TIME_SERIES_MONTHLY
This API returns monthly time series (last trading day of each month, monthly open, monthly high, monthly low, monthly close, monthly volume) of the equity specified, covering up to 20 years of historical data. The latest data point is the month that contains the current trading day (updated realtime). The related REST API documentation is here
The very first thing to do before diving into TIME_SERIES_MONTHLY calls is to include the right namespace.
using Avapi.AvapiTIME_SERIES_MONTHLY
The TIME_SERIES_MONTHLY 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 TIME_SERIES_MONTHLY from it.
...
Int_TIME_SERIES_MONTHLY timeseriesmonthly =
connection.GetQueryObject_TIME_SERIES_MONTHLY();
To perform a TIME_SERIES_MONTHLY request you have 2 options:
- The request with constants:
IAvapiResponse_TIME_SERIES_MONTHLY Query(string symbol);
- The request without constants:
IAvapiResponse_TIME_SERIES_MONTHLY QueryPrimitive(string symbol);
To perform an TIME_SERIES_MONTHLY asynchronous request you have 2 options:
- The request with constants:
async Task<IAvapiResponse_TIME_SERIES_MONTHLY> QueryAsync(string symbol);
- The request without constants:
async Task<IAvapiResponse_TIME_SERIES_MONTHLY> QueryAsync(string symbol);
The parameters below are needed to perform the TIME_SERIES_MONTHLY request.
- symbol: The name of the equity
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 TIME_SERIES_MONTHLY request is an object that implements the IAvapiResponse_TIME_SERIES_MONTHLY interface.
public interface IAvapiResponse_TIME_SERIES_MONTHLY
{
string RawData
{
get;
}
IAvapiResponse_TIME_SERIES_MONTHLY_Content Data
{
get;
}
}
The IAvapiResponse_TIME_SERIES_MONTHLY 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_TIME_SERIES_MONTHLY_Content.
Complete Example of a Console App: Display the result of a TIME_SERIES_MONTHLY request by using the method Query (synchronous request)
using System;
using System.IO;
using Avapi.AvapiTIME_SERIES_MONTHLY;
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 TIME_SERIES_MONTHLY query object
Int_TIME_SERIES_MONTHLY time_series_monthly =
connection.GetQueryObject_TIME_SERIES_MONTHLY();
// Perform the TIME_SERIES_MONTHLY request and get the result
IAvapiResponse_TIME_SERIES_MONTHLY time_series_monthlyResponse =
time_series_monthly.Query(
"MSFT");
// Printout the results
Console.WriteLine("******** RAW DATA TIME_SERIES_MONTHLY ********");
Console.WriteLine(time_series_monthlyResponse.RawData);
Console.WriteLine("******** STRUCTURED DATA TIME_SERIES_MONTHLY ********");
var data = time_series_monthlyResponse.Data;
if (data.Error)
{
Console.WriteLine(data.ErrorMessage);
}
else
{
Console.WriteLine("Information: " + data.MetaData.Information);
Console.WriteLine("Symbol: " + data.MetaData.Symbol);
Console.WriteLine("LastRefreshed: " + data.MetaData.LastRefreshed);
Console.WriteLine("TimeZone: " + data.MetaData.TimeZone);
Console.WriteLine("========================");
Console.WriteLine("========================");
foreach (var timeseries in data.TimeSeries)
{
Console.WriteLine("open: " + timeseries.open);
Console.WriteLine("high: " + timeseries.high);
Console.WriteLine("low: " + timeseries.low);
Console.WriteLine("close: " + timeseries.close);
Console.WriteLine("volume: " + timeseries.volume);
Console.WriteLine("DateTime: " + timeseries.DateTime);
Console.WriteLine("========================");
}
}
}
}
}
Complete Example of a Windows Form App: Display the result of a TIME_SERIES_MONTHLY request by using the method QueryAsync (asynchronous request)
using Avapi;
using Avapi.AvapiTIME_SERIES_MONTHLY
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private IAvapiConnection m_connection = AvapiConnection.Instance;
private Int_TIME_SERIES_MONTHLY m_time_series_monthly;
private IAvapiResponse_TIME_SERIES_MONTHLY m_time_series_monthlyResponse;
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 TIME_SERIES_MONTHLY query object
m_time_series_monthly = m_connection.GetQueryObject_TIME_SERIES_MONTHLY();
base.OnLoad(e);
}
private async void TIME_SERIES_MONTHLYAsyncButton_Click(object sender, EventArgs e)
{
// Perform the TIME_SERIES_MONTHLY request and get the result
m_time_series_monthlyResponse =
await m_time_series_monthly.QueryAsync(
"MSFT");
// Show the results
resultTextBox.AppendText("******** RAW DATA TIME_SERIES_MONTHLY ********" + "\n");
resultTextBox.AppendText(m_time_series_monthlyResponse.RawData + "\n");
resultTextBox.AppendText("******** STRUCTURED DATA TIME_SERIES_MONTHLY ********" + "\n");
var data = m_time_series_monthlyResponse.Data;
if (data.Error)
{
resultTextBox.AppendText(data.ErrorMessage + "\n");
}
else
{
resultTextBox.AppendText("Information: " + data.MetaData.Information + "\n");
resultTextBox.AppendText("Symbol: " + data.MetaData.Symbol + "\n");
resultTextBox.AppendText("LastRefreshed: " + data.MetaData.LastRefreshed + "\n");
resultTextBox.AppendText("TimeZone: " + data.MetaData.TimeZone + "\n");
resultTextBox.AppendText("========================" + "\n");
resultTextBox.AppendText("========================" + "\n");
foreach (var timeseries in data.TimeSeries)
{
resultTextBox.AppendText("open: " + timeseries.open + "\n");
resultTextBox.AppendText("high: " + timeseries.high + "\n");
resultTextBox.AppendText("low: " + timeseries.low + "\n");
resultTextBox.AppendText("close: " + timeseries.close + "\n");
resultTextBox.AppendText("volume: " + timeseries.volume + "\n");
resultTextBox.AppendText("DateTime: " + timeseries.DateTime + "\n");
resultTextBox.AppendText("========================" + "\n");
}
}
}
}
}