为什么我的策略不进仓?

问题描述 投票:0回答:1

为什么我的策略不进仓?

你好,我在制定我的第一个策略时遇到了问题。该策略不会进入任何仓位。

我已经检查了我的语法几十次,甚至将它发送到 ChatGPT 寻求任何建议,但无济于事。我还绘制了所有变量,以便我可以在数据窗口中看到它们以进行调试。正在满足入仓条件 (haTrendStartUp == true),但没有入仓。帮助??提前谢谢你!

//@author Yocracra
//
//@version=5

strategy('No. 1', shorttitle='No. 1', overlay=true, scale=scale.left)


// GUI user inputs
thresholdSignificance = input(.25, title='Heikin-Ashi Strength Threshold', tooltip = 'Threshold for what tail length is considered “significant”')
macdFastPeriod = input.int(12, 'MACD Fast Length', minval=1, tooltip = 'Period for the Fast EMA')
macdSlowPeriod = input.int(26, 'MACD Slow Length', minval=1, tooltip = 'Period for the Slow EMA')
macdSignalPeriod = input.int(9, 'MACD Signal Length', minval=1, tooltip = 'Period for the MACD Signal Line')
macdOVThreshold = input(0.25, 'MACD Overvalue Threshold', tooltip = 'When the price exceeds the real price by this much, the stock is considered overvalued.')

// Calculate and define the Heikin-Ashi candlestick components
haOpen = (open[1] + close[1]) / 2  // Calculate Heikin-Ashi Open (HAOpen)
haClose = (open + high + low + close) / 4  // Calculate Heikin-Ashi Close (HAClose)
haHigh = math.max(high, haOpen, haClose)  // Calculate Heikin-Ashi High (HAHigh)
haLow = math.min(low, haOpen, haClose)  // Calculate Heikin-Ashi Low (HALow)

// Determine if the candlestick is bullish or bearish.
haBullish = haClose > haOpen  // A variable in PineScript is, by default, a boolean.

// Calculate and define upper and lower Shadow length
haUpperSL = haBullish ? haHigh - haClose : haHigh - haOpen
haLowerSL = haBullish ? haOpen - haLow : haClose - haLow

// Checks if the Heikin-Ashi candles indicate a significant trend
haUpperSLSignificant = haUpperSL > thresholdSignificance
haLowerSLSignificant = haLowerSL > thresholdSignificance
haUpperZero = haUpperSL == 0 ? true : false
haLowerZero = haLowerSL == 0 ? true : false
haTrendContinueUp = haUpperSLSignificant and not haLowerSLSignificant
haTrendContinueDown = haLowerSLSignificant and not haUpperSLSignificant
haTrendUp = haUpperSLSignificant and haLowerZero ? true : false
haTrendDown = haLowerSLSignificant and haUpperZero ? true : false
haTrendStartUp = haTrendUp and haTrendUp[1] and not haTrendUp[2] ? true : false
haTrendStartDown = haTrendDown and haTrendDown[1] and not haTrendDown[2] ? true : false

// Define and calculate the MACD
[macdLine, macdSignal, macdHisto] = ta.macd(close, macdFastPeriod, macdSlowPeriod, macdSignalPeriod)  // ta.macd returns a three-value tuple

// Checks if the market is undervalued or overvalued according to the MACD
macdOvervalued = macdHisto > macdOVThreshold
macdUndervalued = macdHisto < macdOVThreshold



// Displays
plot(0, 'Zero', color.new(color.white, 0), 2, plot.style_line)  // Just plots a line on y=0
plot(macdLine, 'MACD', color.new(color.orange, 0), 1, plot.style_line)  // Plots the MACD Line
plot(macdSignal, 'Signal', color.new(color.blue, 0), 1, plot.style_line)  // Plots the MACD Signal Line
plot(macdHisto, 'Histo', color.new(color.gray, 50), 2, plot.style_columns)  // Plots the MACD Histogram
plot(macdOVThreshold, 'MACD Overvalue Threshold', color.red, 1, plot.style_line)  // Plots MACD Overvalue Threshold
plot(macdOVThreshold * -1, 'MACD Undervalue Threshold', color.green, 1, plot.style_line)  // Plots Undervalue Threshold

test = false

//Strategy entries
if haTrendStartUp == true 
    strategy.entry("Long", strategy.long, qty = 1)
    test := true
else
    test := false


plotshape(haTrendStartUp ? true : na, style=shape.circle, location=location.absolute, color=color.green, size=size.small)
plotshape(haTrendStartDown ? true : na, style=shape.circle, location=location.absolute, color=color.red, size=size.small)

plotchar(thresholdSignificance,'thresholdSignificance','')
plotchar(macdFastPeriod,'macdFastPeriod','')
plotchar(macdSlowPeriod,'macdSlowPeriod','')
plotchar(macdOVThreshold,'macdOVThreshold','')
plotchar(haOpen,'haOpen','')
plotchar(haClose,'haClose','')
plotchar(haHigh,'haHigh','')
plotchar(haLow,'haLow','')
plotchar(haBullish,'haBullish','')
plotchar(haUpperSL,'haUpperSL','')
plotchar(haLowerSL,'haLowerSL','')
plotchar(haUpperSLSignificant,'haUpperSLSignificant','')
plotchar(haLowerSLSignificant,'haLowerSLSignificant','')
plotchar(haUpperZero,'haUpperZero','')
plotchar(haLowerZero,'haLowerZero','')
plotchar(haTrendContinueUp,'haTrendContinueUp','')
plotchar(haTrendContinueDown,'haTrendContinueDown','')
plotchar(haTrendUp,'haTrendUp','')
plotchar(haTrendDown,'haTrendDown','')
plotchar(haTrendStartUp,'haTrendStartUp','')
plotchar(haTrendStartDown,'haTrendStartDown','')
plotchar(macdLine,'macdLine','')
plotchar(macdSignal,'macdSignal','')
plotchar(macdHisto,'macdHisto','')
plotchar(macdOVThreshold,'macdOVThreshold','')
plotchar(macdOvervalued,'macdOvervalued','')
plotchar(macdUndervalued,'macdUndervalued','')
plotchar(test, 'test', '')
pine-script pine-script-v5
1个回答
0
投票

它进入交易但头寸始终处于打开状态。您需要在代码中添加

strategy.exit()
strategy.close()
,这样您就可以关闭未平仓头寸并再次进入新交易。

© www.soinside.com 2019 - 2024. All rights reserved.