FVG Instantaneous Mitigation Signals [LuxAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
CNJcuq5.png

Author Message:

The FVG Instantaneous Mitigation Signals indicator detects and highlights "instantaneously" mitigated fair value gaps (FVG), that is FVGs that get mitigated one bar after their creation, returning signals upon mitigation.

Take profit/stop loss areas, as well as a trailing stop loss are also included to complement the signals.

Read More: https://www.tradingview.com/v/xYpl5UdE/

CODE:

CSS:
#// Indicator for TOS
#// © LuxAlgo
#indicator("FVG Instantaneous Mitigation Signals [LuxAlgo]", "LuxAlgo - FVG Instantaneous Mitigation Signals",
# converted by Sam4Cok@Samer800    - 08/2024
input colorInTradeBars = yes;
input FvgWidthFilter = 0.0;     # 'FVG Width Filter'
input showTpArea = no;          # 'TP Area'
input tpMultiplier = 4.0;       # 'Multiplier'
input showSlArea = no;          # 'SL Area'
input slMultiplier = 2.0;       # 'Multiplier'
#//Trailing Stop
input ResetTrailingStop = {default "Every Signals", "Inverse Signals"}; # 'Reset Trailing Stop'
input TrailingStopMultiplier = 3.0; #, 'Multiplier'
#//Style
input showBullishImfvg = yes; #(true, 'Bullish IMFVG', inline = 'bull', group = 'Style')
input showBullishAverage = yes; #(true, 'Average', inline = 'bull', group = 'Style')
input showBearishImfvg = yes; # 'Bearish IMFVG', inline = 'bear', group = 'Style')
input showBearishAverage = yes; # 'Average', inline = 'bear', group = 'Style')

def na = Double.NaN;
def all = ResetTrailingStop==ResetTrailingStop."Every Signals";
DefineGlobalColor("up", Color.LIGHT_GREEN); #CreateColor(0, 137, 123));
DefineGlobalColor("dn", Color.DOWNTICK); #CreateColor(242, 54, 69));

script tpsl {
    input condition = close;
    input opposite_condition = no;
    input level = close;
    input islong = yes;
    input tpMult = 4;
    input slMult = 2;
    input showTp = yes;
    input showSl = yes;
    def na = Double.NaN;
    def atr = ATR(Length = 200);
    def tp_area;
    def sl_area;
    def entry;
    def reached1;
    def reached;
    if condition {
        if islong {
            entry   = level;
            tp_area = if showTp then level + atr * tpMult else na;
            sl_area = if showSl then level - atr * slMult else na;
        } else {
            entry   = level;
            tp_area = if showTp then level - atr * tpMult else na;
            sl_area = if showSl then level + atr * slMult else na;
        }
        reached1 = no;
    } else {
        reached1 = if opposite_condition then yes else reached[1];
        entry   = if (showTp or showSl) then entry[1] else na;
        tp_area = if showTp then tp_area[1] else na;
        sl_area = if showSl then sl_area[1] else na;
    }
    if islong {
        reached = if high > tp_area or low < sl_area then yes else reached1;
    } else {
        reached = if low < tp_area or high > sl_area then yes else reached1;
    }
    plot enter = if !reached then entry else na;
    plot tp = if !reached then tp_area else na;
    plot sl = if !reached then sl_area else na;
    plot reach = reached;
}
script trailing_stop {
    input trigger = 0;
    input trend = 0;
    input mult = 3.0;
    def atr = ATR(Length = 200);
    def nATR = atr * mult;
    def up = close - nATR;
    def dn = close + nATR;
    def ts;
    def reached;
    if trigger {
        ts = if trend == 1 then up else dn;
#             if trend == 0 then dn else ts[1];
        reached = no;
    } else {
        ts = if trend == 1 then
             if close - ts[1] > nATR then up else ts[1] else
#             if trend == 0 then
             if ts[1] - close > nATR then dn else ts[1]; # else ts[1];
        reached = if close < ts and trend == 1 then yes else
                  if close > ts and trend == 0 then yes else reached[1];
    }
    plot trailing = if ts then ts else Double.NaN;
    plot reach = if !isNaN(reached) then reached else no;
}
script filter {
    input a = high;
    input b = low;
    input filterWidth = 0;
    def nATR = ATR(Length = 200) * filterWidth;
    def filter = (a - b) > nATR ;
    plot out = filter;
}
#//Signals
def l = low;
def l1 = low[1];
def l2 = low[2];
def l3 = low[3];
def h = high;
def h1 = high[1];
def h2 = high[2];
def h3 = high[3];
def c = close;
def c2 = close[2];
def filterBull = filter(l3, h1, FvgWidthFilter);
def filterBear = filter(l1, h3, FvgWidthFilter);
def bull = l3 > h1 and c2 < l3 and c > l3 and filterBull and showBullishImfvg;
def bear = l1 > h3 and c2 > h3 and c < h3 and filterBear and showBearishImfvg;
def avgBull = (l3 + h1) / 2;
def avgBear = (l1 + h3) / 2;
#//Bullish Signals
def cntBull;
def bull_line;
def impBullTop;
def impBullBot;
def labelBull;
def bull_lvl_reached;
#//Set bullish tpsl zones
if bull {
    cntBull    = 0;
    impBullTop = h1;
    impBullBot = l3;
    bull_line  = if showBullishAverage then avgBull else na;
    labelBull  = l;
} else {
    cntBull    = cntBull[1] + 1;
    impBullTop = if cntBull > 3 then na else impBullTop[1];
    impBullBot = if cntBull > 3 then na else impBullBot[1];
    bull_line  = if bull_lvl_reached[1] then na else bull_line[1];
    labelBull  = no;
}
    bull_lvl_reached = !showBullishAverage or close < bull_line;
#//Set bearish tpsl zones
def cntBear;
def bear_line;
def impBearTop;
def impBearBot;
def labelBear;
def bear_lvl_reached;
if bear {
    cntBear    = 0;
    impBearTop = h3;
    impBearBot = l1;
    bear_line  = if showBearishAverage then avgBear else na;
    labelBear  = h;
} else {
    cntBear    = cntBear[1] + 1;
    impBearTop = if cntBear > 3 then na else impBearTop[1];
    impBearBot = if cntBear > 3 then na else impBearBot[1];
    bear_line  = if bear_lvl_reached[1] then na else bear_line[1];
    labelBear  = no;
}
    bear_lvl_reached = !showBearishAverage or close > bear_line;
#//Trailing Stop
def ImbalanceBullTop = impBullTop[-3];
def ImbalanceBullBot = impBullBot[-3];
def ImbalanceBearTop = impBearTop[-3];
def ImbalanceBearBot = impBearBot[-3];

def bull_Entry   = tpsl(bull, bear, avgBull, yes, tpMultiplier, slMultiplier, showTpArea, showSlArea).enter;
def bull_TP      = tpsl(bull, bear, avgBull, yes, tpMultiplier, slMultiplier, showTpArea, showSlArea).tp;
def bull_SL      = tpsl(bull, bear, avgBull, yes, tpMultiplier, slMultiplier, showTpArea, showSlArea).sl;
def bull_reached = tpsl(bull, bear, avgBull, yes, tpMultiplier, slMultiplier, showTpArea, showSlArea).reach;
def bear_Entry   = tpsl(bear, bull, avgBear, no,  tpMultiplier, slMultiplier, showTpArea, showSlArea).enter;
def bear_TP      = tpsl(bear, bull, avgBear, no,  tpMultiplier, slMultiplier, showTpArea, showSlArea).tp;
def bear_SL      = tpsl(bear, bull, avgBear, no,  tpMultiplier, slMultiplier, showTpArea, showSlArea).sl;
def bear_reached = tpsl(bear, bull, avgBear, no,  tpMultiplier, slMultiplier, showTpArea, showSlArea).reach;

def os = if bull then 1 else if bear then 0 else os[1];
def ts_reset = if all then (bull or bear) else (os != os[1]);

def ts = trailing_stop(ts_reset, os, TrailingStopMultiplier).trailing;
def ts_reached = trailing_stop(ts_reset, os, TrailingStopMultiplier).reach;
def tsCond = ts_reached or bull or bear;

plot TrailingStop = if tsCond then na else ts;
plot avgBullLine = if cntBull[-1]!=0 and bull_line then bull_line else na;
plot avgBearLine = if cntBear[-1]!=0 and bear_line then bear_line else na;
avgBullLine.SetStyle(Curve.SHORT_DASH);
avgBearLine.SetStyle(Curve.SHORT_DASH);
avgBullLine.SetDefaultColor(Color.CYAN);
avgBearLine.SetDefaultColor(Color.MAGENTA);

plot entryBull = if bull_Entry then bull_Entry else na;
plot tpBull = if bull_TP then bull_TP else na;
plot slBull = if bull_SL then bull_SL else na;
plot entryBear = if bear_Entry then bear_Entry else na;
plot tpBear = if bear_TP then bear_TP else na;
plot slBear = if bear_SL then bear_SL else na;

TrailingStop.AssignValueColor(if os then GlobalColor("up") else GlobalColor("dn"));
entryBull.SetDefaultColor(Color.DARK_GRAY);
entryBear.SetDefaultColor(Color.DARK_GRAY);
tpBull.SetDefaultColor(Color.DARK_GREEN);
tpBear.SetDefaultColor(Color.DARK_GREEN);
slBull.SetDefaultColor(Color.DARK_RED);
slBear.SetDefaultColor(Color.DARK_RED);

AddCloud(tpBull, entryBull, Color.DARK_GREEN);
AddCloud(entryBull, slBull, Color.DARK_RED);

AddCloud(entryBear, tpBear, Color.DARK_GREEN);
AddCloud(slBear, entryBear, Color.DARK_RED);

AddCloud(ImbalanceBullTop, ImbalanceBullBot, Color.CYAN, Color.CYAN, yes);
AddCloud(ImbalanceBearTop, ImbalanceBearBot, Color.MAGENTA, Color.MAGENTA, yes);

AddChartBubble(labelBull, labelBull, "B", Color.GREEN, no);
AddChartBubble(labelBear, labelBear, "S", Color.RED);

def barCol = if ts_reached then 0 else
             if os == 1 and !bull_reached then 1 else
             if os == 0 and !bear_reached then -1 else 0;

AssignPriceColor(if !colorInTradeBars then Color.CURRENT else
                 if barCol > 0 then Color.CYAN else
                 if barCol < 0 then Color.MAGENTA else Color.CURRENT);

#-- END of CODE
 

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