Vin's Playzone Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vvaz_
//base-on CDC ActionZone By Piriya   a simple 2EMA and is most suitable for use with medium volatility market
//@version=5
strategy(title='Playzone', shorttitle='VXIZ', overlay=true, initial_capital=1000)

//variable
srcf = input(title='Source of price', defval=close)
tffix = input(title='Fixed Timeframe?', defval=true)
tfn = input.timeframe(title='Timeframe in', defval='D')
ema1 = input(title='Fast EMA', defval=13)
ema2 = input(title='Slow EMA', defval=26)
ema3 = input(title='Base-Line', defval=60)
smooter = input(title='Smoothing period', defval=2)
fillbar = input(title='Fill Bar Color', defval=false)
emasw = input(title='Show Green-Red EMA?', defval=true)
bssw = input(title='Show Buy-Sell signal?', defval=true)
blsw = input(title='Show Base-line?', defval=true)
fxbl = input(title='Fixed Base-line?', defval=true)
plotmm = input(title='Show Long-Short?', defval=true)
plotmmsm = input.int(title='RSI Smoothing', defval=3, minval=0, maxval=3)

//ema
xcross = ta.ema(srcf, smooter)
efast = tffix ? ta.ema(request.security(syminfo.tickerid, tfn, ta.ema(srcf, ema1), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on), smooter) : ta.ema(xcross, ema1)
eslow = tffix ? ta.ema(request.security(syminfo.tickerid, tfn, ta.ema(srcf, ema2), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on), smooter) : ta.ema(xcross, ema2)
ema3x = fxbl ? ta.ema(request.security(syminfo.tickerid, tfn, ta.ema(srcf, ema3), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on), smooter) : ta.ema(xcross, ema3)
emabl = ta.ema(xcross, 1)


//Zone
Bull = efast > eslow
Bear = efast < eslow

G1 = Bull and xcross > efast  //buy
G2 = Bear and xcross > efast and xcross > eslow  //pre-buy1
G3 = Bear and xcross > efast and xcross < eslow  //pre-buy2

R1 = Bear and xcross < efast  //sell
R2 = Bull and xcross < efast and xcross < eslow  //pre-sell1
R3 = Bull and xcross < efast and xcross > eslow  //pre-sell2

//color
bcl = G1 ? color.green : G2 ? color.yellow : G3 ? color.orange : R1 ? color.red : R2 ? color.orange : R3 ? color.yellow : color.black
barcolor(color=fillbar ? bcl : na)

//plots
line1 = plot(blsw ? ema3x : na, 'Base-Line', color=color.new(color.yellow, 0))
line2 = plot(emasw ? efast : na, 'Fast EMA', color=color.new(color.green, 0))
line3 = plot(emasw ? eslow : na, 'Slow EMA', color=color.new(color.red, 30))
fillcl = Bull ? color.new(color.green, 80) : Bear ? color.new(color.red, 80) : color.new(color.black, 99)
fill(line2, line3, fillcl )

//actions
buywhen = G1 and G1[1] == 0
sellwhen = R1 and R1[1] == 0

bullish = ta.barssince(buywhen) < ta.barssince(sellwhen)
bearish = ta.barssince(sellwhen) < ta.barssince(buywhen)

buy = bearish[1] and buywhen
sell = bullish[1] and sellwhen

//plot trend
plotshape(bssw ? buy : na, style=shape.diamond, title='BUY', location=location.belowbar, color=color.new(color.green, 0), size=size.tiny)
plotshape(bssw ? sell : na, style=shape.diamond, title='Sell', location=location.abovebar, color=color.new(color.red, 0), size=size.tiny)

// Momentum Signal using StochRSI

smoothK = input.int(27, 'StochRSI smooth K', minval=1)
smoothD = input.int(18, 'StochRSI smooth D', minval=1)
RSIlen = input.int(42, 'RSI length', minval=1)
STOlen = input.int(42, 'Stochastic length', minval=1)
SRsrc = input(close, 'Source for StochasticRSI')
OSlel = input.float(30, 'Oversold Threshold', minval=0.00)
OBlel = input.float(70, 'overbought Threshold', minval=0.00)
rsil = ta.rsi(SRsrc, RSIlen)
K = ta.sma(ta.stoch(rsil, rsil, rsil, STOlen), smoothK)
D = ta.sma(K, smoothD)

crossover_1 = ta.crossover(K, D)
crossover_2 = ta.crossover(K, D)
iff_1 = D > OSlel and crossover_2 ? 2 : 0
iff_2 = D < OSlel and crossover_1 ? 4 : iff_1
buymore = bullish ? iff_2 : 0
crossunder_1 = ta.crossunder(K, D)
crossunder_2 = ta.crossunder(K, D)
iff_3 = D < OBlel and crossunder_2 ? 2 : 0
iff_4 = D > OBlel and crossunder_1 ? 4 : iff_3
sellmore = bearish ? iff_4 : 0

//plot momentum
plotshape(plotmm ? buymore > plotmmsm ? buymore : na : na, 'Long', style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.tiny)
plotshape(plotmm ? sellmore > plotmmsm ? sellmore : na : na, 'Short', style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.tiny)

//stratgy excuter

//condition
buycon =  buy
sellcon = sell
buycon1 = buymore > plotmmsm
sellcon1 = sellmore > plotmmsm
//confirm_trend
buyfirm = emabl >= ema3x and Bull
sellfirm = emabl <= ema3x and Bear
//Long
strategy.entry('Long', strategy.long, when= buycon and buyfirm , comment='Buy', alert_message='Buy Now!')
strategy.entry('Long', strategy.long, when= buycon1 and buyfirm , comment='Long', alert_message='Buy More!')
strategy.close('Long', when= sell or Bear, comment='Stop-L', alert_message='Take profit Long')
//Short
strategy.entry('Short', strategy.short, when= sellcon and sellfirm , comment='Sell', alert_message='Sell Now!')
strategy.entry('Short', strategy.short, when= sellcon1 and sellfirm , comment='Short', alert_message='Sell More!')
strategy.close('Short', when= buy or Bull, comment='Stop-S', alert_message='Take profit Short')