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

What STOCHF does?

This API returns the stochastic fast (STOCHF) values. The related REST API documentation is here


Including the STOCHF namespace

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


using Avapi.AvapiSTOCHF

How to get a STOCHF object?

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


...
Int_STOCHF stochf = 
	connection.GetQueryObject_STOCHF();

Perform a STOCHF Synchronous Request

To perform a STOCHF request you have 2 options:

  1. The request with constants:

IAvapiResponse_STOCHF Query(string symbol,
		STOCHF_interval interval,
		int fastkperiod [OPTIONAL],
		int fastdperiod [OPTIONAL],
		STOCHF_fastdmatype fastdmatype [OPTIONAL]);

  1. The request without constants:

IAvapiResponse_STOCHF QueryPrimitive(string symbol,
		string interval,
		string fastkperiod [OPTIONAL],
		string fastdperiod [OPTIONAL],
		string fastdmatype [OPTIONAL]);

Perform an STOCHF Asynchronous Request

To perform an STOCHF asynchronous request you have 2 options:

  1. The request with constants:

async Task<IAvapiResponse_STOCHF> QueryAsync(string symbol,
		STOCHF_interval interval,
		int fastkperiod [OPTIONAL],
		int fastdperiod [OPTIONAL],
		STOCHF_fastdmatype fastdmatype [OPTIONAL]);

  1. The request without constants:

async Task<IAvapiResponse_STOCHF> QueryAsync(string symbol,
		string interval,
		string fastkperiod [OPTIONAL],
		string fastdperiod [OPTIONAL],
		string fastdmatype [OPTIONAL]);

Parameters

The parameters below are needed to perform the STOCHF request.

  • symbol: The name of the equity
  • interval: The time interval between two consecutive data points in the time series.
  • fastkperiod [OPTIONAL]: It is a optional value; positive integers are accepted. By default, fastkperiod=5
  • fastdperiod [OPTIONAL]: It is a optional value; positive integers are accepted. By default, fastdperiod=3
  • fastdmatype [OPTIONAL]: Moving average type for the fastd moving average, it is a optional value. By default: fastdmatype=0, check the Alphavantage documentation for the mapping.

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:

  • STOCHF_interval
  • STOCHF_fastdmatype

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


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

STOCHF_fastdmatype: Moving average type for the fastd moving average, it is a optional value. By default: fastdmatype=0, check the Alphavantage documentation for the mapping.


public enum STOCHF_fastdmatype
{
	none,
	n_0,
	n_1,
	n_2,
	n_3,
	n_4,
	n_5,
	n_6,
	n_7,
	n_8
}


STOCHF Response

The response of a STOCHF request is an object that implements the IAvapiResponse_STOCHF interface.


public interface IAvapiResponse_STOCHF
{
    string RawData
    {
        get;
    }
    IAvapiResponse_STOCHF_Content Data
    {
        get;
    }
}

The IAvapiResponse_STOCHF 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_STOCHF_Content.

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


using System;
using System.IO;
using Avapi.AvapiSTOCHF;

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 STOCHF query object
            Int_STOCHF stochf =
                connection.GetQueryObject_STOCHF();

            // Perform the STOCHF request and get the result
            IAvapiResponse_STOCHF stochfResponse = 
            stochf.Query(
                 "MSFT",
                 Const_STOCHF.STOCHF_interval.n_1min,
                 10,
                 10,
                 Const_STOCHF.STOCHF_fastdmatype.n_0);

            // Printout the results
            Console.WriteLine("******** RAW DATA STOCHF ********");
            Console.WriteLine(stochfResponse.RawData);

            Console.WriteLine("******** STRUCTURED DATA STOCHF ********");
            var data = stochfResponse.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("FastKPeriod: " + data.MetaData.FastKPeriod);
                Console.WriteLine("FastDPeriod: " + data.MetaData.FastDPeriod);
                Console.WriteLine("FastDMAType: " + data.MetaData.FastDMAType);
                Console.WriteLine("TimeZone: " + data.MetaData.TimeZone);
                Console.WriteLine("========================");
                Console.WriteLine("========================");
                foreach (var technical in data.TechnicalIndicator)
                {
                    Console.WriteLine("FastK: " + technical.FastK);
                    Console.WriteLine("FastD: " + technical.FastD);
                    Console.WriteLine("DateTime: " + technical.DateTime);
                    Console.WriteLine("========================");
                }
            }
        }
    }
}

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


using Avapi;
using Avapi.AvapiSTOCHF
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private IAvapiConnection m_connection = AvapiConnection.Instance;
        private Int_STOCHF m_stochf;
        private IAvapiResponse_STOCHF m_stochfResponse;

        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 STOCHF query object
            m_stochf = m_connection.GetQueryObject_STOCHF();

            base.OnLoad(e);
        }

        private async void STOCHFAsyncButton_Click(object sender, EventArgs e)
        {
            // Perform the STOCHF request and get the result
            m_stochfResponse = 
                await m_stochf.QueryAsync(
                     "MSFT",
                     Const_STOCHF.STOCHF_interval.n_1min,
                     10,
                     10,
                     Const_STOCHF.STOCHF_fastdmatype.n_0);

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

            resultTextBox.AppendText("******** STRUCTURED DATA STOCHF ********" + "\n");
            var data = m_stochfResponse.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("FastKPeriod: " + data.MetaData.FastKPeriod + "\n");
                resultTextBox.AppendText("FastDPeriod: " + data.MetaData.FastDPeriod + "\n");
                resultTextBox.AppendText("FastDMAType: " + data.MetaData.FastDMAType + "\n");
                resultTextBox.AppendText("TimeZone: " + data.MetaData.TimeZone + "\n");
                resultTextBox.AppendText("========================" + "\n");
                resultTextBox.AppendText("========================" + "\n");
                foreach (var technical in data.TechnicalIndicator)
                {
                    resultTextBox.AppendText("FastK: " + technical.FastK + "\n");
                    resultTextBox.AppendText("FastD: " + technical.FastD + "\n");
                    resultTextBox.AppendText("DateTime: " + technical.DateTime + "\n");
                    resultTextBox.AppendText("========================" + "\n");
                }
            }
        }
    }
}

Clone this wiki locally