Coding

Computing the Total Movement of a Stock Over Time with Financial Modeling Prep’s API

DISCLAIMER: This is not financial education or advice. I am not a financial expert. Financial markets are extremely risky. Proceed with caution.

In this tutorial, we are going to create something I call a Total Movement Calculator. The way you can look at a stock over time is that if it goes from say 50 dollars per share to 150 in a perfectly linear fashion, that stock will have moved in the shortest distance possible. For that stock, you would have had the opportunity to make exactly $100 by going long. However, if there was some fluctuation in the price, say at one point it started at 50, went down to 25, up to 175, and settled at 150, there would have been $25 to make on the way down from $50 – > $25 by going short, $150 to be made going up to $175, and then another $25 to be made going down to $150 from $175 by going short again. So the total profit off of this volatility is greater. It’s $200 ($25 + $150 + $25). Let’s build something that allows you to choose the interval and duration, and we’ll calculate total maximum perfect profit.

Inputs

So let’s define the input items we’ll need to accept. We’ll create a load file with a python dictionary. In that dictionary, we’re going to need the following inputs:

  • Stock Symbol
  • Period (1 minute, 5 minute, 15 minute, 30 minute, hourly, 4 hour, and daily)

Return value

I’ve thought about it. And for the scope of this part of the project, we’re just going to return the absolute summed value of percentage changes. For example, we take the 1 minute high and low, subtract the high from the low, take the absolute value, and divide it by the price. We then sub that up and that tells us the total movement as an absolute value, regardless of whether its going up or down, as a percentage of that current price. I divided by current price because the number isn’t as comparable because not all stocks are priced the same.

The Code

Imports:

import loadfile
try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen
import certifi
import json
def get_jsonparsed_data(url):
    response = urlopen(url, cafile=certifi.where())
    data = response.read().decode("utf-8")
    return json.loads(data)

Obtaining the JSON object from the URL in Financial Modeling Prep API

def get_jsonparsed_data(url):
    response = urlopen(url, cafile=certifi.where())
    data = response.read().decode("utf-8")
    return json.loads(data)

Computing the Absolute Value Percentage

def query_api(symbol,interval):
    i = 0
    pricesum=0
    base_url = loadfile.loadfile["endpoints"][interval]
    url = base_url + symbol + "?apikey=" + loadfile.loadfile['endpoints']['api_key']
    the_data = get_jsonparsed_data(url)
    sum_of_movement = 0
    num_of_data_points = len(the_data)
    print(the_data[0])
    while(i < num_of_data_points):
        absolute_value_ofIndividual_movement = abs((the_data[i]['high']-the_data[i]['low']))
        absolute_value_ofIndividual_movement_as_percent = (absolute_value_ofIndividual_movement / the_data[i]['high'])
        sum_of_movement = sum_of_movement +absolute_value_ofIndividual_movement_as_percent
        print(sum_of_movement)
        i = i + 1
    print('between the dats of: ' + the_data[0]['date'] + 'and: ' + the_data[num_of_data_points-1]['date'])
    return sum_of_movement

result = query_api("AAPL",'1min')

The Full Output

between the dates of: 2021-11-17 16:00:00 and: 2021-11-15 14:55:00
0.579429187142

Interpreting the Output

What this might tell me is that if I perfectly went long and short minute to minute over the course of these two days, then I could have earned a 57% return. This is completely hypothetical and basically impossible to do in practice.