pinescript
//@version=4
strategy("双指标策略示例", overlay=true)
// 定义第一个指标(例如,5日均线)
ma5Period = input(5, title="5日均线周期")
ma5 = sma(close, ma5Period)
// 定义第二个指标(例如,30日均线)
ma30Period = input(30, title="30日均线周期")
ma30 = sma(close, ma30Period)
// 定义第三个指标(例如,MACD)
[macdLine, signalLine, _] = macd(close, 12, 26, 9)
macdCross = macdLine > signalLine ? 1 : macdLine < signalLine ? -1 : 0
// 设置进入策略的条件
enterConditionLong = crossover(ma5, ma30) and macdCross == 1
enterConditionShort = crossunder(ma5, ma30) and macdCross == -1
// 设置离开策略的条件
exitConditionLong = crossunder(ma5, ma30) and macdCross == -1
exitConditionShort = crossover(ma5, ma30) and macdCross == 1
// 进入多头仓位
strategy.entry("Enter Long", strategy.long, when=enterConditionLong)
// 离开多头仓位
strategy.close("Enter Long", when=exitConditionLong)
// 进入空头仓位
strategy.entry("Enter Short", strategy.short, when=enterConditionShort)
// 离开空头仓位
strategy.close("Enter Short", when=exitConditionShort)
在这个策略中,我们使用了一个5日均线和一个30日均线作为价格指标,以及 MACD 指标。当5日均线上穿30日均线且 MACD 出现金叉时,我们进入多头仓位;当5日均线下穿30日均线且 MACD 出现死叉时,我们进入空头仓位。