//@version=4
//@author=FOX
StrategyName = "MYTS"
ShortStrategyName = "MYTS"
strategy(title=StrategyName, shorttitle=ShortStrategyName, overlay=true,
pyramiding=0, default_qty_value=100, precision=7, currency=currency.EUR,
commission_value=0.2,commission_type=strategy.commission.percent, initial_capital=10000)
///////////////////////////////////////////////////////////////////////////////
AQUA = #00FFFFFF
BLUE = #0000FFFF
RED = #FF0000FF
LIME = #00FF00FF
GRAY = #808080FF
DARKRED = #8B0000FF
DARKGREEN = #006400FF
GOLD = #FFD700
WHITE = color.white
// Plots
GREEN_LIGHT = color.new(color.green, 40)
RED_LIGHT = color.new(color.red, 40)
BLUE_LIGHT = color.new(color.aqua, 40)
PURPLE_LIGHT = color.new(color.purple, 40)
source = input(close)
///////////////////////////////////////////////////////////////////////////////
// input
///////////////////////////////////////////////////////////////////////////////
mult = input (3.0, title="ATR Moltiplicatore")
length = input(10, title="ATR Periodo")
type = input(1, title="Stile? (0 => HighLow, 1 => Close)")
ticksize = input(1)
rsiperiod = input(11, title="RSI Periodo", minval = 1, step = 1)
///////////////////////////////////////////////////////////////////////////////
// calc
///////////////////////////////////////////////////////////////////////////////
uu = type == 1 ? close : high
ll = type == 1 ? close : low
satr = atr(length)
svs = low+ceil(mult*satr/ticksize)*ticksize
lvs = high-ceil(mult*satr/ticksize)*ticksize
///////////////////////////////////////////////////////////////////////////////
funcX(xtype, xlength, xmult, xticksize, xclose, xhigh, xlow) =>
uu=xtype == 1 ? xclose : xhigh
ll=xtype == 1 ? xclose : xlow
satr=atr(xlength)
svs = xlow+ceil(xmult*satr/xticksize)*xticksize
lvs = xhigh-ceil(xmult*satr/xticksize)*xticksize
shortvs = 0.0
shortvs := na(shortvs[1]) ? svs : iff(uu>shortvs[1], svs , min(svs,shortvs[1]))
longvs = 0.0
longvs := na(longvs[1]) ? lvs : iff(ll<longvs[1], lvs, max(lvs,longvs[1]))
longswitch =iff ((uu>=shortvs[1] and uu[1]<shortvs[1]), 1, 0)
shortswitch =iff ((ll<=longvs[1] and ll[1]>longvs[1]), 1 , 0)
direction = 0
direction := iff(na(direction[1]), 0, iff (direction[1]<=0 and longswitch, 1, iff (direction[1]>=0 and shortswitch, -1, direction[1])))
pc=direction>=0?longvs:shortvs
[direction, pc]
///////////////////////////////////////////////////////////////////////////////
// study
///////////////////////////////////////////////////////////////////////////////
[st_direction, st_pc] = funcX(type, length, mult, ticksize, close, high, low)
plot(st_pc, color=(st_direction == 0 ? WHITE : (st_direction > 0 ? GREEN_LIGHT : RED_LIGHT)), linewidth=3)
plotshape(crossover(close,st_pc), color=WHITE, textcolor=color.black, style=shape.labeldown, text="buy")
plotshape(crossunder(close,st_pc), color=WHITE, textcolor=color.red, style=shape.labeldown, text="sell")
plotshape(crossunder(rsi(close, rsiperiod),75), text="RSI MAX", style=shape.labeldown, color=color.white, textcolor=color.black, location=location.abovebar, transp=0, offset = -1)
///////////////////////////////////////////////////////////////////////////////
// strategy
///////////////////////////////////////////////////////////////////////////////
isLong = st_direction == 1
isShort = st_direction != 1
longClose = isLong[1] and isShort
shortClose = isShort[1] and isLong
//strategy.entry("Long", 1, when=isLong)
//strategy.entry("Short", 0, when=isShort)
//strategy.close("Long", when=longClose )
//strategy.close("Short", when=shortClose )