//@version=5
indicator("买入下跌策略 (任意币种)", overlay=true)
// === 输入 ===
stochKLen = input.int(14, "随机指标 %K 长度")
stochDLen = input.int(3, "随机指标 %D 长度")
stochSmooth = input.int(3, "随机指标平滑")
buyZone = input.float(0.98, "买入区域 % (例如 0.98 = 低于 2%)", step=0.01)
tpMultiplier = input.float(1.05, "获利了结 % (例如 1.05 = 高于 5%)", step=0.01)
slMultiplier = input.float(0.97, "止损 % (例如 0.97 = 低于 3%)", step=0.01)
// === 随机振荡器 ===
k = ta.sma(ta.stoch(close, high, low, stochKLen), stochSmooth)
d = ta.sma(k, stochDLen)
// === 动态水平 ===
var float entryPrice = na
var bool inTrade = false
// === 买入条件 ===
buyCondition = ta.crossover(k, d) and k < 80
if (buyCondition and not inTrade)
entryPrice := close
inTrade := true
// === 获利和止损水平 ===
takeProfitPrice = entryPrice * tpMultiplier
stopLossPrice = entryPrice * slMultiplier
// === 退出条件 ===
exitConditionTP = inTrade and close >= takeProfitPrice
exitConditionSL = inTrade and close <= stopLossPrice
if (exitConditionTP or exitConditionSL)
inTrade := false
entryPrice := na
// === 绘图 ===
plotshape(buyCondition and not inTrade, title="买入信号", location=location.belowbar, color=color.green, style=shape.labelup, text="买入")
plotshape(exitConditionTP, title="获利了结", location=location.abovebar, color=color.red, style=shape.labeldown, text="TP")
plotshape(exitConditionSL, title="止损", location=location.abovebar, color=color.orange, style=shape.labeldown, text="SL")
plot(entryPrice, title="进场价格", color=color.new(color.green, 60))
plot(inTrade ? takeProfitPrice : na, title="获利了结水平", color=color.new(color.red, 60), style=plot.style_line)
plot(inTrade ? stopLossPrice : na, title="止损水平", color=color.new(color.orange, 60), style=plot.style_line)
#pinescript #Write2Earn