So I m writing a pine script strategy with a specific candlestick condition to be met along with over-bought/sold stochastic and sma crosses.
For a long trade, I need the stock, which is both the k and d values to be below 30, and for short above 70 obviously.
I ve added the built-in stochastic script to my strategy script. The problem I m having is 99% of all my conditions are met, but occasionally on my trigger candle, the one that my stop-loss, entry, and TP are calculated off, the k value of the stochastic is below 30 on the close of the trigger candle, but the d value is not.
I ve added historical operators to the oversold overbought stoch conditions and changed bar states but nothing seems to change it.
you can see in the screenshot that the trigger candles stoch values arent below 30
strategy("My Strategy",
overlay=true,
initial_capital = 10000,
currency = USDT ,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 100)
//Get user input
var g_rsi = Rsi Settings
periodK = input.int(5, title="%K Length", minval=1, group = g_rsi)
smoothK = input.int(3, title="%K Smoothing", minval=1, group = g_rsi)
periodD = input.int(3, title="%D Smoothing", minval=1, group = g_rsi)
upperBand = input.int(title="Upper Band", defval=70, group = g_rsi)
lowerBand = input.int(title="Lower Band", defval=30, group = g_rsi)
//Get sma Lookback input
var g_sma = SMA Lookback
smaLookback = input.int(title = SMA Lookback , defval = 20, group = g_sma)
smaLookback1 = input.int(title = SMA Lookback , defval = 50, group = g_sma)
//Get R:R inputs
var g_rr = Risk:Reward Settings
multiplier = input.float(title = ATR Multiplier , defval = 1.0, group = g_rr, tooltip = Multiplies SL and TP based on ATR eg. 1:1, 2:2 )
rr = input.float(title = Risk:Reward , defval = 3.4, group = g_rr, tooltip = Adjusts your profit Level, eg. 2:1, 3:1, 4:1 )
rrlookback = input.int(title = Lookback , defval = 1, group = g_rr)
//Get ATR value
atr = ta.atr(14)
//Get SMA TA
sma1 = ta.sma(close, smaLookback)
sma2 = ta.sma(close, smaLookback1)
//Get stoch TA
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
//Get RSI ob/os filters
isRSIos = k and d <= lowerBand
isRSIob = k and d >= upperBand
//Get SMA crossovers
islongEntrysma = sma1 > sma2
isshortEntrysma = sma1 < sma2
//Get long candle patterns
lessthanprevHigh = high < high[1]
bullishCandle = close > open
long = lessthanprevHigh and bullishCandle
//Get short candle patterns
greaterthanprevLow = low > low[1]
bearishCandle = close < open
short = greaterthanprevLow and bearishCandle
//Get entry conditions
buyCondition = long and islongEntrysma and isRSIos and not na(atr) and barstate.isconfirmed
sellCondition = short and isshortEntrysma and isRSIob and not na(atr) and barstate.isconfirmed
//Calculate stops and targets
longStop = ta.lowest(low, rrlookback) - (atr * multiplier)
shortStop = ta.highest(high, rrlookback) + (atr * multiplier)
longStopDistance = close - longStop
shortStoDistance = shortStop - close
longTarget = close + (longStopDistance * rr)
shortTarget = close - (shortStoDistance * rr)
//Save stops and targets
var t_stop = 0.0
var t_target = 0.0
//Enter our buy order
if buyCondition and strategy.position_size == 0
t_stop := longStop
t_target := longTarget
strategy.entry(id = Long , direction = strategy.long)//here we are asking, is buySignal true? if so, enter our trade using strategy.entry with a id(title), of Long and direction of long
//Enter our sell order
if sellCondition and strategy.position_size == 0
t_stop := shortStop
t_target := shortTarget
strategy.entry(id = Short , direction = strategy.short)//here we are asking, is buySignal true? if so, enter our trade using strategy.entry with a id(title), of Long and direction of long
//Manage exit orders
strategy.exit(id = Long Exit , from_entry = Long , limit = t_target, stop = t_stop, when = strategy.position_size > 0)
strategy.exit(id = Short Exit , from_entry = Short , limit = t_target, stop = t_stop, when = strategy.position_size < 0)
//Plot SL and TP targets
plot(strategy.position_size != 0 ? t_stop : na, color = color.red, style = plot.style_linebr)
plot(strategy.position_size != 0 ? t_target : na, color = color.green, style = plot.style_linebr)