//@version=4 // Copyright (c) 2019-present, Franklin Moormann (cheatcountry) // Historical Volatility Percentile + SMA [CC] script may be freely distributed under the MIT license. strategy(title="Historical Volatility Percentile + SMA [CC]", overlay=false) inp = input(title="Source", type=input.source, defval=close) res = input(title="Resolution", type=input.resolution, defval="") rep = input(title="Allow Repainting?", type=input.bool, defval=false) bar = input(title="Allow Bar Color Change?", type=input.bool, defval=true) src = security(syminfo.tickerid, res, inp[rep ? 0 : barstate.isrealtime ? 1 : 0])[rep ? 0 : barstate.isrealtime ? 0 : 1] length = input(title="Length", type=input.integer, defval=21, minval=1) annualLength = input(title="AnnualLength", type=input.integer, defval=252, minval=1) // the reason why I don't use the pinescript stddev function below is because we are doing sample std deviation not population r = log(src / nz(src[1], src)) rAvg = sma(r, length) hv = sqrt(sum(pow(r - rAvg, 2), length) / (length - 1)) * sqrt(annualLength) count = 0. for i = 0 to annualLength - 1 count := count + (nz(hv[i]) < hv ? 1 : 0) hvp = count / annualLength * 100 hvpSma = sma(hvp, length) srcEma = ema(src, length) sig = hvp >= hvpSma and src > srcEma ? 1 : hvp >= hvpSma and src < srcEma ? -1 : 0 alertcondition(crossover(sig, 0), "Buy Signal", "Bullish Change Detected") alertcondition(crossunder(sig, 0), "Sell Signal", "Bearish Change Detected") hvpColor = sig > 0 ? color.green : sig < 0 ? color.red : color.black barcolor(bar ? hvpColor : na) plot(hvp, title="HVP", linewidth=2, color=hvpColor) plot(hvpSma, title="HVPSMA", linewidth=2, color=color.blue) // buy_signal = (crossover(sig, 0) and close > srcEma) // sell_signal = (crossunder(sig, 0) and close < srcEma) buy_signal = crossover(sig, 0) sell_signal = crossunder(sig, 0) //INTRADAY AlgoJi s=input(title="INTRA DAY TRADE SESSION",type=input.session,defval="0915-1450") st=time(timeframe.period,s) e=input(title="END SESSION",type=input.session,defval="1515-1520") et=time(timeframe.period,e) quant=input(title="Trade Quantity",defval=1) //AlgoJi adding stoploss and target option ut=input(defval=false,title="USE TARGET") us=input(defval=false,title="USE STOPLOSS") tar=input(defval=10.0,title="TARGET IN RS") stop=input(defval=7.0,title="STOP LOSS IN RS") tar:=tar/syminfo.mintick stop:=stop/syminfo.mintick //setting automated alerts required by apibridge lxse="TYPE: LX" + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) + " :TYPE:SE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) sxle="TYPE: SX" + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) + " :TYPE:LE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) le="TYPE:LE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) lx="TYPE:LX " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) se="TYPE:SE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) sx="TYPE:SX " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(quant) if(buy_signal and st and strategy.position_size==0) strategy.entry("BUY",strategy.long,comment=le) if(sell_signal and st and strategy.position_size==0) strategy.entry("SELL",strategy.short,comment=se) if(buy_signal and st and strategy.position_size!=0) strategy.entry("BUY",strategy.long,comment=sxle) if(sell_signal and st and strategy.position_size!=0) strategy.entry("SELL",strategy.short,comment=lxse) if(ut==true and us==false) strategy.exit(id="LX",from_entry="BUY",profit=tar,comment=lx) strategy.exit(id="SX",from_entry="SELL",profit=tar,comment=sx) if(us==true and ut==false) strategy.exit(id="LX",from_entry="BUY",loss=stop,comment=lx) strategy.exit(id="SX",from_entry="SELL",loss=stop,comment=sx) if(ut==true and us==true) strategy.exit(id="LX",from_entry="BUY",profit=tar,loss=stop,comment=lx) strategy.exit(id="SX",from_entry="SELL",profit=tar,loss=stop,comment=sx) strategy.close(id="BUY",when=et,comment=lx) strategy.close(id="SELL",when=et,comment=sx)