Aroon Oscillator Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mohanee

//@version=4
strategy(title="Aroon Oscillator Strategy", overlay=false, pyramiding=2,    initial_capital=10000, currency=currency.USD)  //default_qty_value=10, default_qty_type=strategy.fixed, 

var stopLossVal=0.00

//variables BEGIN
aroonLength=input(169,title="Aroon Length")   //square root of 13
rsiLength=input(13, title="RSI Length")
stopLoss = input(title="Stop Loss%", defval=5, minval=1)
//variables  END

//RSI 
rsi13=rsi(close,rsiLength)

fastEma1=ema(close,5)
ema13=ema(close,13)
ema169=ema(close,169)

exitOnAroonOscCrossingDown = input(true, title="Exit when Aroon Osc cross down zero ")


// Drawings

//Aroon oscillator

arronUpper = 100 * (highestbars(high, aroonLength+1) + aroonLength)/aroonLength
aroonLower = 100 * (lowestbars(low, aroonLength+1) + aroonLength)/aroonLength

aroonOsc  = arronUpper - aroonLower

aroonMidpoint = 0
oscPlot = plot(aroonOsc, color=color.green)
midLine= plot(aroonMidpoint, color=color.green)
topLine = plot(90,style=plot.style_circles, color=color.green)
bottomLine = plot(-90,style=plot.style_circles, color=color.red)

fill(oscPlot, midLine, color=aroonOsc>0?color.green:color.red, transp=50)
fill(topLine,bottomLine, color=color.blue)
fill(topLine,oscPlot, color=aroonOsc>90?color.purple:na, transp=25)


// RSI 
//plot(rsi13, title="RSI", linewidth=2, color=color.purple)
//hline(50, title="Middle Line", linestyle=hline.style_dotted)
//obLevel = hline(80, title="Overbought", linestyle=hline.style_dotted)
//osLevel = hline(30, title="Oversold", linestyle=hline.style_dotted)
//fill(obLevel, osLevel, title="Background", color=rsi13 >=30 ? color.green:color.purple, transp=65)  // longTermRSI >=50


//Entry--

strategy.entry(id="Long Entry", comment="LE", qty=(strategy.initial_capital * 0.10)/close, long=true,  when= crossover(aroonOsc,0)   )     //crossover(close,ema34)  //and close>ema34  //crossover(rsi5Val,rsiBuyLine)

//Add
if(strategy.position_size>=1 and aroonOsc>0  and close < strategy.position_avg_price and crossover(rsi13,30))
    qty1=(strategy.initial_capital * 0.30)/close
    stopLossVal:= abs(strategy.position_size)>1 ?  ( (close > close[1] and close > open and close>strategy.position_avg_price) ? close[1]*(1-stopLoss*0.01) : stopLossVal ) : 0.00 
    //stopLossVal:= abs(strategy.position_size)>1 ?  strategy.position_avg_price*(1-stopLoss*0.01) :  0.00 
    strategy.order(id="Long Entry", comment="Add SL="+tostring(stopLossVal, "####.##"), qty=qty1, long=true )     //crossover(close,ema34)  //and close>ema34  //crossover(rsi5Val,rsiBuyLine)  --


//stopLossVal:= abs(strategy.position_size)>1 ? strategy.position_avg_price*(1-0.5) : 0.00 

stopLossVal:= abs(strategy.position_size)>1 ?  ( (close > close[1] and close > open and close>strategy.position_avg_price) ? close[1]*(1-stopLoss*0.01) : stopLossVal ) : 0.00 
//stopLossVal:= abs(strategy.position_size)>1 ?  strategy.position_avg_price*(1-stopLoss*0.01) :  0.00

barcolor(color=strategy.position_size>=1? aroonOsc>90 ? color.purple: color.blue:na)

//close partial
strategy.close(id="Long Entry", comment="Partial X  SL="+tostring(stopLossVal, "####.##"),  qty=strategy.position_size/5, when=abs(strategy.position_size)>=1 and crossunder(aroonOsc, 90) )   //close<ema55 and rsi5Val<20 //ema34<ema55
//strategy.close(id="Long Entry", comment="Partial X  SL="+tostring(stopLossVal, "####.##"),  qty=strategy.position_size/3, when=abs(strategy.position_size)>=1 and ( (close - ema169)  > ema169*0.10 ) and crossunder(close,ema13))   //close<ema55 and rsi5Val<20 //ema34<ema55 


//close All
if(exitOnAroonOscCrossingDown)
    strategy.close(id="Long Entry", comment="Exit All",  when=abs(strategy.position_size)>=1 and crossunder(aroonOsc, 0) )   //close<ema55 and rsi5Val<20 //ema34<ema55  //close<ema89

//close All on stop loss
if(not exitOnAroonOscCrossingDown)
    strategy.close(id="Long Entry", comment="Stoploss X",  when=abs(strategy.position_size)>=1 and close < stopLossVal )   //close<ema55 and rsi5Val<20 //ema34<ema55  //close<ema89
