我有一个 MACD 策略代码,它只持有多头头寸(基于 Kodify.net tutorial)。 当 MACD 线穿过信号线时,进入条件发生。 当MACD线在信号线下方交叉时出现退出条件。
//@version=4
strategy(title="MACD example strategy", overlay=false, max_bars_back=2000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000)
// Create inputs
fastLen = input(title="Fast Length", type=input.integer, defval=12)
slowLen = input(title="Slow Length", type=input.integer, defval=26)
sigLen = input(title="Signal Length", type=input.integer, defval=9)
// Get MACD values
[macdLine, signalLine, _] = macd(close, fastLen, slowLen, sigLen)
// Plot MACD values and line
plot(series=macdLine, color=#6495ED, linewidth=2)
plot(series=signalLine, color=color.orange, linewidth=2)
hline(price=0)
// Determine long and short conditions
entryCondition = crossover(macdLine, signalLine)
exitCondition = crossunder(macdLine, signalLine)
// Submit orders
strategy.entry(id="Long Entry", long=true, when=entryCondition)
strategy.close("Long Entry", when=exitCondition)
该策略运行良好,但我想在入场价的基础上增加 5% 的止损。
由于将 strategy.close("Long Entry", when=exitCondition)
行替换为 strategy.exit("Long Exit", from_entry="Long Entry", stop=strategy.position_avg_price * 0.95, when=exitCondition)
行不起作用,因此如何完成此操作?
答案 0 :(得分:0)
试试这个:
...
// Submit orders
strategy.entry(id="Long Entry", long=true, when=entryCondition)
strategy.exit("Long Exit", from_entry="Long Entry", stop=strategy.position_avg_price * 0.95)
strategy.close("Long Entry", when=exitCondition)