پرش به مطلب اصلی

سفارش حد

سفارش حد وقتی اجرا می‌شود که بازار به قیمت هدف شما برسد. مبلغ لازم همان لحظه‌ی ثبت قفل می‌شود، پس نمی‌توانید آن را جای دیگری خرج کنید.

POST /quotes/ (with target_price) → (≤ 30 s) → POST /limit-orders/ → active
active → done | cancelled | expired

۱. بررسی بازار و انتخاب قیمت هدف​

  • trading.limit_enabled باید true باشد.
  • مقدار بین limits.limit.min_buy/max_buy (یا min_sell/max_sell) باشد.
  • قیمت هدف خرید باید کمتر از قیمت فعلی خرید، و قیمت هدف فروش بیشتر از قیمت فعلی فروش باشد. وگرنه invalid_target_price می‌گیرید.
curl -s -H "X-API-Key: $TALANO_API_KEY" \
https://service.talano.ir/api/public/v1/markets/<MARKET_ID>/price/
{
"market_id": "3f1c9a52-6a7e-4d6b-9a7e-0b1e7c2d9f10",
"buy": { "price": "10512", "fee": "52.56" },
"sell": { "price": "10388", "fee": "51.94" },
"timestamp": "2026-11-07T08:54:11.162172Z"
}

۲. پیش‌فاکتور حد​

همان POST /quotes/ است، به‌اضافه‌ی target_price (تومان به ازای میلی‌گرم؛ عدد یا رشته با حداکثر ۲ رقم اعشار و حداقل 0.01):

curl -s -X POST \
-H "X-API-Key: $TALANO_API_KEY" -H "Content-Type: application/json" \
-d '{"market_id":"<MARKET_ID>","side":"buy","base_amount":1000,"target_price":"10300.00"}' \
https://service.talano.ir/api/public/v1/quotes/

در پاسخ kind برابر "limit" و price برابر target_price است و کارمزد با نرخ سفارش حد حساب می‌شود.

۳. ثبت سفارش حد​

ظرف ۳۰ ثانیه:

curl -s -X POST \
-H "X-API-Key: $TALANO_API_KEY" -H "Content-Type: application/json" \
-d '{"quote_id":"<QUOTE_ID>","base_amount":1000,"expires_at":"2026-11-14T10:00:00Z","client_id":"bot-limit-000045"}' \
https://service.talano.ir/api/public/v1/limit-orders/
فیلدقوانین
quote_idالزامی. یک پیش‌فاکتور حد. بازار، سمت و قیمت هدف از آن خوانده می‌شود
base_amountالزامی، میلی‌گرم، با حدود limits.limit بررسی می‌شود
expires_atاختیاری، ISO 8601 با offset. باید در آینده و حداکثر تا سقف سرور (پیش‌فرض ۳۰ روز؛ قابل تغییر توسط پشتیبانی) باشد. اگر نفرستید، انقضای پیش‌فرض سرور اعمال می‌شود
client_idمثل سفارش آنی. client_idای که برای سفارش آنی استفاده شده اینجا قابل استفاده نیست (409)

پاسخ 201 (یا 200 برای تکرار با همان client_id) یک شیء سفارش با status: "active" و locked_amount است. در خرید، total تومانی و در فروش، base_amount میلی‌گرم طلا قفل می‌شود. اگر موجودی کافی نباشد: 400 insufficient_balance (درگاه پرداخت وجود ندارد).

قیمت هدف دوباره بررسی می‌شود

هنگام ثبت سفارش، قیمت هدف دوباره با قیمت لحظه‌ای مقایسه می‌شود. اگر از زمان پیش‌فاکتور بازار از قیمت هدف شما گذشته باشد (مثلاً قیمت خرید به زیر هدف خرید شما رسیده)، خطای invalid_target_price می‌گیرید. در این حالت قیمت جدید را بخوانید و هدف را دوباره انتخاب کنید.

۴. پیگیری وضعیت​

curl -s -H "X-API-Key: $TALANO_API_KEY" \
https://service.talano.ir/api/public/v1/orders/<ORDER_ID>/

یا همه‌ی سفارش‌های حد فعال:

curl -s -H "X-API-Key: $TALANO_API_KEY" \
"https://service.talano.ir/api/public/v1/orders/?order_type=limit&status=active"

وقتی سفارش اجرا شود status برابر done می‌شود و execution_price و traded_at پر می‌شوند. Webhook در نسخه‌ی ۱ وجود ندارد، پس باید poll کنید. فاصله‌ی چند ثانیه‌ای یا بیشتر کافی است؛ سقف درخواست را در نظر داشته باشید.

۵. لغو​

curl -s -X POST -H "X-API-Key: $TALANO_API_KEY" \
https://service.talano.ir/api/public/v1/limit-orders/<ORDER_ID>/cancel/

بدون بدنه. فقط سفارش‌های حد active لغو می‌شوند (از جمله سفارش‌هایی که در اپلیکیشن ثبت کرده‌اید) و مبلغ قفل‌شده آزاد می‌شود.

پاسخمعنی
200سفارش با status: "cancelled"
400 order_not_cancellableسفارش دیگر active نیست (اجرا، لغو یا منقضی شده)
404 not_foundچنین سفارش حدی برای شما وجود ندارد (برای سفارش آنی هم همین خطا)

نمونه‌ی کامل پایتون​

limit_order.py
"""Limit order: quote with target_price -> place -> poll -> cancel.

A buy target must be BELOW the current buy price; a sell target ABOVE the current sell price.
"""
import os
import time
import uuid
from datetime import datetime, timedelta, timezone
from decimal import Decimal

import requests

BASE = os.environ.get("TALANO_BASE_URL", "https://service.talano.ir/api/public/v1")

s = requests.Session()
s.headers["X-API-Key"] = os.environ["TALANO_API_KEY"]


def call(method: str, path: str, **kwargs) -> dict:
r = s.request(method, f"{BASE}{path}", timeout=10, **kwargs)
if r.status_code >= 400:
print("error:", r.status_code, r.text)
r.raise_for_status()
return r.json()


market = next(m for m in call("GET", "/markets/") if m["symbol"] == "MLG/TMN")
if not market["trading"]["limit_enabled"]:
raise SystemExit("limit orders are disabled on this market right now")

# 1. Target 2% below the current buy price (Toman per mg, 2 decimals, sent as a string)
price = call("GET", f"/markets/{market['id']}/price/")
buy_price = Decimal(price["buy"]["price"])
target = (buy_price * Decimal("0.98")).quantize(Decimal("0.01"))
print("buy price", buy_price, "-> target", target)

# 2. Limit quote (valid 30 s)
quote = call("POST", "/quotes/", json={
"market_id": market["id"],
"side": "buy",
"base_amount": 1000,
"target_price": str(target),
})
print("quote kind:", quote["kind"], "price:", quote["price"]) # kind is "limit"

# 3. Place it; funds are locked now. Expire in 7 days.
expires_at = (datetime.now(timezone.utc) + timedelta(days=7)).isoformat(timespec="seconds")
order = call("POST", "/limit-orders/", json={
"quote_id": quote["quote_id"],
"base_amount": int(quote["base_amount"]), # response numbers are strings
"expires_at": expires_at,
"client_id": f"limit-{uuid.uuid4()}",
})
print("placed", order["id"], order["status"], "locked", order["locked_amount"], "Toman")

# 4. Poll a few times (each GET counts against the read bucket: 60/min by default)
for _ in range(3):
order = call("GET", f"/orders/{order['id']}/")
print("status:", order["status"])
if order["status"] != "active":
break
time.sleep(1)

# 5. Still waiting? Cancel it and release the locked Toman.
if order["status"] == "active":
order = call("POST", f"/limit-orders/{order['id']}/cancel/")
print("cancelled:", order["status"])

خطاهای مخصوص سفارش حد​

کدعلت
limit_disabledسفارش حد در این بازار خاموش است
invalid_target_priceقیمت هدف در سمت اشتباه قیمت فعلی است، یا هنگام ثبت سفارش بازار از آن گذشته است
invalid_expires_atexpires_at گذشته یا بیش از سقف مجاز است
quote_kind_mismatchپیش‌فاکتور آنی را به /limit-orders/ فرستاده‌اید (یا برعکس)
order_not_cancellableسفارش دیگر active نیست