Here is the Power Momentum Formula, a Momentum Divergence study/strategy. So far, it checks for the high/low over the last 60 bars, and if the Momentum(28) is not a high/low to go along with the price high/low and they're more than 6 bars apart, then lines are drawn. It still needs a little work, and I still have to add the trigger point (trade entry) conditions with alerts and potential profit ranges, then make it into a strategy.
Upper Indicator
Code:
declare upper;
# DEFS
def length = 28;
def barsback = 60;
def bn = BarNumber();
input showarrows = yes;
# MOMENTUM
def mom = close - close[length];
# PRICE BULLISH
plot low60 = Lowest(low, barsback); low60.setdefaultColor(color.dark_green);
def pricelowest = low == Lowest(low, barsback);# and low < low[1] and low < low[-1];
def pricelowBN = if pricelowest then bn else pricelowBN[1];
# PRICE BEARISH
plot high60 = Highest(high, barsback); high60.setdefaultColor(color.dark_red);
def pricehighest = high == Highest(high, barsback);
def pricehighBN = if pricehighest then bn else pricelowBN[1];
# BULLISH MOMENTUM
def momlowest = Lowest(mom, barsback);
#AddLabel(yes, "momlowest: " + momlowest, Color.GRAY);
def momlowBN = if mom == momlowest then bn else momlowBN[1];
# BEARISH MOMENTUM
def momhighest = Highest(mom, barsback);
def momhighBN = if mom == momhighest then bn else momhighBN[1];
# BULLISH SETUP
def bullA = GetValue(low, BarNumber() - pricelowBN, 0);
def bullB = GetValue(mom, BarNumber() - pricelowBN, 0);
def bullC = GetValue(mom, BarNumber() - momlowBN, 0);
def bullD = GetValue(low, BarNumber() - momlowBN, 0);
#def bullE = if pricelowBN > momlowbn then highest(mom[momlowbn:pricelowBN] else highest(mom[pricelowBN:momlowbn];
def bullsetup =
AbsValue(momlowBN - pricelowBN) > 5 and
bullA < bullD and
bullB > bullC and
pricelowest and pricelowBN > pricelowBN[1] and
low < low[1] and low < low[-1] and
mom != momlowest and
mom < mom[1] and mom < mom[-1];
#bullsetup.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#bullsetup.setdefaultcolor(Color.MAGENTA);
AddLabel(bullsetup, "PMF bullish setup", Color.DARK_GREEN);
AddLabel(bullsetup, "priceLowBN: " + pricelowBN, Color.GRAY);
AddLabel(bullsetup, "momlowBN: " + momlowBN, Color.GRAY);
AddLabel(bullsetup, "bullA: " + bullA, Color.GRAY);
AddLabel(bullsetup, "bullB: " + bullB, Color.GRAY);
AddLabel(bullsetup, "bullC: " + bullC, Color.GRAY);
AddLabel(bullsetup, "bullD: " + bullD, Color.GRAY);
#addchartBubble(pricelowest and bullsetup, low, "A", color.green, yes);
#addchartBubble(mom == momlowest and bullsetup, low, "D", color.green, yes);
# BEARISH SETUP
def bearA = GetValue(high, BarNumber() - pricehighBN, 0);
def bearB = GetValue(mom, BarNumber() - pricehighBN, 0);
def bearC = GetValue(mom, BarNumber() - momhighBN, 0);
def bearD = GetValue(high, BarNumber() - momhighBN, 0);
def bearsetup = AbsValue(pricehighBN - momhighBN) > 5 and
bearA > bearD and
bearB < bearC and
pricehighest and pricehighBN > pricehighBN[1] and
high > high[1] and high > high[-1]
and mom != momhighest and
mom > mom[1] and mom > mom[-1];
AddLabel(bearsetup, "PMF bullish setup", Color.DARK_GREEN);
AddLabel(bearsetup, "pricehighBN: " + pricehighBN, Color.GRAY);
AddLabel(bearsetup, "momhighBN: " + momhighBN, Color.GRAY);
AddLabel(bearsetup, "bearA: " + bearA, Color.GRAY);
AddLabel(bearsetup, "bearB: " + bearB, Color.GRAY);
AddLabel(bearsetup, "bearC: " + bearC, Color.GRAY);
AddLabel(bearsetup, "bearD: " + bearD, Color.GRAY);
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
# BULL DIV LINE
def cnV = fold i = 1 to lastBar
with b = Double.NaN
while IsNaN(b)
do if GetValue(bullsetup, -i) == 1 then i else Double.NaN;
def startBdiv = if pricelowBN > momlowBN then momlowBN else pricelowBN ;
def startBullDiv = BarNumber() == GetValue(startBdiv, -cnV);
def isBullDiv = if startBullDiv then 1 else if bullsetup[1] then 0 else isBullDiv[1];
def mBull = if startBullDiv then (GetValue(low, -cnV) - low) / cnV else mBull[1];
def line1 = if startBullDiv then low - ATR() / 2 else line1[1] + mBull;
plot bullDivLine = if isBullDiv and !bullsetup then line1 else Double.NaN;
bullDivLine.SetDefaultColor(Color.GREEN);
bullDivLine.SetLineWeight(2);
# BEAR DIV LINE
def cnA = fold j = 1 to lastBar
with c = Double.NaN
while IsNaN(c)
do if GetValue(bearsetup, -j) == 1 then j else Double.NaN;
def initbear = if pricehighBN > momhighBN then momhighBN else pricehighBN;
def startBearDiv = BarNumber() == GetValue(initbear, -cnA);
def isBearDiv = if startBearDiv then 1 else if bearsetup[1] then 0 else isBearDiv[1];
def mBear = if startBearDiv then (GetValue(high, -cnA) - high) / cnA else mBear[1];
def line2 = if startBearDiv then high + ATR() / 2 else line2[1] + mBear;
plot bearDivLine = if isBearDiv and !bearsetup then line2 else Double.NaN;
bearDivLine.SetDefaultColor(Color.RED);
bearDivLine.SetLineWeight(2);
# divergence arrows
plot bullArrow = if bullsetup then bullDivLine[1] + mBull[1] else Double.NaN;
bullArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bullArrow.SetLineWeight(2);
bullArrow.SetDefaultColor(Color.UPTICK);
bullArrow.SetHiding(!showarrows);
plot beaArrow = if bearsetup then bearDivLine[1] + mBear[1] else Double.NaN;
beaArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
beaArrow.SetLineWeight(2);
beaArrow.SetDefaultColor(Color.PINK);
beaArrow.SetHiding(!showarrows);
Lower Indicator
Code:
AssignBackgroundColor(Color.BLACK);
input length = 28;
input barsback = 60;
plot zeroline = 0;
plot mom = close - close[length];
plot highest = Highest(mom, barsback);
plot lowest = Lowest(mom, barsback);
mom.SetDefaultColor(Color.WHITE);
zeroline.SetDefaultColor(Color.GRAY);
highest.SetDefaultColor(Color.DARK_RED);
lowest.SetDefaultColor(Color.DARK_GREEN);
#plot x = if mom == lowest and mom < mom[1] and mom < mom[-1] then -.5 else 0;
#x.SetDefaultColor(Color.GREEN);
#plot y = if mom == highest and mom > mom[1] and mom > mom[-1] then .5 else 0;
#y.SetDefaultColor(Color.RED);
declare upper;
# DEFS
def bn = BarNumber();
input showarrows = yes;
# PRICE BULLISH
def low60 = Lowest(low, barsback);
def pricelowest = low == Lowest(low, barsback);# and low < low[1] and low < low[-1];
def pricelowBN = if pricelowest then bn else pricelowBN[1];
# PRICE BEARISH
def high60 = Highest(high, barsback);
def pricehighest = high == Highest(high, barsback);
def pricehighBN = if pricehighest then bn else pricelowBN[1];
# BULLISH MOMENTUM
def momlowest = Lowest(mom, barsback);
#AddLabel(yes, "momlowest: " + momlowest, Color.GRAY);
def momlowBN = if mom == momlowest then bn else momlowBN[1];
# BEARISH MOMENTUM
def momhighest = Highest(mom, barsback);
def momhighBN = if mom == momhighest then bn else momhighBN[1];
# BULLISH SETUP
def bullA = GetValue(low, BarNumber() - pricelowBN, 0);
def bullB = GetValue(mom, BarNumber() - pricelowBN, 0);
def bullC = GetValue(mom, BarNumber() - momlowBN, 0);
def bullD = GetValue(low, BarNumber() - momlowBN, 0);
#def bullE = if pricelowBN > momlowbn then highest(mom[momlowbn:pricelowBN] else highest(mom[pricelowBN:momlowbn];
def bullsetup =
AbsValue(momlowBN - pricelowBN) > 5 and
bullA < bullD and
bullB > bullC and
pricelowest and pricelowBN > pricelowBN[1] and
low < low[1] and low < low[-1] and
mom != momlowest and
mom < mom[1] and mom < mom[-1];
#bullsetup.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#bullsetup.setdefaultcolor(Color.MAGENTA);
AddLabel(bullsetup, "PMF bullish setup", Color.DARK_GREEN);
AddLabel(bullsetup, "priceLowBN: " + pricelowBN, Color.GRAY);
AddLabel(bullsetup, "momlowBN: " + momlowBN, Color.GRAY);
AddLabel(bullsetup, "bullA: " + bullA, Color.GRAY);
AddLabel(bullsetup, "bullB: " + bullB, Color.GRAY);
AddLabel(bullsetup, "bullC: " + bullC, Color.GRAY);
AddLabel(bullsetup, "bullD: " + bullD, Color.GRAY);
#addchartBubble(pricelowest and bullsetup, low, "A", color.green, yes);
#addchartBubble(mom == momlowest and bullsetup, low, "D", color.green, yes);
# BEARISH SETUP
def bearA = GetValue(high, BarNumber() - pricehighBN, 0);
def bearB = GetValue(mom, BarNumber() - pricehighBN, 0);
def bearC = GetValue(mom, BarNumber() - momhighBN, 0);
def bearD = GetValue(high, BarNumber() - momhighBN, 0);
def bearsetup = AbsValue(pricehighBN - momhighBN) > 5 and
bearA > bearD and
bearB < bearC and
pricehighest and pricehighBN > pricehighBN[1] and
high > high[1] and high > high[-1]
and mom != momhighest and
mom > mom[1] and mom > mom[-1];
AddLabel(bearsetup, "PMF bullish setup", Color.DARK_GREEN);
AddLabel(bearsetup, "pricehighBN: " + pricehighBN, Color.GRAY);
AddLabel(bearsetup, "momhighBN: " + momhighBN, Color.GRAY);
AddLabel(bearsetup, "bearB: " + bearB, Color.GRAY);
AddLabel(bearsetup, "bearC: " + bearC, Color.GRAY);
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
# calculate bull div line
def cnV = fold i = 1 to lastBar
with b = Double.NaN
while IsNaN(b)
do if GetValue(bullsetup, -i) == 1 then i else Double.NaN;
def startBdiv = if pricelowBN > momlowBN then momlowBN else pricelowBN ;
def startBullDiv = BarNumber() == GetValue(startBdiv, -cnV);
def isBullDiv = if startBullDiv then 1 else if bullsetup[1] then 0 else isBullDiv[1];
def mBull = if startBullDiv then (GetValue(mom, -cnV) - mom) / cnV else mBull[1];
def line1 = if startBullDiv then mom - ATR() / 2 else line1[1] + mBull;
plot bullDivLine = if isBullDiv and !bullsetup then line1 else Double.NaN;
bullDivLine.SetDefaultColor(Color.GREEN);
bullDivLine.SetLineWeight(2);
# calculate bear div line
def cnA = fold j = 1 to lastBar
with c = Double.NaN
while IsNaN(c)
do if GetValue(bearsetup, -j) == 1 then j else Double.NaN;
def initbear = if pricehighBN > momhighBN then momhighBN else pricehighBN;
def startBearDiv = BarNumber() == GetValue(initbear, -cnA);
def isBearDiv = if startBearDiv then 1 else if bearsetup[1] then 0 else isBearDiv[1];
def mBear = if startBearDiv then (GetValue(mom, -cnA) - mom) / cnA else mBear[1];
def line2 = if startBearDiv then mom + ATR() / 2 else line2[1] + mBear;
plot bearDivLine = if isBearDiv and !bearsetup then line2 else Double.NaN;
bearDivLine.SetDefaultColor(Color.RED);
bearDivLine.SetLineWeight(2);
# divergence arrows
plot bullArrow = if bullsetup then bullDivLine[1] + mBull[1] else Double.NaN;
bullArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bullArrow.SetLineWeight(2);
bullArrow.SetDefaultColor(Color.UPTICK);
bullArrow.SetHiding(!showarrows);
plot beaArrow = if bearsetup then bearDivLine[1] + mBear[1] else Double.NaN;
beaArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
beaArrow.SetLineWeight(2);
beaArrow.SetDefaultColor(Color.PINK);
beaArrow.SetHiding(!showarrows);
Last edited by a moderator: