> For the complete documentation index, see [llms.txt](https://docs.madjik.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.madjik.io/integration-guides/ml-integration.md).

# Using Madjik API for ML

Build and train machine learning models using Madjik's comprehensive crypto market data.

## Overview

Madjik API provides ML-ready data:

* **Historical time series** for model training
* **Normalized features** (0-100 scales) requiring minimal preprocessing
* **Multiple timeframes** (1min, 5min, 1hr, 1day)
* **Rich feature set** across 44 metrics

## ML Use Cases

### 1. Price Prediction Models

```python
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

# Fetch historical features
def get_training_data(days=90):
    features = ["ME10014", "ME10015", "ME10016", "ME10030"]
    data = []
    
    for metric in features:
        resp = requests.get(
            f"https://api.madjik.io/v1/metrics/{metric.lower()}/history",
            params={"days": days},
            headers={"Authorization": "Bearer YOUR_API_KEY"}
        )
        data.append(pd.DataFrame(resp.json()['data']))
    
    return pd.concat(data, axis=1)

# Train model
X = get_training_data()
y = get_price_returns()  # Your price data
model = RandomForestRegressor()
model.fit(X, y)
```

### 2. Regime Classification

```python
from sklearn.cluster import KMeans

# Cluster market regimes
def classify_regime():
    features = requests.get(
        "https://api.madjik.io/v1/metrics/batch",
        params={"metrics": "ME10018,ME10020,ME10023"},
        headers={"Authorization": "Bearer YOUR_API_KEY"}
    ).json()
    
    # K-means clustering
    kmeans = KMeans(n_clusters=4)  # Bull, Bear, Accumulation, Distribution
    regime = kmeans.predict([[
        features['ME10018']['value'],
        features['ME10020']['value'],
        features['ME10023']['value']
    ]])
    return regime
```

### 3. Anomaly Detection

```python
from sklearn.ensemble import IsolationForest

# Detect unusual market conditions
def detect_anomalies():
    metrics = ["ME10007", "ME10035", "ME10006"]  # Volume, Tether comms, Manipulation
    
    data = []
    for m in metrics:
        resp = requests.get(
            f"https://api.madjik.io/v1/metrics/{m.lower()}/history",
            params={"hours": 24},
            headers={"Authorization": "Bearer YOUR_API_KEY"}
        )
        data.append(resp.json()['data'])
    
    clf = IsolationForest(contamination=0.1)
    anomalies = clf.fit_predict(data)
    return anomalies
```

## Feature Engineering Tips

| Madjik Metric         | ML Feature Ideas                       |
| --------------------- | -------------------------------------- |
| ME10014 (Funding)     | Rolling mean, z-score, regime changes  |
| ME10016 (Liquidation) | Threshold crossings, acceleration      |
| ME10030 (Sentiment)   | Delta, momentum, divergence from price |
| ME10010 (Whale)       | Flow direction, accumulation streaks   |

## Best Practices

1. **Normalize consistently** - Madjik metrics are 0-100, but double-check
2. **Handle missing data** - Use forward-fill or interpolation
3. **Feature selection** - Start with correlated metrics, prune redundant
4. **Cross-validation** - Crypto regimes shift; use time-series CV
5. **Retrain regularly** - Market structure changes

## See Also

* [AI Integration Guide](/integration-guides/ai-integration.md)
* [MCP Integration Guide](/integration-guides/mcp-integration.md)
* [All Metrics](https://github.com/madjik-io/madjik-api-docs/blob/main/metrics/README.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.madjik.io/integration-guides/ml-integration.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
