4

I am using pine editor to make a strategy that buys when the D line which is the green line crosses under 17.5 and then sells when the D line crosses over 78. As you can see from the chart I have posted it should buy and sell a couple times but only buys once and does nothing else. I cant seem to figure out what I am missing. The bottom of the code is where I am telling it to by and sell in the strategy. Thanks

strategy("stochastic")
length = input(21, minval=1, title="length")  
rsilength = input(21, minval=1, title="rsi length")  
smoothk = input(4, minval=1, title="smoothk")
smoothd = input(10, minval=1, title="smoothd")
rsi = rsi(close, rsilength)

sto = stoch(close,highest(length),lowest(length), length)
K = sma(sto,smoothk)
D = sma(K,smoothd)

//plot(rsi, title="rsi", color=color.black)
plot(D, title="%D",color=color.green)
plot(K, title="%K",color=color.red)

hline(78,title="upper limit", color=color.red)
hline(17, title="lower limit",color=color.blue)
//plot(sto, title = "sto",color=color.black)


// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 8, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 5, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2019, title = "From Year", minval = 2017)
ToMonth   = input(defval = 9, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false // create function "within window of time"

// === EXECUTION ===
shares = 10000/close
buy = crossunder(D,17.5)
sell = crossover(D,78)

strategy.entry("buy", shares, when = window() and buy)  // buy long when "within window of time" AND crossover
strategy.close("sell", when = window() and sell)                // sell long when "within window of time" AND crossunder         

This should buy and sell a couple times as seen highlighted in the picture. https://ibb.co/37L0wSK That is the link, sorry I do not have enough rep to post images.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
ColtonMSU
  • 41
  • 6

1 Answers1

1

Your strategy.close() call wasn't using the same order id than the strategy.entry() call. I added markers in last 3 lines (you can turn them off via inputs) to show where your conditions are met.

Note: make sure you include the first line in the script when you transport Pine scripts. It's the compiler directive that specifies which version of Pine the code is using, so it's important.

//@version=4
strategy("stochastic")
length = input(21, minval=1, title="length")  
rsilength = input(21, minval=1, title="rsi length")  
smoothk = input(4, minval=1, title="smoothk")
smoothd = input(10, minval=1, title="smoothd")
showMarkers = input(true, "Show Markers")

rsi = rsi(close, rsilength)

sto = stoch(close,highest(length),lowest(length), length)
K = sma(sto,smoothk)
D = sma(K,smoothd)

//plot(rsi, title="rsi", color=color.black)
plot(D, title="%D",color=color.green)
plot(K, title="%K",color=color.red)

hline(78,title="upper limit", color=color.red)
hline(17, title="lower limit",color=color.blue)
//plot(sto, title = "sto",color=color.black)


// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 8, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 5, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2019, title = "From Year", minval = 2017)
ToMonth   = input(defval = 9, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false // create function "within window of time"

// === EXECUTION ===
shares = 10000/close
buy = crossunder(D,17.5)
sell = crossover(D,78)

strategy.entry("buy", shares, when = window() and buy)  // buy long when "within window of time" AND crossover
strategy.close("buy", when = window() and sell)         // sell long when "within window of time" AND crossunder

plotshape(showMarkers and buy, "buy", shape.triangleup, location.bottom, color.green, 0, text = "buy", size = size.tiny)
plotshape(showMarkers and sell, "sell", shape.triangledown, location.top, color.maroon, 0, text = "sell", size = size.tiny)
bgcolor(showMarkers ? window() ? color.green : color.maroon : na)

enter image description here

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21