File size: 3,901 Bytes
d4c51f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Conviction-based position sizing — 倉位跟著把握程度走.

We never blindly copy the target's dollar amount. Instead we infer *how
convinced the target was* on each entry and scale OUR stake to that conviction,
inside hard risk caps sized for a small (<$1k), conservative bankroll.

Conviction ∈ [0,1] blends two signals:
  stake_signal : how large this bet was vs the target's own typical bet.
                 Betting 3x their usual size => they are unusually sure.
  edge_signal  : how far the entry price sits from a coin flip (|p-0.5|*2).
                 An extreme price means a strong directional view.

conviction = w_stake * stake_signal + w_edge * edge_signal

Our stake = bankroll * frac, where frac scales linearly from min_frac (no
conviction) to max_frac (full conviction), then is clamped by:
  - kelly_cap * kelly_fraction  (edge-aware ceiling)
  - remaining room under max_frac_per_market
"""
from __future__ import annotations

import math
from dataclasses import dataclass


def _logistic(x: float) -> float:
    return 1.0 / (1.0 + math.exp(-x))


def stake_signal(trade_usdc: float, rolling_avg_usdc: float) -> float:
    """0.5 at the target's average bet, ->1 for oversized, ->0 for tiny."""
    if rolling_avg_usdc <= 0 or trade_usdc <= 0:
        return 0.5
    return _logistic(math.log(trade_usdc / rolling_avg_usdc))


def edge_signal(price: float) -> float:
    """Distance from a coin flip, in [0,1]."""
    return max(0.0, min(1.0, abs(price - 0.5) * 2.0))


def conviction(trade_usdc: float, rolling_avg_usdc: float, price: float,
               w_stake: float, w_edge: float) -> float:
    s = stake_signal(trade_usdc, rolling_avg_usdc)
    e = edge_signal(price)
    tot = w_stake + w_edge or 1.0
    return (w_stake * s + w_edge * e) / tot


def kelly_fraction(price: float, assumed_edge: float) -> float:
    """Full-Kelly stake for buying a YES share at `price` that pays $1.

    Buying at `price` costs `price` and returns 1 on win, so net decimal odds
    b = (1/price) - 1. The catch: the market price already IS the implied
    probability, so if we plug price in as our win prob the edge is zero and
    Kelly is zero. Copying a sharp trader only makes sense if we believe the
    true probability sits ABOVE the price — that gap is `assumed_edge`, which we
    scale with conviction in position_fraction(). Returns a fraction in [0,1)."""
    price = min(max(price, 1e-4), 1 - 1e-4)
    p_true = min(price + max(0.0, assumed_edge), 0.999)   # our belief
    b = (1.0 / price) - 1.0
    q = 1.0 - p_true
    f = (b * p_true - q) / b if b > 0 else 0.0
    return max(0.0, min(f, 1.0))


@dataclass
class SizingConfig:
    max_frac_per_trade: float = 0.05
    max_frac_per_market: float = 0.15
    min_frac_per_trade: float = 0.005
    w_stake: float = 0.6
    w_edge: float = 0.4
    kelly_cap: float = 0.5
    assumed_max_edge: float = 0.15   # max true-vs-price gap we ascribe at full conviction


def position_fraction(trade_usdc: float, rolling_avg_usdc: float, price: float,
                      market_exposure_frac: float, cfg: SizingConfig) -> tuple[float, float]:
    """Return (fraction_of_bankroll_to_stake, conviction) for one copied entry.

    market_exposure_frac = fraction of bankroll already committed to this market.
    """
    conv = conviction(trade_usdc, rolling_avg_usdc, price, cfg.w_stake, cfg.w_edge)

    # linear map conviction -> [min_frac, max_frac_per_trade]
    frac = cfg.min_frac_per_trade + conv * (cfg.max_frac_per_trade - cfg.min_frac_per_trade)

    # edge-aware Kelly ceiling: assumed edge scales with conviction
    assumed_edge = conv * cfg.assumed_max_edge
    frac = min(frac, cfg.kelly_cap * kelly_fraction(price, assumed_edge))

    # respect aggregate per-market cap
    room = max(0.0, cfg.max_frac_per_market - market_exposure_frac)
    frac = min(frac, room)

    return max(0.0, frac), conv