|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In this blog post, we delve into cryptocurrency price prediction using CoinGecko API and machine learning – the ability to learn patterns from data and make informed predictions.
このブログ投稿では、CoinGecko API と機械学習 (データからパターンを学習し、情報に基づいた予測を行う機能) を使用した暗号通貨の価格予測について詳しく説明します。

Level of Functionality
We'll first examine the key functionalities of our Python program:
機能のレベルまず、Python プログラムの主要な機能を調べます。
- Fetching Data: The program fetches real-time and historical data from the CoinGecko API. This includes the current market data, historical data, and OHLC (Open, High, Low, Close) data for a specific coin.
- Data Processing: The program processes the fetched data to prepare it for the machine learning model. This includes selecting relevant features, scaling the data, and formatting it into the appropriate shape.
- Machine Learning Model: The program uses a Linear Regression model from the Scikit-learn library to learn from the processed data.
- Price Prediction: The program uses the trained model to predict future prices of the selected cryptocurrency.
- Displaying Predictions: The program displays the predicted prices to the user in a simple and intuitive web interface. The user can select a coin and the number of days for which they want to predict prices, and the program will display the predictions in a table.
- User Interaction: The program provides an interactive form for the user to input their preferences. It also handles invalid inputs gracefully by displaying an error message.
- Security Measures: The program ensures the security of sensitive data such as API keys and private keys. It uses secure connections and encrypts sensitive data.
Which machine learning model is best for crypto price prediction?
Among the various machine learning models for crypto price prediction, the Linear Regression model is most optimal for predicting crypto prices because it is simple to understand and implement, and works well with linearly separable data. This model assumes a linear relationship between the input variables (independent variables) and a single output variable (dependent variable).
データの取得: プログラムは、CoinGecko API からリアルタイム データと履歴データを取得します。これには、特定のコインの現在の市場データ、過去のデータ、OHLC (始値、高値、安値、終値) データが含まれます。 データ処理: プログラムは、取得したデータを処理して、機械学習モデル用に準備します。これには、関連する特徴の選択、データのスケーリング、および適切な形状へのフォーマット設定が含まれます。機械学習モデル: プログラムは、Scikit-learn ライブラリの線形回帰モデルを使用して、処理されたデータから学習します。価格予測: プログラムは、トレーニングされたデータを使用します。選択した暗号通貨の将来の価格を予測するモデル。予測の表示: プログラムは、シンプルで直感的な Web インターフェイスで予測価格をユーザーに表示します。ユーザーは、価格を予測したいコインと日数を選択すると、プログラムが予測を表に表示します。 ユーザーインタラクション: プログラムは、ユーザーが好みを入力するためのインタラクティブなフォームを提供します。また、エラー メッセージを表示することで、無効な入力を適切に処理します。セキュリティ対策: このプログラムは、API キーや秘密キーなどの機密データのセキュリティを確保します。安全な接続を使用し、機密データを暗号化します。仮想通貨価格の予測にはどの機械学習モデルが最適ですか?仮想通貨価格予測のためのさまざまな機械学習モデルの中で、線形回帰モデルは理解と実装が簡単であるため、仮想通貨価格の予測に最も最適です。 、線形分離可能なデータでうまく機能します。このモデルは、入力変数 (独立変数) と単一の出力変数 (従属変数) の間に線形関係があると仮定します。
When this assumption holds true, Linear Regression can provide a good baseline model for crypto price prediction. However, it’s important to note that crypto prices are influenced by a multitude of factors and may not always exhibit a linear relationship. Therefore, more complex models may sometimes offer better predictive performance. Always remember to validate the assumptions of your model and test its performance using appropriate metrics.
この仮定が当てはまる場合、線形回帰は暗号通貨の価格予測に優れたベースライン モデルを提供できます。ただし、仮想通貨の価格は多数の要因の影響を受け、必ずしも直線的な関係を示すわけではないことに注意することが重要です。したがって、より複雑なモデルの方がより優れた予測パフォーマンスを提供できる場合があります。モデルの仮定を検証し、適切なメトリクスを使用してパフォーマンスをテストすることを常に忘れないでください。
Prerequisites
Before we start, ensure you have the following:
前提条件開始する前に、次のものが揃っていることを確認してください。
- Python 3.7 or higher installed on your system. Python is a powerful, easy-to-learn programming language that we will use to build our bot.
- A basic understanding of Python programming. While this guide will be detailed, having a basic understanding of Python syntax and programming concepts will be beneficial.
- A text editor: You will need a text editor or an Integrated Development Environment (IDE) to write your code. Some popular options include Visual Studio Code, Sublime Text, and Atom.
- CoinGecko API (Demo): We will be using CoinGecko API to fetch the market chart data for cryptocurrencies. The CoinGecko API has a free Demo plan accessible to all users with a 30 calls/min rate limit and a monthly cap of 10,000 calls. This guide shows you how to generate your Demo API key.
Do note that the API key used in this guide is a placeholder and not a real API key. Please replace it with your own CoinGecko API key for the code to work.
Python 3.7 以降がシステムにインストールされていること。 Python は、ボットの構築に使用する強力で学びやすいプログラミング言語です。Python プログラミングの基本的な理解。このガイドでは詳細が説明されていますが、Python 構文とプログラミング概念の基本を理解していると役に立ちます。テキスト エディター: コードを作成するには、テキスト エディターまたは統合開発環境 (IDE) が必要です。人気のあるオプションには、Visual Studio Code、Sublime Text、Atom.CoinGecko API (デモ) などがあります。CoinGecko API を使用して、暗号通貨の市場チャート データを取得します。 CoinGecko API には、すべてのユーザーがアクセスできる無料のデモ プランがあり、30 コール/分のレート制限と月間 10,000 コールの上限があります。このガイドでは、デモ API キーを生成する方法を説明します。このガイドで使用される API キーはプレースホルダーであり、実際の API キーではないことに注意してください。コードが機能するには、これを独自の CoinGecko API キーに置き換えてください。
The Python Program
The program is a Flask web application. It starts by importing necessary libraries and initializing a Flask app:
Python プログラムこのプログラムは Flask Web アプリケーションです。まず、必要なライブラリをインポートし、Flask アプリを初期化します。
In this section, we’ll break down the Python code for our Cryptocurrency prediction to understand each part and its role in the overall functionality of the predictor:
このセクションでは、仮想通貨予測の Python コードを分析して、予測器の全体的な機能における各部分とその役割を理解します。
Importing Necessary Libraries and Modules: The program commences by importing the necessary libraries and modules. These include Flask, a micro web framework written in Python that provides tools, technologies, and possibilities for web application development. It also imports requests, a Python module used for making various types of HTTP requests like GET and POST.
必要なライブラリとモジュールのインポート: プログラムは、必要なライブラリとモジュールをインポートすることから始まります。これらには、Web アプリケーション開発のためのツール、テクノロジー、可能性を提供する Python で書かれたマイクロ Web フレームワークである Flask が含まれます。また、GET や POST などのさまざまなタイプの HTTP リクエストを作成するために使用される Python モジュールであるリクエストもインポートします。
Setting Up Flask Application and API Key: The Flask application is set up, creating an instance of the Flask class which is our WSGI application. The API key for accessing the CoinGecko API, a comprehensive cryptocurrency API that provides access to a wide range of data, is defined.
Flask アプリケーションと API キーのセットアップ: Flask アプリケーションがセットアップされ、WSGI アプリケーションである Flask クラスのインスタンスが作成されます。幅広いデータへのアクセスを提供する包括的な暗号通貨 API である CoinGecko API にアクセスするための API キーが定義されています。
Defining the Main Route: The main route (/) is defined, which supports both GET and POST methods. When a GET request is made, it fetches the list of available coins from the CoinGecko API. This is done by making a GET request to the CoinGecko API endpoint, which returns a list of all available cryptocurrencies.
メイン ルートの定義: GET メソッドと POST メソッドの両方をサポートするメイン ルート (/) が定義されます。 GET リクエストが行われると、CoinGecko API から利用可能なコインのリストが取得されます。これは、CoinGecko API エンドポイントに GET リクエストを行うことで実行され、利用可能なすべての暗号通貨のリストが返されます。
Handling POST Requests: If a POST request is made, it gets the coin_id and days from the form data. If the coin_id is valid, it calls the get_market_chart function with coin_id and days as parameters. This function fetches the market chart data for the given coin over the specified number of days.
POSTリクエストの処理: POSTリクエストが行われると、フォームデータからcoin_idとdaysを取得します。 Coin_id が有効な場合は、coin_id と days をパラメータとして get_market_chart 関数を呼び出します。この関数は、指定された日数にわたる特定のコインの市場チャート データを取得します。
Defining the get_market_chart Function: This function does several things, namely, it calculates the start and end timestamps based on the number of days, fetches the OHLC (Open, High, Low, Close) data for the given coin_id and days, and fetches the historical data for a specific date and the current market data from the CoinGecko API. Next, it prepares the feature matrix X and target vector y for the linear regression model, and scales the data using MinMaxScaler, a feature scaling technique that transforms features by scaling each feature to a given range. Thereafter, it trains the LinearRegression model with X and y and makes predictions using the trained model. It creates a list of dates for the predictions and combines the dates and predictions into a dictionary, and finally, it returns the predictions.
get_market_chart 関数の定義: この関数はいくつかのことを行います。つまり、日数に基づいて開始タイムスタンプと終了タイムスタンプを計算し、指定された Coin_id と日数の OHLC (始値、高値、安値、終値) データを取得し、特定の日付の履歴データと CoinGecko API からの現在の市場データ。次に、線形回帰モデルの特徴行列 X とターゲット ベクトル y を準備し、MinMaxScaler を使用してデータをスケーリングします。これは、各特徴を指定された範囲にスケーリングすることで特徴を変換する特徴スケーリング手法です。その後、X と y を使用して LinearRegression モデルをトレーニングし、トレーニングされたモデルを使用して予測を行います。予測の日付のリストを作成し、日付と予測を辞書に結合して、最後に予測を返します。
Running the Flask Application: Finally, if the script is run directly, it starts the Flask development server. This server is a built-in, lightweight WSGI web server and provides a quick way to run the application. It is however not recommended for production environments.
Flask アプリケーションの実行: 最後に、スクリプトを直接実行すると、Flask 開発サーバーが起動します。このサーバーは、組み込みの軽量 WSGI Web サーバーであり、アプリケーションを迅速に実行する方法を提供します。ただし、実稼働環境ではお勧めできません。
HTML Structure of the Crypto Price Predictons
This application allows users to select a cryptocurrency and a number of days, and then predicts the price of the selected cryptocurrency for the specified number of days. The predictions are displayed in a table format.
暗号通貨価格予測の HTML 構造このアプリケーションでは、ユーザーが暗号通貨と日数を選択し、指定された日数における選択した暗号通貨の価格を予測できます。予測は表形式で表示されます。
If there’s an error, such as an invalid coin ID, the application displays an error message. The application uses Jinja templating for dynamic content rendering, which allows it to display different content based on the user’s input and the results of the price prediction.
無効なコイン ID などのエラーが発生した場合、アプリケーションはエラー メッセージを表示します。このアプリケーションは、動的なコンテンツのレンダリングに Jinja テンプレートを使用しており、ユーザーの入力と価格予測の結果に基づいてさまざまなコンテンツを表示できます。
Installing Required Python Libraries
Before running the Python program, it’s important to ensure that all the necessary Python libraries are installed. Here’s a brief explanation of the libraries used in the program and how to install them:
必要な Python ライブラリのインストールPython プログラムを実行する前に、必要な Python ライブラリがすべてインストールされていることを確認することが重要です。プログラムで使用されるライブラリとそのインストール方法について簡単に説明します。
- Flask: Flask is a lightweight web application framework. It’s used to create the web interface for the program. Install it with pip:
pip install flask - requests: Requests is a library for making HTTP requests. It’s used to fetch data from the CoinGecko API. Install it with pip:
pip install requests - json: The json module is used to parse the JSON data returned by the CoinGecko API. It’s included in the standard Python library, so you don’t need to install it separately.
- NumPy: NumPy is a library for numerical computing. It’s used to manipulate the data fetched from the API. Install it with pip:
pip install numpy - scikit-learn: Scikit-learn is a library for machine learning. It’s used to create the linear regression model for price prediction. Install it with pip:
pip install scikit-learn - time: The time module is used to work with timestamps. It’s included in the standard Python library, so you don’t need to install it separately.
- datetime: The datetime module is used to work with dates and times. It’s included in the standard Python library, so you don’t need to install it separately.
To install all these libraries at once, you can use the following command:
Flask: Flask は軽量の Web アプリケーション フレームワークです。プログラムの Web インターフェイスを作成するために使用されます。 pip でインストールします。 pip install flaskrequests: Requests は、HTTP リクエストを行うためのライブラリです。 CoinGecko API からデータを取得するために使用されます。 pip でインストールします: pip install requestsjson: json モジュールは、CoinGecko API によって返された JSON データを解析するために使用されます。標準の Python ライブラリに含まれているため、別途インストールする必要はありません。 NumPy: NumPy は数値計算用のライブラリです。 API から取得したデータを操作するために使用されます。 pip でインストールします: pip install numpyscikit-learn: Scikit-learn は機械学習用のライブラリです。価格予測のための線形回帰モデルを作成するために使用されます。 pip でインストールします。 pip install scikit-learntime: time モジュールは、タイムスタンプを操作するために使用されます。これは標準の Python ライブラリに含まれているため、別途インストールする必要はありません。datetime: datetime モジュールは、日付と時刻を操作するために使用されます。これは標準の Python ライブラリに含まれているため、個別にインストールする必要はありません。これらのライブラリをすべて一度にインストールするには、次のコマンドを使用できます。
pip install flask requests numpy scikit-learn
pip install flaskリクエスト numpy scikit-learn
Remember to run these commands in your virtual environment if you’re using one. This will keep your global Python environment clean and manage dependencies for this project separately. If you’re not using a virtual environment, you might need to use pip3 instead of pip and add sudo at the beginning of the commands to install the libraries globally.
仮想環境を使用している場合は、必ず仮想環境でこれらのコマンドを実行してください。これにより、グローバル Python 環境がクリーンな状態に保たれ、このプロジェクトの依存関係が個別に管理されます。仮想環境を使用していない場合は、ライブラリをグローバルにインストールするには、pip の代わりに pip3 を使用し、コマンドの先頭に sudo を追加する必要がある場合があります。
Testing the Cryptocurrency Prediction
To test the predictor, follow these steps:
暗号通貨予測のテスト予測子をテストするには、次の手順に従います。
Run the Python script in your terminal using python
.py, in this case python predictor.py python .py (この場合は pythondictor.py ) を使用して、ターミナルで Python スクリプトを実行します。
Open the Web Application: Once the Flask server is running, open a web browser and navigate to the local server’s address (usually http://localhost:5000).
Web アプリケーションを開きます。Flask サーバーが実行されたら、Web ブラウザを開いてローカル サーバーのアドレス (通常は http://localhost:5000) に移動します。
Select a Cryptocurrency: You’ll see a form with a dropdown list of cryptocurrencies. Select the cryptocurrency you’re interested in.
暗号通貨を選択します: 暗号通貨のドロップダウン リストを含むフォームが表示されます。興味のある暗号通貨を選択してください。
Enter the Number of Days: In the ‘Days’ input field, enter the number of days for which you want to predict prices.
日数を入力します。「日数」入力フィールドに、価格を予測する日数を入力します。
Submit the Form: Click the ‘Predict’ button to submit the form. The program will now fetch the relevant data, train the machine learning model, and make predictions.
フォームを送信する: [予測] ボタンをクリックしてフォームを送信します。プログラムは関連データを取得し、機械学習モデルをトレーニングし、予測を行います。
View the Predictions: The predicted prices will be displayed in a table on the webpage. Each row in the table represents one day, with the date and the predicted price.
予測の表示: 予測価格が Web ページ上の表に表示されます。テーブルの各行は 1 日を表し、日付と予測価格が示されます。
Test Different Cryptocurrencies and Timeframes: Try selecting different cryptocurrencies and entering different numbers of days. Check whether the program returns reasonable predictions and handles edge cases well (e.g., a very large number of days).
さまざまな暗号通貨と期間をテストする: さまざまな暗号通貨を選択し、さまざまな日数を入力してみてください。プログラムが妥当な予測を返し、エッジケース (非常に長い日数など) を適切に処理しているかどうかを確認します。
Remember, the accuracy of the predictions depends on many factors, including the volatility of the cryptocurrency market and the amount of historical data available. Always use such predictions as just one of many tools in your cryptocurrency trading toolkit.
予測の精度は、暗号通貨市場のボラティリティや利用可能な履歴データの量など、多くの要因に依存することに注意してください。このような予測は、暗号通貨取引ツールキットに含まれる多くのツールの 1 つとして常に使用してください。
Advanced Functionalities and Useful Endpoints
While the guide above covers the basic development of a cryptocurrency price prediction program, CoinGecko API offers a wealth of endpoints that can be used to add more advanced functionalities. Here are some possibilities:
- Historical Snapshots: The ‘/coins/{id}/history’ endpoint provides historical data for a specific date. This could be used to add a feature that allows users to view ‘snapshots’ of the market at different points in the past.
- Anomaly Detection: By using the ‘/coins/markets’ endpoint to fetch real-time market data, you could implement an anomaly detection feature, which alerts users to sudden significant changes in a coin’s price or trading volume.
- Coin Information: The ‘/coins/{id}’ endpoint returns detailed information about a specific coin, from tickers, market data, community data, metadata and more. This could provide users with information on development status, community strength, public interest and more.
- Trending Coins: The ‘/search/trending’ endpoint provides information about trending coins in the last 24 hours. This data point can keep users informed about which coins are currently popular in the market.

Conclusion
In conclusion, this Python program demonstrates the power of machine learning and APIs in predicting cryptocurrency prices. By leveraging the CoinGecko API, the program fetches real-time and historical market data, which is then used to train a linear regression model for Bitcoin price predictions. The program also provides an intuitive web interface for users to interact with, making it a practical tool for anyone interested in cryptocurrencies.
Moreover, the CoinGecko API offers a wealth of endpoints that can be used to add more advanced functionalities to the program, such as sentiment analysis, anomaly detection, historical snapshots, coin information, and trending coins. These functionalities can provide users with deeper insights and a more comprehensive understanding of the cryptocurrency market.
さらに、CoinGecko API は、センチメント分析、異常検出、履歴スナップショット、コイン情報、トレンドコインなど、より高度な機能をプログラムに追加するために使用できる豊富なエンドポイントを提供します。これらの機能により、ユーザーは仮想通貨市場についてのより深い洞察とより包括的な理解を得ることができます。
Testing the program is a crucial step to ensure its accuracy and reliability. By following the step-by-step guide provided, users can test different cryptocurrencies and timeframes, and check the program’s performance under various scenarios.
プログラムのテストは、その正確さと信頼性を保証するための重要なステップです。提供されるステップバイステップのガイドに従うことで、ユーザーはさまざまな暗号通貨と期間をテストし、さまざまなシナリオの下でプログラムのパフォーマンスをチェックできます。
Remember, while such a program can provide valuable predictions and insights, it should be used as just one of many tools in your cryptocurrency trading toolkit. Always consider multiple factors, sources of information and do your own research when making trading decisions. Happy coding and trading!
このようなプログラムは貴重な予測や洞察を提供しますが、仮想通貨取引ツールキットの多くのツールの 1 つとしてのみ使用する必要があることに注意してください。取引の決定を下す際には、常に複数の要素、情報源を考慮し、独自の調査を行ってください。コーディングとトレードを楽しんでください!
If you found this tutorial useful, check out other similar API Guides.
このチュートリアルが役立つと思われた場合は、他の同様の API ガイドを確認してください。
Rollend is a Microsoft Certified Cloud Architect with 16 years of experience. He is the author of the book “Automate Your Life: Streamline Your Daily Tasks with Python: 30 Powerful Ways to Streamline Your Daily Tasks with Python”.Follow the author on Twitter @RollendXavier
CoinGecko のコンテンツ編集ガイドラインCoinGecko のコンテンツは、暗号通貨業界の謎を解き明かすことを目的としています。表示される特定の投稿にはスポンサーが付いている場合がありますが、私たちは編集の品質と整合性の最高基準を維持するよう努めており、編集者によって精査されていないコンテンツは公開しません。詳細はこちらこの記事が気に入ったか教えてください!Rollend XavierRollend は、 16 年の経験を持つ Microsoft 認定クラウド アーキテクト。彼は、『Automate Your Life: Streamline Your Daily Tasks with Python: 30 Powerful Ways to Streamline Your Daily Tasks with Python』という本の著者です。Twitter @RollendXavier で著者をフォローしてください。
Rollend Xavier の続きを読む
免責事項:info@kdj.com
提供される情報は取引に関するアドバイスではありません。 kdj.com は、この記事で提供される情報に基づいて行われた投資に対して一切の責任を負いません。暗号通貨は変動性が高いため、十分な調査を行った上で慎重に投資することを強くお勧めします。
このウェブサイトで使用されているコンテンツが著作権を侵害していると思われる場合は、直ちに当社 (info@kdj.com) までご連絡ください。速やかに削除させていただきます。

































