pine-script - 进入策略后如何保存价值作为条件?
问题描述
我遇到以下问题:
我输入了一个在 close < ema 之后关闭的策略。为了更好的退出,我想在关闭 > ema 但低点(进入后任何给定柱的)低于 ema(低 < ema)时关闭它。
我无法弄清楚如何在“进入后任何给定的酒吧”那一刻做。我猜如果为真,脚本应该以某种方式存储前一个柱的值,但是当策略实际开始时,脚本就会出现问题。任何帮助,将不胜感激!
PS。如您所见,我不是编码员,这可能很难理解。我真的很抱歉,并感谢您的时间。
米海尔
我尝试用 strategy.position_avg_price > 0 说明进入条件何时开启,并添加所需的条件:
h = nz(strategy.position_avg_price) > 0 and not
crossunder(close,ema(close,length)) and
crossunder(low,ema(close,length)) ? 1 : 0
rightborder = barstate.islast // treat the last bar (most recent bar)
as the right edge of the lookback window range
// if examining the last bar (newest bar, rightborder is true)
// set variable "val" to the previous value of series variable "h"
// else set to na so nothing is plotted
val = rightborder ? h[1] : na
但是没有成功...
scalp = b and c and d and e and f and g ? 1 : 0 // scalp is main
variable, if 1 the strategy is entered//
if (scalp)
strategy.entry("Short", strategy.short, when = scalp) // entry of
strategy
if (crossunder(close,ema(close,length))) // usual close of strategy
strategy.close("Short")
if (not crossunder(close,ema(close,length)) and
crossunder(low,ema(close,length))) // attempt for a better exit!
strategy.close("Short")
在处理米奇的建议后:
///Entry
if entry_on == 0 and scalp
strategy.entry("Short", strategy.short)
entry_on := 1
///Desired exit
if entry_on == 1 and crossunder(close,ema(close,length))
strategy.close("Short")
entry_on := 0
/// Risk mitigation - 1 - Additional risk mitigation (when close > ema but
low < ema of any given candle after entry -> exit at breakeven)
if entry_on == 1 and close > ema(close, length) and low < ema(close, length)
entry_on := 2
if entry_on == 2 and crossover(close,strategy.position_avg_price)
strategy.close("Short")
entry_on := 0
/// Risk mitigation - 2 - exit 15 bars after entry if not desired exit or
risk mitigation - 1
if entry_on == 1 and scalp[15]
strategy.close("Short")
entry_on := 0
解决方案
尝试这样的事情:
entry_on = 0.0
entry_on := entry_on[1] //this will carry entry_on result from last candle
if entry_on == 0 and close > ema(close, length)
xx enter your open position code
entry_on := 1
if entry_on == 1
if close < ema(close, length) or low < ema(close, length)
xx enter your close position code
entry_on := 0
推荐阅读
- flutter - 在 InitState 中设置提供程序状态会出错
- sas - Sas base:按最大表一对一读取或从下一行获取数据
- python - 通过忽略粗线从粗线图中检测连接点
- git - push 后是否对 repo 的 .git/hooks/pre-commit 部分进行了更改?或者他们只是出现在我当地?
- python - 有没有办法使用正则表达式组合多个字符串?
- html - 当我尝试格式化它们以适合我的规格时,我的 div 中的两个元素会变得“不平衡”
- node.js - firebase 管理节点,权限不足
- java - 避免重写装饰器中的所有方法
- java - 如何解决问题:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465;?
- javascript - 将服装快捷操作添加到 Tampermonkey 脚本中