Tho*_*mas 6 trading algorithmic-trading forex back-testing pine-script
感谢大家迄今为止的帮助。到目前为止,我已经在 pinescript 中编写了许多不同的策略,并且我已经阅读了 pinescript 手册和许多谷歌文章,但我仍然对追踪止损在 pinescript 中如何工作感到困惑。
例如,对于strategy.exit,我有一个trail_price,它标记要激活的追踪止损条目。然而,我所有的回溯测试都表明追踪止损位于该特定蜡烛线的最高点,即使 Trail_offset 尚未被触及。仅仅是因为交易视图回溯测试假设在一根蜡烛线中达到了最大利润,即使后续蜡烛线继续朝着您的目标方向发展?
例如,这是我的strategy.exit 的示例。Strategy.exit("long_TP", "long", Trail_price = 入场价格 + ATR, Trail_offset = ATR, stop= 入场价格 - ATR). 我注意到,只要在该特定交易收盘前获利,我将赚取 2 倍到 3 倍的 Trail_offset(在本例中基于 ATR,即,如果 ATR 为 50 点,我将赚取 100 甚至 150 点)。蜡烛吧。任何后续的蜡烛线,即使做多,即使没有达到 Trail_offset 止损,也不会被纳入计算(即,即使我的 ATR 是 50 点,当蜡烛线收盘时我可能会赚取 70 点,即使随后的蜡烛继续做多)。
我的假设是否不正确(即我的代码),或者这只是回溯测试的限制,因为程序无法知道蜡烛条内部发生了什么,只能知道最高价、最低价、开盘价和收盘价?然而,我确实对此感到好奇,因为有时即使蜡烛线处于最低点,也没有达到 Trail_offset,因此理论上利润应该继续累积,而不是在蜡烛线收盘后止损。
编辑:我添加了一些更多信息以进行澄清 - 这是一个带有一些解释的示例代码:
If condition == true
long = strategy.position_size[0] > strategy.position_size[1] //go long if there is order entry
entry_price_long = valuewhen(long, open, 0) //entry price is the opening price, based off the closing price of the previous candle if condition is fulfilled
atr_long = valuewhen(long, atr, 0) //stop loss and 1st take profit based off the number of pips depending on average true range, period 14
long_TP = entry_price_long + atr_long //1st take profit limit
long_SL = entry_price_long - atr_long //stop loss
strategy.entry("long", strategy.long, when=go_long, comment="long", qty=positionSize) //enter a long position when condition fulfilled
if strategy.position_size[0] > strategy.position_size[1]
strategy.exit("long_TP", "long", trail_price=long_TP, trail_offset=atr_long, stop=long_SL) //this is where I am confused.
Run Code Online (Sandbox Code Playgroud)
我的strategy.exit指出,如果达到初始止损,则退出多头头寸。但如果市场按照预期走多,则当触及第一个止盈限额(由 Trail_price 定义)时,追踪止损就会被激活。Trail_offset(以点数为单位)基于 ATR。因此,如果触及 Trail_price,则应通过追踪止损持续获利。但实际情况是,利润会被提取到我进入交易的特定蜡烛的最高点。我附上了一张图片供参考。
在图中,我们看到已达到第一个利润限制,因此跟踪被激活。ATR 约为 150 点,因此入场价与止盈位之间的距离约为 150 点。追踪止损设置为ATR,因此一旦达到第一个利润限制(利润=150点),理论上交易应该继续获利,直到触及追踪止损。但在图中我们看到,实际上,一旦触及蜡烛的高点,我的头寸就退出了,尽管持续上涨趋势,但没有获得任何进一步的利润(最终利润= 181 点)。为什么是这样?
再次感谢您的帮助。托马斯
我不确定你是否有像我一样的问题,但就我而言,我忽略了trail_offset必须在刻度中指定!我的示例当然不是示例代码,但它说明了解决方案。关键点:
syminfo.mintick(这似乎是 v5 中的刻度大小)并使用它来计算tickFactor.trailOffset为在查看图表时具有直观意义的值,在本例中为 2 日元(使用 USDJPY 作为符号)。trail_offset trailOffset * tickFactor<<===这个!这是松树脚本:
//@version=5
// Try it on USDJPY
strategy("Trailing Stop Example", overlay=true)
varip float calculatedTrailingStop = na
tickSize = syminfo.mintick
tickFactor = 1/tickSize
if barstate.islastconfirmedhistory
label.new(bar_index, high, "Tick Size (mintick): " + str.tostring(tickSize), yloc = yloc.abovebar, textcolor = color.white)
entryPrice = 140
trailPrice = entryPrice * 1.01
trailOffset = 2 // 2 Yen trailing stop
// Simulate a long entry order
if bar_index == last_bar_index - 80
strategy.entry("Long", strategy.long)
// Simulate a trailing stop order
strategy.exit("Trailing Stop", "Long", trail_price = trailPrice, trail_offset = trailOffset*tickFactor)
if strategy.position_size > 0
calculatedTrailingStop := math.max(high - trailOffset, na(calculatedTrailingStop) ? 0 : calculatedTrailingStop)
else
calculatedTrailingStop := na
// Plot the trailing stop activation level
plot(strategy.position_size > 0 ? trailPrice : na, color = color.green, style = plot.style_line)
// Plot the trailing stop price
plot(calculatedTrailingStop, color = color.red, style = plot.style_line)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6149 次 |
| 最近记录: |