簡體   English   中英

不能使用可變變量作為安全函數的參數?

[英]Cannot use a mutable variable as an argument of the security function?

我在將可變變量作為安全函數的參數包含在內時遇到問題。 我嘗試在函數中使用可變變量包裝代碼,就像這篇文章建議的那樣。 但它在 v4 上似乎對我不起作用。 安全功能是必需的,因為我的策略在 4 個不同的時間范圍內運行。 以下代碼是一個簡化版本,用於顯示問題發生的位置。 有什么建議嗎? https://www.tradingview.com/wiki/Pine_Version_3_Migration_Guide#Resolving_a_problem_with_a_mutable_variable_in_the_security_expression

//@version=4
strategy(title = "My Strategy", overlay = true, pyramiding = 0, calc_on_order_fills = false, calc_on_every_tick = false, default_qty_type = strategy.percent_of_equity, default_qty_value = 98, initial_capital = 100, commission_type = strategy.commission.percent, commission_value = 0.075, process_orders_on_close = true, close_entries_rule = "FIFO")

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STATE

hasOpenTrade() => strategy.opentrades != 0

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// VARIABLES

var maxSinceLastBuySell = 0.
var minSinceLastBuySell = 0.

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// FUNCTIONS

if hasOpenTrade()
    maxSinceLastBuySell := max(maxSinceLastBuySell, high)
    minSinceLastBuySell := min(minSinceLastBuySell, low)

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// CORE

price_B = low < maxSinceLastBuySell * 0.9 ? 1 : 0
price_B_persistence = sum(price_B, 2) == 2 ? true : false

price_S() => 
    sum((high > minSinceLastBuySell * 1.1 ? 1 : 0), 2) == 2

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// EXECUTION

longCondition = security(syminfo.tickerid, "60", price_B_persistence)
if (longCondition)
    maxSinceLastBuySell := high
    minSinceLastBuySell := low
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition_sec = security(syminfo.tickerid, "60", price_S())
if (shortCondition_sec)
    maxSinceLastBuySell := high
    minSinceLastBuySell := low
    strategy.entry("My Short Entry Id", strategy.short)

您必須放置不同的安全性,然后根據您的需要選擇其中之一:

//@version=4
study("My Script")
s = bar_index % 2 == 0 ? security("F", "1", close) : security("BA", "1", close)
plot(s)

在你的情況下,它會是這樣的:

...
// one security when the price_B_persistence is true and one - when it false
longCondition = price_B_persistence ? security(syminfo.tickerid, "60", true) : security(syminfo.tickerid, "60", false)
...
shortCondition_sec = price_S() ? security(syminfo.tickerid, "60", true) : security(syminfo.tickerid, "60", false)
price_S(h) => 
    sum((h > minSinceLastBuySell * 1.1 ? 1 : 0), 2) == 2

high60 = security(syminfo.tickerid, "60", high)
shortCondition_sec = price_S(high60)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM