自上次买入/卖出以来的最高价格

Max Price since last Buy / Sell

目标是存储和绘制自上次买入/卖出以来的最高价格。

以下方法将当前高点与自上次多头/空头以来过去蜡烛的历史高点进行比较。如果当前高点更大,它将成为历史高点。

该图确实以某种方式进行了计算,但它似乎不正确或不符合预期。有什么建议吗?

//@version=4
strategy(title="Max since last buy/sell", overlay=true, process_orders_on_close=true)


// —————————— STATES

hasOpenTrade = strategy.opentrades != 0
notHasOpenTrade = strategy.opentrades == 0
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0


// —————————— VARIABLES

var entryPrice = close


// —————————— MAIN

maxSinceLastBuySell = hasOpenTrade ? high > highest(bar_index) ? high : highest(bar_index) : na


// —————————— EXECUTIONS

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)

只保留了所需的代码,以便更容易查看增量。差异是:

  • 使用 var 声明变量,以便在整个柱中保留状态。
  • 交易开始时重置 var。
//@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)