# Trend Pivots
# 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.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Code has added historical pivot plots as well as visual trade signals.
# No guarantees as to accuracy or reliability.
# Removed Risk Off portions from code
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input n = 5;
input R_Mult = .7;
def h = high;
def l = low;
def c = close;
def x = BarNumber();
def nan = Double.NaN;
def ts = TickSize();
def tr = TrueRange(h, c, l);
def hh = if Sum(h > h[1], n) >= n and
Sum(l > l[1], n) >= n - 1
then h
else if h > hh[1]
then h
else hh[1];
def xh = if h == hh
then x
else nan;
plot hh_ = if x >= HighestAll(xh)
then HighestAll(if IsNaN(c[-1])
then hh
else nan)
else nan;
hh_.SetDefaultColor(Color.RED);
hh_.HideTitle();
hh_.HideBubble();
def hR = if h == hh
then Round(Average(tr, n) / TickSize(), 0) * TickSize()
else hR[1];
def PrevL = if h == hh
then l[1]
else PrevL[1];
plot STO = if x >= HighestAll(xh)
then HighestAll(if IsNaN(c[-1])
then Round((Max(PrevL, hh_ - (hR * R_Mult))) / ts, 0) * ts
else nan)
else nan;
STO.SetDefaultColor(Color.RED);
STO.HideTitle();
STO.HideBubble();
def ll = if Sum(l < l[1], n) >= n and
Sum(h < h[1], n) >= n - 1
then l
else if l < ll[1]
then l
else ll[1];
def xl = if l == ll
then x
else nan;
plot ll_ = if x >= HighestAll(xl)
then HighestAll(if IsNaN(c[-1])
then ll
else nan)
else nan;
ll_.SetDefaultColor(Color.GREEN);
ll_.HideTitle();
ll_.HideBubble();
def lR = if l == ll
then Round(Average(tr, n) / TickSize(), 0) * TickSize()
else lR[1];
def PrevH = if l == ll
then h[1]
else PrevH[1];
plot BTO = if x >= HighestAll(xl)
then HighestAll(if IsNaN(c[-1])
then Round((Min(PrevH, ll_ + (lR * R_Mult))) / ts, 0) * ts
else nan)
else nan;
BTO.SetDefaultColor(Color.GREEN);
BTO.HideTitle();
BTO.HideBubble();
AddCloud(STO, hh_, Color.LIGHT_RED, Color.LIGHT_RED);
AddCloud(ll_, BTO, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
# End Code Trend Pivots
#~~~~~~~~~~~~~~~~~~~~~~~~~ begin historical pivots ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input show_historical_pivots = no;
plot lowestpivot = if show_historical_pivots then ll else nan;
lowestpivot.SetDefaultColor(Color.GREEN);
plot highestpivot = if show_historical_pivots then hh else nan;
highestpivot.SetDefaultColor(Color.RED);
plot shortoncrossbelow = if show_historical_pivots
then Round((Max(PrevL, highestpivot - (hR * R_Mult))) / ts, 0) * ts
else nan;
shortoncrossbelow.SetDefaultColor(Color.RED);
plot longoncrossabove = if show_historical_pivots
then Round((Min(PrevH, lowestpivot + (lR * R_Mult))) / ts, 0) * ts
else nan;
longoncrossabove.SetDefaultColor(Color.GREEN);
AddCloud(highestpivot,shortoncrossbelow,Color.RED,Color.RED);
AddCloud(lowestpivot,longoncrossabove,Color.GREEN,Color.GREEN);
#~~~~~~~~~~~~~~~~~~~~~~ end historical pivots ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~ begin arrow signals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input show_arrows = yes;
def SOCB = Round((Max(PrevL, hh - (hR * R_Mult))) / ts, 0) * ts;
def LOCA = Round((Min(PrevH, ll + (lR * R_Mult))) / ts, 0) * ts;
def datadn = close[1] > SOCB and close < SOCB;
def dataup = close[1] < LOCA and close > LOCA;
plot arrowdn = if show_arrows then !datadn[1] and datadn else nan;
arrowdn.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
arrowdn.setdefaultcolor(color.magenta);
arrowdn.setlineweight(3);
plot arrowup = if show_arrows then !dataup[1] and dataup else nan;
arrowup.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
arrowup.setdefaultcolor(color.cyan);
arrowup.setlineweight(3);
#~~~~~~~~~~~~~~~~~~~~~~~~~ end arrow signals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~ begin bubble signals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input show_bubbles = yes;
input Buy_Sell_Bubble_Data = {default current, historical};
def short;
def long;
switch (Buy_Sell_Bubble_Data)
{
case historical:
short = hh;
long = ll;
case current:
short = hh_;
long = ll_;
}
def SOCB1 = Round((Max(PrevL, short - (hR * R_Mult))) / ts, 0) * ts;
def LOCA1 = Round((Min(PrevH, long + (lR * R_Mult))) / ts, 0) * ts;
def SE = close[1] > SOCB1 and close <= SOCB1;
def LE = close[1] < LOCA1 and close >= LOCA1;
def exitbuy = l < long[1];
def exitsell = h > short[1];
def holdLE = if LE and !LE[1] then 1 else if !exitbuy then holdLE[1] else 0;
def holdSE = if SE and !SE[1] then 1 else if !exitsell then holdSE[1] else 0;
def enter_long = !holdLE[1] and holdLE;
def enter_short = !holdSE[1] and holdSE;
def exit_long = holdLE[1] and !holdLE;
def exit_short = holdSE[1] and !holdSE;
AddChartBubble(if show_bubbles then enter_long else nan, low, "Enter Long", Color.GREEN, 0);
AddChartBubble(if show_bubbles then enter_short else nan, high, "Enter Short", Color.RED, 1);
AddChartBubble(if show_bubbles then exit_long else nan, high, "Exit Long", Color.GREEN, 1);
AddChartBubble(if show_bubbles then exit_short else nan, low, "Exit Short", Color.RED, 0);
#~~~~~~~~~~~~~~~~~~~~~~~~~~ end bubble signals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~ begin explanation bubbles ~~~~~~~~~~~~~~~~~~~~~~~~~
input explanation_bubbles = yes;
AddChartBubble(explanation_bubbles and x == HighestAll(x), STO, "Entry Line", STO.TakeValueColor(), 0);
AddChartBubble(explanation_bubbles and x == HighestAll(x), BTO, "Entry Line", BTO.TakeValueColor(), 1);
AddChartBubble(explanation_bubbles and x == HighestAll(x), hh_, "Pivot Line", hh_.TakeValueColor(), 1);
AddChartBubble(explanation_bubbles and x == HighestAll(x), ll_, "Pivot Line", ll_.TakeValueColor(), 0);
#~~~~~~~~~~~~~~~~~~~~~~~~ end explanation bubbles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~