L2 Bull-Bear Momentum [blackcat] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
rDITbuF.png

* This is a momentum not trend indicator. I added cloud color and dynamic threshold option
Author Message:
Background
Momentum effect is generally called "inertia effect". Momentum effect was proposed by Jegadeesh and Titman (1993), which refers to the tendency of the return rate of the stock to continue the original direction of movement, that is, the return rate of the stock with a higher return rate in the past period will still be higher than the return rate in the past low-yielding stocks.
Function
The Bullish and Bearish Momentum Technical Indicator is a strategy for buying and selling by analyzing the strength and weakness of recent price trends. Traders seek to take advantage of the rising or falling trend of stock prices. When this technical indicator indicates that the stock is entering a strong upward trend, the trader will buy the stock; Will choose to short the stock.
In short, momentum trading is trading with the trend. Momentum trading is based on the idea that if there is enough momentum behind the current price action, it will continue to move in the same direction. When an asset reaches a higher price, it usually attracts more investor attention, driving up the market price. The price rise continues until sellers start to enter the market consistently, and once sellers slowly outpace buyers, momentum weakens and the trend may reverse.

I have not marked special tags for this indicator usage. Users are expected to define according to their own understanding. On the whole, the basic usage is to start long positions when the first green column appears; when the first red column appears, close long positions or open short positions.

CODE:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// © blackcat1402
#study("[blackcat] L2 Bull-Bear Momentum","L2 Bull-Bear Momentum", overlay=false, max_bars_back=5000, max_labels_count =500)
# Converted by Sam4Cok@Samer800 - 12/2022
declare lower;
declare zerobase;
#//input
input ShowCloud = yes;
input scaler = 1.0;
input DynamicThreshold = yes;
input ManualThreshold = 1.0;
input periodForDynamicThreshold = 100;

#--Calc
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def h = high; def l = low; def c = close; def o =open;
script dev {
input source = close;
input length = 14;
    def mean = Average(source, length);
    def sum;
    sum = fold i = 0 to length - 1 with p do
        p + AbsValue(source[i] - mean);
    def dev = sum/length;
plot retutn = dev;
}
#//algo
def var1c = ExpAverage(100 * (c - Lowest(l[1], 100)) / (Highest(h[1], 100) - Lowest(l[1], 100)), 13) / 4 * scaler;
def var1d = ExpAverage(c, 2) - ExpAverage(c, 89) * scaler;
def var1e = ExpAverage(var1d, 30) * scaler;
def var1f = 2 * (var1d - var1e) * 10 * scaler;
def var20 = Power(var1f, 3) * 0.1 + Power(var1f, 2) * scaler;
def var21 = Sqrt(Sqrt(l * h * o * c));
def var22 = ExpAverage(var21 * 0.97, 3) * scaler;
def var23 = (h + l + c) / 3 * scaler;
def var24 = (var23 - Average(var23, 14)) / (0.015 * dev(var23, 14)) * scaler;
def var25 = If(var1f > 0.015, var20, 0) / 45 * scaler;
def yz = var25 / 10;
def var26 = ExpAverage(c, 2) - ExpAverage(c, 150);
def var27 = ExpAverage(var26, 100);
def var28 = 2 * (var26 - var27);
def var29 = Power(var28, 3) * 0.1 + Power(var28, 1);
def var2a = Sqrt(Sqrt(l * h * o * c));
def var2b = ExpAverage(var2a * 0.97, 3);
def var2c = (h + l + c) / 3;
def var2d = (var2c - Average(var2c, 14)) / (0.015 * dev(var2c, 14));
def zl = If(var28 > 0.1, var29, 0) * 5 / 10;
def bear = ExpAverage((zl + yz), 22);
def bull = If(bear > bear[1], bear, na);

#//plots
def avgBear = stdev(bear,20);
def DynLine = if !DynamicThreshold then na else highest(avgBear, periodForDynamicThreshold);
plot DynamicLine = DynLine;
DynamicLine.SetDefaultColor(Color.ORANGE);

plot thresholdLine = if isNaN(c) or DynamicThreshold then na else ManualThreshold;
thresholdLine.SetDefaultColor(Color.ORANGE);
thresholdLine.SetStyle(Curve.LONG_DASH);

def thresh = if DynamicThreshold then DynamicLine else thresholdLine;
plot bullHist = bull;
bullHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bullHist.AssignValueColor(if thresh>bullHist then CreateColor(143,255,143) else Color.DARK_GREEN);

plot bearHist = if !isNaN(bull) then na else bear;
bearHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bearHist.AssignValueColor(if thresh>bearHist then CreateColor(255,143,143) else Color.DARK_RED);

#--- cloud Color

AddCloud(if !ShowCloud then na else if !isNaN(bearHist) then if thresh<bearHist then pos else neg else na,if thresh<bearHist then neg else pos, Color.DARK_RED, CreateColor(255,143,143));
AddCloud(if !ShowCloud then na else if isNaN(bearHist) then if thresh<bullHist then pos else neg else na, if thresh<bullHist then neg else pos, Color.DARK_GREEN, CreateColor(143,255,143));

#--- END Code
 
Last edited by a moderator:

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

@samer800 Can you help me write a code where the candles would change color whenever the histogram is above or below the dynamic line? This is what I did but it still isn't giving me the proper results:

assignpriceColor(if bullHist > dynamicline then color.Light_GREEN else color.current);
assignpriceColor(if bullHist < dynamicLine then color.RED else color.current);
assignpriceColor(if bearhist > dynamicLine then color.RED else color.current);
assignpriceColor(if bearhist < dynamicLine then color.LIGHT_GREEN else

It colors the candlesticks when the histogram changes colors which isn't my goal
 
@samer800 Can you help me write a code where the candles would change color whenever the histogram is above or below the dynamic line? This is what I did but it still isn't giving me the proper results:

assignpriceColor(if bullHist > dynamicline then color.Light_GREEN else color.current);
assignpriceColor(if bullHist < dynamicLine then color.RED else color.current);
assignpriceColor(if bearhist > dynamicLine then color.RED else color.current);
assignpriceColor(if bearhist < dynamicLine then color.LIGHT_GREEN else

It colors the candlesticks when the histogram changes colors which isn't my goal
add this at the end of the code.

CSS:
input BarColor = yes;
AssignPriceColor(if !BarColor then Color.CURRENT else
                 if !isNaN(bearHist) then
                 if thresh<bearHist then Color.DARK_RED else CreateColor(255,143,143) else
                 if isNaN(bearHist) then
                 if thresh<bullHist then Color.DARK_GREEN else CreateColor(143,255,143) else Color.GRAY);
 
Is it Possible to add high time frame aggression to this ?
try the below.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// © blackcat1402
#study("[blackcat] L2 Bull-Bear Momentum","L2 Bull-Bear Momentum", overlay=false, max_bars_back=5000, max_labels_count =500)
# Converted by Sam4Cok@Samer800 - 12/2022
# Update - Added MTF.
declare lower;
declare zerobase;
#//input
input BarColor = yes;
input ShowCloud = yes;
input scaler = 1.0;
input useChartTime = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
input DynamicThreshold = yes;
input ManualThreshold = 1.0;
input periodForDynamicThreshold = 100;

#--Calc
def na  = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def h; def l; def c; def o;
if useChartTime {
     h = high;
     l = low;
     c = close;
     o = open;
   } else {
     h = high(Period=Aggregation);
     l = low(Period=Aggregation);
     c = close(Period=Aggregation);
     o = open(Period=Aggregation);
}
script dev {
input source = close;
input length = 14;
    def mean = Average(source, length);
    def sum;
    sum = fold i = 0 to length - 1 with p do
        p + AbsValue(source[i] - mean);
    def dev = sum/length;
plot retutn = dev;
}
#//algo
def var1c = ExpAverage(100 * (c - Lowest(l[1], 100)) / (Highest(h[1], 100) - Lowest(l[1], 100)), 13) / 4 * scaler;
def var1d = ExpAverage(c, 2) - ExpAverage(c, 89) * scaler;
def var1e = ExpAverage(var1d, 30) * scaler;
def var1f = 2 * (var1d - var1e) * 10 * scaler;
def var20 = Power(var1f, 3) * 0.1 + Power(var1f, 2) * scaler;
def var21 = Sqrt(Sqrt(l * h * o * c));
def var22 = ExpAverage(var21 * 0.97, 3) * scaler;
def var23 = (h + l + c) / 3 * scaler;
def var24 = (var23 - Average(var23, 14)) / (0.015 * dev(var23, 14)) * scaler;
def var25 = If(var1f > 0.015, var20, 0) / 45 * scaler;
def yz = var25 / 10;
def var26 = ExpAverage(c, 2) - ExpAverage(c, 150);
def var27 = ExpAverage(var26, 100);
def var28 = 2 * (var26 - var27);
def var29 = Power(var28, 3) * 0.1 + Power(var28, 1);
def var2a = Sqrt(Sqrt(l * h * o * c));
def var2b = ExpAverage(var2a * 0.97, 3);
def var2c = (h + l + c) / 3;
def var2d = (var2c - Average(var2c, 14)) / (0.015 * dev(var2c, 14));
def zl = If(var28 > 0.1, var29, 0) * 5 / 10;
def bear = ExpAverage((zl + yz), 22);
def bull = If(bear > bear[1], bear, na);

#//plots
def avgBear = stdev(bear,20);
def DynLine = if !DynamicThreshold then na else highest(avgBear, periodForDynamicThreshold);
plot DynamicLine = DynLine;
DynamicLine.SetDefaultColor(Color.ORANGE);

plot thresholdLine = if isNaN(c) or DynamicThreshold then na else ManualThreshold;
thresholdLine.SetDefaultColor(Color.ORANGE);
thresholdLine.SetStyle(Curve.LONG_DASH);

def thresh = if DynamicThreshold then DynamicLine else thresholdLine;
plot bullHist = bull;
bullHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bullHist.AssignValueColor(if thresh>bullHist then CreateColor(143,255,143) else Color.DARK_GREEN);

plot bearHist = if !isNaN(bull) then na else bear;
bearHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bearHist.AssignValueColor(if thresh>bearHist then CreateColor(255,143,143) else Color.DARK_RED);

#--- cloud Color

AddCloud(if !ShowCloud then na else if !isNaN(bearHist) then if thresh<bearHist then pos else neg else na,if thresh<bearHist then neg else pos, Color.DARK_RED, CreateColor(255,143,143));
AddCloud(if !ShowCloud then na else if isNaN(bearHist) then if thresh<bullHist then pos else neg else na, if thresh<bullHist then neg else pos, Color.DARK_GREEN, CreateColor(143,255,143));

#--- Bar Color

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if !isNaN(bearHist) then
                 if thresh<bearHist then Color.DARK_RED else CreateColor(255,143,143) else
                 if isNaN(bearHist) then
                 if thresh<bullHist then Color.DARK_GREEN else CreateColor(143,255,143) else Color.GRAY);

#--- END Code
 
rDITbuF.png

* This is a momentum not trend indicator. I added cloud color and dynamic threshold option
Author Message:
Background
Momentum effect is generally called "inertia effect". Momentum effect was proposed by Jegadeesh and Titman (1993), which refers to the tendency of the return rate of the stock to continue the original direction of movement, that is, the return rate of the stock with a higher return rate in the past period will still be higher than the return rate in the past low-yielding stocks.
Function
The Bullish and Bearish Momentum Technical Indicator is a strategy for buying and selling by analyzing the strength and weakness of recent price trends. Traders seek to take advantage of the rising or falling trend of stock prices. When this technical indicator indicates that the stock is entering a strong upward trend, the trader will buy the stock; Will choose to short the stock.
In short, momentum trading is trading with the trend. Momentum trading is based on the idea that if there is enough momentum behind the current price action, it will continue to move in the same direction. When an asset reaches a higher price, it usually attracts more investor attention, driving up the market price. The price rise continues until sellers start to enter the market consistently, and once sellers slowly outpace buyers, momentum weakens and the trend may reverse.

I have not marked special tags for this indicator usage. Users are expected to define according to their own understanding. On the whole, the basic usage is to start long positions when the first green column appears; when the first red column appears, close long positions or open short positions.

CODE:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// © blackcat1402
#study("[blackcat] L2 Bull-Bear Momentum","L2 Bull-Bear Momentum", overlay=false, max_bars_back=5000, max_labels_count =500)
# Converted by Sam4Cok@Samer800 - 12/2022
declare lower;
declare zerobase;
#//input
input ShowCloud = yes;
input scaler = 1.0;
input DynamicThreshold = yes;
input ManualThreshold = 1.0;
input periodForDynamicThreshold = 100;

#--Calc
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def h = high; def l = low; def c = close; def o =open;
script dev {
input source = close;
input length = 14;
    def mean = Average(source, length);
    def sum;
    sum = fold i = 0 to length - 1 with p do
        p + AbsValue(source[i] - mean);
    def dev = sum/length;
plot retutn = dev;
}
#//algo
def var1c = ExpAverage(100 * (c - Lowest(l[1], 100)) / (Highest(h[1], 100) - Lowest(l[1], 100)), 13) / 4 * scaler;
def var1d = ExpAverage(c, 2) - ExpAverage(c, 89) * scaler;
def var1e = ExpAverage(var1d, 30) * scaler;
def var1f = 2 * (var1d - var1e) * 10 * scaler;
def var20 = Power(var1f, 3) * 0.1 + Power(var1f, 2) * scaler;
def var21 = Sqrt(Sqrt(l * h * o * c));
def var22 = ExpAverage(var21 * 0.97, 3) * scaler;
def var23 = (h + l + c) / 3 * scaler;
def var24 = (var23 - Average(var23, 14)) / (0.015 * dev(var23, 14)) * scaler;
def var25 = If(var1f > 0.015, var20, 0) / 45 * scaler;
def yz = var25 / 10;
def var26 = ExpAverage(c, 2) - ExpAverage(c, 150);
def var27 = ExpAverage(var26, 100);
def var28 = 2 * (var26 - var27);
def var29 = Power(var28, 3) * 0.1 + Power(var28, 1);
def var2a = Sqrt(Sqrt(l * h * o * c));
def var2b = ExpAverage(var2a * 0.97, 3);
def var2c = (h + l + c) / 3;
def var2d = (var2c - Average(var2c, 14)) / (0.015 * dev(var2c, 14));
def zl = If(var28 > 0.1, var29, 0) * 5 / 10;
def bear = ExpAverage((zl + yz), 22);
def bull = If(bear > bear[1], bear, na);

#//plots
def avgBear = stdev(bear,20);
def DynLine = if !DynamicThreshold then na else highest(avgBear, periodForDynamicThreshold);
plot DynamicLine = DynLine;
DynamicLine.SetDefaultColor(Color.ORANGE);

plot thresholdLine = if isNaN(c) or DynamicThreshold then na else ManualThreshold;
thresholdLine.SetDefaultColor(Color.ORANGE);
thresholdLine.SetStyle(Curve.LONG_DASH);

def thresh = if DynamicThreshold then DynamicLine else thresholdLine;
plot bullHist = bull;
bullHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bullHist.AssignValueColor(if thresh>bullHist then CreateColor(143,255,143) else Color.DARK_GREEN);

plot bearHist = if !isNaN(bull) then na else bear;
bearHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
bearHist.AssignValueColor(if thresh>bearHist then CreateColor(255,143,143) else Color.DARK_RED);

#--- cloud Color

AddCloud(if !ShowCloud then na else if !isNaN(bearHist) then if thresh<bearHist then pos else neg else na,if thresh<bearHist then neg else pos, Color.DARK_RED, CreateColor(255,143,143));
AddCloud(if !ShowCloud then na else if isNaN(bearHist) then if thresh<bullHist then pos else neg else na, if thresh<bullHist then neg else pos, Color.DARK_GREEN, CreateColor(143,255,143));

#--- END Code
Hi, @samer800

I do not know who you are but thank you for all the work and coding you are doing. Because of your hard work with indicator from TV to TOS, it has helped me alot!! You are always good responding to requests! Truly appreciate all the work! Keep it up!..

P.s I have very little knowledge to none for coding. I'm in Healthcare field.
 
Hi, @samer800

I do not know who you are but thank you for all the work and coding you are doing. Because of your hard work with indicator from TV to TOS, it has helped me alot!! You are always good responding to requests! Truly appreciate all the work! Keep it up!..

P.s I have very little knowledge to none for coding. I'm in Healthcare field.
thanks bro. I am not into coding as well.. I just learned little recently.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
445 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