QuantWingConnector

Ai_King_9319 · library · 54 行 · 点赞 0 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// SPDX-License-Identifier: Apache-2.0
// Copyright Ai_King_9319
//@version=6
// @description QuantWing order-fill metadata v1. Strategy execution requires separate validation.
// The alert's OUTER template supplies actual filled quantity, symbol, timestamp and positions.
// Do not put TradingView placeholders inside this library's returned string.
library("QuantWingConnector", overlay = false)
 
// @function Escape a JSON string without changing protocol keys.
// @param value Unescaped text.
// @returns JSON-safe text, without surrounding quotes.
export escape(series string value) =>
    // Pine supports \n and \t literals, but not \r, \b or \f literals.
    // Reject other ASCII controls instead of silently replacing ordinary letters.
    if str.length(str.match(value, "[\\x00-\\x08\\x0B-\\x1F]")) > 0
        runtime.error("QuantWingConnector: unsupported control character")
    s = str.replace_all(value, "\\", "\\\\")
    s := str.replace_all(s, "\"", "\\\"")
    s := str.replace_all(s, "\n", "\\n")
    str.replace_all(s, "\t", "\\t")
 
// @function Build metadata for a strategy order-fill alert; not a complete webhook body.
// @param action buy, sell, or close. close means FULL flatten, never partial reduction.
// @param strategyName Display name, independent of the source-stream identity.
// @param reason Original order reason; unknown wording is preserved.
// @param language ZH for Traditional Chinese, otherwise English.
// @param stream Unique non-secret identifier for THIS alert instance. Change on alert recreation.
// @param sequence Optional VERIFIED monotonic fill sequence. Never use submission order.
// @param eventId Optional stable event identity, unique within the stream.
// @param predecessor Optional explicit full-close event preceding this flat-to-entry fill.
// @returns JSON object to embed as signal via {{strategy.order.alert_message}} in the outer template.
export order(series string action, series string strategyName, series string reason, series string language, series string stream, series int sequence = na, series string eventId = "", series string predecessor = "") =>
    if action != "buy" and action != "sell" and action != "close"
        runtime.error("QuantWingConnector: action must be buy, sell or close")
    if str.length(stream) == 0
        runtime.error("QuantWingConnector: set a unique alert-instance stream")
    if not na(sequence) and sequence < 0
        runtime.error("QuantWingConnector: sequence must be non-negative")
    if str.length(eventId) > 128 or str.length(predecessor) > 128 or (str.length(predecessor) > 0 and (eventId == "" or predecessor == eventId or action == "close"))
        runtime.error("QuantWingConnector: invalid explicit handoff identity")
    label = language == "ZH" ? (action == "buy" ? "買進" : action == "sell" ? "賣出" : "平倉") : (action == "buy" ? "Buy" : action == "sell" ? "Sell" : "Close")
    note = label + " | " + strategyName + (str.length(reason) > 0 ? " | " + reason : "")
    seqField = na(sequence) ? "" : ",\"source_sequence\":" + str.tostring(sequence)
    eventField = eventId == "" ? "" : ",\"source_event_id\":\"" + escape(eventId) + "\""
    predecessorField = predecessor == "" ? "" : ",\"source_predecessor_event_id\":\"" + escape(predecessor) + "\""
    "{\"action\":\"" + action + "\",\"strategy\":\"" + escape(strategyName) + "\",\"PS\":\"" + escape(note) + "\",\"source_stream\":\"" + escape(stream) + "\"" + seqField + eventField + predecessorField + "}"
 
// @function Correlation ID for one explicit market-close handoff per route/bar.
// @param barTime Opening time of the submitting bar, not a claimed fill timestamp.
// @param route Unique handoff route within the strategy.
// @param heldEntryId The actual open entry ID targeted by the market close.
// @returns A stream-scoped correlation ID; never a monotonic fill sequence.
export handoffId(series int barTime, series string route, series string heldEntryId) =>
    str.tostring(barTime) + ":" + route + ":" + heldEntryId

← 返回列表