Support Resistance Levels script testing

Svanoy

Expert
VIP
Lifetime
ESU23 9/11/23 18:57
Oudo620.png
 

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

Here is what I've been working with.
Two studies plotting past moving average crosses above and below the open at 6:00p.m. EST for the following night and day.
Using a 30 day 10 min chart.
It has its limitations, but very interesting results.
Working on a improved version now.

UYzqKnT.png

Ruby:
#Support/Resistance Above
declare once_per_bar;

input Time_Of_First_Bar_Of_Day = 1800;
input spacing = 10;

def ADOpen = CompoundValue(1, if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then open else ADOpen[1], 0);
def BarOpen = CompoundValue(1, if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then AbsValue(BarNumber()) else BarOpen[1], 0);

def SMA100 = reference SimpleMovingAvg(close, 100);
def SMA200 = reference SimpleMovingAvg(close, 200);
def SMA500 = reference SimpleMovingAvg(close, 500);
def a = (SMA100+ SMA200+ SMA500) / 3;

def Cross = if Crosses(SMA100, SMA200, CrossingDirection.ANY) then SMA200 else if Crosses(SMA100, SMA500, CrossingDirection.ANY) then SMA500 else if Crosses(SMA200, SMA500, CrossingDirection.ANY) then SMA500 else if Crosses(SMA100, a, CrossingDirection.ANY) then a else if Crosses(SMA200, a, CrossingDirection.ANY) then a else if Crosses(SMA500, a, CrossingDirection.ANY) then a else Cross[1];

#######################################################################################
#Define/Plot Levels..##################################################################
#######################################################################################
script Targets {
    input Time_Of_First_Bar_Of_Day = 0;
    input BarOpen = 0;
    input Cross = 0;
    input ADOpen = 0;
    input Spacing = 0;   
  
    def TA = if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 and !isnan(close) then
            fold a = 0 to BarOpen
            with inta = Double.POSITIVE_INFINITY
            do if IsNaN(GetValue(Cross,a)) then inta
            else if GetValue(Cross,a) < ADOpen then inta
            else if GetValue(Cross,a) > inta then inta
            else if GetValue(Cross,a) < inta and GetValue(Cross,a) > ADOpen + Spacing
            then GetValue(Cross,a)
            else inta
            else TA[1];
    plot Targets = TA;
}

################################
plot PAC1 = Targets(Time_Of_First_Bar_Of_Day,BarOpen,Cross,ADOpen,0);
PAC1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC1.SetLineWeight(1);

plot PAC2 = Targets(Time_Of_First_Bar_Of_Day,BarOpen,Cross,PAC1,Spacing);
PAC2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC2.SetLineWeight(1);

plot PAC3 = Targets(Time_Of_First_Bar_Of_Day,BarOpen,Cross,PAC2,Spacing);
PAC3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC3.SetLineWeight(1);

plot PAC4 = Targets(Time_Of_First_Bar_Of_Day,BarOpen,Cross,PAC3,Spacing);
PAC4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC4.SetLineWeight(1);

plot PAC5 = Targets(Time_Of_First_Bar_Of_Day,BarOpen,Cross,PAC4,Spacing);
PAC5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC5.SetLineWeight(1);

Ruby:
#Support/Resistance Below
declare once_per_bar;

input Time_Of_First_Bar_Of_Day = 1800;
input spacing = 10;

def ADOpen = CompoundValue(1, if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then open else ADOpen[1], 0);
def BarOpen = CompoundValue(1, if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then AbsValue(BarNumber()) else BarOpen[1], 0);

def SMA100 = reference SimpleMovingAvg(close, 100);
def SMA200 = reference SimpleMovingAvg(close, 200);
def SMA500 = reference SimpleMovingAvg(close, 500);
def a = (SMA100 + SMA200 + SMA500) / 3;

def Cross = if Crosses(SMA100, SMA200, CrossingDirection.ANY) then SMA200 else if Crosses(SMA100, SMA500, CrossingDirection.ANY) then SMA500 else if Crosses(SMA200, SMA500, CrossingDirection.ANY) then SMA500 else if Crosses(SMA100, a, CrossingDirection.ANY) then a else if Crosses(SMA200, a, CrossingDirection.ANY) then a else if Crosses(SMA500, a, CrossingDirection.ANY) then a else Cross[1];

#######################################################################################
#Define/Plot Levels..##################################################################
#######################################################################################
script Targets {
    input Time_Of_First_Bar_Of_Day = 0;
    input BarOpen = 0;
    input Cross = 0;
    input ADOpen = 0;
    input Spacing = 0;

    def TA = if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 and !IsNaN(close) then
            fold a = 0 to BarOpen
            with inta = Double.NEGATIVE_INFINITY
            do if IsNaN(GetValue(Cross,a)) then inta
            else if GetValue(Cross, a) > ADOpen then inta
            else if GetValue(Cross, a) < inta then inta
            else if GetValue(Cross, a) > inta and GetValue(Cross, a) < ADOpen - Spacing
            then GetValue(Cross, a)
            else inta
            else TA[1];
    plot Targets = if !TA then Double.NEGATIVE_INFINITY else TA;
}

################################
plot PBC1 = Targets(Time_Of_First_Bar_Of_Day, BarOpen, Cross, ADOpen, 0);
PBC1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC1.SetLineWeight(1);

plot PBC2 = Targets(Time_Of_First_Bar_Of_Day, BarOpen, Cross, PBC1, Spacing);
PBC2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC2.SetLineWeight(1);

plot PBC3 = Targets(Time_Of_First_Bar_Of_Day, BarOpen, Cross, PBC2, Spacing);
PBC3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC3.SetLineWeight(1);

plot PBC4 = Targets(Time_Of_First_Bar_Of_Day, BarOpen, Cross, PBC3, Spacing);
PBC4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC4.SetLineWeight(1);

plot PBC5 = Targets(Time_Of_First_Bar_Of_Day, BarOpen, Cross, PBC4, Spacing);
PBC5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC5.SetLineWeight(1);
 
Combined above and below into 1 study.
Can only look back a max of 25 days on a 10 min chart due to fold limitations.
Removed the spacing between plots.
Seems to do a fairly good job overall.
Believe this is about as far I'll go with it.
Ruby:
declare once_per_bar;

input Time_Of_First_Bar_Of_Day = 1800;

def ADOpen = CompoundValue(1, if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then open else ADOpen[1], 0);
def BarOpen = CompoundValue(1, if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then AbsValue(BarNumber()) else BarOpen[1], 0);

def SMA100 = reference SimpleMovingAvg(close, 100);
def SMA200 = reference SimpleMovingAvg(close, 200);
def SMA500 = reference SimpleMovingAvg(close, 500);
def a = (SMA100 + SMA200 + SMA500) / 3;

def Cross = if Crosses(SMA100, SMA200, CrossingDirection.ANY) then Round(Round(SMA200 / TickSize(), 0) * TickSize(),2) else if Crosses(SMA100, SMA500, CrossingDirection.ANY) then Round(Round(SMA500 / TickSize(), 0) * TickSize(),2) else if Crosses(SMA200, SMA500, CrossingDirection.ANY) then Round(Round(SMA500 / TickSize(), 0) * TickSize(),2) else if Crosses(SMA100, a, CrossingDirection.ANY) then Round(Round(a / TickSize(), 0) * TickSize(),2) else if Crosses(SMA200, a, CrossingDirection.ANY) then Round(Round(a / TickSize(), 0) * TickSize(),2) else if Crosses(SMA500, a, CrossingDirection.ANY) then Round(Round(a / TickSize(), 0) * TickSize(),2) else Cross[1];

#######################################################################################
#Define/Plot Levels..##################################################################
#######################################################################################
script TargetsA {
    input Time_Of_First_Bar_Of_Day = 0;
    input BarOpen = 0;
    input Cross = 0;
    input ADOpen = 0;
     
    def TA = if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 and !isnan(close) then
            fold a = 0 to BarOpen
            with inta = Double.POSITIVE_INFINITY
            do if IsNaN(GetValue(Cross,a)) then inta
            else if GetValue(Cross,a) < ADOpen then inta
            else if GetValue(Cross,a) > inta then inta
            else if GetValue(Cross,a) < inta and GetValue(Cross,a) > ADOpen
            then GetValue(Cross,a)
            else inta
            else TA[1];
    plot Targets = TA;
}
#######################################################################################
script TargetsB {
    input Time_Of_First_Bar_Of_Day = 0;
    input BarOpen = 0;
    input Cross = 0;
    input ADOpen = 0;
 
    def TA = if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 and !IsNaN(close) then
            fold a = 0 to BarOpen
            with inta = Double.NEGATIVE_INFINITY
            do if IsNaN(GetValue(Cross,a)) then inta
            else if GetValue(Cross, a) > ADOpen then inta
            else if GetValue(Cross, a) < inta then inta
            else if GetValue(Cross, a) > inta and GetValue(Cross, a) < ADOpen
            then GetValue(Cross, a)
            else inta
            else TA[1];
    plot Targets = if !TA then Double.NEGATIVE_INFINITY else TA;
}
def CAC = TargetsA(Time_Of_First_Bar_Of_Day,BarOpen,Cross,ADOpen);
def CBC = TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, Cross, ADOpen);
def C = CompoundValue(1,if SecondsFromTime(Time_Of_First_Bar_Of_Day) == 0 then if AbsValue(ADOpen - CAC) >= AbsValue(ADOpen - CBC) then ADOpen - AbsValue(ADOpen - CBC) else ADOpen + AbsValue(ADOpen - CAC) else C[1],ADOpen);#TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, Cross, Cross, 0);

plot PC = C;
PC.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PC.SetLineWeight(1);

################################
plot PAC1 = TargetsA(Time_Of_First_Bar_Of_Day,BarOpen,PC,PC);
PAC1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC1.SetLineWeight(1);

plot PAC2 = TargetsA(Time_Of_First_Bar_Of_Day,BarOpen,PC,PAC1);
PAC2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC2.SetLineWeight(1);

plot PAC3 = TargetsA(Time_Of_First_Bar_Of_Day,BarOpen,PC,PAC2);
PAC3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC3.SetLineWeight(1);

plot PAC4 = TargetsA(Time_Of_First_Bar_Of_Day,BarOpen,PC,PAC3);
PAC4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC4.SetLineWeight(1);

plot PAC5 = TargetsA(Time_Of_First_Bar_Of_Day,BarOpen,PC,PAC4);
PAC5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC5.SetLineWeight(1);

################################
plot PBC1 = TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, PC, PC);
PBC1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC1.SetLineWeight(1);


plot PBC2 = TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, PC, PBC1);
PBC2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC2.SetLineWeight(1);

plot PBC3 = TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, PC, PBC2);
PBC3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC3.SetLineWeight(1);

plot PBC4 = TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, PC, PBC3);
PBC4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC4.SetLineWeight(1);

plot PBC5 = TargetsB(Time_Of_First_Bar_Of_Day, BarOpen, PC, PBC4);
PBC5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC5.SetLineWeight(1);
 
Hi, looks really interesting. Quick question, when I copy and paste the code into mine, I am not getting nearly as many lines. Can you help me with this?
1695269791671.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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