不能使用可变变量作为安全函数的参数? [英] Cannot use a mutable variable as an argument of the security function?

查看:76
本文介绍了不能使用可变变量作为安全函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将可变变量包含为安全函数的参数时遇到问题.我尝试在函数中使用可变变量包装代码,就像这篇文章建议的那样.但它在 v4 上似乎对我不起作用.安全功能是必需的,因为我的策略在 4 个不同的时间范围内运行.以下代码是一个简化版本,用于显示问题发生的位置.有什么建议吗?https://www.tradingview.com/wiki/Pine_Version_3_Migration_variable_a_resolving_the_security_expression>

I have experiencing problem to include a mutable variable as an argument for security function. I tried wrapping the code with a mutable variable in a function, like this article suggested. But it doesn't seem to work for me on v4. The security function is required because my strategy runs on 4 different timeframes. The following code is a simplified version to show where the issue occurs. Any advices? 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)

推荐答案

你必须设置不同的安全性,然后根据你的需要选择其中一种:

You have to put there different security and then choose one of the depending on your needs:

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

在你的情况下,它会是这样的:

In your case it'll be something like:

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

这篇关于不能使用可变变量作为安全函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆