我一直在用 pinescript 编写我的策略,除了我无法解决的一件事之外,一切都很好。
有没有办法告诉策略在最后一次入场是空头时只做多,反之亦然?
编辑:抱歉之前没有在下面添加正确的代码,但这是因为我从来没有看到它不允许我保存它,因为我在帖子中没有足够的信息。
我当前的代码是:
//@version=4
strategy("My Script", overlay = true)
ma = ema(close, 200)
// Base condition
aboveMa = close > ma
belowMa = close < ma
//long conditions
threeAboveMa = aboveMa[1] and aboveMa[2] and aboveMa[3]
first3AboveMa = not aboveMa[4] and threeAboveMa
//short conditions
threeBelowMa = belowMa[1] and belowMa[2] and belowMa[3]
first3BelowMa = not belowMa[4] and threeBelowMa
// Debugging
plot(ma)
plotchar(aboveMa, "aboveMa", ".", location.top)
plotchar(threeAboveMa, "threeAboveMa", "•", location.top)
plotchar(first3AboveMa, "first3AboveMa", "▲", location.top)
// Debugging
plot(ma)
plotchar(belowMa, "aboveMa", ".", location.bottom)
plotchar(threeBelowMa, "threeAboveMa", "•", location.bottom)
plotchar(first3BelowMa, "first3AboveMa", "▲", location.bottom)
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(8.0, title='Stop Loss %', type=input.float) / 100
takePer = input(8.0, title='Take Profit %', type=input.float) / 100
// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)
//TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - trailStop)
max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + trailStop)
min(stopValue, shortStopPrice[1])
else
999999
//Long Entry
entrylong() => threeAboveMa
exitlong() => first3AboveMa
//Short Entry
entryshort() => threeBelowMa
exitshort() => first3AboveMa
if (threeAboveMa)
strategy.entry(id = "long_ma", long = true, when = entrylong())
if (threeBelowMa)
strategy.entry(id = "short_ma", long = false, when = entryshort())
if (threeAboveMa)
strategy.exit(id = "exit_long", stop=longStop, limit=longTake)
if (threeBelowMa)
strategy.exit(id = "exit_short", stop=shortStop, limit=shortTake)
//strategy.entry(id = "long_ma", long = true, when = entry())
//strategy.close(id = "long_ma", when = exit())
你可以实现这个
这是经过这些修改的脚本:
//@version=4
strategy("My Script", overlay = true)
ma = ema(close, 200)
// Base condition
aboveMa = close > ma
belowMa = close < ma
//long conditions
threeAboveMa = aboveMa[1] and aboveMa[2] and aboveMa[3]
first3AboveMa = not aboveMa[4] and threeAboveMa
//short conditions
threeBelowMa = belowMa[1] and belowMa[2] and belowMa[3]
first3BelowMa = not belowMa[4] and threeBelowMa
// Debugging
plot(ma)
plotchar(aboveMa, "aboveMa", ".", location.top)
plotchar(threeAboveMa, "threeAboveMa", "•", location.top)
plotchar(first3AboveMa, "first3AboveMa", "▲", location.top)
plotchar(belowMa, "belowMa", ".", location.bottom)
plotchar(threeBelowMa, "threeBelowMa", "•", location.bottom)
plotchar(first3BelowMa, "first3BelowMa", "▲", location.bottom)
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(8.0, title='Stop Loss %', type=input.float) / 100
takePer = input(8.0, title='Take Profit %', type=input.float) / 100
// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)
// TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - trailStop)
max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + trailStop)
min(stopValue, shortStopPrice[1])
else
999999
// Create a variable to track the last trade direction: 1 for long, -1 for short, 0 for no trade
var lastTrade = 0 // 0 = none, 1 = long, -1 = short
// Long Entry
entrylong() => threeAboveMa and lastTrade == -1 // Only enter long if last trade was a short
exitlong() => first3AboveMa
// Short Entry
entryshort() => threeBelowMa and lastTrade == 1 // Only enter short if last trade was a long
exitshort() => first3BelowMa
if (threeAboveMa and lastTrade == -1)
strategy.entry(id = "long_ma", long = true, when = entrylong())
lastTrade := 1 // Update last trade to long
if (threeBelowMa and lastTrade == 1)
strategy.entry(id = "short_ma", long = false, when = entryshort())
lastTrade := -1 // Update last trade to short
// Exits for long and short
if (strategy.position_size > 0)
strategy.exit(id = "exit_long", stop=longStop, limit=longTake)
if (strategy.position_size < 0)
strategy.exit(id = "exit_short", stop=shortStop, limit=shortTake)
var lastTrade = 0:跟踪最后交易类型的变量:
1:上次交易很长。 -1:上次交易是空头。 0:还没有交易。 Entrylong() 和 Entryshort():仅当最后一笔交易为空头 (lastTrade == -1) 时才允许多头入场,仅当最后一笔交易为多头 (lastTrade == 1) 时才允许空头入场。
更新lastTrade:每次入场(多头或空头)后,lastTrade 变量都会更新以反映新交易的方向。