Open Close Cross Alerts "OCC - NoRepaint" for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
message from the Author
// Description:
// ============
// Strategy based around Open-Close Crossovers. NRP is the Non repaint version.
// * USE AT YOUR OWN RISK *

PnLoJmt.png


CODE BELOW

CSS:
#// Description:
#//  Strategy based around Open-Close Crossovers. NRP is the Non repaint version.
#//  *** USE AT YOUR OWN RISK ***
#// Copyright 2017, 2019 JustUncleL
#https://www.tradingview.com/script/BLY1wfiD-Open-Close-Cross-Alerts-NoRepaint-Version-by-JustUncleL/
#// The GNU General Public License can be found here
#// <http://www.gnu.org/licenses/>.
#study(title="Open Close Cross Alerts NoRepaint Version by JustUncleL"
#// Revision:        NRP
#// Author:          JustIncleL
# Converted and Mod by Sam4COK @ Samer800 - 08/2022
declare lower;
#// === INPUTS ===
input useResolution = yes;          #"Use Alternate Resolution?"
input MultiplierForResolution = 6;  #"Multiplier for Alernate Resolution"
input basisType  = {default "ZEMA", "VAR", "SMA", "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMMA", "HullMA",  "SSMA", "TMA"};
input Signal = {default Filter1 , Filter2}; #MA Period"
input MAPeriod     = 8;        #"MA Period"
input DivChannel   = no;    #"Show Divergence Channel"
input ChannelWidth = 0.5;  #"Divergence Channel Width Factor (Stddev)"
input Divergence   = no;    #"Show Regular Divergence"

#// === /INPUTS ===
def na = Double.NaN;
def intRes = if useResolution then MultiplierForResolution else 1;
script nz {
    input data  = 1;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
########### Theme 1################
DefineGlobalColor("green100"   , CreateColor(0, 128, 0));
DefineGlobalColor("lime100"    , CreateColor(0, 255, 0));
DefineGlobalColor("red100"     , CreateColor(255, 0, 0));
DefineGlobalColor("blue100"    , CreateColor(0, 0, 255));
DefineGlobalColor("darkred100" , CreateColor(139, 0, 0));
DefineGlobalColor("aqua100"    , CreateColor(0,255,255));
DefineGlobalColor("gray100"    , CreateColor(128,128,128));
######################
#// SuperSmoother filter
#// © 2013  John F. Ehlers
#variant_supersmoother(src, len) =>
script variant_supersmoother {
    input src = close;
    input len = 0;
    def a1 = Exp(-1.414 * 3.14159 / len);
    def b1 = 2 * a1 * Cos(1.414 * 3.14159 / len);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def v9 = c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2]);
    plot result = v9;
}
#variant_smoothed(src, len) =>
script variant_smoothed {
    input src = close;
    input len = 0;
    def sma_1 = SimpleMovingAvg(src, len);
    def v5 = if IsNaN(v5[1]) then sma_1 else (v5[1] * (len - 1) + src) / len;
    plot resullt = v5;
}
#variant_zerolagema(src, len) =>
script variant_zerolagema {
    input src = close;
    input len = 0;
    def lag = (len - 1) / 2;
    def emaSrc = src + src - src[lag];
    def v10 = ExpAverage(emaSrc, len);
    plot result = v10;
}
#variant_doubleema(src, len) =>
script variant_doubleema {
    input src = close;
    input len = 0;
    def v2 = ExpAverage(src, len);
    def v6 = 2 * v2 - ExpAverage(v2, len);
    plot result = v6;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#variant_tripleema(src, len) =>
script variant_tripleema {
    input src = close;
    input len = 0;
    def v2 = ExpAverage(src, len);
    def v7 = 3 * (v2 - ExpAverage(v2, len)) + ExpAverage(ExpAverage(v2, len), len);
    plot result = v7;
}
#variant_lag(p, g) =>
script variant_lag {
    input p = 0;
    input g = 0;
    def L0;
    def L1;
    def L2;
    def L3;
    L0 = (1 - g) * p + g * nz(L0[1]);
    L1 = -g * L0 + nz(L0[1]) + g * nz(L1[1]);
    L2 = -g * L1 + nz(L1[1]) + g * nz(L2[1]);
    L3 = -g * L2 + nz(L2[1]) + g * nz(L3[1]);
    def f = (L0 + 2 * L1 + 2 * L2 + L3) / 6;
    plot result = f;
}
#variant(type, src, len) =>
script variant {
    input type = "ZEMA";
    input src = close;
    input len = 0;
    def ema_1 = ExpAverage(src, len);
    def wma_1 = WMA(src, len);
    def vwma_1 = vwma(src, len);
    def variant_smoothed__1 = variant_smoothed(src, len);
    def variant_doubleema__1 = variant_doubleema(src, len);
    def variant_tripleema__1 = variant_tripleema(src, len);
    def wma_2 = WMA(src, len / 2);
    def wma_3 = WMA(src, len);
    def wma_4 = WMA(2 * wma_2 - wma_3, Round(Sqrt(len)));
    def variant_supersmoother__1 = variant_supersmoother(src, len);
    def variant_zerolagema__1 = variant_zerolagema(src, len);
    def sma_1 = SimpleMovingAvg(src, len);
    def sma_2 = SimpleMovingAvg(sma_1, len);
    def sma_3 = SimpleMovingAvg(src, len);
    def ma = if type == "EMA" then ema_1 else if type == "WMA" then wma_1 else
  if type == "VWMA" then vwma_1 else if type == "SMMA" then variant_smoothed__1 else
  if type == "DEMA" then variant_doubleema__1 else if type == "TEMA" then  variant_tripleema__1 else 
  if type == "HullMA" then wma_4 else if type == "SSMA" then variant_supersmoother__1 else
  if type == "ZEMA" then variant_zerolagema__1 else if type == "TMA" then sma_2 else sma_3;
    plot result = ma;
}
#// === SERIES SETUP ===
def closeSeries = variant(basisType, close, MAPeriod);
def openSeries = variant(basisType, open, MAPeriod);
def closeOpenAvg = (closeSeries + openSeries) / 2;
#// Simulate Alternate resolution Series by increasing MA period.
def closeSeriesAlt = variant(basisType, close, MAPeriod * intRes);
def openSeriesAlt = variant(basisType, open, MAPeriod * intRes);
#// === /SERIES ===
#// === ALERT ===
def xlong = closeSeriesAlt crosses above openSeriesAlt;
def xshort = closeSeriesAlt crosses below openSeriesAlt;
def longCond = if Signal == Signal.Filter1 then xlong else
if isNan(longCond[1]) then (xlong or xlong[1]) and close>closeSeriesAlt and close>=open else na;
def shortCond = if Signal == Signal.Filter1 then xshort else
if isNan(shortCond[1])then (xshort or xshort[1]) and close<closeSeriesAlt and close<=open else na;

#// === /ALERT conditions
#// === PLOTTING ===
def diff = closeSeriesAlt - openSeriesAlt;
def pcd = 50000.0 * diff / closeOpenAvg;
def alert = longCond or shortCond;
def trendColour = if closeSeriesAlt > openSeriesAlt then 1 else 0;

plot ZeroLine = 0;
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(GlobalColor("gray100"));
ZeroLine.HideTitle();
plot pcdLine = pcd;                        #"OCC Difference Factor"
pcdLine.AssignValueColor(if DivChannel then GlobalColor("gray100") else if trendColour > 0 then GlobalColor("lime100") else GlobalColor("red100"));

plot Dots = if alert then -20 else na;     # "OCC Alert Plot"
Dots.SetPaintingStrategy(PaintingStrategy.POINTS);
Dots.AssignValueColor(if trendColour > 0 then GlobalColor("lime100") else GlobalColor("red100"));
Dots.SetLineWeight(4);
Dots.HideTitle();

AddCloud(pcd, ZeroLine, GlobalColor("green100"), GlobalColor("darkred100") , no);
#//  ||  Functions
#f_top_fractal(_src) =>
script f_top_fractal {
 input _src = close;
    def top_fractal =  _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0];
    plot return = top_fractal;
}
#f_bot_fractal(_src) =>
script f_bot_fractal {
   input _src = close;
    def bot_fractal = _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0];
    plot result = bot_fractal;
}
#f_fractalize(_src) =>
script f_fractalize {
    input _src = close;
    def bot_fractal__1 = f_bot_fractal(_src);
    def fractalize = if f_top_fractal(_src) then 1 else if bot_fractal__1 then -1 else 0;
    plot return = fractalize;
}
def rsi_high = pcd;
def rsi_low = pcd;
def offset_ = ChannelWidth * StDev(pcd, 20);
def fractal_top_rsi = if f_fractalize(rsi_high) > 0 then rsi_high[2] else na;
def fractal_bot_rsi = if f_fractalize(rsi_low) < 0 then rsi_low[2] else na;
def top_rsi = fractal_top_rsi + offset_;
def bot_rsi = fractal_bot_rsi - offset_;

plot RSI_HD = if DivChannel then top_rsi[-2] else na;
plot RSI_LD = if DivChannel then bot_rsi[-2] else na;
RSI_HD.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_HD.SetDefaultColor(GlobalColor("gray100"));
RSI_HD.SetLineWeight(2);
RSI_HD.HideTitle();
RSI_LD.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_LD.SetDefaultColor(GlobalColor("gray100"));
RSI_LD.SetLineWeight(2);
RSI_LD.HideTitle();
#--------------------------------------------------------------
def LastUP = CompoundValue(1, if isNaN(RSI_HD) then RSI_HD[1] else RSI_HD, 0);
def LastDN = CompoundValue(1, if isNan(RSI_LD) then RSI_LD[1] else RSI_LD, 0);
#--------------------------------------------------------------
plot channelUP = LastUP;
channelup.SetStyle(Curve.FIRM);
channelup.SetDefaultColor(Color.RED);
channelup.EnableApproximation();
channelUP.HideTitle();
plot channeldn = LastDN;
channeldn.SetStyle(Curve.FIRM);
channeldn.SetDefaultColor(Color.GREEN);
channeldn.EnableApproximation();
channeldn.HideTitle();

##### DIVERGANCE
    def bar = BarNumber();
    def n = 20;
    def CurrH = if pcd > 0
                then fold i = 1 to Floor(n / 2)
                with p = 1
                while p
                do pcd > GetValue(pcd, -i)
                else 0;

    def CurrPivotH = if (bar > n and
                         pcd == Highest(pcd, Floor(n / 2)) and CurrH) then pcd else na;

    def CurrL = if pcd < 0
                then fold j = 1 to Floor(n / 2)
                with q = 1
                while q
                do pcd < GetValue(pcd, -j)
                else 0;

    def CurrPivotL =  if (bar > n and
                         pcd == Lowest(pcd, Floor(n / 2)) and CurrL) then pcd else na;

    def CurrPHBar = if !IsNaN(CurrPivotH) then bar else CurrPHBar[1];
    def CurrPLBar = if !IsNaN(CurrPivotL) then bar else CurrPLBar[1];

    def PHpoint = if !IsNaN(CurrPivotH) then CurrPivotH else PHpoint[1];
    def PLpoint = if !IsNaN(CurrPivotL) then CurrPivotL else PLpoint[1];

    def priorPHBar = if PHpoint != PHpoint[1] then CurrPHBar[1] else priorPHBar[1];
    def priorPLBar = if PLpoint != PLpoint[1] then CurrPLBar[1] else priorPLBar[1];

    def HighPivots = bar >= HighestAll(priorPHBar);
    def LowPivots  = bar >= HighestAll(priorPLBar);

    def pivotHigh = if HighPivots then CurrPivotH else na;
    def pivotLow  = if LowPivots  then CurrPivotL else na;

    plot PlotHline = if Divergence then pivotHigh else na;
    PlotHline.EnableApproximation();
    PlotHline.SetDefaultColor(Color.RED);
    PlotHline.SetStyle(Curve.SHORT_DASH);
    PlotHline.HideTitle();

    plot PlotLline = if Divergence then pivotLow else na;
    PlotLline.EnableApproximation();
    PlotLline.SetDefaultColor(Color.LIME);
    PlotLline.SetStyle(Curve.SHORT_DASH);
    PlotLline.HideTitle();

    plot PivotDot = if Divergence then if !IsNaN(pivotHigh) then pivotHigh else
                if !IsNaN(pivotLow)  then pivotLow  else na else na;
    PivotDot.SetDefaultColor(Color.YELLOW);
    PivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
    PivotDot.SetLineWeight(3);
    PivotDot.HideTitle();

#//eof
 

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