Update README.md
Browse filesHow It Works:
Data Fetching:
Historical data for ETH/USDT is fetched via Binance API.
Data Preparation:
Past price windows are converted into Llama documents for indexing.
LlamaIndex Integration:
Using the SimpleKeywordTableIndex from LlamaIndex, documents are indexed and a simple prompt is queried to simulate market prediction or pattern detection.
Response Interpretation:
The language model's response provides insights or possible trends based on indexed price data.
Prerequisites:
Install the required libraries:
bash
Kodu kopyala
pip install pandas numpy llama-index requests
Sample Prompt Result:
A sample response might look like:
vbnet
Kodu kopyala
Prediction response from LlamaIndex:
Based on the trend from 2023-10-01 12:00 to 2023-10-01 17:00, the next price is expected to be $12
@@ -1,3 +1,84 @@
|
|
1 |
---
|
2 |
license: llama3.3
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: llama3.3
|
3 |
+
datasets:
|
4 |
+
- HuggingFaceTB/finemath
|
5 |
+
base_model:
|
6 |
+
- Datou1111/shou_xin
|
7 |
---
|
8 |
+
from llama_index import SimpleKeywordTableIndex, Document
|
9 |
+
from datetime import datetime, timedelta
|
10 |
+
import requests
|
11 |
+
import numpy as np
|
12 |
+
import pandas as pd
|
13 |
+
|
14 |
+
# Fetch historical price data
|
15 |
+
def fetch_price_data(symbol="ETHUSDT", interval="1h", limit=500):
|
16 |
+
"""
|
17 |
+
Fetch historical price data from Binance API.
|
18 |
+
"""
|
19 |
+
url = f"https://api.binance.com/api/v3/klines?symbol={symbol}&interval={interval}&limit={limit}"
|
20 |
+
response = requests.get(url)
|
21 |
+
data = response.json()
|
22 |
+
|
23 |
+
# Parse to DataFrame
|
24 |
+
df = pd.DataFrame(data, columns=[
|
25 |
+
"timestamp", "open", "high", "low", "close", "volume", "close_time",
|
26 |
+
"quote_asset_volume", "number_of_trades", "taker_buy_base",
|
27 |
+
"taker_buy_quote", "ignore"
|
28 |
+
])
|
29 |
+
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
|
30 |
+
df["close"] = df["close"].astype(float)
|
31 |
+
return df[["timestamp", "close"]]
|
32 |
+
|
33 |
+
# Prepare price history as documents
|
34 |
+
def prepare_documents(df, window_size=5):
|
35 |
+
"""
|
36 |
+
Convert sliding price windows into Llama documents.
|
37 |
+
"""
|
38 |
+
prices = df["close"].values
|
39 |
+
timestamps = df["timestamp"].values
|
40 |
+
|
41 |
+
documents = []
|
42 |
+
for i in range(len(prices) - window_size):
|
43 |
+
time_range = f"{timestamps[i]} to {timestamps[i + window_size]}"
|
44 |
+
price_window = prices[i:i + window_size]
|
45 |
+
content = f"Price trend from {time_range}: {price_window.tolist()}"
|
46 |
+
documents.append(Document(content))
|
47 |
+
return documents
|
48 |
+
|
49 |
+
# LlamaIndex-based predictive task
|
50 |
+
def train_with_llama_index(documents):
|
51 |
+
"""
|
52 |
+
Create a Simple Keyword Table Index to simulate trend prediction using LlamaIndex.
|
53 |
+
"""
|
54 |
+
index = SimpleKeywordTableIndex.from_documents(documents)
|
55 |
+
|
56 |
+
# Simulating a market prediction
|
57 |
+
prompt = (
|
58 |
+
"Based on historical trends, what might be the next ETH/USDT price, "
|
59 |
+
"assuming consistent linear progression? Focus on patterns."
|
60 |
+
)
|
61 |
+
response = index.query(prompt)
|
62 |
+
return response
|
63 |
+
|
64 |
+
# Main pipeline
|
65 |
+
def main():
|
66 |
+
# Step 1: Fetch historical data
|
67 |
+
symbol = "ETHUSDT"
|
68 |
+
df = fetch_price_data(symbol)
|
69 |
+
print("Fetched historical data:")
|
70 |
+
print(df.head())
|
71 |
+
|
72 |
+
# Step 2: Prepare documents for LlamaIndex
|
73 |
+
window_size = 5
|
74 |
+
documents = prepare_documents(df, window_size)
|
75 |
+
|
76 |
+
# Step 3: Train a Simple Keyword Table Index and predict trends
|
77 |
+
prediction_response = train_with_llama_index(documents)
|
78 |
+
|
79 |
+
# Step 4: Display response
|
80 |
+
print(f"\nPrediction response from LlamaIndex:\n{prediction_response}")
|
81 |
+
|
82 |
+
# Entry point
|
83 |
+
if __name__ == "__main__":
|
84 |
+
main()
|