> For the complete documentation index, see [llms.txt](https://docs.synthdata.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.synthdata.co/insights/trading.md).

# Trading

### Directional Trading

{% code title="directional\_signal.py" %}

```python
import requests

def get_signal(asset, threshold=0.55):
    response = requests.get(
        "https://api.synthdata.co/insights/prediction-percentiles",
        headers={"Authorization": "Apikey YOUR_API_KEY"},
        params={"asset": asset}
    )
    
    data = response.json()
    current_price = data['current_price']
    final = data['forecast_future']['percentiles'][-1]
    
    median = final['0.5']
    p35 = final['0.35']
    p65 = final['0.65']
    
    if median > current_price and p35 > current_price:
        return "LONG"
    elif median < current_price and p65 < current_price:
        return "SHORT"
    else:
        return "NEUTRAL"
```

{% endcode %}

### Position Sizing

{% code title="kelly\_size.py" %}

```python
def kelly_size(prob_win, avg_win, avg_loss, max_kelly=0.25):
    win_loss_ratio = avg_win / avg_loss
    kelly = (prob_win * win_loss_ratio - (1 - prob_win)) / win_loss_ratio
    return max(0, min(kelly, max_kelly))
```

{% endcode %}

***

## LP Bounds

Optimal liquidity provider ranges with impermanent loss estimates.

Endpoint:

```
GET /insights/lp-bounds
```

### Response

{% code title="lp-bounds-response.json" %}

```json
{
  "current_price": 88997.48,
  "data": [
    {
      "interval": {
        "full_width": "1.0%",
        "lower_bound": 88552.49,
        "upper_bound": 89442.47
      },
      "probability_to_stay_in_interval": {"24": 0.0},
      "expected_time_in_interval": 71.99,
      "expected_impermanent_loss": 0.0064
    },
    {
      "interval": {
        "full_width": "3.0%",
        "lower_bound": 87662.52,
        "upper_bound": 90332.44
      },
      "probability_to_stay_in_interval": {"24": 0.2135},
      "expected_time_in_interval": 732.20,
      "expected_impermanent_loss": 0.0046
    }
  ]
}
```

{% endcode %}

### Example

{% code title="example\_lp\_bounds.py" %}

```python
response = requests.get(
    "https://api.synthdata.co/insights/lp-bounds",
    headers={"Authorization": "Apikey YOUR_API_KEY"},
    params={"asset": "BTC"}
)

for level in response.json()['data']:
    width = level['interval']['full_width']
    prob = level['probability_to_stay_in_interval']['24']
    il = level['expected_impermanent_loss']
    print(f"{width} range: {prob:.1%} stay probability, {il:.2%} expected IL")
```

{% endcode %}

***

## LP Probabilities

Price distribution probabilities for LP range decisions.

Endpoint:

```
GET /insights/lp-probabilities
```

### Response

{% code title="lp-probabilities-response.json" %}

```json
{
  "current_price": 87289.63,
  "data": {
    "24h": {
      "probability_above": {
        "81304.50": 0.9999,
        "82663.77": 0.9975,
        "84023.04": 0.9747,
        "85382.32": 0.8615,
        "86741.59": 0.6062,
        "88100.86": 0.3095,
        "89460.14": 0.1138,
        "90819.41": 0.0323,
        "92178.69": 0.0079,
        "93537.96": 0.0016,
        "94897.23": 0.0
      },
      "probability_below": {
        "81304.50": 0.0,
        "82663.77": 0.0025,
        "84023.04": 0.0253,
        "85382.32": 0.1385,
        "86741.59": 0.3938,
        "88100.86": 0.6905,
        "89460.14": 0.8862,
        "90819.41": 0.9677,
        "92178.69": 0.9921,
        "93537.96": 0.9984,
        "94897.23": 0.9999
      }
    }
  }
}
```

{% endcode %}

### Example

{% code title="example\_lp\_bounds\_chart.py" %}

```python
response = requests.get(
    "https://api.synthdata.co/insights/lp-probabilities",
    headers={"Authorization": "Apikey YOUR_API_KEY"},
    params={"asset": "BTC"}
)

data = response.json()['data']['24h']
for price, prob in data['probability_above'].items():
    print(f"P(price > ${float(price):,.0f}) = {prob:.1%}")
```

{% endcode %}

***
