 # Need help with trend-following indicator modeled on TD Sequential

#### febreezus

##### New member
I'm trying to implement a simplified version of the logic behind this strategy, but I'm having trouble. Below is the code I've written so far. The code is a mess, I know - I can't figure out the order in which to assign the variables because each line requires another variable.

The idea is to trace trends. We are in an uptrend until we cross below the open of the bar 4 bars before the latest high... whereupon we switch to a downtrend, and remain so for as long as we make lower lows from this point... until we cross back above the open 4 bars before the latest low... etc.

Code:
``````def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def hi =  if firstBarOfDay then if close > open then close else open else
if dir == -1 and close > brk or dir == 11 and close > hi then close else hi;
def lo =  if firstBarOfDay then if close < open then close else open else
if dir == 1 and close < brk or dir == -1 and close < lo then close else lo;
def dir = if firstBarOfDay then (if close < open then -1 else if close > open then 1 else 0) else
if close>brk then 1 else if close<brk then -1 else dir;
def inc = if firstBarOfDay then 4 else
if brk==brk then inc+1 else 4;
def brk = if firstBarOfDay then open else
if close>hi or close<lo then open else GetValue(close,inc);``````

Anyone have any suggestions?

##### Active member
2019 Donor
Was there an update close to figuring this out ?

#### febreezus

##### New member
I think I mostly figured it out, although it still needs a little tweaking.

Code:
``````def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def bar2 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def bar3 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def bar4 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open else
if (close>brk and close>close) or (close<brk and close<close) then open else brk;
def hi =  if bar1 then if close > open then close else open else if bar2 or bar3 or bar4 then hi else
if open <= brk and close > brk then open else hi;
def lo =  if bar1 then if close > open then open else open else if bar2 or bar3 or bar4 then lo else
if open >= brk and close < brk then close else lo;
def dir = if bar1 or bar2 or bar3 then 0 else if bar4 then (if close < open then -1 else if close > open then 1 else 0) else
if close>brk then 1 else if close<brk then -1 else dir;

plot dir_plot = round(dir,0);
dir_plot.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
dir_plot.SetDefaultColor(Color.white);``````

#### febreezus

##### New member
The main problem I can see with the code right now is that the "def brk" line should ideally read

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open else
if (close>brk and close>hi) or (close<brk and close<lo) then open else brk;

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open else
if (close>brk and close>close) or (close<brk and close<close) then open else brk;

But obviously I can't reference "hi" and "lo" before defining them, and I need to reference "brk" when defining "hi" and "lo"... hence my conundrum. Any help would be appreciated.

##### Active member
2019 Donor
The main problem I can see with the code right now is that the "def brk" line should ideally read

But obviously I can't reference "hi" and "lo" before defining them, and I need to reference "brk" when defining "hi" and "lo"... hence my conundrum. Any help would be appreciated.
there's a guy in here Robert Payne he has a thread that he offers help and this is the type of stuff he can help with, ask him he seems cool

#### febreezus

##### New member
Thanks, I'll do that.

#### febreezus

##### New member
Robert Payne helped me to figure out the variable issue. The code still needs work, but here it is as I've got it now.

Code:
``````def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk;
def hi;
def lo;
def dir;
def brk_lookback = 4;

brk = if bar1 then open else
if (close>brk and close>hi) or (close<brk and close<lo) then open[brk_lookback] else brk;
hi =  if bar1 then if close > open then close else open else
if dir == -1 and close > brk or (dir == 1 and close > hi) then close else hi;
lo =  if bar1 then if close > open then open else open else
if dir == 1 and close < brk or dir == -1 and close < lo then close else lo;
dir = if bar1 then (if close < open[brk_lookback] then -1 else if close > open[brk_lookback] then 1 else 0) else
if close>brk then 1 else if close<brk then -1 else dir;

AssignPriceColor(if dir==-1
then Color.RED
else Color.GREEN);``````

• ##### Active member
2019 Donor
He’s the man, good job

#### febreezus

##### New member
Here's a version that, rather than painting the trend, places a buy arrow 9 bars into a downtrend and a sell arrow 9 bars into an uptrend, as per the instructions.

Code:
``````def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk;
def hi;
def lo;
def dir;
def brk_lookback = 4;

brk = if bar1 then open else
if (close>brk and close>hi) or (close<brk and close<lo) then open[brk_lookback] else brk;
hi =  if bar1 then if close > open then close else open else
if dir == -1 and close > brk or (dir == 1 and close > hi) then close else hi;
lo =  if bar1 then if close > open then open else open else
if dir == 1 and close < brk or dir == -1 and close < lo then close else lo;
dir = if bar1 then (if close < open[brk_lookback] then -1 else if close > open[brk_lookback] then 1 else 0) else
if close>brk then 1 else if close<brk then -1 else dir;

def sum = sum(dir,9);
def sell_cond = close>close and close>close and close>close;
def buy_cond = close<close and close<close and close<close;

plot Buy = if buy_cond and sum==-9 and sum!=-9 then low else Double.NaN;
plot Sell = if sell_cond and sum==9 and sum!=9 then high else Double.NaN;
Sell.AssignValueColor(color.red);
Sell.SetPaintingStrategy(PaintingStrategy.arrow_down);
Sell.SetLineWeight(5);

#AssignPriceColor(if dir==-1
#                then Color.RED
#                 else Color.GREEN);``````

• ##### Active member
2019 Donor
Here's a version that, rather than painting the trend, places a buy arrow 9 bars into a downtrend and a sell arrow 9 bars into an uptrend, as per the instructions.

Code:
``````def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk;
def hi;
def lo;
def dir;
def brk_lookback = 4;

brk = if bar1 then open else
if (close>brk and close>hi) or (close<brk and close<lo) then open[brk_lookback] else brk;
hi =  if bar1 then if close > open then close else open else
if dir == -1 and close > brk or (dir == 1 and close > hi) then close else hi;
lo =  if bar1 then if close > open then open else open else
if dir == 1 and close < brk or dir == -1 and close < lo then close else lo;
dir = if bar1 then (if close < open[brk_lookback] then -1 else if close > open[brk_lookback] then 1 else 0) else
if close>brk then 1 else if close<brk then -1 else dir;

def sum = sum(dir,9);
def sell_cond = close>close and close>close and close>close;
def buy_cond = close<close and close<close and close<close;

plot Buy = if buy_cond and sum==-9 and sum!=-9 then low else Double.NaN;
plot Sell = if sell_cond and sum==9 and sum!=9 then high else Double.NaN;
Sell.AssignValueColor(color.red);
Sell.SetPaintingStrategy(PaintingStrategy.arrow_down);
Sell.SetLineWeight(5);

#AssignPriceColor(if dir==-1
#                then Color.RED
#                 else Color.GREEN);``````
nothing came up on the 3 min or 15 time frame

#### febreezus

##### New member
That's not an issue with the code. I don't know what symbol you're looking at, but you might need to expand the time frame to get a signal. I get signals on any symbol and aggregation period. I haven't done a close examination to see at which aggregation period these signals seem the most useful, but I've noticed I get far fewer the larger the aggregation period, 1 min giving the most.