name: trading-manifold description: “使用Manifold Markets的REST API进行下注” emoji: “🎲” gates: envs: - MANIFOLD_API_KEY
Manifold Markets 交易技能
真实可用的方法,用于在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
# 在概率非常低(< 10%)时买入YES
if prob < 0.10:
print(f"低概率: {m['question'][:50]} at {prob*100:.1f}%")
if balance > 50:
bet(m["id"], 50, "YES", limit_prob=0.15)
# 在概率非常高(> 90%)时买入NO
elif prob > 0.90:
print(f"高概率: {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']}")
重要注意事项
- Mana是虚拟货币 - 可以捐赠给慈善机构
- 下注无限制 - 不同于真实货币市场
- 做市商AMM - 价格根据下注变动
- 限价单 - 使用
limitProb获取更好价格 - API速率限制 - 请温和使用,约每分钟60次请求
- 股份 ≠ Mana - 股份根据概率变化