Welcome! Log In Create A New Profile

Get Earnings and Seasonal Trends - Subscribe Today!

Advanced

Need a little TOS codeing help

Posted by EARNINGS TRADER 
Need a little TOS codeing help
August 10, 2022 01:45PM
I want to connect two points (trendline) on a lower study. Can't seem to figure it out. I keep getting two flat lines rather than an intergrated signal. Any help is appreciated.


declare lower;

input Channel_Length = 10;
input Average_Length = 21;
input over_bought_1 = 60;
input over_bought_2 = 53;
input over_sold_1 = -60;
input over_sold_2 = -53;
input show_bubbles = yes;
input show_sec_bbls = no;
input show_alerts = yes;
def ap = hlc3;
def esa = ExpAverage(ap, Channel_Length);
def d = ExpAverage(AbsValue(ap - esa), Channel_Length);
def ci = (ap - esa) / (0.015 * d);
def tci = ExpAverage(ci, Average_Length);
def wt1 = tci;
def wt2 = SimpleMovingAvg(wt1, 4);

def green = if wt1 > wt1[1] AND wt1[1] <= wt1[2] then 1 ELSE 0;
def red = if wt1 < wt1[1] AND wt1[1] >= wt1[2] then 1 ELSE 0;

plot wt1_1 = wt1;
wt1_1.SetStyle(Curve.POINTS);
wt1_1.AssignValueColor(if green then (CreateColor(55, 251, 55)) else if red then (CreateColor(255, 0, 0)) else (color.gray));
wt1_1.SetLineWeight(5);



input numBars = 5;
input TrendResistanceStart = 0;
input TrendResistanceEnd = 0;
input TrendSupportStart = 0;
input TrendSupportEnd = 0;


def UserSetResistance = TrendResistanceStart > 0 and TrendResistanceEnd > 0;
def UserSetSupport = TrendSupportStart > 0 and TrendSupportEnd > 0;
def currentHigh = high;
def currentLow = low;
def currentBar = BarNumber();
def PH;
def PL;

# +---------------------+
# | PIVOT HIGH |
# +---------------------+

def isHigherThanNextBars = fold i = 1 to numBars + 1 with p = 1
while p do currentHigh > GetValue(high, -i);

PH = if wt1 < wt1[1] AND wt1[1] >= wt1[2] then currentHigh else Double.NaN;

# +---------------------+
# | PIVOT LOW |
# +---------------------+

def isLowerThanNextBars = fold j = 1 to numBars + 1 with q = 1
while q do currentLow < GetValue(low, -j);

PL = if wt1 > wt1[1] AND wt1[1] <= wt1[2] then currentLow else Double.NaN;

# +---------------------+
# | ESTABLISH THE DOTS |
# +---------------------+

rec PHBar = if UserSetResistance then TrendResistanceEnd else if !IsNaN(PH) then currentBar else PHBar[1];
rec PLBar = if UserSetSupport then TrendSupportEnd else if !IsNaN(PL) then currentBar else PLBar[1];
rec PHL = if !IsNaN(PH) then PH else PHL[1];
rec priorPHBar = if UserSetResistance then TrendResistanceStart else if PHL != PHL[1] then PHBar[1] else priorPHBar[1];
rec PLL = if !IsNaN(PL) then PL else PLL[1];
rec priorPLBar = if UserSetSupport then TrendSupportStart else if PLL != PLL[1] then PLBar[1] else priorPLBar[1];
def isFinalTwoHighPivots = currentBar >= HighestAll(priorPHBar);
def isFinalTwoLowPivots = currentBar >= HighestAll(priorPLBar);
def ResistanceFinishOffset = if isFinalTwoHighPivots then currentBar - PHBar else 0;
def ResistanceStartOffset = if isFinalTwoHighPivots then currentBar - priorPHBar else 0;
def ResistanceSlope = (GetValue(PH, ResistanceFinishOffset) - GetValue(PH, ResistanceStartOffset)) / (PHBar - priorPHBar);
def SupportFinishOffset = if isFinalTwoLowPivots then currentBar - PLBar else 0;
def SupportStartOffset = if isFinalTwoLowPivots then currentBar - priorPLBar else 0;
def SupportSlope = (GetValue(PL, SupportFinishOffset) - GetValue(PL, SupportStartOffset)) / (PLBar - priorPLBar);
rec ResistanceExtend = if currentBar == HighestAll(PHBar) then 1 else ResistanceExtend[1];
rec SupportExtend = if currentBar == HighestAll(PLBar) then 1 else SupportExtend[1];

def pivotHigh = if
ph>0 then PH else Double.NaN;

def pivotLow = if
pl>0 then PL else Double.NaN;

def pivotdot = if !IsNaN(pivotHigh) then pivotHigh else if !IsNaN(pivotLow) then pivotLow else Double.NaN;

# +---------------------+
# | CONNECT THE DOTS |
# +---------------------+

script Line {
input v = 0; #value
input s = 0; #start
input e = 0; #end
input xl = 0; #extend left
input xr = Double.POSITIVE_INFINITY; #extend right
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def x1 = Min(s, e);
def x2 = Max(s, e);
def y1 = GetValue(v, bn - x1);
def y2 = GetValue(v, bn - x2);
def m = (y2 - y1) / (x2 - x1);
def x = bn - x1;
def b = y1;
plot return = if bn < (x1 - xl) or bn > (x2 + xr) then Double.NaN else m * x + b;
}
def bn = BarNumber();
def prevPH = if !IsNaN(PH[1]) then high else prevPH[1];
def prevBh = if !IsNaN(PH[1]) then bn[1] else prevBh[1];
def prevPL = if !IsNaN(PL[1]) then low else prevPL[1];
def prevBl = if !IsNaN(PL[1]) then bn[1] else prevBl[1];
def rEnd = HighestAll(if !IsNaN(PH) and high < prevPH then bn else 0);
def rStart = HighestAll(if bn == rEnd then prevBh else 0);
def sEnd = HighestAll(if !IsNaN(PL) and low > prevPL then bn else 0);
def sStart = HighestAll(if bn == sEnd then prevBl else 0);

plot resistance = line(high, rStart, rEnd);
resistance.SetDefaultColor(CreateColor( 255, 126, 156));
resistance.SetLineWeight(3);

plot support = line(low, sStart, sEnd);
support.SetDefaultColor(CreateColor( 51, 255, 51));
support.SetLineWeight(3);
Sorry, only registered users may post in this forum.

Click here to login