0

I am trying to write percentage trailing stop. The code is designed to perform highest high over the previous number of bars since entry.

longStopPrice = 0.0, shortStopPrice = 0.0, lengthinmp = 0, highesthigh = 0.0, stopValue = 0.0

longStopPrice := if (strategy.position_size > 0) lengthinmp := barssince(strategy.position_size == 0)
highesthigh = highest(high, barssince(strategy.position_size == 0)) // had used lengthinmp in an earlier version on this line stopValue = highesthigh * (1 - StopPerc) // max(stopValue, longStopPrice[1]) else 0

The error I get is line 49: Cannot call 'highest' with arguments (series[float], series[integer]); available overloads: highest(series[float], integer) => series[float]; highest(integer) => series[float]

My understanding is that when working it would not included the current bar. Does anyone know how to include the current bar? Thanks

4

1 回答 1

1

以下是如何跟踪最高价和最低价的示例:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=4
strategy("My Strategy", overlay=true)

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

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

var float hh = na
hh := strategy.position_size > 0 ? max(nz(hh), high) : na
plot(hh, style = plot.style_linebr, color = color.blue)

var float ll = na
ll := strategy.position_size < 0 ? min(na(ll) ? low : ll, low) : na
plot(ll, style = plot.style_linebr, color = color.red)
于 2020-04-02T16:48:08.537 回答