شعار Broker Api Idempotent Cancel Requests

Broker Api Idempotent Cancel Requests

المجتمع
HimanshuJ16
broker-api-idempotent-cancel-requests

Use when cancelling live orders over an unreliable network. De-duplicates cancel retries and separates a cancel the broker acknowledged from one it completed, so a timeout, 5xx or order-not-found is never read as proof the order is dead.

نظرة عامة

الناشرHimanshuJ16
المستودعAlgo-Trading-Skills
اسم المهارةbroker-api-idempotent-cancel-requests
النجوم
50
التفريعات
7
الملفات المرفقة
5
الترخيصApache-2.0
الروابط
  • تعليمات بصيغة Markdown

    ملف SKILL.md يُحمّله النموذج عند الحاجة، فلا يستهلك توكنات إلا عندما يَرِد طلب ذو صلة فعلًا.

  • تعمل مع أي نموذج لغوي كبير

    مهارات الذكاء الاصطناعي مكتوبة بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا تعمل مع GPT أو Claude أو Gemini أو Grok أو نموذج محلي.

  • الملفات المرفقة: 5

    نصوص برمجية وقوالب ومراجع يمكن للنموذج قراءتها أثناء عمله. الملفات للقراءة فقط ولا تُنفّذ أبدًا.

  • مفتوحة المصدر

    نشرها HimanshuJ16 على GitHub. اطّلع على المصدر قبل تثبيتها.

التثبيت

ثبّت مهارة الذكاء الاصطناعي Broker Api Idempotent Cancel Requests في TypingMind لاستخدامها مع أي نموذج لغوي كبير، أو أضفها إلى وكيل ذكاء اصطناعي آخر يقرأ SKILL.md.

1

التثبيت في TypingMind

يُثبّت TypingMind المهارة مباشرةً من مجلدها على GitHub — فيقرأ SKILL.md، ويجمع ملفات الموارد في حزمة، ويحفظ النتيجة محليًا.

  1. افتح التطبيق وانتقل إلى «الإضافات» ثم «المهارات».
  2. اختر «تثبيت من GitHub».
  3. الصق رابط مجلد المهارة الموضّح أدناه وأكّد التثبيت.
    https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/broker-api-idempotent-cancel-requests
  4. فعّل المهارة في كل محادثة تريد إتاحتها فيها.
الإضافات ← المهارات ← إضافة مهارة ← من رابط GitHub، ثم الصق رابط المجلد واضغط على «متابعة».
2

التثبيت في وكيل ذكاء اصطناعي آخر

يمكن لأي وكيل ذكاء اصطناعي يقرأ تنسيق Agent Skills استخدام هذه المهارة — انسخ المجلد إلى مجلد المهارات الخاص بذلك الوكيل.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/HimanshuJ16/Algo-Trading-Skills.git /tmp/Algo-Trading-Skills
mkdir -p .claude/skills
cp -r /tmp/Algo-Trading-Skills/skills/broker-api-idempotent-cancel-requests .claude/skills/broker-api-idempotent-cancel-requests
أعد تشغيل Claude Code بعد النسخ ليتعرّف على المهارة الجديدة.

استخدمها في TypingMind

فعّل Broker Api Idempotent Cancel Requests في أي محادثة على TypingMind وسيتولى النموذج الباقي. تتضمن مطالبة النظام اسمها ووصفها، وبمجرد ورود طلب ذي صلة، يُحمّل النموذج التعليمات الكاملة تلقائيًا — فلا تحتاج إلى استدعائها يدويًا، ولا تستهلك أي توكنات حتى تُستخدم فعليًا.

يُحمّل النموذج مهارة Broker Api Idempotent Cancel Requests تلقائيًا بمجرد ورود طلب ذي صلة بها.

تعمل مع أي نموذج ذكاء اصطناعي

مهارات الذكاء الاصطناعي هي تعليمات بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا لا ترتبط Broker Api Idempotent Cancel Requests بالنموذج الذي كُتبت له. ثبّتها مرة واحدة في TypingMind واستخدمها مع GPT-5 أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو نموذج محلي تشغّله بنفسك — كل ذلك باستخدام مفاتيح API الخاصة بك.

  • تُحمَّل عند الحاجة فقط

    تتضمن مطالبة النظام الاسم والوصف فقط. وتُجلب التعليمات عند أول طلب ذي صلة، لذا لا تستهلك تعليمات المهارة أي توكنات ما دامت المهارة غير مستخدمة.

  • بدّل النماذج أثناء المحادثة

    لأن المهارة تعليمات وليست شيفرة، فإن تغيير النموذج لا يعطّلها — فالنموذج التالي يقرأ ملف SKILL.md نفسه.

تعليمات المهارة

هذا هو محتوى SKILL.md الذي يُحمّله النموذج. اقرأه قبل التثبيت — فالمهارة عبارة عن تعليمات سيتبعها نموذجك.

When to Use

Invoke this skill when a bot issues order cancel requests over an unreliable network, from concurrent threads, or in a retry loop. Cancellation carries race conditions that order placement does not: the matching engine may fill the order microseconds before the cancel arrives, and a lost response leaves the client unable to tell whether the cancel landed.

The skill exists to enforce one distinction that most cancel code gets wrong:

A cancel request is a request, not an outcome.

Every broker consulted says so in its own words. FIX 4.4 defines OrdStatus (tag 39) value 6 — "Pending Cancel (e.g. result of Order Cancel Request <F>)" — as a state distinct from 4 = "Canceled"; only a subsequent ExecutionReport (35=8) moves the order to 4. Alpaca's pending_cancel status is documented as "The order is waiting to be canceled", and its docs state orders "will remain in pending_cancel until canceled by the execution venue that Alpaca routed the order to for execution" — the DELETE /v2/orders/{id} returns 204 long before that. Zerodha Kite Connect states plainly that "Successful placement of an order via the API does not imply its successful execution" and directs clients to postbacks for the actual transition.

So an HTTP 2xx on a cancel means pending cancel, and the order can still fill. Code that frees capital, decrements exposure, or fires a replacement order on that 2xx is acting on an order that is still working.

When NOT to Use

  • As confirmation that an order is dead. This classifies one cancel dispatch. Only the broker's order-state stream — ExecutionReport, postback, or an order-status query — settles the outcome. Use webhook-based-order-fill-notifications for that stream.
  • As a cross-restart idempotency guarantee. The dedup cache is in-memory and per-process. It does not survive a restart and is not shared across replicas. Durable cancel dedup needs an intent ledger — see order-placement-idempotency.
  • As a substitute for a kill switch. Cancelling orders one id at a time through a retrying client is not a flatten-everything control. Use kill-switch-and-drawdown-circuit-breakers and the broker's mass-cancel endpoint.
  • As a rate-limit strategy. It backs off and honours Retry-After, but sizing cancel traffic against a broker's quota belongs to multi-broker-rate-limit-handling and order-to-trade-ratio-fee-penalty-avoidance.

Prerequisites

  • The broker's order id or ClOrdID for the order being cancelled.
  • A cancel id (client_cancel_id) that is stable across retries of the same cancel intent — reusing it is what makes the retry idempotent.
  • Access to the broker's order-state stream or order-status endpoint, for the reconciliation step. Without it, every non-terminal outcome below is a dead end.
  • Documented cancel semantics for your specific broker: which HTTP codes it returns, and whether its cancel endpoint is asynchronous (Alpaca, Kite) or synchronous (Binance DELETE /api/v3/order returns the order already in state CANCELED).

Workflow

  1. Mint a stable cancel id. One id per cancel intent, not per network attempt. Retries of the same intent reuse it; a genuinely new cancel decision gets a new one. Include a process-unique component — a per-process counter alone collides across restarts.

  2. Claim the id before dispatching. Check the dedup cache and the in-flight set under a single lock. Checking the cache and dispatching as two separate steps lets two threads both miss and both hit the broker — Binance auto-bans an IP with HTTP 418 for continuing to send after 429s, so a cancel storm can cost you the ability to cancel at all. A duplicate concurrent caller waits on the first dispatch; it does not add to it.

  3. Dispatch with a bounded, jittered retry. Retry on 5xx, 408, 429, and transport errors. Honour Retry-After when present — RFC 9110 allows either delay-seconds or an HTTP-date, so parse both. Cap it: if the broker asks for longer than your retry budget, stop and hand the number back to the caller rather than sleeping a risk-reducing cancel thread through a ban. Reset the response buffer each attempt, or the final classification runs against a stale earlier body.

  4. Classify — and let ambiguity resolve toward "reconcile". The two error directions are not symmetric. Reporting a live order as dead stops the caller from ever retrying and strands working exposure; reporting a dead order as uncertain costs one status query.

    Broker responseStatusOrder state
    2xxPENDING_CANCELStill working — can fill until an execution report says otherwise
    2xx, synchronous-cancel broker (opt-in)CANCELLEDDead
    4xx "too late to cancel" / "already filled"FILLED_BEFORE_CANCELDead — it filled
    4xx "already cancelled"ALREADY_CANCELLEDDead
    404, or "unknown order" / "no such order"ORDER_UNKNOWNUnknown — reconcile
    Other 4xx (incl. bare Alpaca 422)REJECTEDPresumed still working — reconcile
    Exhausted 5xx / 429 / transport errorUNKNOWNUnknown — reconcile

    Two classifications deserve their reasoning spelled out, because the obvious shortcut is wrong in both:

    • "Order not found" is not proof of cancellation. Binance returns -2011 (CANCEL_REJECTED) and -2013 (NO_SUCH_ORDER, "Order does not exist.") for an order that is genuinely gone and for a request carrying the wrong API key, symbol, or order id — where the order is still live and working. Treat it as ORDER_UNKNOWN and query the order book.
    • "Partially filled" is not "filled". A partial fill does not close the order; the remainder is usually still cancellable. Classifying it as FILLED_BEFORE_CANCEL overstates the executed quantity and corrupts position accounting.

    Match error text with word boundaries and negation guards, not bare substrings — "filled" in detail matches "order was not filled, cannot cancel" and hands the caller a false fill.

  5. Cache only what the broker asserted. PENDING_CANCEL, the three terminal statuses, and REJECTED are cacheable: the broker answered, so re-sending the same id is a duplicate. UNKNOWN and ORDER_UNKNOWN must not be cached. Binance's REST documentation is explicit for the 5xx case: "It is important to NOT treat this as a failure operation; the execution status is UNKNOWN and could have been a success." Caching an indeterminate outcome as terminal makes one lost response permanent — every later retry under that cancel id replays the cached failure and never reaches the broker, leaving a live order that can no longer be cancelled.

  6. Reconcile before acting. Anything that is not terminal means: read the order's state from the broker before adjusting exposure, freeing capital, or re-cancelling. Re-dispatch the same cancel id after an UNKNOWN; use a new one after a REJECTED you have diagnosed and fixed.

Full procedure and per-broker response tables: see references/workflows.md. Standards, FIX mappings, and cited sources: see references/standards.md. Printable pre-flight checklist: see assets/checklist.md.

Common Pitfalls

  • Treating an HTTP 2xx as a completed cancellation. It is an acknowledgement. FIX models this explicitly as OrdStatus 6 (Pending Cancel) preceding 4 (Canceled), and Alpaca orders sit in pending_cancel until the execution venue confirms. The order can still fill.
  • Caching an indeterminate outcome under the cancel id. The single most damaging bug in this pattern: a timeout or exhausted 5xx stored as a terminal failure means every subsequent retry of that cancel id short-circuits to the cache, and the order is never cancelled again.
  • Concluding "order not found" means "order cancelled". The same broker error covers a wrong id, wrong symbol, and wrong API key, all of which leave the order live.
  • Substring matching on broker error text. "filled" in detail fires on "not filled" and on "partially filled". Use anchored patterns with negation guards.
  • Retrying a cancel on a hot loop without deduplication or jitter. Cancel storms trip broker throttles and order-to-trade-ratio penalties; Binance escalates from 429 to a 418 IP ban, at which point you cannot cancel anything.
  • Checking the dedup cache and dispatching as two separate steps. Under concurrency both threads miss and both dispatch. The check and the claim must happen under one lock.
  • Sleeping through a long Retry-After on a cancel path. A cancel reduces risk; blocking it for ten minutes because the broker suggested so is worse than returning control and letting the desk decide.
  • Assuming in-memory idempotency survives a restart. After a process restart the cache is empty and the sequence counter has reset. Persist the cancel id with the order intent.

Verification

  • Run python -m unittest discover -s skills/broker-api-idempotent-cancel-requests/scripts and confirm all tests pass.
  • Confirm an HTTP 200, 202, and 204 each yield PENDING_CANCEL with requires_reconciliation true, and that treat_ack_as_cancelled=True is the only way to get CANCELLED.
  • Drop the response on the first attempt, then retry under the same client_cancel_id, and confirm the retry actually reaches the broker — a cached indeterminate result would silently skip it.
  • Feed {"detail": "order was not filled, cannot cancel"} and {"detail": "order partially filled"} and confirm neither is classified FILLED_BEFORE_CANCEL.
  • Feed a 404 "Order not found" and confirm ORDER_UNKNOWN, not ALREADY_CANCELLED.
  • Feed a bare Alpaca-style 422 "The order status is not cancelable." and confirm REJECTED with is_terminal false.
  • Fire eight concurrent cancels of one client_cancel_id against a slow transport and confirm exactly one broker dispatch and one non-replayed result.
  • Return 429 with Retry-After: 600 against a 5 s retry budget and confirm no sleep occurs, the call returns UNKNOWN, and retry_after_s is surfaced to the caller.

Related Skills

  • order-placement-idempotency
  • webhook-based-order-fill-notifications
  • broker-agnostic-adapter-interface
  • multi-broker-rate-limit-handling
  • broker-side-order-throttle-detection
  • order-to-trade-ratio-fee-penalty-avoidance

الملفات المرفقة

يقرأ النموذج هذه الملفات عند الحاجة ما دامت المهارة محمّلة. وهي متاحة للقراءة ولا تُنفّذ أبدًا.

الأسئلة الشائعة

ماذا تفعل مهارة الذكاء الاصطناعي Broker Api Idempotent Cancel Requests؟

Use when cancelling live orders over an unreliable network. De-duplicates cancel retries and separates a cancel the broker acknowledged from one it completed, so a timeout, 5xx or order-not-found is never read as proof the order is dead.

لماذا تستخدم Broker Api Idempotent Cancel Requests على TypingMind؟

لأنك تثبّتها مرة واحدة وتستخدمها مع أي نموذج. مهارة Broker Api Idempotent Cancel Requests عبارة عن نص Markdown وليست شيفرة خاصة بمزود بعينه، لذا تعمل المهارة نفسها مع GPT-5 أو Claude أو Gemini أو Grok أو نموذج محلي، ويمكنك تبديل النموذج أثناء المحادثة دون تعطّل المهارة. يعمل TypingMind بمفاتيح API الخاصة بك، فتدفع للمزودين مباشرةً بدلًا من اشتراك لكل مقعد، وتبقى مهاراتك ومحادثاتك في مساحة التخزين الخاصة بك.

كيف أثبّت Broker Api Idempotent Cancel Requests في TypingMind؟

في TypingMind، افتح «الإضافات»، ثم «المهارات»، ثم اختر «التثبيت من GitHub» والصق https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/broker-api-idempotent-cancel-requests. يقرأ TypingMind ملف SKILL.md الخاص بالمهارة، ويجمع ملفاتها ويثبّتها كمهارة يمكنك تفعيلها لكل محادثة على حدة.

ما نماذج الذكاء الاصطناعي التي يمكنها استخدام Broker Api Idempotent Cancel Requests؟

أي نموذج تربطه في TypingMind. مهارات الذكاء الاصطناعي عبارة عن تعليمات Markdown وليست شيفرة خاصة بمزود بعينه، لذا يمكن لكل من GPT وClaude وGemini وGrok والنماذج المحلية تحميل هذه المهارة عندما يتوافق الطلب معها.

كم نموذج ذكاء اصطناعي يمكنني استخدامه مع Broker Api Idempotent Cancel Requests؟

أي عدد تريده. ما دام النموذج يدعم المهارات، يمكنك استخدام Broker Api Idempotent Cancel Requests معه، سواء كان GPT أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو غيرها، وكل ذلك على TypingMind باستخدام مفاتيح API الخاصة بك.

هل مهارة الذكاء الاصطناعي Broker Api Idempotent Cancel Requests مجانية؟

نعم. نشرها HimanshuJ16 على GitHub بموجب ترخيص Apache-2.0. لا تدفع إلا لمزود الذكاء الاصطناعي الخاص بك مقابل التوكنات التي تستخدمها.

ما هي مهارات الذكاء الاصطناعي؟

مهارة الذكاء الاصطناعي حزمة تعليمات قابلة لإعادة الاستخدام تعلّم نموذج الذكاء الاصطناعي كيفية تنفيذ مهمة محددة. وتتبع تنسيق Agent Skills المفتوح: ملف SKILL.md يتضمن اسمًا ووصفًا، إلى جانب أي نصوص برمجية أو قوالب أو ملفات مرجعية قد يحتاج إليها النموذج. لا يقرأ النموذج التعليمات إلا عندما يتوافق طلبك مع المهارة، لذا لا تترتب على المهارة المثبتة أي تكلفة حتى تُستخدم.

كيف تختلف مهارات الذكاء الاصطناعي عن الإضافات أو موصلات MCP؟

تمنح الإضافة أو موصل MCP النموذج أدوات جديدة يمكنه استدعاؤها، وهي شيفرة تُنفَّذ في مكان ما وتُرجع نتيجة. أما مهارة الذكاء الاصطناعي فتزوّد النموذج بالمعرفة ومنهجية العمل: كيفية التعامل مع مهمة، والخطوات التي ينبغي اتباعها، وما ينبغي أن تكون عليه المخرجات الجيدة. المهارات عبارة عن نصوص Markdown، لذا لا تحتاج إلى خادم أو مفتاح API أو بيئة تشغيل، وتعمل مع أي نموذج.

عرض الكل

أنشئ مساحة عملك للذكاء الاصطناعي الآن

اشترك في نشرتنا الإخبارية لتصلك أخبار الميزات الجديدة والهدايا القادمة 👇