trading-manifold trading-manifold

Manifold Markets 交易技能,使用虚拟货币 Mana 在 Manifold Markets 上进行市场投注

算法交易 0 次安装 0 次浏览 更新于 3/5/2026

Manifold Markets 交易技能

使用 Manifold Markets 的 REST API 在 Manifold Markets 上使用 Mana(可以捐赠给慈善机构的虚拟货币)进行真实、有效的市场投注。

设置

从以下链接获取您的 API 密钥:https://manifold.markets/profile(API 密钥部分)

import os
import requests

API_URL = "https://api.manifold.markets/v0"
API_KEY = os.getenv("MANIFOLD_API_KEY")

def headers():
    return {
        "Authorization": f"Key {API_KEY}",
        "Content-Type": "application/json"
    }

搜索市场

def search_markets(query: str, limit: int = 10):
    """搜索市场"""
    r = requests.get(f"{API_URL}/search-markets", params={
        "term": query,
        "limit": limit,
        "filter": "open",
        "sort": "liquidity"
    })
    r.raise_for_status()
    markets = r.json()

    for m in markets[:5]:
        prob = m.get("probability", 0.5)
        print(f"
市场: {m['question']}")
        print(f"ID: {m['id']}")
        print(f"概率: {prob*100:.1f}%")
        print(f"URL: {m.get('url', '')}")

    return markets

markets = search_markets("AI")

通过 ID 或 Slug 获取市场

def get_market(id_or_slug: str):
    """获取市场详情"""
    # 首先尝试通过 ID
    r = requests.get(f"{API_URL}/market/{id_or_slug}")
    if r.status_code == 404:
        # 尝试通过 slug
        r = requests.get(f"{API_URL}/slug/{id_or_slug}")

    r.raise_for_status()
    return r.json()

market = get_market("will-gpt5-be-released-before-2025")
print(f"问题: {market['question']}")
print(f"概率: {market.get('probability', 0.5)*100:.1f}%")

下注

def place_bet(
    market_id: str,
    amount: int,           # Mana 下注金额
    outcome: str = "YES",  # "YES" 或 "NO"
    limit_prob: float = None  # 可选限价单概率
):
    """
    在 Manifold 下注

    参数:
        market_id: 市场 ID(不是 slug!)
        amount: 下注的 Mana 数量
        outcome: "YES" 或 "NO"
        limit_prob: 可选 - 如果设置,在此概率下创建限价单
    """
    payload = {
        "contractId": market_id,
        "amount": amount,
        "outcome": outcome
    }

    if limit_prob is not None:
        payload["limitProb"] = limit_prob

    r = requests.post(f"{API_URL}/bet", headers=headers(), json=payload)
    r.raise_for_status()
    result = r.json()

    print(f"下注成功!")
    print(f"股份: {result.get('shares', 0):.2f}")
    print(f"下注后概率: {result.get('probAfter', 0)*100:.1f}%")

    return result

# 市场下注 - 按当前价格购买
result = place_bet(
    market_id="abc123",
    amount=100,  # 100 Mana
    outcome="YES"
)

# 限价单 - 仅在 40% 或更低时成交
result = place_bet(
    market_id="abc123",
    amount=100,
    outcome="YES",
    limit_prob=0.40
)

取消下注(限价单)

def cancel_bet(bet_id: str):
    """取消限价单"""
    r = requests.post(f"{API_URL}/bet/cancel/{bet_id}", headers=headers())
    r.raise_for_status()
    return True

cancel_bet("bet123")

出售股份

def sell_shares(
    market_id: str,
    outcome: str = "YES",
    shares: float = None  # None = 全部出售
):
    """
    出售市场中的股份

    参数:
        market_id: 市场 ID
        outcome: "YES" 或 "NO" - 出售哪种股份
        shares: 出售的股份数量(None = 全部)
    """
    payload = {
        "contractId": market_id,
        "outcome": outcome
    }

    if shares is not None:
        payload["shares"] = shares

    r = requests.post(f"{API_URL}/market/{market_id}/sell", headers=headers(), json=payload)
    r.raise_for_status()
    return r.json()

# 出售所有 YES 股份
sell_shares("abc123", "YES")

# 出售特定数量
sell_shares("abc123", "YES", shares=50.0)

获取您的下注

def get_my_bets(market_id: str = None):
    """获取您的下注"""
    params = {}
    if market_id:
        params["contractId"] = market_id

    r = requests.get(f"{API_URL}/bets", headers=headers(), params=params)
    r.raise_for_status()
    bets = r.json()

    for b in bets[:10]:
        print(f"下注: {b['outcome']} {b['amount']}M @ {b.get('probBefore', 0)*100:.0f}%")

    return bets

bets = get_my_bets()

获取您的持仓

def get_positions():
    """获取所有市场中的当前持仓"""
    # 首先获取用户信息
    r = requests.get(f"{API_URL}/me", headers=headers())
    r.raise_for_status()
    user = r.json()

    # 获取下注以计算持仓
    r = requests.get(f"{API_URL}/bets", headers=headers(), params={"limit": 1000})
    bets = r.json()

    # 按市场聚合
    positions = {}
    for bet in bets:
        mid = bet["contractId"]
        if mid not in positions:
            positions[mid] = {"yes": 0, "no": 0, "invested": 0}

        if bet["outcome"] == "YES":
            positions[mid]["yes"] += bet.get("shares", 0)
        else:
            positions[mid]["no"] += bet.get("shares", 0)

        if not bet.get("isSold", False):
            positions[mid]["invested"] += bet["amount"]

    return positions, user.get("balance", 0)

positions, balance = get_positions()
print(f"余额: {balance} Mana")
for mid, pos in positions.items():
    if pos["yes"] > 0 or pos["no"] > 0:
        print(f"市场 {mid}: YES={pos['yes']:.1f}, NO={pos['no']:.1f}")

获取余额

def get_balance():
    """获取您的 Mana 余额"""
    r = requests.get(f"{API_URL}/me", headers=headers())
    r.raise_for_status()
    user = r.json()
    return user.get("balance", 0)

balance = get_balance()
print(f"余额: {balance} Mana")

完整的交易机器人示例

#!/usr/bin/env python3
"""
Manifold 套利机器人 - 寻找定价错误的市场
"""

import os
import time
import requests

API_URL = "https://api.manifold.markets/v0"
API_KEY = os.getenv("MANIFOLD_API_KEY")

def h():
    return {"Authorization": f"Key {API_KEY}", "Content-Type": "application/json"}

def search(query):
    r = requests.get(f"{API_URL}/search-markets",
                    params={"term": query, "limit": 20, "filter": "open"})
    return r.json()

def bet(market_id, amount, outcome, limit_prob=None):
    payload = {"contractId": market_id, "amount": amount, "outcome": outcome}
    if limit_prob:
        payload["limitProb"] = limit_prob
    r = requests.post(f"{API_URL}/bet", headers=h(), json=payload)
    return r.json()

def get_balance():
    r = requests.get(f"{API_URL}/me", headers=h())
    return r.json().get("balance", 0)

# 策略:购买极端概率(可能回归)
MIN_LIQUIDITY = 1000  # 只交易流动性好的市场

while True:
    try:
        balance = get_balance()
        print(f"
余额: {balance} Mana")

        # 搜索趋势市场
        markets = search("2024")

        for m in markets:
            prob = m.get("probability", 0.5)
            liquidity = m.get("totalLiquidity", 0)

            if liquidity < MIN_LIQUIDITY:
                continue

            # 购买 YES 非常低的概率(< 10%)
            if prob < 0.10:
                print(f"LOW: {m['question'][:50]} at {prob*100:.1f}%")
                if balance > 50:
                    bet(m["id"], 50, "YES", limit_prob=0.15)

            # 购买 NO 非常高的概率(> 90%)
            elif prob > 0.90:
                print(f"HIGH: {m['question'][:50]} at {prob*100:.1f}%")
                if balance > 50:
                    bet(m["id"], 50, "NO", limit_prob=0.85)

        time.sleep(300)  # 每 5 分钟检查一次

    except Exception as e:
        print(f"错误: {e}")
        time.sleep(60)

多项选择市场

def bet_multiple_choice(market_id: str, answer_id: str, amount: int):
    """在多项选择市场上下注"""
    payload = {
        "contractId": market_id,
        "amount": amount,
        "answerId": answer_id
    }

    r = requests.post(f"{API_URL}/bet", headers=headers(), json=payload)
    r.raise_for_status()
    return r.json()

# 获取带答案的市场
market = get_market("who-will-win-2024-election")
for answer in market.get("answers", []):
    print(f"{answer['text']}: {answer['probability']*100:.1f}% (ID: {answer['id']})")

# 在特定答案上下注
bet_multiple_choice("market123", "answer456", 100)

创建市场

def create_market(
    question: str,
    description: str = "",
    close_time: int = None,  # Unix 时间戳
    initial_prob: float = 0.5,
    ante: int = 100  # 初始流动性
):
    """创建一个新的二元市场"""
    payload = {
        "outcomeType": "BINARY",
        "question": question,
        "description": description,
        "initialProb": int(initial_prob * 100),
        "ante": ante
    }

    if close_time:
        payload["closeTime"] = close_time * 1000  # 毫秒

    r = requests.post(f"{API_URL}/market", headers=headers(), json=payload)
    r.raise_for_status()
    return r.json()

# 创建市场
new_market = create_market(
    question="明天纽约会下雨吗?",
    initial_prob=0.3,
    ante=100
)
print(f"创建: {new_market['url']}")

重要提示

  1. Mana 是虚拟货币 - 可以捐赠给慈善机构
  2. 没有下注限制 - 与真实货币市场不同
  3. 市场做市商 AMM - 价格随下注变动
  4. 限价单 - 使用 limitProb 获得更好的价格
  5. API 速率限制 - 请节制,约 60 个请求/分钟
  6. 股份 ≠ Mana - 股份根据概率变化