{ "cells": [ { "cell_type": "markdown", "id": "68e3c2ce-ed33-4c9d-851c-86184d9d3414", "metadata": {}, "source": [ "# Load NASDAQ API key (Data Link API) - Silver\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "7629bec6-f9e7-4180-aa56-0c2e857d34cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import load_dotenv\n", "import os\n", "\n", "# Load the environment variables from .env file\n", "load_dotenv(\"api_keys\")" ] }, { "cell_type": "code", "execution_count": 23, "id": "aa5b3443-449e-4f02-b231-1ffdef4d11ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loaded key\n" ] } ], "source": [ "nasdaq_api_key = os.getenv('NASDAQ_API_KEY')\n", "if len(nasdaq_api_key) > 0:\n", " print(\"loaded key\")" ] }, { "cell_type": "markdown", "id": "5fdcd717-d4a3-4e7e-b8b5-a4369bd1bc83", "metadata": {}, "source": [ "## Retrieve 2 year of COT data (% shifts producers long / short)" ] }, { "cell_type": "code", "execution_count": 40, "id": "fc5d6a91-c107-443e-8b6e-84e10d945ca2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Open Interest Producer/Merchant/Processor/User Longs \\\n", "Date \n", "2023-11-28 139144.0 3472.0 \n", "2023-12-05 139753.0 4714.0 \n", "2023-12-12 134281.0 4311.0 \n", "2023-12-19 127549.0 3701.0 \n", "2023-12-26 131408.0 4063.0 \n", "\n", " Producer/Merchant/Processor/User Shorts Swap Dealer Longs \\\n", "Date \n", "2023-11-28 40323.0 33045.0 \n", "2023-12-05 41340.0 33328.0 \n", "2023-12-12 38849.0 30707.0 \n", "2023-12-19 39714.0 30908.0 \n", "2023-12-26 41011.0 29911.0 \n", "\n", " Swap Dealer Shorts Swap Dealer Spreads Money Manager Longs \\\n", "Date \n", "2023-11-28 44049.0 3770.0 45604.0 \n", "2023-12-05 48716.0 2932.0 43852.0 \n", "2023-12-12 43665.0 2832.0 33472.0 \n", "2023-12-19 43686.0 1911.0 34931.0 \n", "2023-12-26 45585.0 2415.0 36546.0 \n", "\n", " Money Manager Shorts Money Manager Spreads \\\n", "Date \n", "2023-11-28 21842.0 6885.0 \n", "2023-12-05 22322.0 7544.0 \n", "2023-12-12 24805.0 8439.0 \n", "2023-12-19 20060.0 6662.0 \n", "2023-12-26 19749.0 6847.0 \n", "\n", " Other Reportable Longs Other Reportable Shorts \\\n", "Date \n", "2023-11-28 17276.0 6758.0 \n", "2023-12-05 19389.0 4616.0 \n", "2023-12-12 22212.0 2421.0 \n", "2023-12-19 17700.0 2820.0 \n", "2023-12-26 18013.0 2947.0 \n", "\n", " Other Reportable Spreads Total Reportable Longs \\\n", "Date \n", "2023-11-28 2445.0 112497.0 \n", "2023-12-05 1607.0 113366.0 \n", "2023-12-12 1706.0 103679.0 \n", "2023-12-19 1577.0 97390.0 \n", "2023-12-26 2137.0 99932.0 \n", "\n", " Total Reportable Shorts Non Reportable Longs \\\n", "Date \n", "2023-11-28 126072.0 26647.0 \n", "2023-12-05 129077.0 26387.0 \n", "2023-12-12 122717.0 30602.0 \n", "2023-12-19 116430.0 30159.0 \n", "2023-12-26 120691.0 31476.0 \n", "\n", " Non Reportable Shorts \n", "Date \n", "2023-11-28 13072.0 \n", "2023-12-05 10676.0 \n", "2023-12-12 11564.0 \n", "2023-12-19 11119.0 \n", "2023-12-26 10717.0 \n" ] } ], "source": [ "import requests\n", "import pandas as pd\n", "from datetime import datetime\n", "import urllib.parse\n", "\n", "# API endpoint components\n", "base_url = \"https://data.nasdaq.com/api/v3/datasets/\"\n", "dataset = \"CFTC/084691_F_ALL\"\n", "data_format = \".json\"\n", "\n", "# Parameters\n", "params = {\n", " \"start_date\": \"2022-01-01\",\n", " \"end_date\": \"2024-01-01\",\n", " \"api_key\": nasdaq_api_key # Make sure this variable is defined\n", "}\n", "\n", "# Construct the full URL with parameters\n", "param_string = urllib.parse.urlencode(params)\n", "url = f\"{base_url}{dataset}{data_format}?{param_string}\"\n", "\n", "# Make the API request\n", "response = requests.get(url)\n", "\n", "# Check if the request was successful\n", "if response.status_code == 200:\n", " # Parse the JSON response\n", " data = response.json()\n", " \n", " # Extract the dataset from the response\n", " dataset = data['dataset']\n", " \n", " # Create a pandas DataFrame\n", " df = pd.DataFrame(dataset['data'], columns=dataset['column_names'])\n", " \n", " # Convert the date column to datetime\n", " df['Date'] = pd.to_datetime(df['Date'])\n", " \n", " # Set the date as the index\n", " df.set_index('Date', inplace=True)\n", " \n", " # Sort the DataFrame by date\n", " df.sort_index(inplace=True)\n", " \n", " # Display the first few rows of the DataFrame\n", " print(df.tail())\n", " \n", " # You can now use this DataFrame for further analysis or visualization\n", " \n", "else:\n", " print(f\"Error fetching data: {response.status_code}\")\n", " print(response.text)" ] }, { "cell_type": "code", "execution_count": 41, "id": "a1ab3a82-5682-4701-ab9e-414d6d4ff115", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['Open Interest', 'Producer/Merchant/Processor/User Longs',\n", " 'Producer/Merchant/Processor/User Shorts', 'Swap Dealer Longs',\n", " 'Swap Dealer Shorts', 'Swap Dealer Spreads', 'Money Manager Longs',\n", " 'Money Manager Shorts', 'Money Manager Spreads',\n", " 'Other Reportable Longs', 'Other Reportable Shorts',\n", " 'Other Reportable Spreads', 'Total Reportable Longs',\n", " 'Total Reportable Shorts', 'Non Reportable Longs',\n", " 'Non Reportable Shorts'],\n", " dtype='object')" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "code", "execution_count": 44, "id": "61ac72a5-b8f9-451f-ae0e-2c9b5ed7358a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Open Interest | \n", "Producer/Merchant/Processor/User Longs | \n", "Producer/Merchant/Processor/User Shorts | \n", "Swap Dealer Longs | \n", "Swap Dealer Shorts | \n", "Swap Dealer Spreads | \n", "Money Manager Longs | \n", "Money Manager Shorts | \n", "Money Manager Spreads | \n", "Other Reportable Longs | \n", "Other Reportable Shorts | \n", "Other Reportable Spreads | \n", "Total Reportable Longs | \n", "Total Reportable Shorts | \n", "Non Reportable Longs | \n", "Non Reportable Shorts | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
2023-11-28 | \n", "139144.0 | \n", "3472.0 | \n", "40323.0 | \n", "33045.0 | \n", "44049.0 | \n", "3770.0 | \n", "45604.0 | \n", "21842.0 | \n", "6885.0 | \n", "17276.0 | \n", "6758.0 | \n", "2445.0 | \n", "112497.0 | \n", "126072.0 | \n", "26647.0 | \n", "13072.0 | \n", "
2023-12-05 | \n", "139753.0 | \n", "4714.0 | \n", "41340.0 | \n", "33328.0 | \n", "48716.0 | \n", "2932.0 | \n", "43852.0 | \n", "22322.0 | \n", "7544.0 | \n", "19389.0 | \n", "4616.0 | \n", "1607.0 | \n", "113366.0 | \n", "129077.0 | \n", "26387.0 | \n", "10676.0 | \n", "
2023-12-12 | \n", "134281.0 | \n", "4311.0 | \n", "38849.0 | \n", "30707.0 | \n", "43665.0 | \n", "2832.0 | \n", "33472.0 | \n", "24805.0 | \n", "8439.0 | \n", "22212.0 | \n", "2421.0 | \n", "1706.0 | \n", "103679.0 | \n", "122717.0 | \n", "30602.0 | \n", "11564.0 | \n", "
2023-12-19 | \n", "127549.0 | \n", "3701.0 | \n", "39714.0 | \n", "30908.0 | \n", "43686.0 | \n", "1911.0 | \n", "34931.0 | \n", "20060.0 | \n", "6662.0 | \n", "17700.0 | \n", "2820.0 | \n", "1577.0 | \n", "97390.0 | \n", "116430.0 | \n", "30159.0 | \n", "11119.0 | \n", "
2023-12-26 | \n", "131408.0 | \n", "4063.0 | \n", "41011.0 | \n", "29911.0 | \n", "45585.0 | \n", "2415.0 | \n", "36546.0 | \n", "19749.0 | \n", "6847.0 | \n", "18013.0 | \n", "2947.0 | \n", "2137.0 | \n", "99932.0 | \n", "120691.0 | \n", "31476.0 | \n", "10717.0 | \n", "