Merge branch 'Symen11-master-patch-c584' into 'master'
Create file 02_API.ipynb See merge request deemanone/materia!2
This commit is contained in:
177
notebooks/02_API.ipynb
Normal file
177
notebooks/02_API.ipynb
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\t1. 📈 Market Prices & Volume\n",
|
||||||
|
"\t\t○ Yahoo Finance API → Futures Prices # via yfinance or yahoo_fin\n",
|
||||||
|
"\t\t○ Alpha Vantage API → Market Data # https://www.alphavantage.co/documentation/ - key: H4RIQ01UTWVVVVN3\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\t2. 🌱 Supply & Demand Reports\n",
|
||||||
|
"\t\t○ USDA API → WASDE Report, Crop Production # https://www.ers.usda.gov/developer/data-apis - key: n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\n",
|
||||||
|
"\t\t○ FAO API → Global Coffee Production # https://data.apps.fao.org/gismgr/web/v2/#/workspaces - key: a5961a8c8cc9852265754a4e414d7faa7bf1789e7d49245001be2e75e7e5d908cd29eb3eaf63d782\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\t3. 💵 Macroeconomic Indicators\n",
|
||||||
|
"\t\t○ FRED API → Interest rates, Inflation, Employment # https://fredaccount.stlouisfed.org/ - key: bc80b84ca1d2bb8283b065bd0455d84b\n",
|
||||||
|
"\t\t○ EIA API → Fuel/Oil Prices for Transportation # https://www.eia.gov/opendata/ - key: e0PMDw5AvTblYgotvAIB7aPyNcS2X2n5BZ4aayfB\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\t4. 📝 Options Positioning & Futures Sentiment\n",
|
||||||
|
"\t\t○ CFTC COT Data → Commitment of Traders Report (CFTC) # cot_reports or Socrata Open Data Api https://dev.socrata.com/foundry/publicreporting.cftc.gov/6dca-aqww\n",
|
||||||
|
"\t\t○ Barchart API → Options Sentiment & Futures Positioning (Free Tier Available but need to sign up)\n",
|
||||||
|
"\t\t○ CBOE → Put/Call Ratios (via Web Scraping) # Barchart API alternatively\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\t5. ⛅ Weather Data (Affects Coffee Crop Yields)\n",
|
||||||
|
"\t\t○ OpenWeatherMap API → Real-Time Weather for Commodity Regions # https://openweathermap.org/ - key: b781d014d69dca442c50ce4cc64fbbca\n",
|
||||||
|
"\t\t○ NOAA API → Historical Climate Trends #https://www.ncdc.noaa.gov/cdo-web/ - key: fcYPiwZurnEAGcrudtMNXhIavzfHNdRU\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\t6. 📦 Coffee Supply Chain Data\n",
|
||||||
|
"\t\t○ ICE US Futures Data → Warehouse Stocks (via Web Scraping) # via USDA / EIA / Quandl / Barchart\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#Example Code for API Requests\n",
|
||||||
|
"import requests\n",
|
||||||
|
"import yfinance as yf\n",
|
||||||
|
"from alpha_vantage.timeseries import TimeSeries\n",
|
||||||
|
"from fredapi import Fred\n",
|
||||||
|
"from pyowm.owm import OWM\n",
|
||||||
|
"import datetime\n",
|
||||||
|
"import json\n",
|
||||||
|
"\n",
|
||||||
|
"# --- 1. 📈 Market Prices & Volume --------------------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- Market Prices: Coffee & Cocoa Futures (Yahoo Finance) ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"coffee = yf.Ticker(\"KC=F\") # Coffee C futures\n",
|
||||||
|
"cocoa = yf.Ticker(\"CC=F\") # Cocoa futures\n",
|
||||||
|
"\n",
|
||||||
|
"coffee_data = coffee.history(period=\"5d\")[['Close', 'Volume']]\n",
|
||||||
|
"cocoa_data = cocoa.history(period=\"5d\")[['Close', 'Volume']]\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Coffee Futures:\\n\", coffee_data)\n",
|
||||||
|
"print(\"Cocoa Futures:\\n\", cocoa_data)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- Alpha Vantage Market Data (USD Index as proxy) ---\")\n",
|
||||||
|
"av_key = 'H4RIQ01UTWVVVVN3'\n",
|
||||||
|
"ts = TimeSeries(key=av_key, output_format='pandas')\n",
|
||||||
|
"data, meta = ts.get_intraday(symbol='USD', interval='15min', outputsize='compact')\n",
|
||||||
|
"print(data.head())\n",
|
||||||
|
"\n",
|
||||||
|
"# --- 2. 🌱 Supply & Demand Reports --------------------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- USDA: Coffee & Cocoa Production Reports ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"usda_key = \"n1cDatmPp2b18qqKOgDKVndjdrUk4oUgB9HALoVA\"\n",
|
||||||
|
"usda_url = f\"https://api.nal.usda.gov/fdc/v1/foods/search?query=coffee&api_key={usda_key}\"\n",
|
||||||
|
"\n",
|
||||||
|
"response = requests.get(usda_url)\n",
|
||||||
|
"if response.status_code == 200:\n",
|
||||||
|
" print(\"USDA Coffee Report:\\n\", json.dumps(response.json(), indent=2))\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"USDA API error:\", response.status_code)\n",
|
||||||
|
"\n",
|
||||||
|
"# --- FAO API Call (Coffee Production Dataset) ---\n",
|
||||||
|
"# FAO is complex — below is a real coffee dataset pull from their FAOSTAT bulk API\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- FAO: Global Coffee Production Data ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"fao_dataset_url = \"http://fenixservices.fao.org/faostat/api/v1/en/data/QC?area=all&item=Coffee%2C%20green&element=Production%20Quantity&year=2021&format=JSON\"\n",
|
||||||
|
"\n",
|
||||||
|
"response = requests.get(fao_dataset_url)\n",
|
||||||
|
"if response.status_code == 200:\n",
|
||||||
|
" coffee_fao_data = response.json().get('data', [])[:5]\n",
|
||||||
|
" print(\"FAO Coffee Production (2021):\\n\", coffee_fao_data)\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"FAO API error:\", response.status_code)\n",
|
||||||
|
"\n",
|
||||||
|
"# --- 3. 💵 Macroeconomic Indicators --------------------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- FRED: Interest Rates & Inflation ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"fred = Fred(api_key=\"bc80b84ca1d2bb8283b065bd0455d84b\")\n",
|
||||||
|
"fed_rate = fred.get_series('FEDFUNDS').tail()\n",
|
||||||
|
"inflation = fred.get_series('CPIAUCSL').tail()\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Fed Funds Rate:\\n\", fed_rate)\n",
|
||||||
|
"print(\"Inflation (CPI):\\n\", inflation)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- EIA: WTI Crude Oil Prices (Transportation Cost Proxy) ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"eia_key = \"e0PMDw5AvTblYgotvAIB7aPyNcS2X2n5BZ4aayfB\"\n",
|
||||||
|
"eia_url = f\"https://api.eia.gov/series/?api_key={eia_key}&series_id=PET.RWTC.D\"\n",
|
||||||
|
"\n",
|
||||||
|
"response = requests.get(eia_url)\n",
|
||||||
|
"if response.status_code == 200:\n",
|
||||||
|
" print(\"EIA Crude Prices:\\n\", json.dumps(response.json()['series'][0]['data'][:5], indent=2))\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"EIA API error:\", response.status_code)\n",
|
||||||
|
"\n",
|
||||||
|
"# --- 4. 📝 Futures Sentiment & COT Positioning --------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- CFTC: Commitment of Traders Report for Coffee ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"cot_url = \"https://publicreporting.cftc.gov/resource/6dca-aqww.json?$limit=5&commodity=COFFEE\"\n",
|
||||||
|
"response = requests.get(cot_url)\n",
|
||||||
|
"if response.status_code == 200:\n",
|
||||||
|
" print(\"COT Coffee Report:\\n\", response.json())\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"CFTC COT API error:\", response.status_code)\n",
|
||||||
|
"\n",
|
||||||
|
"# --- 5. ⛅ Weather Data for Key Growing Regions --------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- Weather: Real-Time Conditions for Coffee/Cocoa Regions ---\")\n",
|
||||||
|
"\n",
|
||||||
|
"owm_key = 'b781d014d69dca442c50ce4cc64fbbca'\n",
|
||||||
|
"owm = OWM(owm_key)\n",
|
||||||
|
"mgr = owm.weather_manager()\n",
|
||||||
|
"\n",
|
||||||
|
"regions = {\n",
|
||||||
|
" \"Sao Paulo, Brazil (Coffee)\": \"Sao Paulo,BR\",\n",
|
||||||
|
" \"Addis Ababa, Ethiopia (Coffee)\": \"Addis Ababa,ET\",\n",
|
||||||
|
" \"Abidjan, Ivory Coast (Cocoa)\": \"Abidjan,CI\",\n",
|
||||||
|
" \"Accra, Ghana (Cocoa)\": \"Accra,GH\"\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"for region_name, location in regions.items():\n",
|
||||||
|
" weather = mgr.weather_at_place(location).weather\n",
|
||||||
|
" temp = weather.temperature('celsius')\n",
|
||||||
|
" print(f\"\\nWeather in {region_name}:\")\n",
|
||||||
|
" print(f\"Temperature: {temp['temp']}°C, Humidity: {weather.humidity}%, Condition: {weather.detailed_status}\")\n",
|
||||||
|
"\n",
|
||||||
|
"# --- 6. 📦 Supply Chain / Warehouse Stocks ------------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\n--- Supply Chain Note: Warehouse stocks usually require web scraping or paid APIs (ICE, USDA GAIN, Barchart).\")\n",
|
||||||
|
"\n",
|
||||||
|
"# Placeholder for warehouse stock via scraping or subscription APIs like Quandl or ICE\n",
|
||||||
|
"# Alternative: USDA GAIN reports: https://apps.fas.usda.gov/gainsearch/\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"language_info": {
|
||||||
|
"name": "python"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user