0

我正在对交易视图上的交易策略进行回测,并正在使用 pine 编辑器对其进行编程(在示例和 youtube 视频的帮助下)。到目前为止,我已经制作了几个在正确进入和退出等方面起作用的。每次我按顺序得到一个时,我都会尝试让它变得更复杂一些。

我正在处理的最后一个似乎没有做任何事情。“策略测试器”屏幕中根本没有交易。我一遍又一遍地浏览了代码,但我找不到原因。

这是我觉得奇怪的地方:

  1. 我有一些关于“交易止损价格”和“交易目标价格”的图,每次我有一个正确的入场设置时,这些图。所以它确实识别条目。
  2. 我没有更改 strategy.entry 或 strategy.exit 部分,我从以前的策略中接管了它确实有效

希望有人可以帮助我?先感谢您!

(另外,如果你看到任何让你“哇,那是愚蠢的”的东西,请随时发表评论)

科罗夫

// Previously 3 or more strong HA candles in same direction
// Closed last HA with body between 40 and 60 percent of wick


//@version=4
strategy("Heikin Ashi Indecisive after strong move", overlay = true, initial_capital = 1000, default_qty_value = 2, default_qty_type = strategy.percent_of_equity)

// input from user
stopMultiplier = input(title="Stop Loss ATR", type=input.float, defval=1.0, tooltip="Stop loss multiplier (x ATR)")
rr = input(title="Risk:Reward", type=input.float, defval=1.0, step=0.5, tooltip="Risk:Reward profile")

// defined variables
start = timestamp(2021, 10, 28, 0, 0)
end = timestamp(2021, 11, 28, 0, 0)
symbol = syminfo.tickerid
period = "5"
lookback = 2

// calculate atr
atr = atr(14)

// get Heikin Ashi values
heikinashiOpen = security(heikinashi(symbol), period, open)
heikinashiClose = security(heikinashi(symbol), period, close)
heikinashiHigh = security(heikinashi(symbol), period, high)
heikinashiLow = security(heikinashi(symbol), period, low)

// define a green (rising) and red (falling) Heikin Ashi candle
rising = heikinashiClose > heikinashiOpen
falling = heikinashiClose < heikinashiOpen

// calculate candle- wick- and bodysizes
candleSize = heikinashiHigh - heikinashiLow
bodySize = heikinashiOpen > heikinashiClose ? heikinashiOpen - heikinashiClose : heikinashiClose - heikinashiOpen
upperWick = heikinashiOpen > heikinashiClose ? heikinashiHigh - heikinashiOpen : heikinashiHigh - heikinashiClose
lowerWick = heikinashiOpen > heikinashiClose ? heikinashiClose - heikinashiLow : heikinashiOpen - heikinashiLow

// define a Heikin Ashi with hard push in one direction
hardRise = rising and (heikinashiLow == heikinashiOpen)
hardFall = falling and (heikinashiHigh == heikinashiOpen)

// define HA end of move candle
indecisiveCandle = upperWick > candleSize * 0.35 and lowerWick > candleSize * 0.35

// determine if we have a valid setup
switchLongComing = (indecisiveCandle or rising) and hardFall[1] and hardFall[2] and hardFall[3] and not na(atr)
switchShortComing = (indecisiveCandle or falling) and hardRise[1] and hardRise[2] and hardRise[3] and not na(atr)

// check if we have confirmation for a trade
validLong = switchLongComing and strategy.position_size == 0 and barstate.isconfirmed
validShort = switchShortComing and strategy.position_size == 0 and barstate.isconfirmed

// calculate stops and targets
stopSize = atr * stopMultiplier
lowestLow = lowest(low, lookback)
longStopPrice = lowestLow - stopSize
longStopDistance = close - longStopPrice
longTargetPrice = close + (longStopDistance * rr)
highestHigh = highest(high, lookback)
shortStopPrice = highestHigh + stopSize
shortStopDistance = shortStopPrice - close
shortTargetPrice = close - (longStopDistance * rr)

// save stops and targets for trade
var tradeStopPrice = 0.0
var tradeTargetPrice = 0.0
if validLong
    tradeStopPrice := longStopPrice
    tradeTargetPrice := longTargetPrice
if validShort
    tradeStopPrice := shortStopPrice
    tradeTargetPrice := shortTargetPrice

// enter trades when a valid entry is found
if time >= start and time <= end
    strategy.entry(id="Long", long=strategy.long, when=validLong)
    strategy.entry(id="Short", long=strategy.short, when=validShort)

// exit trades whenever a stop or target is hit
strategy.exit("Long Exit", from_entry = "Long", limit = tradeTargetPrice, stop = tradeStopPrice, when = strategy.position_size > 0)
strategy.exit("Short Exit", from_entry = "Short", limit = tradeTargetPrice, stop = tradeStopPrice, when = strategy.position_size < 0)

// graphical information
plot(strategy.position_size != 0 or validLong or validShort ? tradeStopPrice : na, title = "Trade Stop Price", color = color.red, style = plot.style_linebr)
plot(strategy.position_size != 0 or validLong or validShort ? tradeTargetPrice : na, title = "Trade Target Price", color = color.green, style = plot.style_linebr)
inLongTrade = strategy.position_size > 0
inShortTrade = strategy.position_size < 0
notInTrade = strategy.position_size == 0
bgcolor(notInTrade == false ? inLongTrade ? color.green : color.red : color.white)

编辑:经过更多测试后,我发现不开仓的问题发生在某些图表上,而在其他图表上则没有。例如,该脚本将进行外汇对交易,但不适用于大多数期货或股票。还是不知道为什么...

4

1 回答 1

0

尝试:

if validLong
    tradeStopPrice := longStopPrice
    tradeTargetPrice := longTargetPrice
    strategy.entry(id="Long", long=strategy.long)


if validShort
    tradeStopPrice := shortStopPrice
    tradeTargetPrice := shortTargetPrice
    strategy.entry(id="Short", short=strategy.short)

嗯...我看到您在输入逻辑的一部分中错过了将“多头”更改为“空头”;“长=策略.短”

if time >= start and time <= end
    strategy.entry(id="Long", long=strategy.long, when=validLong)
    strategy.entry(id="Short", long=strategy.short, when=validShort)

让我知道这是否有帮助。我只是快速浏览了您的代码。

于 2021-12-02T01:46:14.683 回答