You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在入场交易前将止损设置在前一根K线低点(静态止损)

Pine Script 静态止损设置问题

我没法在入场交易前把止损设置在前一根K线的低点,明明这应该是个简单操作却一直出问题。我的具体需求是:以入场前的前一根K线低点作为止损价,且该止损价保持静态直至被触发。我尝试了多种方法都没能成功,相关代码如下:

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

//@version=5
strategy(title='First Try', initial_capital=1000, overlay=true, process_orders_on_close = true)

// Set days variant
days = 24

// picks the lowest value of the previous 24 candles (the number of 24 candles was determined in line 8)
lower = ta.lowest(low[0], days)

// picks the highest value of the previous 20 candles (the number of 24 candles was determined in line 8)
upper = ta.highest(high[0], days)

// calculates the average of lower and highest candles (determined in lines 11 & 14) 
basis = math.avg(upper, lower)

// Drwas a line in order to present the avewrage
plot(basis, 'Basis', color=color.new(#FF6D00, 0))

// Draws a line in order to present the highest value of the previous 20 candles
u = plot(upper, 'Upper', color=color.new(#2962FF, 0))

// Draws a line in order to present the lowesr value of the previous 20 candles
l = plot(lower, 'Lower', color=color.new(#2962FF, 0))

//Colours the area betwen highest line and lowest line
fill(u, l, color=color.rgb(33, 150, 243, 95), title='Background')


// calculates the moving average
MA = ta.sma(close, 240)

// Drwas the moving average
plot(MA, color=color.new(color.black, 0), linewidth=3)

a = close[0]
b = close[1]

buycondition1 = MA < close[0]
buycondition2 = a > b
buycondition3 = low[1] == lower

goLong = buycondition3 and buycondition1 and buycondition2

if goLong
    strategy.entry(id='Go Long', direction=strategy.long)


//TP = strategy.position_avg_price + (strategy.position_avg_price - lower)*1.5

// Stop loss method 1 (not working)
//plot(ta.valuewhen(goLong,low,1), color=color.fuchsia, style=plot.style_circles,linewidth=3, title="Last entry price")
//SL = ta.valuewhen(goLong,low,1)

// Stop loss method 2 (not working)
//plot(ta.valuewhen(strategy.position_size,low,1), color=color.fuchsia, style=plot.style_circles,linewidth=3, title="Last entry price")
//SL = ta.valuewhen(strategy.position_size,low,1)

// Stop loss method 3 (not working)
//plot(strategy.position_size[1]-strategy.position_size[2], color=color.fuchsia, style=plot.style_circles,linewidth=3, title="Last entry price")
//SL = strategy.position_size[1]-strategy.position_size[2]

// Stop loss method 4 (not working)
//plot(ta.valuewhen(strategy.position_size[1]-strategy.position_size[2],low,1), color=color.fuchsia, style=plot.style_circles,linewidth=3, title="Last entry price")
//SL = ta.valuewhen(strategy.position_size[1]-strategy.position_size[2],low,1)

//strategy.exit("Stop loss Long", from_entry="Go Long", stop=SL)



sellcondition1 = MA > close[0]
sellcondition2 = a < b
sellcondition3 = high[1] == upper

goShort = sellcondition1 and sellcondition2 and sellcondition3

if goShort
    strategy.entry(id='Go Short', direction=strategy.short)

内容的提问来源于stack exchange,提问作者Mario Gigli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 11:40:24