Files
LLM_Engineering_OLD/week1/community-contributions/day1-BitcoinMarketPrediction.ipynb

231 lines
7.4 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"id": "7db973a2-c95e-4939-a0d7-b54edec4d2cf",
"metadata": {},
"source": [
"# Bitcoin Market Prediction uisng CoinmarketCap\n",
"An AI-powered project using historical CoinMarketCap data to predict Bitcoin price trends and offer actionable insights for traders."
]
},
{
"cell_type": "markdown",
"id": "b792b517-bbc8-4e2c-bff2-45fad1a784dc",
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51523d62-825a-4a15-aec2-7c910beb5fda",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI"
]
},
{
"cell_type": "markdown",
"id": "2e3816b0-4557-4225-bfb9-9933d813548a",
"metadata": {},
"source": [
"## .env configuration"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02be59e7-01cc-41b5-88c3-a47860570078",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")"
]
},
{
"cell_type": "markdown",
"id": "3fc32555-ea4e-45fe-ad44-9dbf4441afd1",
"metadata": {},
"source": [
"### This line creates an authenticated OpenAI client instance, used to make API requests in your code."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0845c687-6610-4f83-89e8-fb94bc47ddd2",
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"openai = OpenAI(api_key=api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d140db1a-dd72-4986-8f38-09f8d8f97b00",
"metadata": {},
"outputs": [],
"source": [
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Website:\n",
"\n",
" def __init__(self, url):\n",
" \"\"\"\n",
" Create this Website object from the given url using the BeautifulSoup library\n",
" \"\"\"\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" soup = BeautifulSoup(response.content, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fdc96768-94a8-4a08-acf1-32a62b699b94",
"metadata": {},
"outputs": [],
"source": [
"system_prompt = \"\"\"\n",
"You are an intelligent assistant specialized in Bitcoin market prediction. Your tasks are:\n",
"\n",
"- Collect, preprocess, and analyze historical Bitcoin price and volume data sourced from CoinMarketCap historical data tables or API.\n",
"- Extract relevant time series and technical features from OHLC (open, high, low, close) and volume data.\n",
"- Use machine learning or statistical models to forecast future Bitcoin price trends.\n",
"- Output clear, concise, and actionable insights, focusing on predicted price direction and potential trading signals.\n",
"- Ensure all data collection respects CoinMarketCaps terms of service.\n",
"- Present findings in user-friendly language, explaining prediction confidence and market risks.\n",
"- Continuously improve prediction accuracy through back-testing on updated datasets.\n",
"\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d39e983-5b65-4de1-bdf0-e4239c3eb03f",
"metadata": {},
"outputs": [],
"source": [
"def user_prompt_for(website):\n",
" user_prompt = f\"You are analyzing historical Bitcoin market data from the webpage titled '{website.title}'.\\n\"\n",
" user_prompt += (\n",
" \"The data includes daily open, high, low, close prices, trading volume, \"\n",
" \"and market capitalization presented in a table format.\\n\"\n",
" \"Please provide a clear and concise analysis in Markdown format, focusing on recent trends, \"\n",
" \"price movements, volatility, and any insights that could help forecast Bitcoin price directions.\\n\"\n",
" \"If possible, include technical indicators, significant patterns, or notable market events mentioned in the data.\\n\\n\"\n",
" )\n",
" user_prompt += website.text\n",
" return user_prompt\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3d41ed3-4753-49f2-b51f-37e8be43102c",
"metadata": {},
"outputs": [],
"source": [
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0eb99fcf-75a2-41b8-bf53-568f94264438",
"metadata": {},
"outputs": [],
"source": [
"# And now: call the OpenAI API. You will get very familiar with this!\n",
"\n",
"def summarize(url):\n",
" website = Website(url)\n",
" response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages_for(website)\n",
" )\n",
" return response.choices[0].message.content\n",
"\n",
"# A function to display this nicely in the Jupyter output, using markdown\n",
"\n",
"def display_summary(summary): \n",
" display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0e57921-5132-40c6-834b-03a11a96425c",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://coinmarketcap.com/currencies/bitcoin/historical-data/3\"\n",
"summary = summarize(url)\n",
"display_summary(summary)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19d9b69a-6493-402d-a0b4-a486c322c816",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}