首页 > 解决方案 > 进入策略后如何保存价值作为条件?

问题描述

我遇到以下问题:

我输入了一个在 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

标签: pine-script

解决方案


尝试这样的事情:

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

推荐阅读