OpenClaw x Đa thị: tay để dạy bạn làm thế nào để xây dựng một máy bán hàng tự động người

Đa thị là gì?
Đa thị là thị trường dự báo lớn nhất thế giới, nơi mà người dùng có thể trao đổi xác suất của các sự kiện — từ các cuộc thi thể thao cho đến các cuộc bầu cử chính trị, từ giá tiền được mã hóa đến tin tức khoa học và công nghệ. Cách giao dịch diễn ra tương tự như "quyền lực kinh tế": mua một vật "có" hay "không" cho một kết quả nhất định và được thưởng nếu kết quả xảy ra.
Sức mạnh cốt lõi của nó là:
- Hoàn toàn phân biệt.: Mở và trong suốt dựa trên chuỗi Đa giác
- Khám phá giá cả thực tế: Các dự báo thị trường thường chính xác hơn các cuộc thăm dò theo truyền thống
- Comment: Cung cấp các giao dịch hoàn chỉnh ADI để hỗ trợ các giao dịch tự động
Chuẩn bị
Cài đặt Phụ thuộc Python
Chúng ta cần nó. py-clob-client Nhân viên chính thức:
pip install py-clob-client requests python-dotenv
Truy cập vào ví tiền và khoá ADI
Đa dấu hỗ trợ hai loại xác thực:
- Ví EOA(Hãy tự bảo vệ mình với Gas)
- Ví ủy nhiệm( Phi cơ giao dịch)
Đây là cách đơn giản nhất để chứng minh ví EOA:
- Tạo ví đa giác (dùng MetaMask)
- Bỏ USDC.e(để buôn bán) và nhỏ PL(cho Gas)
- Vì cái ví. Khóa riêng
- Dùng SDK
create_or_derive_api_creds()Phương pháp để tự động tạo ra các chứng chỉ CN
Môi trường OpenClaw 3.
OpenClaw exec Công cụ thực hiện mã Python rất phù hợp để chạy văn lệnh tự động.
Mã lõi được mã hóa
Cơ bản: Theo dõi thị trường và danh sách tự động
import os
import time
import requests
from py_clob_client.client import ClobClient
from py_clob_client.clob_types import OrderArgs, OrderType
from py_clob_client.order_builder.constants import BUY, SELL
HOST = "https://clob.polymarket.com"
CHAIN_ID = 137
PRIVATE_KEY = os.getenv("POLYMARKET_PRIVATE_KEY")
FUNDER_ADDRESS = os.getenv("POLYMARKET_FUNDER")
def init_client():
temp_client = ClobClient(HOST, key=PRIVATE_KEY, chain_id=CHAIN_ID)
api_creds = temp_client.create_or_derive_api_creds()
client = ClobClient(HOST, key=PRIVATE_KEY, chain_id=CHAIN_ID, creds=api_creds, signature_type=0, funder=FUNDER_ADDRESS)
return client
def get_markets(tag=None, limit=10):
url = "https://gamma-api.polymarket.com/markets"
params = {"active": "true", "closed": "false", "limit": limit}
if tag:
params["tagName"] = tag
resp = requests.get(url, params=params)
return resp.json()
def get_price(token_id):
url = f"https://gamma-api.polymarket.com/markets/{token_id}/price"
resp = requests.get(url)
return resp.json()
def place_order(client, token_id, price, size, side=BUY):
order = client.create_and_post_order(
OrderArgs(token_id=token_id, price=price, size=size, side=side, order_type=OrderType.GTC),
options={"tick_size": "0.01", "neg_risk": False}
)
return order
Tiến hành: Trung bình trở lại máy chiến lược người
Ý tưởng chính của chiến lược này là mua nó một cách tự động khi giá cả đi lệch khỏi mức trung bình và chờ trả lại.
import logging
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class MeanReversionBot:
def __init__(self, client, token_id, threshold=0.1, size=10):
self.client = client
self.token_id = token_id
self.threshold = threshold
self.size = size
def get_mid_price(self):
data = get_price(self.token_id)
return float(data.get("price", 0.5))
def check_and_trade(self):
mid_price = self.get_mid_price()
logger.info(f"当前中间价: {mid_price}")
if mid_price < 0.5 - self.threshold:
logger.info(f"价格偏低,买入!")
order = place_order(self.client, self.token_id, mid_price, self.size, BUY)
logger.info(f"订单已下单: {order}")
elif mid_price > 0.5 + self.threshold:
logger.info(f"价格偏高,卖出!")
order = place_order(self.client, self.token_id, mid_price, self.size, SELL)
logger.info(f"订单已下单: {order}")
else:
logger.info("价格在合理区间,不交易")
def run(self, interval=60):
logger.info(f"机器人启动,监控 token: {self.token_id}")
while True:
try:
self.check_and_trade()
except Exception as e:
logger.error(f"错误: {e}")
time.sleep(interval)
if __name__ == "__main__":
client = init_client()
markets = get_markets(tag="politics", limit=1)
if markets:
token_id = markets[0]["clobTokenIds"][0]
print(f"监控市场: {markets[0]['question']}")
bot = MeanReversionBot(client, token_id, threshold=0.15, size=5)
bot.run(interval=120)
Hợp nhất OpenClaw
Rất đơn giản để chạy robot này ở OpenClaw. Bạn có thể lưu các mã trên là polymarket_bot.pyvà sau đó thực hiện trong OpenClaw:
运行 python polymarket_bot.py
Chú ý
Lời khuyên rủi ro
- Dự án biến đổi thị trường, xin chỉ sử dụng các quỹ có giá phải chăng
- Đề nghị kiểm tra mạng lưới trước.
- Các giao dịch robot đòi hỏi sự giám sát liên tục để ngăn chặn những thái cực.
Hạn chế 2. Steve
- Mở ADI mà không có xác thực
- Thỏa thuận AI cần ví và khoá API
- Coi chừng giới hạn tốc độ.
Comment
- Chế độ EOA đòi hỏi gas thanh toán PL
- Bạn có thể áp dụng cho ví ủy nhiệm để thực hiện giao dịch không có ga