-
Notifications
You must be signed in to change notification settings - Fork 2
MIDPOINT
This API returns the midpoint (MIDPOINT) values. MIDPOINT = (highest value + lowest value)/2. The related REST API documentation is here
The very first thing to do before diving into MIDPOINT calls is to include the right namespace.
using Avapi.AvapiMIDPOINT
The MIDPOINT 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 MIDPOINT from it.
...
Int_MIDPOINT midpoint =
connection.GetQueryObject_MIDPOINT();
To perform a MIDPOINT request you have 2 options:
- The request with constants:
IAvapiResponse_MIDPOINT Query(string symbol,
MIDPOINT_interval interval,
int time_period,
MIDPOINT_series_type series_type);
- The request without constants:
IAvapiResponse_MIDPOINT QueryPrimitive(string symbol,
string interval,
string time_period,
string series_type);
To perform an MIDPOINT asynchronous request you have 2 options:
- The request with constants:
async Task<IAvapiResponse_MIDPOINT> QueryAsync(string symbol,
MIDPOINT_interval interval,
int time_period,
MIDPOINT_series_type series_type);
- The request without constants:
async Task<IAvapiResponse_MIDPOINT> QueryAsync(string symbol,
string interval,
string time_period,
string series_type);
The parameters below are needed to perform the MIDPOINT request.
- symbol: The name of the equity
- interval: The time interval between two consecutive data points in the time series.
- time_period: Number of data points used to calculate each MIDPOINT value.
- series_type: The price type in the time series. The types supported are: close, open, high, low
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:
- MIDPOINT_interval
- MIDPOINT_series_type
MIDPOINT_interval: The time interval between two consecutive data points in the time series.
public enum MIDPOINT_interval
{
none,
n_1min,
n_5min,
n_15min,
n_30min,
n_60min,
daily,
weekly,
monthly
}
MIDPOINT_series_type: The price type in the time series. The types supported are: close, open, high, low
public enum MIDPOINT_series_type
{
none,
close,
open,
high,
low
}
The response of a MIDPOINT request is an object that implements the IAvapiResponse_MIDPOINT interface.
public interface IAvapiResponse_MIDPOINT
{
string RawData
{
get;
}
IAvapiResponse_MIDPOINT_Content Data
{
get;
}
}
The IAvapiResponse_MIDPOINT 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_MIDPOINT_Content.
Complete Example of a Console App: Display the result of a MIDPOINT request by using the method Query (synchronous request)
using System;
using System.IO;
using Avapi.AvapiMIDPOINT;
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 MIDPOINT query object
Int_MIDPOINT midpoint =
connection.GetQueryObject_MIDPOINT();
// Perform the MIDPOINT request and get the result
IAvapiResponse_MIDPOINT midpointResponse =
midpoint.Query(
"MSFT",
Const_MIDPOINT.MIDPOINT_interval.n_1min,
10,
Const_MIDPOINT.MIDPOINT_series_type.close);
// Printout the results
Console.WriteLine("******** RAW DATA MIDPOINT ********");
Console.WriteLine(midpointResponse.RawData);
Console.WriteLine("******** STRUCTURED DATA MIDPOINT ********");
var data = midpointResponse.Data;
if (data.Error)
{
Console.WriteLine(data.ErrorMessage);
}
else
{
Console.WriteLine("Symbol: " + data.MetaData.Symbol);
Console.WriteLine("Indicator: " + data.MetaData.Indicator);
Console.WriteLine("LastRefreshed: " + data.MetaData.LastRefreshed);
Console.WriteLine("Interval: " + data.MetaData.Interval);
Console.WriteLine("TimePeriod: " + data.MetaData.TimePeriod);
Console.WriteLine("SeriesType: " + data.MetaData.SeriesType);
Console.WriteLine("TimeZone: " + data.MetaData.TimeZone);
Console.WriteLine("========================");
Console.WriteLine("========================");
foreach (var technical in data.TechnicalIndicator)
{
Console.WriteLine("MIDPOINT: " + technical.MIDPOINT);
Console.WriteLine("DateTime: " + technical.DateTime);
Console.WriteLine("========================");
}
}
}
}
}
Complete Example of a Windows Form App: Display the result of a MIDPOINT request by using the method QueryAsync (asynchronous request)
using Avapi;
using Avapi.AvapiMIDPOINT
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private IAvapiConnection m_connection = AvapiConnection.Instance;
private Int_MIDPOINT m_midpoint;
private IAvapiResponse_MIDPOINT m_midpointResponse;
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 MIDPOINT query object
m_midpoint = m_connection.GetQueryObject_MIDPOINT();
base.OnLoad(e);
}
private async void MIDPOINTAsyncButton_Click(object sender, EventArgs e)
{
// Perform the MIDPOINT request and get the result
m_midpointResponse =
await m_midpoint.QueryAsync(
"MSFT",
Const_MIDPOINT.MIDPOINT_interval.n_1min,
10,
Const_MIDPOINT.MIDPOINT_series_type.close);
// Show the results
resultTextBox.AppendText("******** RAW DATA MIDPOINT ********" + "\n");
resultTextBox.AppendText(m_midpointResponse.RawData + "\n");
resultTextBox.AppendText("******** STRUCTURED DATA MIDPOINT ********" + "\n");
var data = m_midpointResponse.Data;
if (data.Error)
{
resultTextBox.AppendText(data.ErrorMessage + "\n");
}
else
{
resultTextBox.AppendText("Symbol: " + data.MetaData.Symbol + "\n");
resultTextBox.AppendText("Indicator: " + data.MetaData.Indicator + "\n");
resultTextBox.AppendText("LastRefreshed: " + data.MetaData.LastRefreshed + "\n");
resultTextBox.AppendText("Interval: " + data.MetaData.Interval + "\n");
resultTextBox.AppendText("TimePeriod: " + data.MetaData.TimePeriod + "\n");
resultTextBox.AppendText("SeriesType: " + data.MetaData.SeriesType + "\n");
resultTextBox.AppendText("TimeZone: " + data.MetaData.TimeZone + "\n");
resultTextBox.AppendText("========================" + "\n");
resultTextBox.AppendText("========================" + "\n");
foreach (var technical in data.TechnicalIndicator)
{
resultTextBox.AppendText("MIDPOINT: " + technical.MIDPOINT + "\n");
resultTextBox.AppendText("DateTime: " + technical.DateTime + "\n");
resultTextBox.AppendText("========================" + "\n");
}
}
}
}
}