Search code examples
pine-scriptpine-script-v5

Why won’t my strategy enter any positions?


Why won't my strategy enter any positions?

Hello, I've been having a problem developing my first strategy. The strategy won't enter any positions.

I've checked my syntax a couple dozen times and even sent it to ChatGPT for any advice, but to no avail. I've also plotted all the variables so I can see them in the data window for debugging. The conditions to enter a position are being met (haTrendStartUp == true), but no positions are being entered. Help?? Thank you in advance!

//@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', '')

Solution

  • It enters a trade but the position is always open. You need to add strategy.exit() or strategy.close() to your code so you can close your open positions and enter a new trade again.

    enter image description here