Skip to content
essegisolutions edited this page Jan 13, 2018 · 8 revisions

What BOP does?

This API returns the balance of power (BOP) values. The related REST API documentation is here


Including the BOP namespace

The very first thing to do before diving into BOP calls is to include the right namespace.


using Avapi.AvapiBOP

How to get a BOP object?

The BOP 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 BOP from it.


...
Int_BOP bop = 
	connection.GetQueryObject_BOP();

Perform a BOP Synchronous Request

To perform a BOP request you have 2 options:

  1. The request with constants:

IAvapiResponse_BOP Query(string symbol,
		BOP_interval interval);

  1. The request without constants:

IAvapiResponse_BOP QueryPrimitive(string symbol,
		string interval);

Perform an BOP Asynchronous Request

To perform an BOP asynchronous request you have 2 options:

  1. The request with constants:

async Task<IAvapiResponse_BOP> QueryAsync(string symbol,
		BOP_interval interval);

  1. The request without constants:

async Task<IAvapiResponse_BOP> QueryAsync(string symbol,
		string interval);

Parameters

The parameters below are needed to perform the BOP request.

  • symbol: The name of the equity
  • interval: The time interval between two consecutive data points in the time series.

Please notice that the info above are copied from the official alphavantage documentation, that you can find here.


The request with constants

The request with constants implies the use of different enums:

  • BOP_interval

BOP_interval: The time interval between two consecutive data points in the time series.


public enum BOP_interval
{
	none,
	n_1min,
	n_5min,
	n_15min,
	n_30min,
	n_60min,
	daily,
	weekly,
	monthly
}


BOP Response

The response of a BOP request is an object that implements the IAvapiResponse_BOP interface.


public interface IAvapiResponse_BOP
{
    string RawData
    {
        get;
    }
    IAvapiResponse_BOP_Content Data
    {
        get;
    }
}

The IAvapiResponse_BOP 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_BOP_Content.

Complete Example of a Console App: Display the result of a BOP request by using the method Query (synchronous request)


using System;
using System.IO;
using Avapi.AvapiBOP;

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 BOP query object
            Int_BOP bop =
                connection.GetQueryObject_BOP();

            // Perform the BOP request and get the result
            IAvapiResponse_BOP bopResponse = 
            bop.Query(
                 "MSFT",
                 Const_BOP.BOP_interval.n_1min);

            // Printout the results
            Console.WriteLine("******** RAW DATA BOP ********");
            Console.WriteLine(bopResponse.RawData);

            Console.WriteLine("******** STRUCTURED DATA BOP ********");
            var data = bopResponse.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("TimeZone: " + data.MetaData.TimeZone);
                Console.WriteLine("========================");
                Console.WriteLine("========================");
                foreach (var technical in data.TechnicalIndicator)
                {
                    Console.WriteLine("BOP: " + technical.BOP);
                    Console.WriteLine("DateTime: " + technical.DateTime);
                    Console.WriteLine("========================");
                }
            }
        }
    }
}

Complete Example of a Windows Form App: Display the result of a BOP request by using the method QueryAsync (asynchronous request)


using Avapi;
using Avapi.AvapiBOP
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private IAvapiConnection m_connection = AvapiConnection.Instance;
        private Int_BOP m_bop;
        private IAvapiResponse_BOP m_bopResponse;

        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 BOP query object
            m_bop = m_connection.GetQueryObject_BOP();

            base.OnLoad(e);
        }

        private async void BOPAsyncButton_Click(object sender, EventArgs e)
        {
            // Perform the BOP request and get the result
            m_bopResponse = 
                await m_bop.QueryAsync(
                     "MSFT",
                     Const_BOP.BOP_interval.n_1min);

             // Show the results
            resultTextBox.AppendText("******** RAW DATA BOP ********" + "\n");
            resultTextBox.AppendText(m_bopResponse.RawData + "\n");

            resultTextBox.AppendText("******** STRUCTURED DATA BOP ********" + "\n");
            var data = m_bopResponse.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("TimeZone: " + data.MetaData.TimeZone + "\n");
                resultTextBox.AppendText("========================" + "\n");
                resultTextBox.AppendText("========================" + "\n");
                foreach (var technical in data.TechnicalIndicator)
                {
                    resultTextBox.AppendText("BOP: " + technical.BOP + "\n");
                    resultTextBox.AppendText("DateTime: " + technical.DateTime + "\n");
                    resultTextBox.AppendText("========================" + "\n");
                }
            }
        }
    }
}

Clone this wiki locally