BIAS Indicator for ThinkorSwim

mashume

Well-known member
VIP
About the Indicator

The basic idea of this indicator is well summed up in several places around the internet:

https://support.moomoo.com/en-us/topic245

https://www.tradingview.com/script/HWl23Wk9/

https://medium.com/@FMZ_Quant/deviation-rate-bias-trading-strategy-18f3d2e2b5b

Calculation Variations
I have included a few additional variations, simply because I like them:
1. Standard Calculation by the method described above.
2. Alternate calculation which shows the deviation between the moving average of the close with the moving average of HL2. This was my original _mis_reading of the formula. It is here because it seemed interesting
3. VWAP as the moving average instead of HL2 for average price. Again, because VWAP.

CX Options
1. "a-b" This calculates the crossovers between the a and b lines. Difference between the fastest and medium speed lines can be useful to display as an histogram.
2. "a-zero" This calculates crossovers of the a line and the zero line.
3. "b-zero" This is similar to #2 but uses the medium speed line and the zero.

Moving Average Options
Take your pick of all of your favourites!

Display Options
1. Display cx - this will show arrows where the CX-option setting defines them to be.

Eye Candy
uceSO6b.png

1. Showing histogram for "a-b" difference option.

sBRtvQU.png

2. Showing zero crossovers.

Code:
##################
#
#   BIAS Deviance Rate Indicator
#    for ThinkOrSwim
#
#   Version 1 2021-03-18
#   This Version by @Mashume
#   released under GPL as applicable
#   to the useThinkScript community
#
####################


declare lower;

input a_length = 6;
input b_length = 12;
input c_length = 24;
input method = {default "Standard", "Modified", "VWAP"};
input show_cx = yes;
input cx_type = {default "a-b", "a-zero", "b-zero"};
input ma_type = AverageType.Simple;


script bias{
input length = 6;
input method = {default "Standard", "Modified", "VWAP"};
input ma_type = AverageType.Simple;

def average_close = MovingAverage(ma_type, close, length);
def average_price = MovingAverage(ma_type, hl2[1], length);
plot bias;
switch (method) {
    case Standard:
        bias = (close - average_price) / average_price * 100;
    case Modified:
        bias = (average_close / average_price) - 1;
    case VWAP:
        bias = (close - MovingAverage(ma_type, vwap[1], length)) / MovingAverage(ma_type, vwap[1], length) * 100;
    }
}

plot a = bias(a_length, method, ma_type);
plot b = bias(b_length, method, ma_type);
plot c = bias(c_length, method, ma_type);

a.setDefaultColor(getColor(3));
b.setDefaultColor(getColor(9));
c.setDefaultColor(getColor(5));

plot midline = 0;
midline.setDefaultColor(getColor(7));

plot cxd;
switch (cx_type) {
    case "a-b":
        cxd = if a crosses above b then 0 else double.nan;
    case "a-zero":
        cxd = if a crosses below 0 then 0 else double.nan;
    case "b-zero":
        cxd =  if b crosses below 0 then 0 else double.nan;
}
cxd.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
cxd.SetDefaultColor(GetColor(5));
cxd.SetHiding(!show_cx);

plot cxu;
switch (cx_type) {
    case "a-b":
        cxu = if a crosses below b then 0 else double.nan;
    case "a-zero":
        cxu = if a crosses above 0 then 0 else double.nan;
    case "b-zero":
        cxu =  if b crosses above 0 then 0 else double.nan;
}

cxu.setPaintingStrategy(paintingStrategy.ARROW_UP);
cxu.SetDefaultColor(GetColor(9));
cxu.SetHiding(!show_cx);

plot hist;
switch (cx_type) {
    case "a-b":
        hist = b - a;
    case "a-zero":
        hist = double.nan;
    case "b-zero":
        hist =  double.nan;
}
hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
hist.AssignValueColor(if hist < 0 then GetColor(4) else GetColor(3));

Shareable link: http://tos.mx/yqAVX6p


Happy Trading,
mashume
 
Last edited by a moderator:

Similar threads

Top