B4 Upper Indicator

Keith_J

New member
Platform
  1. Thinkorswim
This is my first post, so apologies if this is incorrect.

I'm not a member of the B4 team, but I used the public B4 Lower indicator and created an upper indicator. I did this because I only wanted to see the first buy or sell signal after the BullBear line turned. I believe this will mimic the functionality of what we are currently referring to as the B4 Pro. The indicator will color the bars the same as the lower indicator and it adds a label and a red or green line for the BullBear Line. Use at your own risk.

Code:
# B4 Upper Indicator
#

input ShowLabels = yes;  #hint ShowLabels: New Labels showing Buy or Sell Signal values

def bn = BarNumber();

### MACDBB

input MACDBB_FastLength = 12;
input MACDBB_SlowLength = 26;
input MACDBB_BandLength = 15;
input MACDBB_NumDev = 1.0;

def MACDBB_Data = MACD(fastLength = MACDBB_FastLength, slowLength = MACDBB_SlowLength, MACDLength = 5);

def MACDBB_Upper = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                             Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).UpperBand;
def MACDBB_Lower = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                             Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).Lowerband;

def MACDBB_Buy = MACDBB_Data > MACDBB_Upper;
def MACDBB_Sell = MACDBB_Data <= MACDBB_Lower;

### RSI/STOCASTIC/MACD CONFLUENCE COMBO

def RSM_MACD_Diff = reference MACD("fast length" = 12, "slow length" = 26, "macd length" = 9).Diff;
def RSM_RSI = reference RSI(length = 7).RSI;


def RSM_Stoch_Val = 100 * (close - lowest(low, 14)) / (highest(high, 14) - lowest(low, 14));
def RSM_StochSlowK = SimpleMovingAvg(SimpleMovingAvg(RSM_Stoch_Val,3),3);

def RSM_rsiGreen = RSM_RSI >= 50;
def RSM_rsiRed = RSM_RSI < 50;
def RSM_stochGreen = RSM_StochSlowK >= 50;
def RSM_stochRed = RSM_StochSlowK < 50;
def RSM_macdGreen = RSM_MACD_Diff >= 0;
def RSM_macdRed = RSM_MACD_Diff < 0;
def RSM_Buy = RSM_rsiGreen and RSM_stochGreen and RSM_macdGreen;
def RSM_Sell = RSM_rsiRed and RSM_stochRed and RSM_macdRed;

# Shade areas based on criteria; adjust as needed

### Divergance

input HMA_Length = 55;
input HMA_Lookback = 2;

def HMA = HullMovingAvg(price = HL2, length = HMA_Length);
def HMA_delta = HMA[1] - HMA[HMA_Lookback + 1];
def HMA_delta_per_bar = HMA_delta / HMA_Lookback;
def HMA_next_bar = HMA[1] + HMA_delta_per_bar;
def HMA_concavity = if HMA > HMA_next_bar then 1 else -1;
def HMA_MA_Max = if HMA[-1] < HMA and HMA > HMA[1] then HMA else Double.NaN;
def HMA_MA_Min = if HMA[-1] > HMA and HMA < HMA[1] then HMA else Double.NaN;
def HMA_divergence = HMA - HMA_next_bar;

### RSI IFT

def RSI_IFT_R = reference RSI(5, close) - 50;
def RSI_IFT_AvgRSI = MovingAverage(AverageType.Exponential,RSI_IFT_R,9);
def RSI_IFT_InverseRSI = (Power(Double.E, 2 * RSI_IFT_AvgRSI) - 1) / (Power(Double.E, 2 * RSI_IFT_AvgRSI) + 1);
def RSI_IFT_Direction = if BarNumber() == 0 then 0
                        else if (RSI_IFT_InverseRSI[1] > 0) and (RSI_IFT_InverseRSI < 0) then -1
                        else if (RSI_IFT_InverseRSI > 0) and (RSI_IFT_InverseRSI[1] < 0) then 1
                        else RSI_IFT_Direction[1];

### Fibonacci SuperTrend

input FST_Length = 11;
input FST_Retrace = 23.6;
input FST_UseHighLow = yes;

def FST_h = if FST_UseHighLow then high else close;
def FST_l = if FST_UseHighLow then low else close;
def FST_minL = Lowest(FST_l, FST_Length);
def FST_maxH = Highest(FST_h, FST_Length);

def FST_hh = if FST_h > FST_maxH[1] then FST_h else FST_hh[1];
def FST_ll = if FST_l < FST_minL[1] then FST_l else FST_ll[1];
def FST_trend = if FST_h > FST_maxH[1] then 1 else if FST_l < FST_minL[1] then -1 else FST_trend[1];
def FST_Direction = if BarNumber() == 0 then 0
                    else if FST_trend != 1 then -1
                    else if FST_trend == 1 then 1
                    else FST_Direction[1];

### Bar Color

input BarColor = { "None", default "RSM", "FibonacciSuperTrend" };    #hint BarColor: Paint bars with RSM or Fibonacci SuperTrend direction

AssignPriceColor(
    if BarColor == BarColor.FibonacciSuperTrend then
        if FST_trend == 1 then Color.GREEN else Color.RED
    else if BarColor == BarColor.RSM then
        if RSM_Buy then Color.GREEN
        else if RSM_Sell then Color.RED
        else Color.GRAY
    else Color.CURRENT
);

### Strategy

input BullBear_Include_FST = yes;       #hint BullBear_Include_FST: Include Fibonacci SuperTrend in the vertical line strategy
input BullBear_Include_RSI_IFT = yes;   #hint BullBear_Include_RSI_IFT: Include RSI IFT in the vertical line strategy

def BullBear_Buy = (!BullBear_Include_FST or FST_Direction == 1) and
                   (!BullBear_Include_RSI_IFT or RSI_IFT_Direction == 1);
def BullBear_Sell = (!BullBear_Include_FST or FST_Direction == -1) and
                    (!BullBear_Include_RSI_IFT or RSI_IFT_Direction == -1);
                    
# 1/20/22 Added Code for Buy / Sell Signal - eliminates repeats

def longPlay;
def shortPlay;

def Strategy_Buy = BullBear_Buy and MACDBB_Buy and RSM_Buy;
def Strategy_Sell = BullBear_Sell and MACDBB_Sell and RSM_Sell;
def Strategy_BuySignal = Strategy_Buy and !Strategy_Buy[1];
def Strategy_SellSignal = Strategy_Sell and !Strategy_Sell[1];

plot BuyArrow = if Strategy_BuySignal and !longPlay[1] then (low - 7 * tickSize()) else Double.NaN;
BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.white);
BuyArrow.setLineWeight(5);
BuyArrow.HideTitle();
BuyArrow.HideBubble();

plot SellArrow = if Strategy_SellSignal and !shortPlay[1] then (high + 4 * tickSize())  else Double.NaN;
SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.magenta);
SellArrow.setLineWeight(5);
SellArrow.HideTitle();
SellArrow.HideBubble();

If Strategy_BuySignal then {
    longplay = 1;
    shortplay = 0; }
Else
    {   
    If Strategy_SellSignal then {
        longplay = 0;
        shortplay = 1; }
    Else
    {   
    Longplay = longPlay[1];
    shortplay = shortPlay[1];
    }
}

#Troubleshooting

#addchartbubble(Strategy_BuySignal, Low, LongPlay + " " + longplay[1], color.green, yes);
#addchartbubble(Strategy_SellSignal, High, shortplay + " " + shortplay[1], color.red, no);



##### END 1/20/22 Code                                       
                
# 1/8/22 ADDED CODE FOR LABEL
def buyLine = if BullBear_Buy and !BullBear_Buy[1] then close else buyline[1];
def sellLine = if BullBear_Sell and !BullBear_Sell[1] then close else sellLine[1];


def lowPointBarNumber = HighestAll(if BullBear_Buy and !BullBear_Buy[1]  then bn else 0);
def swinglow1 = if bn < lowPointBarNumber then Double.NaN else lowPointBarNumber;

def highPointBarNumber = HighestAll(if BullBear_Sell and !BullBear_Sell[1] then bn else 0);
def swinghigh1 = if bn < highPointBarNumber then Double.NaN else highPointBarNumber;

# UseSwingLow and High variables to determine if lines should print
def useSwingLow = swinglow1 > swinghigh1;
def useSwingHigh = swinglow1 < swinghigh1;

plot buyLineUp = if useSwingLow then (HighestAll(if IsNaN(buyLine[-1])
                            then buyLine
                            else Double.NaN))
                            else Double.Nan;

buyLineUp.SetStyle(Curve.FIRM);
buyLineUp.AssignValueColor(Color.Green);
buyLineUp.SetLineWeight(2);

plot sellLineDown = if useSwingHigh then (HighestAll(if IsNaN(sellLine[-1])
                            then sellLine
                            else Double.NaN))
                            else Double.Nan;

sellLineDown.SetStyle(Curve.FIRM);
sellLineDown.AssignValueColor(Color.red);
sellLineDown.SetLineWeight(2);

### 1/8/22 ADDED LABELS
AddLabel(showlabels, "B4:", Color.light_Gray);
addlabel(showlabels and BullBear_Buy, asdollars(buyline), color.green);
addlabel(showlabels and BullBear_Sell, asdollars(sellLine), color.red);

 
Top