PMF (Power Momentum Formula) for ThinkorSwim

isaacramsay

New member
2019 Donor
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.

7KNZ3Mw.png


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:
Any chance you can confirm the delay and if you have made any adjustments to this indicator since posting? Seems to work well once triggered. Im just wondering how long until confirmation bar is plotted.

Thank you in advance!
 
Thanks for sharing, curios if you were able to make further progress on the strategy?
 
Hey isaac I'm new to trading for about 2 months now and I love your indicator. Its pin point accurate. However I am alittle concerned I think there may be a broken code written somewhere within it. When I try to use it in live virtual trading, it makes the price action chart not move. Like the bars with go up and down but it wont show a trend across the screen like it normally those. Only when I take it off will the chart move again. So I've been using it when i think there could be a possible divergence i would install the script to see where then uninstall it when I'm not using it. Could you please please fix it. I really love this indicator.....thank you so much

It don't work properly in on-demand so I'm having to install and uninstall when I need to use it and when I dont
 
@craig81 There are several indicators or setups that don't play well with OnDemand... Renko Bars and Range Bars just freeze after a few seconds, if they work at all, and some MTF indicators... Everything is hit and miss with OnDemand... I tried it again last week and gave up within a few minutes...
 
Seems to work without on demand but I cant practice with it.

You could use PaperTrading during trading hours... I have a second install of TOS that I use if I really want to test but it still isn't like live trading...
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
411 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top