【问题标题】:Max Price since last Buy / Sell自上次买入/卖出以来的最高价格
【发布时间】:2019-12-19 07:49:39
【问题描述】:

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

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

情节确实计算了,但它似乎不正确或不符合预期。有什么建议吗?

//@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)

【问题讨论】:

    标签: pine-script


    【解决方案1】:

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

    • 使用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)
    

    【讨论】:

    • 你知道你是一个绝对的英雄。在过去的两天里,我尝试了一些实现,但没有一个像您的解决方案那样优雅。谢谢LucF!
    • 乐于助人。 gl!
    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多