Ehler's Instantaneous Trend [LazyBear] For ThinkOrSwim

armybender

Active member
I converted the LazyBear Ehler's Instantaneous Trend. This is used in another study on this forum, but it is not available as a standalone study, so I thought I would add it.

Full disclosure - I don't use this study, but found the math interesting and decided to study it a bit, so I converted it into a ToS study.


1689983038776.png


Ruby:
#Ehlers Instantaneous Trend [LazyBear]
#Converted from https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/

#DECLARATIONS
declare upper;


#USER INPUTS
input price = hl2;
input alpha = 0.07;
input fillTrend = no;
input colorBars = no;
input showCloud = no;


### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));


#CALCULATIONS
def alpha2 = Power(alpha, 2);
def it = ( alpha - ( ( alpha2 ) / 4.0 ) ) * price + 0.5 * alpha2 * price[1] - ( alpha - 0.75 * alpha2 ) * price[2] + 2 * ( 1 - alpha ) * ( if IsNaN( it[1] ) then ( ( price + 2 * price[1] + price[2] ) / 4.0 ) else it[1] ) - ( 1 - alpha ) * ( 1 - alpha ) * ( if IsNaN( it[2] ) then ( ( price + 2 * price[1] + price[2] ) / 4.0 ) else it[2] );
def lag = 2.0 * it - if IsNaN(it[2]) then 0 else it[2];


#PLOTS
plot instTrend = it;
plot instTrendLag = 2.0 * it - (if IsNaN(it[2]) then 0 else it[2]);


#CLOUDS
AddCloud(if showCloud then instTrendLag else Double.NaN, instTrend, GlobalColor("Green"), GlobalColor("Red"));


#BAR COLORING
AssignPriceColor(
    if colorBars then
        if it > it[1] then GlobalColor("Green")
        else if it < it[1] then GlobalColor("Red")
        else GlobalColor("Gray")
    else Color.CURRENT
);


#FORMATTING
instTrend.AssignValueColor(
    if it > it[1] then GlobalColor("Green")
    else if it < it[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrend.HideTitle();
instTrend.HideBubble();
instTrend.SetLineWeight(1);

instTrendLag.AssignValueColor(
    if lag > lag[1] then GlobalColor("Green")
    else if lag < lag[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrendLag.HideTitle();
instTrendLag.HideBubble();
instTrendLag.SetLineWeight(1);
 
Last edited:
Nice. Ehler provided EasyLanguage code for the instantaneous trendline (iTrend) in Cybernetic Analysis for Stocks and Futures. In the EasyLanguage code, he handles the first 7 bars differently than the rest using the following line of code:
Code:
If currentbar < 7 then ITrend = (Price + 2*Price[1] + Price[2]) / 4;
You can recreate this in thinkscript using the CompoundValue function, or an if/then block using BarNumber() < 7. The code using CompoundValue and if/then to define it and lag is below. This removes the ~150 bars of ramp up your posted version has before the instantaneous trendline converges to correct values.

Code using CompoundValue:
Code:
def it = CompoundValue(6,
                 (alpha - alpha * alpha / 4) * price +
                 .5 * alpha * alpha * price[1] -
                 (alpha - .75 * alpha * alpha) * price[2] +
                 2 * (1 - alpha) * it[1] -
                 (1 - alpha) * (1 - alpha) * it[2],
            (price + 2 * price[1] + price[2]) / 4);

def lag = 2.0 * it - it[2];

Code using if/then
Code:
def it;
if barNumber() < 7 {
 it = (price + 2 * price[1] + price[2]) / 4;
}
else{
 it = (alpha - alpha * alpha / 4) * price +
       .5 * alpha * alpha * price[1] -
       (alpha - .75 * alpha * alpha) * price[2] +
       2 * (1 - alpha) * it[1] -
       (1 - alpha) * (1 - alpha) * it[2];
}

def lag = 2.0 * it - it[2];


I really like Ehler's stuff. I've converted most of his published papers on mesasoftware.com, TASC, and his books to thinkscript.
 
Nice. Ehler provided EasyLanguage code for the instantaneous trendline (iTrend) in Cybernetic Analysis for Stocks and Futures. In the EasyLanguage code, he handles the first 7 bars differently than the rest using the following line of code:
Code:
If currentbar < 7 then ITrend = (Price + 2*Price[1] + Price[2]) / 4;
You can recreate this in thinkscript using the CompoundValue function, or an if/then block using BarNumber() < 7. The code using CompoundValue and if/then to define it and lag is below. This removes the ~150 bars of ramp up your posted version has before the instantaneous trendline converges to correct values.

Code using CompoundValue:
Code:
def it = CompoundValue(6,
                 (alpha - alpha * alpha / 4) * price +
                 .5 * alpha * alpha * price[1] -
                 (alpha - .75 * alpha * alpha) * price[2] +
                 2 * (1 - alpha) * it[1] -
                 (1 - alpha) * (1 - alpha) * it[2],
            (price + 2 * price[1] + price[2]) / 4);

def lag = 2.0 * it - it[2];

Code using if/then
Code:
def it;
if barNumber() < 7 {
 it = (price + 2 * price[1] + price[2]) / 4;
}
else{
 it = (alpha - alpha * alpha / 4) * price +
       .5 * alpha * alpha * price[1] -
       (alpha - .75 * alpha * alpha) * price[2] +
       2 * (1 - alpha) * it[1] -
       (1 - alpha) * (1 - alpha) * it[2];
}

def lag = 2.0 * it - it[2];


I really like Ehler's stuff. I've converted most of his published papers on mesasoftware.com, TASC, and his books to thinkscript.
Hi Bigboss, I have no code experience. where would I place that code in the original posted above. thanks
 
Has anyone added arrows at the crossovers?
@csricksdds @3AMBH here is code that incorporates the change I posted above, and implements arrows.

Code:
#Ehlers Instantaneous Trend [LazyBear]
#Converted from https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/
# v1 ToS Conversion by armybender
# v2 bigboss - 1. Implemented arrows and input to control them
#              2. Removed unused input "fillTrend"
#              3. Cleaned up Calculation of it and lag to accomodate null 
#                 data in the first 6 bars, per Ehler's EasyLanguage code.

#DECLARATIONS
declare upper;

#USER INPUTS
input price = hl2;
input alpha = 0.07;
input colorBars = no;
input showCloud = no;
input showArrows = no;

### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));

#CALCULATIONS
def it = CompoundValue(6,
                 (alpha - alpha * alpha / 4) * price +
                 .5 * alpha * alpha * price[1] -
                 (alpha - .75 * alpha * alpha) * price[2] +
                 2 * (1 - alpha) * it[1] -
                 (1 - alpha) * (1 - alpha) * it[2],
            (price + 2 * price[1] + price[2]) / 4);
#PLOTS
plot instTrend = it;
plot instTrendLag = 2.0 * it - it[2];

#ARROWS
plot upArrow = if showArrows and it > it[1] and it[1] < it[2] then 1 else 0;
plot downArrow = if showArrows and it < it[1] and it[1] > it[2] then 1 else 0;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(GlobalColor("Green"));
upArrow.SetLineWeight(5);

downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downArrow.SetDefaultColor(GlobalColor("Red"));
downArrow.SetLineWeight(5);

#CLOUDS
AddCloud(if showCloud then instTrendLag else Double.NaN,  if showCloud then instTrend else Double.NaN,  GlobalColor("Green"),  GlobalColor("Red"));

#BAR COLORING
AssignPriceColor(
    if colorBars then
        if it > it[1] then GlobalColor("Green")
        else if it < it[1] then GlobalColor("Red")
        else GlobalColor("Gray")
    else Color.CURRENT
);


#FORMATTING
instTrend.AssignValueColor(
    if it > it[1] then GlobalColor("Green")
    else if it < it[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrend.HideTitle();
instTrend.HideBubble();
instTrend.SetLineWeight(1);

instTrendLag.AssignValueColor(
    if instTrendLag > instTrendLag[1] then GlobalColor("Green")
    else if instTrendLag < instTrendLag[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrendLag.HideTitle();
instTrendLag.HideBubble();
instTrendLag.SetLineWeight(1);
 
@csricksdds @3AMBH here is code that incorporates the change I posted above, and implements arrows.

Code:
#Ehlers Instantaneous Trend [LazyBear]
#Converted from https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/
# v1 ToS Conversion by armybender
# v2 bigboss - 1. Implemented arrows and input to control them
#              2. Removed unused input "fillTrend"
#              3. Cleaned up Calculation of it and lag to accomodate null
#                 data in the first 6 bars, per Ehler's EasyLanguage code.

#DECLARATIONS
declare upper;

#USER INPUTS
input price = hl2;
input alpha = 0.07;
input colorBars = no;
input showCloud = no;
input showArrows = no;

### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));

#CALCULATIONS
def it = CompoundValue(6,
                 (alpha - alpha * alpha / 4) * price +
                 .5 * alpha * alpha * price[1] -
                 (alpha - .75 * alpha * alpha) * price[2] +
                 2 * (1 - alpha) * it[1] -
                 (1 - alpha) * (1 - alpha) * it[2],
            (price + 2 * price[1] + price[2]) / 4);
#PLOTS
plot instTrend = it;
plot instTrendLag = 2.0 * it - it[2];

#ARROWS
plot upArrow = if showArrows and it > it[1] and it[1] < it[2] then 1 else 0;
plot downArrow = if showArrows and it < it[1] and it[1] > it[2] then 1 else 0;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(GlobalColor("Green"));
upArrow.SetLineWeight(5);

downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downArrow.SetDefaultColor(GlobalColor("Red"));
downArrow.SetLineWeight(5);

#CLOUDS
AddCloud(if showCloud then instTrendLag else Double.NaN,  if showCloud then instTrend else Double.NaN,  GlobalColor("Green"),  GlobalColor("Red"));

#BAR COLORING
AssignPriceColor(
    if colorBars then
        if it > it[1] then GlobalColor("Green")
        else if it < it[1] then GlobalColor("Red")
        else GlobalColor("Gray")
    else Color.CURRENT
);


#FORMATTING
instTrend.AssignValueColor(
    if it > it[1] then GlobalColor("Green")
    else if it < it[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrend.HideTitle();
instTrend.HideBubble();
instTrend.SetLineWeight(1);

instTrendLag.AssignValueColor(
    if instTrendLag > instTrendLag[1] then GlobalColor("Green")
    else if instTrendLag < instTrendLag[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrendLag.HideTitle();
instTrendLag.HideBubble();
instTrendLag.SetLineWeight(1);
Thanks
 
I like what you did to improve this but in looking at it I think a vertical line at the crossover would be a better addition than the arrows as it would be more visual?

This update has vertical lines. Turn it on and off using the input showVerticalLines

Code:
#Ehlers Instantaneous Trend [LazyBear]
#Converted from https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/
# v1 ToS Conversion by armybender
#
# v2 bigboss - 1. Implemented arrows and input to control them
#              2. Removed unused input "fillTrend"
#              3. Cleaned up Calculation of it and lag to accomodate null 
#                 data in the first 6 bars, per Ehler's EasyLanguage code.
#
# v3 bigboss   1. Added Vertical Lines and an input to control their display.

#DECLARATIONS
declare upper;

#USER INPUTS
input price = hl2;
input alpha = 0.07;
input colorBars = no;
input showCloud = no;
input showArrows = no;
input showVerticalLines = no;

### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));

#CALCULATIONS
def it = CompoundValue(6,
                 (alpha - alpha * alpha / 4) * price +
                 .5 * alpha * alpha * price[1] -
                 (alpha - .75 * alpha * alpha) * price[2] +
                 2 * (1 - alpha) * it[1] -
                 (1 - alpha) * (1 - alpha) * it[2],
            (price + 2 * price[1] + price[2]) / 4);
#PLOTS
plot instTrend = it;
plot instTrendLag = 2.0 * it - it[2];

def buySignal = it > it[1] and it[1] < it[2];
def sellSignal = it < it[1] and it[1] > it[2];

#ARROWS
plot upArrow = if showArrows and buySignal then 1 else 0;
plot downArrow = if showArrows and sellSignal then 1 else 0;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(GlobalColor("Green"));
upArrow.SetLineWeight(5);

downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downArrow.SetDefaultColor(GlobalColor("Red"));
downArrow.SetLineWeight(5);

# VERTICAL LINES
AddVerticalLine(showVerticalLines and buySignal, "BUY",GlobalColor("Green"));
AddVerticalLine(showVerticalLines and sellSignal, "SELL",GlobalColor("Red"));


#CLOUDS
AddCloud(if showCloud then instTrendLag else Double.NaN,  if showCloud then instTrend else Double.NaN,  GlobalColor("Green"),  GlobalColor("Red"));

#BAR COLORING
AssignPriceColor(
    if colorBars then
        if it > it[1] then GlobalColor("Green")
        else if it < it[1] then GlobalColor("Red")
        else GlobalColor("Gray")
    else Color.CURRENT
);


#FORMATTING
instTrend.AssignValueColor(
    if it > it[1] then GlobalColor("Green")
    else if it < it[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrend.HideTitle();
instTrend.HideBubble();
instTrend.SetLineWeight(1);

instTrendLag.AssignValueColor(
    if instTrendLag > instTrendLag[1] then GlobalColor("Green")
    else if instTrendLag < instTrendLag[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);

instTrendLag.HideTitle();
instTrendLag.HideBubble();
instTrendLag.SetLineWeight(1);
 
This update has vertical lines. Turn it on and off using the input showVerticalLines

Code:
#Ehlers Instantaneous Trend [LazyBear]
#Converted from https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/
# v1 ToS Conversion by armybender
#
# v2 bigboss - 1. Implemented arrows and input to control them
#              2. Removed unused input "fillTrend"
#              3. Cleaned up Calculation of it and lag to accomodate null
#                 data in the first 6 bars, per Ehler's EasyLanguage code.
#
# v3 bigboss   1. Added Vertical Lines and an input to control their display.

#DECLARATIONS
declare upper;

#USER INPUTS
input price = hl2;
input alpha = 0.07;
input colorBars = no;
input showCloud = no;
input showArrows = no;
input showVerticalLines = no;

### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));

#CALCULATIONS
def it = CompoundValue(6,
                 (alpha - alpha * alpha / 4) * price +
                 .5 * alpha * alpha * price[1] -
                 (alpha - .75 * alpha * alpha) * price[2] +
                 2 * (1 - alpha) * it[1] -
                 (1 - alpha) * (1 - alpha) * it[2],
            (price + 2 * price[1] + price[2]) / 4);
#PLOTS
plot instTrend = it;
plot instTrendLag = 2.0 * it - it[2];

def buySignal = it > it[1] and it[1] < it[2];
def sellSignal = it < it[1] and it[1] > it[2];

#ARROWS
plot upArrow = if showArrows and buySignal then 1 else 0;
plot downArrow = if showArrows and sellSignal then 1 else 0;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(GlobalColor("Green"));
upArrow.SetLineWeight(5);

downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downArrow.SetDefaultColor(GlobalColor("Red"));
downArrow.SetLineWeight(5);

# VERTICAL LINES
AddVerticalLine(showVerticalLines and buySignal, "BUY",GlobalColor("Green"));
AddVerticalLine(showVerticalLines and sellSignal, "SELL",GlobalColor("Red"));


#CLOUDS
AddCloud(if showCloud then instTrendLag else Double.NaN,  if showCloud then instTrend else Double.NaN,  GlobalColor("Green"),  GlobalColor("Red"));

#BAR COLORING
AssignPriceColor(
    if colorBars then
        if it > it[1] then GlobalColor("Green")
        else if it < it[1] then GlobalColor("Red")
        else GlobalColor("Gray")
    else Color.CURRENT
);


#FORMATTING
instTrend.AssignValueColor(
    if it > it[1] then GlobalColor("Green")
    else if it < it[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);
instTrend.HideTitle();
instTrend.HideBubble();
instTrend.SetLineWeight(1);

instTrendLag.AssignValueColor(
    if instTrendLag > instTrendLag[1] then GlobalColor("Green")
    else if instTrendLag < instTrendLag[1] then GlobalColor("Red")
    else GlobalColor("Gray")
);

instTrendLag.HideTitle();
instTrendLag.HideBubble();
instTrendLag.SetLineWeight(1);
Thanks...you're a gentleman and a scholar!
 
I like this indicator and the changes "bigboss" made by adding arrows and vertical lines! I modified some of the colors, lines and wording to better suit the look for my charting as follows:

#Ehlers Instantaneous Trend [LazyBear]
#Converted from https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/
# v1 ToS Conversion by armybender
#
# v2 bigboss - 1. Implemented arrows and input to control them
# 2. Removed unused input "fillTrend"
# 3. Cleaned up Calculation of it and lag to accomodate null
# data in the first 6 bars, per Ehler's EasyLanguage code.
#
# v3 bigboss 1. Added Vertical Lines and an input to control their display.
#Colors and vertical lines modified by Charles Ricks 7-27-23. Changed cloud transitions from Buy/Sell to CROSSOVER UP and CROSSOVER DOWN.

#DECLARATIONS
declare upper;

#USER INPUTS
input price = hl2;
input alpha = 0.07;
input colorBars = yes;
input showCloud = yes;
input showArrows = yes;
input showVerticalLines = yes;

### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 255, 0));
DefineGlobalColor("Red", CreateColor(225, 0, 0));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));

#CALCULATIONS
def it = CompoundValue(6,
(alpha - alpha * alpha / 4) * price +
.5 * alpha * alpha * price[1] -
(alpha - .75 * alpha * alpha) * price[2] +
2 * (1 - alpha) * it[1] -
(1 - alpha) * (1 - alpha) * it[2],
(price + 2 * price[1] + price[2]) / 4);
#PLOTS
plot instTrend = it;
plot instTrendLag = 2.0 * it - it[2];

def buySignal = it > it[1] and it[1] < it[2];
def sellSignal = it < it[1] and it[1] > it[2];

#ARROWS
plot upArrow = if showArrows and buySignal then 1 else 0;
plot downArrow = if showArrows and sellSignal then 1 else 0;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(GlobalColor("Green"));
upArrow.SetLineWeight(5);

downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downArrow.SetDefaultColor(GlobalColor("Red"));
downArrow.SetLineWeight(5);

# VERTICAL LINES
AddVerticalLine(showVerticalLines and buySignal, "CROSSOVER UP", Color.Green, curve.LONG_DASH);
AddVerticalLine(showVerticalLines and sellSignal, "CROSSOVER DOWN",Color.Red, curve.LONG_DASH);


#CLOUDS
AddCloud(if showCloud then instTrendLag else Double.NaN, if showCloud then instTrend else Double.NaN, GlobalColor("Green"), GlobalColor("Red"));

#BAR COLORING
AssignPriceColor(
if colorBars then
if it > it[1] then GlobalColor("Green")
else if it < it[1] then GlobalColor("Red")
else GlobalColor("Gray")
else Color.CURRENT
);


#FORMATTING
instTrend.AssignValueColor(
if it > it[1] then GlobalColor("Green")
else if it < it[1] then GlobalColor("Red")
else GlobalColor("Gray")
);
instTrend.HideTitle();
instTrend.HideBubble();
instTrend.SetLineWeight(1);

instTrendLag.AssignValueColor(
if instTrendLag > instTrendLag[1] then GlobalColor("Green")
else if instTrendLag < instTrendLag[1] then GlobalColor("Red")
else GlobalColor("Gray")
);

instTrendLag.HideTitle();
instTrendLag.HideBubble();
instTrendLag.SetLineWeight(1);
 

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
499 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