-
Notifications
You must be signed in to change notification settings - Fork 2
MFI
This API returns the money flow index (MFI) values. The related REST API documentation is here
The very first thing to do before diving into MFI calls is to include the right namespace.
using Avapi.AvapiMFI
The MFI 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 MFI from it.
...
Int_MFI mfi =
connection.GetQueryObject_MFI();
To perform a MFI request you have 2 options:
- The request with constants:
IAvapiResponse_MFI Query(string symbol,
MFI_interval interval,
int time_period);
- The request without constants:
IAvapiResponse_MFI QueryPrimitive(string symbol,
string interval,
string time_period);
To perform an MFI asynchronous request you have 2 options:
- The request with constants:
async Task<IAvapiResponse_MFI> QueryAsync(string symbol,
MFI_interval interval,
int time_period);
- The request without constants:
async Task<IAvapiResponse_MFI> QueryAsync(string symbol,
string interval,
string time_period);
The parameters below are needed to perform the MFI 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 MFI value.
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:
- MFI_interval
MFI_interval: The time interval between two consecutive data points in the time series.
public enum MFI_interval
{
none,
n_1min,
n_5min,
n_15min,
n_30min,
n_60min,
daily,
weekly,
monthly
}
The response of a MFI request is an object that implements the IAvapiResponse_MFI interface.
public interface IAvapiResponse_MFI
{
string RawData
{
get;
}
IAvapiResponse_MFI_Content Data
{
get;
}
}
The IAvapiResponse_MFI 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_MFI_Content.
Complete Example of a Console App: Display the result of a MFI request by using the method Query (synchronous request)
using System;
using System.IO;
using Avapi.AvapiMFI;
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 MFI query object
Int_MFI mfi =
connection.GetQueryObject_MFI();
// Perform the MFI request and get the result
IAvapiResponse_MFI mfiResponse =
mfi.Query(
"MSFT",
Const_MFI.MFI_interval.n_1min,
10);
// Printout the results
Console.WriteLine("******** RAW DATA MFI ********");
Console.WriteLine(mfiResponse.RawData);
Console.WriteLine("******** STRUCTURED DATA MFI ********");
var data = mfiResponse.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("TimeZone: " + data.MetaData.TimeZone);
Console.WriteLine("========================");
Console.WriteLine("========================");
foreach (var technical in data.TechnicalIndicator)
{
Console.WriteLine("MFI: " + technical.MFI);
Console.WriteLine("DateTime: " + technical.DateTime);
Console.WriteLine("========================");
}
}
}
}
}
Complete Example of a Windows Form App: Display the result of a MFI request by using the method QueryAsync (asynchronous request)
using Avapi;
using Avapi.AvapiMFI
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private IAvapiConnection m_connection = AvapiConnection.Instance;
private Int_MFI m_mfi;
private IAvapiResponse_MFI m_mfiResponse;
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 MFI query object
m_mfi = m_connection.GetQueryObject_MFI();
base.OnLoad(e);
}
private async void MFIAsyncButton_Click(object sender, EventArgs e)
{
// Perform the MFI request and get the result
m_mfiResponse =
await m_mfi.QueryAsync(
"MSFT",
Const_MFI.MFI_interval.n_1min,
10);
// Show the results
resultTextBox.AppendText("******** RAW DATA MFI ********" + "\n");
resultTextBox.AppendText(m_mfiResponse.RawData + "\n");
resultTextBox.AppendText("******** STRUCTURED DATA MFI ********" + "\n");
var data = m_mfiResponse.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("TimeZone: " + data.MetaData.TimeZone + "\n");
resultTextBox.AppendText("========================" + "\n");
resultTextBox.AppendText("========================" + "\n");
foreach (var technical in data.TechnicalIndicator)
{
resultTextBox.AppendText("MFI: " + technical.MFI + "\n");
resultTextBox.AppendText("DateTime: " + technical.DateTime + "\n");
resultTextBox.AppendText("========================" + "\n");
}
}
}
}
}