Mobius - Trend Pivot
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © coderboring

//@version=5
indicator("Mobius - Trend Pivot", overlay = true)

// Mobius
// V01.01.29.2019
// Uses trend of higher highs with higher lows and trend of lower lows with lower highs to locate pivots. Distance for trend is set by the user. Confirmation of a reversal from pivots is set with a multiple of the pivot bars range. That multiple is also a user input.
// Trading Rules
// 1) Trade when price crosses and closes outside the pivot Confirmation line. At that point looking for best entry. Min trade is 2 contracts
// 2) Know your risk point before entering trade. Typical risk point is the pivot line itself. If your risk is crossed look for an exit. Never use hard stops - you'll often get out for little or no loss
// 3) Know your Risk off point before entering. Typical Risk Off is an ATR multiple. Offer Risk Off as soon as possible for a Risk Free trade
// 4) set mental stop one tick above entry when Risk Off is achieved
// 5) if trade continues your way move mental stop for your runner to last support / resistance each time a new support / resistance is hit.

n      = input( 5, "n     ")
R_Mult = input(.7, "R_Mult")

o = open 
h = high 
l = low  
c = close
x = bar_index
//nan = Double.NaN;
ts = syminfo.mintick

TrueRange(h, c, l) => math.max(c[1], h) - math.min(c[1], l)


tr = TrueRange(h, c, l)
var hh = float(na)
hh := math.sum(h > h[1] ? 1 : 0, n) >= n and
         math.sum(l > l[1] ? 1 : 0, n) >= n-1
         ? h
         : h > hh
             ? h
             : hh

var xh = int(na)
xh := h == hh ? x : xh

hh_ = line.new(na, na, na, na, xloc.bar_index, extend.right, color.red)
if barstate.islast
    line.set_xy1(hh_, xh, hh)
    line.set_xy2(hh_, xh+ 1, hh)

Average(data , len) => math.sum(data, len) / len
TickSize() => syminfo.mintick

var hR = float(na)
hR := h == hh
     ? math.round(Average(tr, n)/TickSize(), 0)*TickSize()
     : hR
var PrevL = float(na)
PrevL := h == hh
     ? l[1]
     : PrevL

STO = line.new(na, na, na, na, xloc.bar_index, extend.right, color.red)
STO_RO = line.new(na, na, na, na, xloc.bar_index, extend.right, color.white, line.style_dashed)

sto = float(na)
if barstate.isconfirmed
    sto := math.round((math.max(PrevL, hh - (hR * R_Mult))) / ts, 0) * ts
    line.set_xy1(STO, xh, sto)
    line.set_xy2(STO, xh+ 1, sto)

    sto_ro = sto - math.min(hR, TickSize() * 16)
    line.set_xy1(STO_RO, xh, sto_ro)
    line.set_xy2(STO_RO, xh+ 1, sto_ro)


var ll = float(na)
ll := math.sum(l < l[1] ? 1 : 0, n) >= n and  math.sum(h < h[1] ? 1 : 0, n) >= n-1 ? l : l < ll[1] ? l : ll

var xl = int(na)
xl := l == ll ? x : xl

ll_ = line.new(na, na, na, na, xloc.bar_index, extend.right, color.green)
if barstate.isconfirmed
    line.set_xy1(ll_, xl, ll)
    line.set_xy2(ll_, xl+ 1, ll)

var lR = float(na)
lR := l == ll ? math.round(Average(tr, n)/TickSize(), 0)*TickSize() : lR

var PrevH = float(na)
PrevH := l == ll ? h[1] : PrevH

BTO = line.new(na, na, na, na, xloc.bar_index, extend.right, color.green)
BTO_RO = line.new(na, na, na, na, xloc.bar_index, extend.right, color.white, line.style_dashed)

bto = float(na)


if barstate.isconfirmed
    bto := math.round((math.min(PrevH, ll + (lR * R_Mult))) / ts, 0) * ts

line.set_xy1(BTO, xl, bto)
line.set_xy2(BTO, xl+ 1, bto)
if barstate.isconfirmed
    bto_ro = bto + math.min(lR, TickSize() * 16)

    line.set_xy1(STO_RO, xl, bto_ro)
    line.set_xy2(STO_RO, xl+ 1, bto_ro)


    linefill.new(STO, hh_, color.new(color.red, 85))
    linefill.new(ll_, BTO, color.new(color.green, 85))


if ta.crossunder(c, sto)
    alert("c crosses below STO")

if ta.crossover(c, bto)
    alert("c crosses above bto")

// Define plot variables
plotshape(ta.crossover(c, bto), "bullish arrow", shape.triangleup, location.belowbar, color.green, size = size.small)
plotshape(ta.crossunder(c, sto), "bearish Arrow", shape.triangledown , location.abovebar, color.red, size =  size.small)

