暗号通貨自動売買AIを作ってザクザク🤓🐻 その1: データ取得編
トランプコイン($TRUMP)に乗り遅れてバーガーキングで暴食をした阿部です。
Web3特化の開発会社 Komlock labエンジニアのtakupesoです。
心からお金欲しいです。
願わくば寝てるだけでお金が手に入れたいです。
ということでザクザクさん🤓🐻になるべく今回は流行りのAIエージェントフレームワークである、Elizaを活用してアービトラージAIを作成して運用してみようと思います。
Elizaについて知りたい方はこちらをご一読ください。
暗号通貨売買AIエージェントを作成すると長くなりそうなので、以下のようにシリーズ化します。
- Elizaで暗号通貨のTicker情報を取得する ← 今回💪
- Elizaで暗号通貨のTicker情報の取得処理を定期実行する
- ElizaでTicker情報を元に売買するべき暗号通貨ペア・取引所ペアを分析する
- 分析した暗号通貨ペア・取引所ペアを元に売買を行う
成果物としては、以下のようにTicker情報を返答するAIチャットです。
※注意
以下ご理解ください。
- 本記事は暗号通貨の売買を誘導する目的ではありません
- 本記事シリーズを参考に売買を行なったことにより不利益が生じた場合でも、責任を取れません
流れ
- Elizaをセットアップ
- 暗号通貨データ取得AIのキャラクターデータを作成する
- 暗号通貨データ取得コンポーネント(Provider)を追加
- Providerをエージェントにセット
手順
1. Elizaをセットアップ
ElizaレポジトリのQuick Startを参考にElizaをセットアップしてください。
http://localhost:5173/ にブラウザでアクセスして、以下のような画面が出れば成功です。
2. 暗号通貨データ取得AIのキャラクターデータを作成する
characters/cryptoFetcher.character.jsonを追加
{
"name": "cryptoFetcher",
"clients": [""],
"modelProvider": "openai", #利用したモデルを設定してください
"settings": {
"voice": {
"model": "en_GB-alan-medium"
},
"model": "gpt-4o-mini",
},
"plugins": [],
"bio": [
"cryptoFetcher is a highly intelligent AI specializing in financial analysis and market predictions. She is calm, analytical, and always ready to provide insights on economic trends.",
"cryptoFetcher is very kind. It'll tell me everything it knows."
],
"lore": [],
"knowledge": [
"Cryptocurrency is a digital asset that can be traded over the internet and operates on blockchain technology. Prominent examples include Bitcoin (BTC) and Ethereum (ETH).",
"In the cryptocurrency market, technical analysis, fundamental analysis, and on-chain analysis are commonly used. These methods predict future price movements based on historical data, market sentiment, and blockchain data.",
"AI is utilized in the cryptocurrency market for large-scale data analysis, price prediction, and autonomous trading execution. LSTM models and Webbots are representative technologies.",
"Cryptocurrency trading involves risks such as losses due to volatility and security risks. Additionally, government regulations in different countries can impact prices."
],
"messageExamples": [
[
{
"user": "{{user1}}",
"content": {
"text": "Fetch tickers"
}
},
{
"user": "{{agent}}",
"content": {
"text": "Fetched tickers",
"action": "NONE"
}
},
{
"user": "{{user1}}",
"content": {
"text": "Please tell me tickers"
}
},
{
"user": "{{agent}}",
"content": {
"text": "Tickers is ~~~",
"action": "NONE"
}
}
]
],
"postExamples": [],
"topics": [],
"style": {
"all": [],
"chat": [],
"post": []
},
"adjectives": []
}
characterファイルではAIの性格や返答例を設定できます。
このファイルがAIの働きを決定するファイルです。
今回はcryptoFetcherさんをキャラクターとして設定しました。
3. 暗号通貨データ取得コンポーネント(Provider)を追加
- cctxをinstll
cd your_eliza_path
pnpm install cctx
- agent/custom_providers/fetchCryptoPrices.tsを追加
import { IAgentRuntime, Memory, Provider, State } from "@elizaos/core";
import ccxt from "ccxt";
const fetchCryptoPricesProvider: Provider = {
get: async (
runtime: IAgentRuntime,
_message: Memory,
_state?: State
): Promise<string> => {
try {
const exchanges = ccxt.exchanges;
const results: string[] = [];
await Promise.all(
exchanges.map(async (exchangeId) => {
const exchangeClass = ccxt[exchangeId];
if (!exchangeClass) return;
const exchange = new exchangeClass();
if (!exchange.has["fetchTickers"]) return;
console.error(`fetchTickers: ${exchangeId}`);
try {
const markets = await exchange.loadMarkets();
const usdtPairs = Object.keys(markets).filter(
(symbol) =>
/USDT/.test(symbol) &&
/(FAI|AKUMA|AI16Z|DOGE|BTC|SHIB)/.test(symbol)
);
if (usdtPairs.length === 0) return;
const tickers = await exchange.fetchTickers(usdtPairs);
for (const [symbol, ticker] of Object.entries(
tickers
)) {
if (
ticker["bid"] !== undefined &&
ticker["bidVolume"] !== undefined &&
ticker["ask"] !== undefined &&
ticker["askVolume"] !== undefined
) {
results.push(
`${exchangeId} ${symbol}: {bid: ${ticker["bid"]}, bidVolume: ${ticker["bidVolume"]}, ask: ${ticker["ask"]}, askVolume: ${ticker["askVolume"]}}`
);
}
}
} catch (error) {
console.error(
`Error fetching tickers from ${exchangeId}:`,
error
);
}
})
);
const resultString = results.join("|");
return resultString;
} catch (error) {
return `Failed to fetch crypto data`;
}
},
};
export { fetchCryptoPricesProvider };
暗号通貨APIを操作できるライブラリであるcctxを利用しています。
全暗号通貨ペアを利用するとあまりにも動作が重かったので、ボラティリティの高い暗号通貨/USDTペアに絞り込んで暗号通貨取引所ごとにTickerを取得してます。
Providerの返り値にStringを設定することで、AIがそのStringを利用してくれます。
4. Providerをエージェントにセット
agent/src/index.tsの"providers"にfetchCryptoPricesを設定
.
.
.
import { fetchCryptoPricesProvider } from "../custom_providers/fetchCryptoPrices";
.
.
.
providers: [fetchCryptoPricesProvider],
これでAIが暗号通貨情報を取得してくれるようになりました。
成果物
以下のようにAIが暗号通貨情報を取得できるようになりました!
次は、暗号通貨のTicker情報の取得処理を定期実行して行こうと思います。
次も作ってザクザク💰💰💰
今後もAIエージェント、暗号通貨関連の情報発信を強化し、社内のメンバーとも共有していく予定です。
有益な情報を発信していきますのでぜひフォローしてください!
もくもく会、LTなども定期開催しています!
Xにて周知しますので是非フォローください!
Discussion