进入策略后如何将价值保存为条件?

时间:2019-03-24 15:57:37

标签: pine-script tradingview-api

我在以下方面遇到麻烦:

我输入关闭 ema时将其关闭,但是低点(进入后任何给定条形的低点)都比ema低(low

我无法弄清楚“进入后任何给定的小节”的时间。我想脚本应该以某种方式存储前一栏的值(如果为true),但是当策略实际开始时,脚本就会出现问题。任何帮助将不胜感激!

PS。如您所见,我不是编码员,这可能很难理解。我真的对此表示歉意,谢谢您的宝贵时间。

Mihail

我已尝试使用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

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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