Pine script 自上次买卖以来的最高价格

Pine script 自上次买卖以来的最高价格,pine-script,Pine Script,目标是存储和绘制自上次买卖以来的最大价格值 以下方法将当前高点与自上次长/短交易以来的历史高点进行比较。如果当前高点更大,它将成为历史高点 不知怎的,这个情节确实在计算,但它似乎不正确,也不像预期的那样。有什么建议吗 //@version=4 strategy(title = "Max since last buy/sell", overlay = true, pyramiding = 0, calc_on_order_fills = false, calc_on_every_tick = fa

目标是存储和绘制自上次买卖以来的最大价格值

以下方法将当前高点与自上次长/短交易以来的历史高点进行比较。如果当前高点更大,它将成为历史高点

不知怎的,这个情节确实在计算,但它似乎不正确,也不像预期的那样。有什么建议吗

//@version=4
strategy(title = "Max since last buy/sell", 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, commission_type = strategy.commission.percent, commission_value = 0.075, process_orders_on_close = true, initial_capital = 100)

// State
hasOpenTrade() => strategy.opentrades != 0
notHasOpenTrade() => strategy.opentrades == 0
isLong() => strategy.position_size > 0 ? true : false
isShort() => strategy.position_size < 0 ? true : false

// Variables
var entryPrice = close
var candle = 0

// Count Function
if candle >= 1
    candle := candle + 1

// Main Function
maxSinceLastBuySell = hasOpenTrade() ? high > highest(candle) ? high : highest(candle) : na

// Execution
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    entryPrice := close
    candle := 1
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    entryPrice := close
    candle := 1
    strategy.entry("My Short Entry Id", strategy.short)

// Debug
plot(entryPrice, color = color.green)
plot(candle, color = color.black)
plot(maxSinceLastBuySell, color = color.red)
/@version=4
策略(title=“自上次买入/卖出以来的最大值”,叠加=true,聚合=0,按订单计算填充=false,每勾选一次计算=false,默认数量类型=策略。股权百分比,默认数量值=98,佣金类型=策略。佣金百分比,佣金值=0.075,处理订单关闭=true,初始资本=100)
//陈述
hasOpenTrade()=>strategy.opentrades!=0
notHasOpenTrade()=>strategy.opentrades==0
isLong()=>strategy.position\u size>0?对:错
isShort()=>strategy.position\u大小<0?对:错
//变数
var entryPrice=close
var=0
//计数函数
如果烛光>=1
蜡烛:=蜡烛+1
//主要功能
maxSinceLastBuySell=hasOpenTrade()?高>最高(烛光)?高:最高(烛光):na
//执行
长条件=交叉(sma(闭合,14),sma(闭合,28))
如果(长期条件)
入口价格:=关闭
蜡烛:=1
strategy.entry(“我的长输入Id”,strategy.Long)
短条件=交叉(sma(闭合,14),sma(闭合,28))
if(短条件)
入口价格:=关闭
蜡烛:=1
strategy.entry(“我的短输入Id”,strategy.Short)
//调试
绘图(入口价格,颜色=颜色。绿色)
绘图(蜡烛,颜色=颜色。黑色)
绘图(MaxSincellastBuySell,颜色=颜色.red)

只是保留了所需的代码,以便更容易看到增量。区别在于:

  • 使用
    var
    声明变量,以便在整个条形图中保留状态
  • 在交易开始时重置var

你知道你是一个绝对的英雄。在过去的两天里,我尝试了一些实现,但没有一个能像您的解决方案那样优雅。谢谢你,LucF!很乐意帮忙。德国劳埃德船级社!
//@version=4
strategy(title = "Max since last buy/sell", 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, commission_type = strategy.commission.percent, commission_value = 0.075, process_orders_on_close = true, initial_capital = 100)

// State
hasOpenTrade() => strategy.opentrades != 0

// Variables
var maxSinceLastBuySell = 0.

// Execution
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    maxSinceLastBuySell := high
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    maxSinceLastBuySell := high
    strategy.entry("My Short Entry Id", strategy.short)

// Trade is open: check for higher high.
if hasOpenTrade()
    maxSinceLastBuySell := max(maxSinceLastBuySell, high)

// Debug
plot(maxSinceLastBuySell, color = color.red)