J-BUY & SELL
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jayant0478

//@version=4
study(title="J-BUY & SELL", overlay=true)

greenCandle = (close > open)
redCandle = (close < open)

myMA5 = sma(close, 5)
myMA10 = sma(close, 10)
myMA20 = sma(close, 20)
//crossUp1 = crossover(close[1], myMA)
//crossUp2 = crossover(close[1], myMA2)
//crossDown1 = crossunder(close[1], myMA)
//crossDown2 = crossunder(close[1], myMA2)


// Bollinger Band attributes

mdl = sma(close, 20)
dev = stdev(close, 20)
upr = mdl + 2*dev
lwr = mdl - 2*dev
pb = ((close - lwr)*100/upr - lwr) //Percent B
bbw = (upr - lwr)*100/mdl //Bollinger Bands Width

// Bollinger Band attributes close

rsiValue = rsi(close, 14)
rsiEMA   = ema(rsiValue, 5)


myBuyCondition1 = myMA5 > myMA10 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle and close > open[2] and close > myMA5
myBuyCondition2 = myMA5 > myMA20 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[2] and greenCandle[1]  and greenCandle and close[1] > close[2] and close > open[2] and close > myMA5
//myBuyCondition3 = greenCandle and close < lwr and close < close[1]
myBuyCondition4 = myMA5 > myMA20 and myMA10 > myMA20  and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[2] and greenCandle[1] and greenCandle and close[1] < myMA5 and close > myMA5 and close > open[2] 
myBuyCondition5 = myMA5 > myMA20 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[1] and redCandle and close[1] < myMA5 and close > myMA5 and close > close[1] and close < upr and close > open[2] 
myBuyCondition6 = myMA5 > myMA10 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[1] and greenCandle and close > open[1] and close > myMA5 and close > open[2] 
myBuyCondition7 = myMA5 > myMA10 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and redCandle and close > open[1] and close > myMA5 and open < upr and close > open[2] 
myBuyCondition8 = myMA5 > myMA10 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[2] and redCandle[1] and greenCandle and close > open[2] and close > myMA5 and close > open[2] 
myBuyCondition9 = redCandle[2] and close[2] < myMA5 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and greenCandle and (close > open[2] or close > myMA5) and myMA5 > myMA20 and close > open[2] 
myBuyCondition10 = myMA5 > myMA10 and myMA5 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[3] and greenCandle[2] and greenCandle[1] and greenCandle and open > myMA5 and close > myMA5 and close > open[3]  and close > close[1] 
myBuyCondition11 = myMA5 > myMA10 and myMA10 > myMA20 and redCandle[2] and greenCandle[1] and greenCandle and open[2] > upr and close > close[1] and close > open[2] and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA))
//myBuyCondition12 = myMA5 < myMA20 and myMA10 > myMA20 and redCandle[2] and greenCandle[1] and greenCandle and close[2] < myMA5 and close[1] > myMA5 and close[1] > open[2] and open > myMA5 and close > myMA5 and close > open[2]   
//myBuyCondition13 = myMA5 < myMA20 and myMA10 > myMA20 and redCandle[2] and greenCandle[1] and greenCandle and close[2] < myMA5 and close > myMA5 and close > open[2] and close > open[2] 
myBuyCondition14 = myMA5 < myMA20 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[2] and greenCandle[1] and redCandle and close > myMA5 and close > open[1] and close > open[2] 
myBuyCondition15 = myMA5 < myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[2] and redCandle[1] and greenCandle and close[1] < lwr and open > close[1] and close > open[1] and close > open[2] 
myBuyCondition16 = myMA5 > myMA10 and myMA10 > myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and redCandle and close[1] > myMA5 and close > open[1] and close > open[2] and close < upr
myBuyCondition17 = myMA5 > myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and greenCandle and close > myMA20 and close > open[2] 
myBuyCondition18 = myMA5 > myMA10 and myMA5 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and greenCandle and close > myMA20 and close > open[2] 
myBuyCondition19 = myMA5 < myMA20 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle and open > myMA20 and close > myMA20
myBuyCondition20 = myMA5 > myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[2] and greenCandle[1] and greenCandle and close[1] > myMA5 and (close[1] > open[1] or (close > myMA5 and close > open[2])) 
myBuyCondition21 = myMA5 > myMA10 and myMA5 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[1] and greenCandle and close[1] < myMA5 and close > myMA5 and close > open[1]  
myBuyCondition22 = myMA5 < myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[3] and redCandle[2] and greenCandle[1] and greenCandle and close > myMA5 and close > open[2] 
myBuyCondition23 = myMA5 < myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and redCandle[3] and redCandle[2] and greenCandle[1] and close > myMA5 and close > open[2]
myBuyCondition24 = myMA5 > myMA10 and myMA5 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and greenCandle and close > myMA5 
myBuyCondition25 = myMA5 > myMA10 and myMA5 < myMA20 and (crossover(rsiValue, rsiEMA)) and greenCandle[2] and greenCandle[1] and greenCandle and close > myMA5 
myBuyCondition26 = myMA5 > myMA10 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and close > myMA5 and close > open[2]
myBuyCondition27 = myMA5 < myMA10 and myMA10 < myMA20 and greenCandle[2] and greenCandle[1] and greenCandle and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA))
myBuyCondition28 = myMA5 < myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle and close > myMA10 and close > open[1]
myBuyCondition29 = myMA5 < myMA10 and myMA10 < myMA20 and ((rsiValue > rsiEMA) or crossover(rsiValue, rsiEMA)) and greenCandle[1] and greenCandle and close > myMA10 

myBuyConditionTotal =  (myBuyCondition29 or myBuyCondition28 or myBuyCondition27 or myBuyCondition26 or myBuyCondition25 or myBuyCondition24 or myBuyCondition23 or myBuyCondition22 or myBuyCondition21 or myBuyCondition20 or myBuyCondition19 or myBuyCondition18 or myBuyCondition17 or myBuyCondition16 or myBuyCondition15 or myBuyCondition14 or myBuyCondition1 or myBuyCondition2 or myBuyCondition4 or myBuyCondition5 or myBuyCondition6 or myBuyCondition7 or myBuyCondition8 or myBuyCondition9 or myBuyCondition10 or myBuyCondition11 )



mySellCondition1 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[4] and greenCandle[3] and redCandle[2] and redCandle[1] and redCandle and close < myMA5 and close < open[3] and (rsiValue < rsiEMA) 
mySellCondition2 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[2] and redCandle[1] and redCandle and close[2] > myMA5  and close < myMA5 and close < open[2] and (rsiValue < rsiEMA) 
mySellCondition3 = myMA5 < myMA20 and myMA10 < myMA20 and greenCandle[2] and redCandle[1] and redCandle and close[2] > myMA5 and close < myMA5 and (rsiValue < rsiEMA) 
mySellCondition4 = myMA5 < myMA10 and myMA10 < myMA20 and redCandle[2] and greenCandle[1] and redCandle and close < myMA5  and ((rsiValue < rsiEMA))
mySellCondition5 = myMA5 > myMA20 and myMA10 < myMA20 and greenCandle[3] and redCandle[2] and greenCandle[1] and redCandle and close[1] > myMA5 and close < myMA5 and close < open[1] and close[2] < myMA5 and (rsiValue < rsiEMA) 
mySellCondition6 = myMA5 > myMA20 and myMA10 < myMA20 and redCandle and close < myMA20 and close < open[3] and (rsiValue < rsiEMA) 
mySellCondition7 = myMA5 < myMA10 and myMA10 < myMA20 and greenCandle[2] and greenCandle[1] and redCandle and close[1] > myMA5 and close < myMA5 and ((rsiValue < rsiEMA))
mySellCondition8 = myMA5 < myMA20 and myMA10 < myMA20 and greenCandle[1] and redCandle and close < myMA5 and (rsiValue < rsiEMA) and ((rsiValue < rsiEMA))
mySellCondition9 = myMA5 < myMA20 and myMA10 < myMA20 and redCandle[2] and greenCandle[1] and redCandle and close < myMA5 and close < open[1] and ((rsiValue < rsiEMA))
mySellCondition10 = myMA5 < myMA20 and myMA10 < myMA20 and redCandle[1] and redCandle and close < myMA5 and (rsiValue < rsiEMA) 
mySellCondition11 = myMA5 > myMA10 and myMA5 < myMA20 and redCandle[2] and greenCandle[1] and redCandle and close < myMA20 and (rsiValue < rsiEMA) 
mySellCondition12 = myMA5 < myMA10 and myMA10 < myMA20 and greenCandle[2] and greenCandle[1] and redCandle and close < open[1] and ((rsiValue < rsiEMA))
//mySellCondition13 = redCandle and open > upr and close > upr and close > close[1]
//mySellCondition14 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[3] and redCandle[2] and greenCandle[1] and redCandle and close < myMA5  and close[2] < myMA5
mySellCondition15 = myMA5 < myMA10 and myMA10 > myMA20 and redCandle[1] and redCandle and close < myMA20 and (rsiValue < rsiEMA) 
mySellCondition16 = myMA5 > myMA10 and myMA10 > myMA20 and redCandle[1] and redCandle and close < myMA20 and (rsiValue < rsiEMA) 
mySellCondition17 = myMA5 < myMA10 and myMA5 > myMA20 and redCandle and close < open[2] and (rsiValue < rsiEMA) 
mySellCondition18 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[2] and greenCandle[1] and redCandle and open > close[1] and close < open[2] and close < myMA5 and (rsiValue < rsiEMA) 
mySellCondition19 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[3] and redCandle[2] and greenCandle[1] and redCandle and close < myMA5 and close < open[3]  and (close[2] < open[3]) and (rsiValue < rsiEMA) 
mySellCondition20 = myMA5 < myMA10 and myMA5 > myMA20 and redCandle[2] and greenCandle[1] and redCandle and ((rsiValue < rsiEMA))
mySellCondition21 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[2] and greenCandle[1] and redCandle and close < open[2] and close < myMA5 and (rsiValue < rsiEMA) 
mySellCondition22 = myMA5 > myMA10 and myMA10 > myMA20 and greenCandle[2] and redCandle[1] and redCandle and close < myMA10 and close < open[2] and (rsiValue < rsiEMA) 
mySellCondition23 = myMA5 > myMA10 and (rsiValue < rsiEMA) and close < myMA5 and greenCandle[3] and greenCandle[2] and redCandle[1] and redCandle and close < open[3] 
mySellCondition24 = myMA5 > myMA10 and myMA10 > myMA20 and redCandle[2] and redCandle[1] and redCandle and ((rsiValue < rsiEMA))
mySellCondition25 = myMA5 > myMA10 and myMA10 > myMA20 and redCandle and close < myMA10 and ((rsiValue < rsiEMA))

mySellConditionTotal = (mySellCondition25 or mySellCondition24 or mySellCondition23 or mySellCondition22 or mySellCondition20 or mySellCondition21 or mySellCondition18 or mySellCondition17 or mySellCondition16 or mySellCondition15 or mySellCondition12 or mySellCondition11 or mySellCondition10 or mySellCondition9 or mySellCondition8 or mySellCondition7 or mySellCondition6 or mySellCondition5 or mySellCondition4 or mySellCondition3 or mySellCondition2 or mySellCondition1)

plot(series=myMA5, color=color.yellow, linewidth=2)
plot(series=myMA10, color=color.blue, linewidth=2)
plot(series=myMA20, color=color.white, linewidth=2)


plotarrow(series=myBuyConditionTotal ? 1 :
         mySellConditionTotal ? -1 :
         na,
     colorup=color.new(color.white,0), colordown=color.new(color.lime,0),
     maxheight=30)

alertcondition(condition=myBuyConditionTotal, title="Buy Bar", message="{{ticker}}-BUY, price = {{close}}")
alertcondition(condition=mySellConditionTotal, title="Sell Bar", message="{{ticker}}-SELL, price = {{close}}")

//currentPrice = security(syminfo.tickerid, "15", close)
//BuyEntryPrice = if (myBuyConditionTotal)
    //currentPrice
//BuyExitPrice = if (currentPrice > BuyEntryPrice * 100.05%)
               // alertcondition(condition=BuyExitPrice, title="buy wxit Bar", message="{{ticker}}-buy exit, price = {{close}}")
                
plot(series=close)
//Copyright: Jayanta Sarkar
