Pine script 如何在输入策略后将值保存为条件?

Pine script 如何在输入策略后将值保存为条件?,pine-script,Pine Script,我遇到以下问题: 我输入一个策略,该策略在关闭ema时关闭它,但low(进入后任何给定条的)低于ema(low0说明何时进入条件,并将所需条件添加到其中: h = nz(strategy.position_avg_price) > 0 and not crossunder(close,ema(close,length)) and crossunder(low,ema(close,length)) ? 1 : 0

我遇到以下问题:

我输入一个策略,该策略在关闭ema时关闭它,但low(进入后任何给定条的)低于ema(low 我不知道如何做'任何给定酒吧后进入'的时刻。我想脚本应该以某种方式存储上一条的值(如果为真),但是当策略实际启动时,脚本就会出现问题。任何帮助都将不胜感激

你可以看到,我不是一个程序员,这可能很难理解。我真的很抱歉,谢谢你抽出时间

米哈伊尔

我已尝试使用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
///条目
如果输入_on==0,则头皮
strategy.entry(“Short”,strategy.Short)
输入值:=1
///期望出口
如果条目_on==1且在下方交叉(闭合,ema(闭合,长度))
策略。结束(“短”)
条目上的值:=0
///风险缓解-1-额外风险缓解(关闭时>均线,但
低<进入后任何给定蜡烛的均线->盈亏平衡时退出)
如果条目_on==1且关闭>均线(关闭,长度)且低<均线(关闭,长度)
输入时间:=2
如果进入=2且交叉(关闭、策略、位置、平均价格)
策略。结束(“短”)
条目上的值:=0
///风险缓解-2-进入后退出15巴,如果不希望退出或
风险缓解-1
如果输入_on==1且头皮[15]
策略。结束(“短”)
条目上的值:=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
entry\u on=0.0
entry\u on:=entry\u on[1]//这将携带上次蜡烛结果的entry\u
如果条目_on==0且关闭>ema(关闭,长度)
xx输入您的空缺职位代码
输入值:=1
如果条目_on==1
如果关闭<均线(关闭,长度)或低<均线(关闭,长度)
xx输入您的关闭位置代码
条目上的值:=0

非常感谢。你的建议肯定有帮助。然而,出于某种原因,尽管代码以策略而不是脚本开始,但当我将其添加到图表中时,不会触发策略,并且在pine选项卡的“添加到图表的脚本”中有一个wierd最后一行(因为代码中没有绘图功能,因此未显示任何内容)。知道为什么吗?你能简单地看一下我在读了你的文章后最终完成的代码,并告诉我它是否有意义吗?我不得不做一些调整,我可能错过了一些再次发生的事情。调整后的代码位于问题的底部。发现“sript已添加到图表中”甚至出现在战略脚本中,因此忽略我的评论。但在某些地方,代码是错误的,因为交易没有触发