Market Structure Oscillator [LuxAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
MO1z0mA.png


Author Message:
The Market Structure Oscillator indicator analyzes and synthesizes short-term, intermediate-term, and long-term market structure shifts and breaks, visualizing the output as oscillators of real-time market structures.
The oscillator presentation of the detected market structures helps traders visualize trend momentum and strength, identifying potential trend reversals, and providing different perspectives to enhance the analysis of classic market structures.

Read More: https://www.tradingview.com/script/K47z5Kmf-Market-Structure-Oscillator-LuxAlgo/

CODE -

CSS:
# // Indicator for TOS
#// © LuxAlgo
#indicator('Market Structure Oscillator [LuxAlgo]', 'LuxAlgo - Market Structure Oscillator'
# converted by Sam4Cok@Samer800    - 08/2024

Declare lower;

input colorBars      = yes;
input lookbackPeriod = 2;
input ShortTermWeight        = 1.0;     # "Short Term Weight"
input IntermediateTermWeight = 3.0;     # "Intermediate Term Weight"
input LongTermWeight         = 2.0;     # "Long Term Weight"
input showMarketStructureOsc = yes;     # "Market Structure Oscillator"
input OscillatorSmoothing = 4;          # "Oscillator Smoothing"
input showCycleHistogram  = yes;        # "Cycle Oscillator - Histogram"
input CycleSmoothing = 7;               # "Cycle Signal Length"
input showShortTermOsc  = no;           # "Short Term Oscillator"
input showIntermTermOsc = no;           # "Intermediate Term Oscillator"
input showLongTermOsc   = no;           # "Long Term Oscillator"

def na = Double.NaN;
def last = isNaN(close);

#-- Colors
DefineGlobalColor("up", CreateColor(12, 107, 224));
DefineGlobalColor("dn", CreateColor(246, 147, 0));
DefineGlobalColor("dup", CreateColor(8, 71, 150));
DefineGlobalColor("ddn", CreateColor(167, 100, 0));
#-- Func
Script queryPatterns {
input lastPrice = close;
input midPrice = close;
input prevPrice = close;
input isSwingHigh = yes;
    def hi = prevPrice < midPrice and midPrice >= lastPrice;
    def lo = prevPrice > midPrice and midPrice <= lastPrice;
    def queryPatterns = if isSwingHigh then hi else lo;
    plot out = queryPatterns;
}
Script marketStructure {
input src = close;
input Patterns = yes;
input isSwingHigh = yes;
    def cond;
    def isCrossed;
    def lastPrice;
    def midPrice;
    def prevPrice;
if Patterns {
    prevPrice = midPrice[1];
    midPrice  = lastPrice[1];
    lastPrice = src;
    isCrossed = no;
    cond = no;
} else if if(isSwingHigh, close > lastPrice[1], close < lastPrice[1]) and !isCrossed[1] {
    prevPrice = prevPrice[1];
    midPrice  = midPrice[1];
    lastPrice = lastPrice[1];
    isCrossed = yes;
    cond = yes;
    } else {
    prevPrice = prevPrice[1];
    midPrice  = midPrice[1];
    lastPrice = lastPrice[1];
    isCrossed = isCrossed[1];
    cond = no;
}
    plot dir = cond;
    plot lastSrc = lastPrice;
    plot midSrc  = midPrice;
    plot prevSrc = prevPrice;
}
script normalize {
    input buy = yes;
    input sell = no;
    input smooth = 4;
    def os  = if buy then 1 else if sell then -1 else os[1];
    def max = if !max[1] then close else
              if os > os[1] then close else
              if os < os[1] then max[1] else Max(close, max[1]);
    def min = if !min[1] then close else
              if os < os[1] then close else
              if os > os[1] then min[1] else Min(close, min[1]);
    def dif = (close - min) / (max - min);
    def normalize = Average(dif, smooth) * 100;
    plot out = if isNaN(normalize) then 0 else normalize;
}
def n2 = Max(lookbackPeriod, 0);
def n1 = floor(n2 / 2);
#-- Short
def sBullC    = queryPatterns(high, high[n1], high[n2], yes);
def sBull     = marketStructure(high[n1], sBullC, yes).dir;
def sBullLast = marketStructure(high[n1], sBullC, yes).lastSrc;
def sBullMid  = marketStructure(high[n1], sBullC, yes).midSrc;
def sBullPrev = marketStructure(high[n1], sBullC, yes).prevSrc;
#--------
def sBearC    = queryPatterns(low, low[n1], low[n2], no);
def sBear     = marketStructure(low[n1], sBearC, no).dir;
def sBearLast = marketStructure(low[n1], sBearC, no).lastSrc;
def sBearMid  = marketStructure(low[n1], sBearC, no).midSrc;
def sBearPrev = marketStructure(low[n1], sBearC, no).prevSrc;
#-- intrerm
def iBullC    = queryPatterns(sBullLast, sBullMid, sBullPrev, yes);
def iBullCond = iBullC and iBullC!=iBullC[1];
def iBull     = marketStructure(sBullMid, iBullCond, yes).dir;
def iBullLast = marketStructure(sBullMid, iBullCond, yes).lastSrc;
def iBullMid  = marketStructure(sBullMid, iBullCond, yes).midSrc;
def iBullPrev = marketStructure(sBullMid, iBullCond, yes).prevSrc;
#--------
def iBearC    = queryPatterns(sBearLast, sBearMid, sBearPrev, no);
def iBearCond = iBearC and iBearC!=iBearC[1];
def iBear     = marketStructure(sBearMid, iBearCond, no).dir;
def iBearLast = marketStructure(sBearMid, iBearCond, no).lastSrc;
def iBearMid  = marketStructure(sBearMid, iBearCond, no).midSrc;
def iBearPrev = marketStructure(sBearMid, iBearCond, no).prevSrc;
#-- Long
def lBullC    = queryPatterns(iBullLast, iBullMid, iBullPrev, yes);
def lBullCond = lBullC and lBullC!=lBullC[1];
def lBull     = marketStructure(iBullMid, lBullCond, yes).dir;
#--------
def lBearC    = queryPatterns(iBearLast, iBearMid, iBearPrev, no);
def lBearCond = lBearC and lBearC!=lBearC[1];
def lBear     = marketStructure(iBearMid, lBearCond, no).dir;

#-- Normalize
def stVal1 = normalize(sbull, sbear, OscillatorSmoothing);
def stVal2 = normalize(ibull, ibear, OscillatorSmoothing);
def stVal3 = normalize(lbull, lbear, OscillatorSmoothing);
def stValue1 = if isNaN(stVal1) then stValue1[1] else stVal1;
def stValue2 = if isNaN(stVal2) then stValue2[1] else stVal2;
def stValue3 = if isNaN(stVal3) then stValue3[1] else stVal3;
def Value1 = if isNaN(stVal1) then 0 else 1;
def Value2 = if isNaN(stVal2) then 0 else 1;
def Value3 = if isNaN(stVal3) then 0 else 1;

def nom = (ShortTermWeight * stValue1 + IntermediateTermWeight * stValue2 + LongTermWeight * stValue3);
def dnom = (ShortTermWeight * Value1 + IntermediateTermWeight * Value2 + LongTermWeight * Value3);
def msOSC  = nom / dnom;
def ema = ExpAverage(msOSC, CycleSmoothing) - 50;
def cycle = msOSC - ema;
def cycleFast = if showCycleHistogram then cycle else 50;
def up = !last and showCycleHistogram and cycleFast > 50;
def dn = !last and showCycleHistogram and cycleFast < 50;
def colOsc = if isNaN(msOSC) then 0 else if msOSC > 100 then 100 else
                                         if msOSC < 0 then 0 else msOSC * 2.55;
# Plot the new Chart
plot msPlot = if !last and showMarketStructureOsc then msOSC else na; # 'Market Structure Oscillator'
msPlot.SetLineWeight(2);
msPlot.AssignValueColor(CreateColor(255 - colOsc, colOsc, 0));

#-- Histogram
AddChart(open = if up then cycleFast else na, high = 50 , low = 50 , close = if up then 50 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("up"));

AddChart(open = if dn then 50 else na, high = 50 , low = 50 , close = if dn then cycleFast else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("dn"));

#-- MT
plot shortTerm  = if !last and showShortTermOsc then stValue1 else na;  # 'Short Term Oscillator'
plot intermTerm = if !last and showIntermTermOsc then stValue2 else na; # 'Intermediate Term Oscillator'
plot longTerm   = if !last and showLongTermOsc then stValue3 else na;   # 'Long Term Oscillator'
shortTerm.SetDefaultColor(Color.DARK_GREEN);
intermTerm.SetDefaultColor(Color.GRAY);
longTerm.SetDefaultColor(Color.PLUM);

#-- Levels
def lvlCond = !last and (showMarketStructureOsc or showShortTermOsc or showIntermTermOsc or showLongTermOsc);

plot midLine  = if lvlCond then 50 else na;   # 'Equilibrium Level'
def oscTop    = if lvlCond then 100 else na;  # 'OSC Top'
def upperBand = if lvlCond then 85 else na;   # 'Overbought Level'
def lowerBand = if lvlCond then 15 else na;   # 'Oversold Level'
def oscBtm    = if lvlCond then 0 else  na;   # 'OSC Bottom'
midLine.SetPaintingStrategy(PaintingStrategy.DASHES);
midLine.AssignValueColor(if up then GlobalColor("up") else
                         if dn then GlobalColor("dn") else Color.DARK_GRAY);

#-- Clouds
AddCloud(oscTop, upperBand, Color.DARK_RED);
AddCloud(lowerBand, oscBtm, Color.DARK_GREEN);
AddCloud(msPlot, midLine, GlobalColor("dup"), GlobalColor("ddn"));

#-- Bar Color
def msOSCsig = sign(msOSC - 50);
def cycleSig = sign(cycleFast - 50);
def msSigUp = (msOSCsig - msOSCsig[1]) > 0;
def msSigDn = (msOSCsig - msOSCsig[1]) < 0;

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if msSigUp then Color.CYAN else
                 if msSigDn then Color.MAGENTA else CreateColor(255 - colOsc, colOsc, 0));

#-- 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
240 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