Sentinels [fikira] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
2G1WDMs.png


Author Message:
Sentinels is a playful variation on combining different mean averages (MA).
A cross of 2 user-defined MA's (MA 1 & MA 2) initiates the drawing of a sentinel with tentacles, which, on its turn can provide potential support/resistance or entry/stop-loss/take profit zones.

More Details : https://www.tradingview.com/v/GlbuIHjp/

CODE:

CSS:
# https://www.tradingview.com/v/GlbuIHjp/
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © fikira
#indicator ("Sentinels"
# Converted and mod by Sam4Cok@Samer800    - 12/2023

input barColoring = yes;
input movAvg1Type = {"SMA", "EMA", "SMMA (RMA)", "HullMA", "WMA", default "VWMA", "DEMA", "TEMA"}; #'Type'
input movAvgSource1 = close;
input movAvgLength1 = 20; # 'Length'
input movAvg2Type = {"SMA", "EMA", "SMMA (RMA)", "HullMA", "WMA", default "VWMA", "DEMA", "TEMA"}; #'Type'
input movAvgSource2 = close;
input movAvgLength2 = 50; # 'Length'
input tentaclesMaType = {"Don't Show", "SMA", "EMA", "SMMA (RMA)", "HullMA", "WMA", default "VWMA", "DEMA", "TEMA"};

def na = Double.NaN;

DefineGlobalColor("UpCol3", CreateColor(8, 165, 170));
DefineGlobalColor("DnCol3", Color.PLUM);

Script Cond {
input count = 0;
input n1 = 0;
input n2 = 0;
    def cond = count >= n1 and count <= n2;
    plot out = cond;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 14;
    input vol = volume;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    plot result = VWMA;
}
script ma {
    input type = "VWMA";
    input length = 20;
    input src = close;
    def ma =
        if type == "SMA"        then Average(src, length) else
        if type == "EMA"        then ExpAverage(src, length) else
        if type == "SMMA (RMA)" then WildersAverage(src, length) else
        if type == "HullMA"     then HullMovingAvg(src, length) else
        if type == "WMA"        then WMA(src, length) else
        if type == "VWMA"       then vwma(src, length) else
        if type == "DEMA"       then DEMA(src, length) else
        if type == "TEMA"       then TEMA(src, length) else Double.NaN;
    plot out = ma;
}
# //  Calculations

def ma1 = ma(movAvg1Type, movAvgLength1, movAvgSource1);
def ma2 = ma(movAvg2Type, movAvgLength2, movAvgSource2);

def trigUp = (ma1 Crosses Above ma2);
def trigDn = (ma1 Crosses Below ma2);
def bsUp = if trigUp then 0 else bsUp[1] + 1;
def bsDn = if trigDn then 0 else bsDn[1] + 1;

def UpR1 = 0;#5;
def UpG1 = 255;#250;
def UpB1 = 255;#131;

def NoR2 = 185;#255;
def NoG2 = 126;#176;
def NoB2 = 0;#6;

def DnR1 = 255;
def DnG1 = 0;
def DnB1 = 255;

def Rdn = (DnR1  -  NoR2) / 12;
def Rup = (UpR1  -  NoR2) / 12;
def Gdn = (DnG1  -  NoG2) / 12;
def Gup = (UpG1  -  NoG2) / 12;
def Bdn = (DnB1  -  NoB2) / 12;
def Bup = (UpB1  -  NoB2) / 12;

def rUp7 = UpR1 - (Rup *  0);
def gUp7 = UpG1 - (Gup *  0);
def bUp7 = UpB1 - (Bup *  0);
def rUp6 = UpR1 - (Rup *  1);
def gUp6 = UpG1 - (Gup *  1);
def bUp6 = UpB1 - (Bup *  1);
def rUp5 = if bsUp <  3 then UpR1 - (Rup *  2) else UpR1 - (Rup *  3);
def gUp5 = if bsUp <  3 then UpG1 - (Gup *  2) else UpG1 - (Gup *  3);
def bUp5 = if bsUp <  3 then UpB1 - (Bup *  2) else UpB1 - (Bup *  3);
def rUp4 = if bsUp <  7 then UpR1 - (Rup *  4) else UpR1 - (Rup *  5);
def gUp4 = if bsUp <  7 then UpG1 - (Gup *  4) else UpG1 - (Gup *  5);
def bUp4 = if bsUp <  7 then UpB1 - (Bup *  4) else UpB1 - (Bup *  5);
def rUp3 = if bsUp < 18 then UpR1 - (Rup *  6) else UpR1 - (Rup *  7);
def gUp3 = if bsUp < 18 then UpG1 - (Gup *  6) else UpG1 - (Gup *  7);
def bUp3 = if bsUp < 18 then UpB1 - (Bup *  6) else UpB1 - (Bup *  7);
def rUp2 = if bsUp < 30 then UpR1 - (Rup *  8) else UpR1 - (Rup *  9);
def gUp2 = if bsUp < 30 then UpG1 - (Gup *  8) else UpG1 - (Gup *  9);
def bUp2 = if bsUp < 30 then UpB1 - (Bup *  8) else UpB1 - (Bup *  9);
def rUp1 = if bsUp < 50 then UpR1 - (Rup *  10) else UpR1 - (Rup *  11);
def gUp1 = if bsUp < 50 then UpG1 - (Gup *  10) else UpG1 - (Gup *  11);
def bUp1 = if bsUp < 50 then UpB1 - (Bup *  10) else UpB1 - (Bup *  11);

def rDn7 = DnR1 - (Rdn *  0);
def gDn7 = DnG1 - (Gdn *  0);
def bDn7 = DnB1 - (Bdn *  0);
def rDn6 = DnR1 - (Rdn *  1);
def gDn6 = DnG1 - (Gdn *  1);
def bDn6 = DnB1 - (Bdn *  1);
def rDn5 = if bsDn <  3 then DnR1 - (Rdn *  2) else DnR1 - (Rdn *  3);
def gDn5 = if bsDn <  3 then DnG1 - (Gdn *  2) else DnG1 - (Gdn *  3);
def bDn5 = if bsDn <  3 then DnB1 - (Bdn *  2) else DnB1 - (Bdn *  3);
def rDn4 = if bsDn <  7 then DnR1 - (Rdn *  4) else DnR1 - (Rdn *  5);
def gDn4 = if bsDn <  7 then DnG1 - (Gdn *  4) else DnG1 - (Gdn *  5);
def bDn4 = if bsDn <  7 then DnB1 - (Bdn *  4) else DnB1 - (Bdn *  5);
def rDn3 = if bsDn <  18 then DnR1 - (Rdn *  6) else DnR1 - (Rdn *  7);
def gDn3 = if bsDn <  18 then DnG1 - (Gdn *  6) else DnG1 - (Gdn *  7);
def bDn3 = if bsDn <  18 then DnB1 - (Bdn *  6) else DnB1 - (Bdn *  7);
def rDn2 = if bsDn <  30 then DnR1 - (Rdn *  8) else DnR1 - (Rdn *  9);
def gDn2 = if bsDn <  30 then DnG1 - (Gdn *  8) else DnG1 - (Gdn *  9);
def bDn2 = if bsDn <  30 then DnB1 - (Bdn *  8) else DnB1 - (Bdn *  9);
def rDn1 = if bsDn <  50 then DnR1 - (Rdn *  10) else DnR1 - (Rdn *  11);
def gDn1 = if bsDn <  50 then DnG1 - (Gdn *  10) else DnG1 - (Gdn *  11);
def bDn1 = if bsDn <  50 then DnB1 - (Bdn *  10) else DnB1 - (Bdn *  11);

# // Sentinels
def condDn7 = Cond(bsDn, 0,  1);
def condDn6 = Cond(bsDn, 1,  2);
def condDn5 = Cond(bsDn, 2,  5);
def condDn4 = Cond(bsDn, 5,  11);
def condDn3 = Cond(bsDn, 11, 27);
def condDn2 = Cond(bsDn, 27, 40);
def condDn1 = Cond(bsDn, 40, 60);
plot dn7 = if condDn7 then ma2 else na;
plot dn6 = if condDn6 then ma2 else na;
plot dn5 = if condDn5 then ma2 else na;
plot dn4 = if condDn4 then ma2 else na;
plot dn3 = if condDn3 then ma2 else na;
plot dn2 = if condDn2 then ma2 else na;
plot dn1 = if condDn1 then ma2 else na;
dn7.SetLineWeight(5);
dn6.SetLineWeight(4);
dn5.SetLineWeight(3);
dn4.SetLineWeight(2);
dn3.SetLineWeight(2);
#dn2.SetLineWeight(2);
dn7.AssignValueColor(CreateColor(rDn7, gDn7, bDn7));
dn6.AssignValueColor(CreateColor(rDn6, gDn6, bDn6));
dn5.AssignValueColor(CreateColor(rDn5, gDn5, bDn5));
dn4.AssignValueColor(CreateColor(rDn4, gDn4, bDn4));
dn3.AssignValueColor(CreateColor(rDn3, gDn3, bDn3));
dn2.AssignValueColor(CreateColor(rDn2, gDn2, bDn2));
dn1.AssignValueColor(CreateColor(rDn1, gDn1, bDn1));

def condUp7 = Cond(bsUp, 0, 1);
def condUp6 = Cond(bsUp, 1, 2);
def condUp5 = Cond(bsUp, 2, 5);
def condUp4 = Cond(bsUp, 5, 11);
def condUp3 = Cond(bsUp, 11, 27);
def condUp2 = Cond(bsUp, 27, 40);
def condUp1 = Cond(bsUp, 40, 60);
plot up7 = if condUp7 then ma1 else na;
plot up6 = if condUp6 then ma1 else na;
plot up5 = if condUp5 then ma1 else na;
plot up4 = if condUp4 then ma1 else na;
plot up3 = if condUp3 then ma1 else na;
plot up2 = if condUp2 then ma1 else na;
plot up1 = if condUp1 then ma1 else na;
up7.SetLineWeight(5);
up6.SetLineWeight(4);
up5.SetLineWeight(3);
up4.SetLineWeight(2);
up3.SetLineWeight(2);
#up2.SetLineWeight(1);
up7.AssignValueColor(CreateColor(rUp7, gUp7, bUp7));
up6.AssignValueColor(CreateColor(rUp6, gUp6, bUp6));
up5.AssignValueColor(CreateColor(rUp5, gUp5, bUp5));
up4.AssignValueColor(CreateColor(rUp4, gUp4, bUp4));
up3.AssignValueColor(CreateColor(rUp3, gUp3, bUp3));
up2.AssignValueColor(CreateColor(rUp2, gUp2, bUp2));
up1.AssignValueColor(CreateColor(rUp1, gUp1, bUp1));


plot poinUp = if bsUp == 0 then ma1 else na;
plot poinDn = if bsDn == 0 then ma2 else na;
poinUp.SetLineWeight(5);
poinDn.SetLineWeight(5);
poinUp.AssignValueColor(CreateColor(rUp7, gUp7, bUp7));
poinDn.AssignValueColor(CreateColor(rDn7, gDn7, bDn7));
poinUp.SetPaintingStrategy(PaintingStrategy.POINTS);
poinDn.SetPaintingStrategy(PaintingStrategy.POINTS);

def diff   = round((movAvgLength2 - movAvgLength1) / 6, 0);

def vwma1  = ma(tentaclesMaType, movAvgLength1 + diff * 1, movAvgSource1);
def vwma2  = ma(tentaclesMaType, movAvgLength1 + diff * 2, movAvgSource1);
def vwma3  = ma(tentaclesMaType, movAvgLength1 + diff * 3, movAvgSource1);
def vwma4  = ma(tentaclesMaType, movAvgLength1 + diff * 4, movAvgSource1);
def vwma5  = ma(tentaclesMaType, movAvgLength1 + diff * 5, movAvgSource1);
def vwma6  = ma(tentaclesMaType, movAvgLength1 + diff * 6, movAvgSource1);

plot tentDn1 = if bsDn > 0 and bsDn <= 15 then
               if bsDn < 3 then ma2 else vwma1 else na;
plot tentDn2 = if bsDn > 0 and bsDn <= 20 then
               if bsDn < 3 then ma2 else vwma2 else na;
plot tentDn3 = if bsDn > 0 and bsDn <= 25 then
               if bsDn < 3 then ma2 else vwma3 else na;
plot tentDn4 = if bsDn > 0 and bsDn <= 30 then
               if bsDn < 3 then ma2 else vwma4 else na;
plot tentDn5 = if bsDn > 0 and bsDn <= 35 then
               if bsDn < 3 then ma2 else vwma5 else na;
plot tentDn6 = if bsDn > 0 and bsDn <= 40 then
               if bsDn < 3 then ma2 else vwma6 else na;

plot tentUp1 = if bsUp > 0 and bsUp <= 15 then
               if bsUp < 3 then ma1 else vwma1 else na;
plot tentUp2 = if bsUp > 0 and bsUp <= 20 then
               if bsUp < 3 then ma1 else vwma2 else na;
plot tentUp3 = if bsUp > 0 and bsUp <= 25 then
               if bsUp < 3 then ma1 else vwma3 else na;
plot tentUp4 = if bsUp > 0 and bsUp <= 30 then
               if bsUp < 3 then ma1 else vwma4 else na;
plot tentUp5 = if bsUp > 0 and bsUp <= 35 then
               if bsUp < 3 then ma1 else vwma5 else na;
plot tentUp6 = if bsUp > 0 and bsUp <= 40 then
               if bsUp < 3 then ma1 else vwma6 else na;

tentDn1.setDefaultColor(GlobalColor("DnCol3"));
tentDn2.setDefaultColor(GlobalColor("DnCol3"));
tentDn3.setDefaultColor(GlobalColor("DnCol3"));
tentDn4.setDefaultColor(GlobalColor("DnCol3"));
tentDn5.setDefaultColor(GlobalColor("DnCol3"));
tentDn6.setDefaultColor(GlobalColor("DnCol3"));
tentUp1.setDefaultColor(GlobalColor("UpCol3"));
tentUp2.setDefaultColor(GlobalColor("UpCol3"));
tentUp3.setDefaultColor(GlobalColor("UpCol3"));
tentUp4.setDefaultColor(GlobalColor("UpCol3"));
tentUp5.setDefaultColor(GlobalColor("UpCol3"));
tentUp6.setDefaultColor(GlobalColor("UpCol3"));

AssignPriceColor(if !barColoring then Color.CURRENT else
                 if condUp7 then CreateColor(rUp7, gUp7, bUp7) else
                 if condUp6 then CreateColor(rUp6, gUp6, bUp6) else
                 if condUp5 then CreateColor(rUp5, gUp5, bUp5) else
                 if condUp4 then CreateColor(rUp4, gUp4, bUp4) else
                 if condUp3 then CreateColor(rUp3, gUp3, bUp3) else
                 if condUp2 then CreateColor(rUp2, gUp2, bUp2) else
                 if condUp1 then CreateColor(rUp1, gUp1, bUp1) else
                 if condDn7 then CreateColor(rDn7, gDn7, bDn7) else
                 if condDn6 then CreateColor(rDn6, gDn6, bDn6) else
                 if condDn5 then CreateColor(rDn5, gDn5, bDn5) else
                 if condDn4 then CreateColor(rDn4, gDn4, bDn4) else
                 if condDn3 then CreateColor(rDn3, gDn3, bDn3) else
                 if condDn2 then CreateColor(rDn2, gDn2, bDn2) else
                 if condDn1 then CreateColor(rDn1, gDn1, bDn1) else Color.GRAY);
#-- 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
389 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