[英]Strategy.long after second crossover, Pine script
當滿足以下條件時,我正在嘗試在交易視圖中進行回測,
1) 多頭 = RSI > 50 且 MACD > SIGNAL(簡稱反之)
2)當兩者都滿足時,進入 OPEN(0) > HIGH(滿足上述條件時的信號條)
達到盈利目標時退出。
3)和 go 很長只是再次發生重新交叉......不是立即我無法在 pine 腳本中執行並需要幫助
以下是相同的代碼:
//@version=4
strategy("Test1 script",overlay=false)
var a = 0
var hiBar = 0
var loBar = 0
//MACD
fast = 12, slow = 26
fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, 9)
//RSI
rsiSource = close //input(title="RSI Source", type=input.source, defval=close)
rsiLength = 7 //input(title="RSI Length", type=input.integer, defval=14)
rsiOverbought = 70 //nput(title="RSI Overbought Level", type=input.integer, defval=80)
rsiOversold = 30 //input(title="RSI Oversold Level", type=input.integer, defval=20)
rsiValue = rsi(rsiSource, rsiLength)
isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold
uptrend1 = rsiValue > 50 and macd > signal
downtrend1 = rsiValue < 50 and macd < signal
if uptrend1 or downtrend1
a := 10
else
a := 20
if uptrend1
hiBar := bar_index
if downtrend1
loBar := bar_index
barcolor(uptrend1 ? color.purple:downtrend1 ? color.yellow: a == 20 ? color.gray: na)
if uptrend1
strategy.entry("Long", strategy.long,when = close[0] > high[hiBar-1] or open[0] > high[hiBar-1])
strategy.exit("exit long","Long", profit = 2400, loss = 1200)
if downtrend1
strategy.entry("Short", strategy.short)
strategy.exit("exit short", "Short", profit = 2800, loss = 1200)
還附上了回測的圖片
這應該實現該邏輯。 您的目標和利潤數字不是以刻度表示的,您現在可以通過Inputs更改它們。 值大於您的值以防止過早退出:
//@version=4
strategy("Test1 script",overlay=true)
targetProfit = input(1000., "Profit target (in currency)") / syminfo.mintick
targetLoss = input(500., "Profit loss (in currency)") / syminfo.mintick
var a = 0
var float hiBar = na
var float loBar = na
var float referenceHi = na
// This is true when a long was exited and we're waiting for downtrend condition to occur.
var waitingForLongReset = false
//MACD
fast = 12, slow = 26
fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, 9)
//RSI
rsiSource = close //input(title="RSI Source", type=input.source, defval=close)
rsiLength = 7 //input(title="RSI Length", type=input.integer, defval=14)
rsiOverbought = 70 //nput(title="RSI Overbought Level", type=input.integer, defval=80)
rsiOversold = 30 //input(title="RSI Oversold Level", type=input.integer, defval=20)
rsiValue = rsi(rsiSource, rsiLength)
isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold
uptrend1 = rsiValue > 50 and macd > signal
downtrend1 = rsiValue < 50 and macd < signal
enterUpTrend = not uptrend1[1] and uptrend1
enterDnTrend = not downtrend1[1] and downtrend1
// Only save bar on transition into trend.
if enterUpTrend and not waitingForLongReset
hiBar := bar_index
referenceHi := high[max(0, bar_index - hiBar)]
if uptrend1 and max(close, open) > referenceHi and not waitingForLongReset
waitingForLongReset := true
strategy.entry("Long", strategy.long)
strategy.exit("exit long","Long", profit = targetProfit, loss = targetLoss)
if strategy.position_size[1] > 0 and strategy.position_size == 0
// Long was exited, wait for reset before next long.
waitingForLongReset := true
if enterDnTrend
loBar := bar_index
// Allow another long condition to become true.
waitingForLongReset := false
barcolor(uptrend1 ? color.purple:downtrend1 ? color.yellow: a == 20 ? color.gray: color.blue)
// Debugging plots.
plotchar(uptrend1, "uptrend1", "▲", location.top)
plotchar(downtrend1, "downtrend1", "▼", location.bottom)
plotchar(waitingForLongReset, "waitingForLongReset", "—", location.bottom)
plotchar(max(close, open) > referenceHi, "max(close, open) > referenceHi", "—", location.top)
plot(referenceHi, "referenceHi", color.green, 2, plot.style_circles)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.