不能使用可变变量作为安全功能的参数吗?

时间:2019-12-22 22:19:36

标签: pine-script

我遇到了将可变变量作为安全功能参数的问题。我尝试将代码中的可变变量包装在函数中,如本文建议的那样。但是在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)

1 个答案:

答案 0 :(得分:1)

您必须在其中放置不同的安全性,然后根据需要选择其中一项:

//@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)