Center Of Gravity (COG) Indicator for ThinkorSwim

BenTen

Administrative
Staff
VIP
Center Of Gravity or known as COG is a momentum indicator used to identify potential reversal point when hitting the upper or lower extreme channel. When price reached one end of the channel, it's most likely going to reverse and come back.

This indicator was converted to ThinkorSwim by baffled1. It can be used on any timeframes. The Center of Gravity can be a great indicator when trading in range-bound markets. You should avoid using it when the price is trending.



thinkScript Code

Rich (BB code):
input price = close;
input length = -10;
def displacement = (length / 2) + 1;
def dPrice = price[displacement];
def tmp = if !IsNaN(dPrice) then Average(dPrice, AbsValue(length)) else tmp[1] + (tmp[1] - tmp
[2]);
def tmp1 = if !IsNaN(price) then tmp else Double.NaN;
plot Data = tmp1;
Data.SetDefaultColor(color.CYAN );
Data.SetStyle(Curve.SHORT_DASH);
def pct=100;
input width=65;
def d=width/pct;
def d1=0.8*d;
def d2=1.05*d;
def d3=1.15*d;
def stdDeviation = Highestall("data" = AbsValue(tmp1 - price));
plot UpperLine = tmp1 + stdDeviation*d1;
plot LowerLine = tmp1 - stdDeviation*d1;
plot UpperLine1 = tmp1 + stdDeviation*d2;
plot LowerLine1 = tmp1 - stdDeviation*d2;
plot UpperLine2 = tmp1 + stdDeviation*d3;
plot LowerLine2 = tmp1 - stdDeviation*d3;
UpperLine.SetDefaultColor(color.red);
UpperLine1.SetDefaultColor(color.red );
UpperLine2.SetDefaultColor(color.red );
LowerLine.SetDefaultColor(color.cyan );
LowerLine1.SetDefaultColor(color.cyan );
LowerLine2.SetDefaultColor(color.cyan );
AddCloud(UpperLine,UpperLine1,color.plum,color.plum);
AddCloud(LowerLine,LowerLine1,color.green,color.dark_green);

Shareable Link

https://tos.mx/wLM6Cq

Adaptive Center of Gravity

Code:
# AdaptiveCOG
# Drew Griffith

declare upper;

# Daily settings

input price = close;
input COGlength = 10;
input InnerValue = 1.0;
input OuterValue = 1.6;
input ATRLength = 14;
input showClosingPriceLine = NO;
input showPriceBar = YES;
input smooth = 1;

def displacement = (-COGlength / 2) + 1;
def dPrice = price[displacement];

def CMA = if !IsNaN(dPrice) then Average(dPrice, AbsValue(COGlength)) else
CMA[1] + (CMA[1] - CMA[2]);

def ATR = Average(TrueRange(high,  close,  low),  ATRLength);

plot UpperOuterBand = if !IsNaN(price) then CMA + (ATR * OuterValue) else
Double.NaN;
plot LowerOuterBand = if !IsNaN(price) then CMA - (ATR * OuterValue) else
Double.NaN;
plot UpperInnerBand = if !IsNaN(price) then CMA + (ATR * InnerValue) else
Double.NaN;
plot LowerInnerBand = if !IsNaN(price) then CMA - (ATR * InnerValue) else
Double.NaN;

plot Rating = if
close > UpperOuterBand then -1
else if close > UpperInnerBand then -0.5
else if close < LowerInnerBand then 0.5
else if close < LowerOuterBand then 1
else 0;

RATING.Hide();

plot CenterLine = if !IsNaN(price) then CMA else Double.NaN;
CenterLine.DefineColor("CMA", GetColor(1));
CenterLine.DefineColor("Extrapolated", GetColor(0));
CenterLine.AssignValueColor(if !IsNaN(dPrice) then CenterLine.Color("CMA")
else
CenterLine.Color("Extrapolated"));
CenterLine.SetLineWeight(2);
CenterLine.Hide();
CenterLine.SetStyle(Curve.SHORT_DASH);

UpperOuterBand.SetDefaultColor(GetColor(5));
UpperOuterBand.SetLineWeight(2);
LowerOuterBand.SetDefaultColor(GetColor(6));
LowerOuterBand.SetLineWeight(2);

UpperInnerBand.SetDefaultColor(GetColor(5));
UpperInnerBand.SetLineWeight(1);
LowerInnerBand.SetDefaultColor(GetColor(6));
LowerInnerBand.SetLineWeight(1);

# Turn AddClouds off by putting a #-sign at the first position of the lines
AddCloud(UpperOuterBand, UpperInnerBand, Color.RED);
AddCloud(LowerInnerBand, LowerOuterBand, Color.GREEN);

HidePricePlot(!showPriceBar);
 
Last edited:

AcquireCurrency

New member
Is it possible to make a scan that alerts when the price of the stock is in the green or below with the COG pointing up? And obviously vice-versa when COG is pointing down and price is in or above the purple portion.

 
Last edited:

wildtrade

New member
VIP
I was wondering if anyone had or converted this indicator to TOS? Been looking for something similar to bollinger bands with Fibs built in. Or maybe COG indicator
 

horserider

Well-known member
VIP
@wildtrade Here you go.
Code:
# Bollinger Bands With Fibonaci levels.
# Horserider 1/24/2020
 
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input fib1 = 0.382;
input fib2 = 0.618;
input fib3 = 0.764;


def sDev = stdev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

plot UpperFibLine1 = (UpperBand - MidLine) * fib1 + MidLine;
plot UpperFibLine2 = (UpperBand - MidLine) * fib2 + MidLine;
plot UpperFibLine3 = (UpperBand - MidLine) * fib3 + MidLine;

plot LowerFibLine1 = (LowerBand - MidLine) * fib1 + MidLine;
plot LowerFibLine2 = (LowerBand - MidLine) * fib2 + MidLine;
plot LowerFibLine3 = (LowerBand - MidLine) * fib3 + MidLine;



LowerBand.SetLineWeight(2);
MidLine.SetLineWeight(3);
UpperBand.SetLineWeight(2);

LowerBand.SetDefaultColor(Color.DARK_RED);
MidLine.SetDefaultColor(Color.GRAY);
UpperBand.SetDefaultColor(Color.DARK_RED);

UpperFibLine1.SetDefaultColor(Color.GRAY);
UpperFibLine2.SetDefaultColor(Color.GRAY);
UpperFibLine3.SetDefaultColor(Color.GRAY);

LowerFibLine1.SetDefaultColor(Color.GRAY);
LowerFibLine2.SetDefaultColor(Color.GRAY);
LowerFibLine3.SetDefaultColor(Color.GRAY);

UpperFibLine1.SetStyle(Curve.SHORT_DASH);
UpperFibLine2.SetStyle(Curve.SHORT_DASH);
UpperFibLine3.SetStyle(Curve.SHORT_DASH);

LowerFibLine1.SetStyle(Curve.SHORT_DASH);
LowerFibLine2.SetStyle(Curve.SHORT_DASH);
LowerFibLine3.SetStyle(Curve.SHORT_DASH);
 

xiaxia

New member
I was wondering if anyone had or converted this indicator to TOS? Been looking for something similar to bollinger bands with Fibs built in. Or maybe COG indicator
I need to try out this TrueCenterBand indicator, if only someone could port it over to tos
 

horserider

Well-known member
VIP
@xiaxia Just guessing but I bet there is nothing special about those bands. You can also simulate them by adding 2 or 3 Bollinger bands studies or exponential deviation band studies and varying the outer bands say to maybe 1 , 2 , and 3 or whatever numbers you prefer.
 

Snorg

New member
The colored charts ( black background ) are polynomial regression channel plots ( not COG plots). Could be a quadratic (2). Could be greater than 2 - now that would be cool.

This dude wants $200 for his TOS code. :p

https://funwiththinkscript.com/indicators/polynomial-regression-channel/
But alas, we have PRC code in C# ( ninja ).

https://www.mypivots.com/board/topic/6450/1/polynomial-regression-channel-bands
The download is safe - extracted the PRC2.cs file only and it appears to work on NT4/5.

So here's the question of the day? Who feels like converting PRC2.CS to TOS?
 

soary

Member
TOS drawing tools has Fib fans, arcs n spirals that would probably give the same information. I use the fib fans a lot and they are great for those price levels.
 

wildtrade

New member
VIP
@horserider Thank you!! Do you know what the DATA plot is exactly? I thought my post got deleted.... so I didn't check back in... @Snorg that would be awesome if some could convert that code for the PRC code!
 

Stockbug

New member
2019 Donor
@xiaxia it looks like @horserider already did in the above post...
That script at #4 is for bollinger bands. What Wildtrade (and I) are looking for is the script for those bands that look kind of like a very smooth sign wave-( at #3 & #5).
I would like to see the code that draws the center curve for pictures at #3 & #5.
Does anyone know how to do it please?
 

bmcgraw393

New member
VIP
@spm009 Must note that this indicator kind of repaint. It will fluctuate along with the price. Careful when using it.
Hey Ben, are you saying that the upper green and lower red repaint? For instance, if one min ago the upper closes at $84, will that close value change 5 min later (kinda like a darvas box)?
 

Vtrades

New member
Is there a way to make this plot on tick charts? I have tried different tick charts and it compresses the candles but when I uncheck the fit studies box the study disappears.
 

Similar threads

Top