我看到錯誤訊息“無法建立負數量訂單”
當策略將其進場部位大小計算為自身權益的百分比時,如果權益跌至0以下,部位大小也會變為負值,這在實際交易中是不可能的,從而導致運算錯誤。
解決此問題的最佳方法是將策略轉換為Pine Script v6。在Pine v6中,策略預設會對部位大小施加限制。如果策略的權益大幅下降,現有部位將因保證金追繳被強制清算,而當資金完全耗盡時,策略不會嘗試建立數量為負的訂單,從而避免此錯誤。
如果您想了解更多關於此錯誤的來源,以及如何在舊版Pine Script中避免,請參閱下方內容。
如果策略淨值變為負並且為strategy.entry()或strategy.order()函數計算的合約總數為負整數值,則會出現此錯誤。可以透過從“屬性”選單調整策略設定,或直接更改策略的邏輯來避免這種情況。
錯誤來源
讓我們看一下腳本,其中透過策略設定中的"訂單大小"設定,或透過策略的Pine數據源中的strategy_percent_of_equity常數,將訂單數量計算為權益的百分比。在每個K線上,呼叫strategy.entry()函數來輸入倉位:
//@version=5
strategy("negative_qty", default_qty_type = strategy.percent_of_equity)
strategy.entry("Short", strategy.short)
plot(strategy.equity)
將腳本新增至1D時間範圍的NASDAQ:AAPL圖表時,腳本崩潰並出現執行時間錯誤:
Cannot create an order with negative quantity.
Current qty_type is percent_of_equity and equity is less than 0
若要了解此錯誤的原因,您應該使用strategy.equity變數繪製資本圖,並使用任何條件運算子對strategy.equity()函數的呼叫新增約束。這樣,就不會在每個K線上呼叫入倉函數(並且不會導致額外的參數重新計算,包括qty值),並且腳本將計算成功:
//@version=5
strategy("negative_qty", default_qty_type = strategy.percent_of_equity)
if strategy.equity > 0
strategy.entry("Short", strategy.short)
hline(0, "zero line", color = color.black, linestyle = hline.style_dashed)
plot(strategy.equity, color = color.black, linewidth = 3)
在第二根K線開盤時(bar_index = 1),策略建立空頭部位。然而,隨著AAPL價格上漲,未平倉空單的盈虧(即strategy.openprofit變數值)迅速下降,最終導致策略的總資本變為負值(strategy.equity = strategy.initial_capital + strategy.netprofit + strategy.openprofit)。
策略引擎計算的合約數量計算公式為qty =(訂單大小 * 權益 / 100)/ 平倉。策略資金變成負值的區域可以顯示如下:
//@version=5
strategy("negative_qty", default_qty_type = strategy.percent_of_equity)
if strategy.equity > 0
strategy.entry("Short", strategy.short)
hline(0, "zero line", color = color.black, linestyle = hline.style_dashed)
plot(strategy.equity, color = color.black, linewidth = 3)
equity_p = 1 // percents of equity order size value (1% is default)
qty = (equity_p * strategy.equity / 100) / close
if qty <= -1
var l1 = label.new(bar_index, strategy.equity, text = "Negative qty_value at \n bar index: " + str.tostring(bar_index) + ".\n" + "Order size: " + str.tostring(math.round(qty)), color = color.white)
var l2 = label.new(bar_index - 1, strategy.equity[1], text = "Order size : " + str.tostring(math.round(qty[1])), color = color.white)
var l3 = label.new(bar_index - 2, strategy.equity[2], text = "Order size: " + str.tostring(math.round(qty[2])), color = color.white)
bgcolor(qty > -1 ? color.green : color.red)
螢幕截圖顯示位於負資產部分的頁籤,其中產生的合約數量為 - 2。

如果在計算策略時,在淨值為負(且合約數量為負)的K線上呼叫strategy.entry(),則策略計算會停止並出現錯誤。
該如何解決?
通常,此錯誤不會出現在正確實施的策略中。為避免錯誤,該策略應使用進入和退出倉位、停損和保證金的條件。
如果發生錯誤,調試策略的正確方法是:
1. 使用保證金槓桿(策略屬性中的多頭/空頭部位保證金,或strategy()函數中的margin_long和margin_short參數)。如果指定,如果策略沒有足夠的淨值來維持,則部分部位將自動清算。您可以在我們的用戶手冊或部落格文章中找到有關此功能的更多資訊。
//@version=5
strategy("", default_qty_type = strategy.percent_of_equity, default_qty_value = 10, margin_long = 100, margin_short = 100)
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
strategy.entry("Long", strategy.long)
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
strategy.entry("Short", strategy.short)
2. 在呼叫strategy.entry()或strategy.order()函數之前,檢查權益值是否大於零,或另外重新定義入場合約的數量。
//@version=5
strategy("", default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
if strategy.equity > 0
strategy.entry("Short", strategy.short) // enter at 10 % of currently available equity
else
strategy.entry("Long", strategy.long, qty = 1) // Reverse position with fixed contract size
3. 使用strategy.risk類別的變數進行風險管理。您可以在我們的用戶手冊中閱讀有關這些內容的更多資訊。