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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[5] == 1 and beforeStart[4] == 0) or (isRollover[4] and beforeStart[4] == 0) then 1 else 0;


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


Anyone have any suggestions?
 

febreezus

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

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

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


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[1] else
if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[4] else brk[1];

instead of

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

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.
 

Trading51

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



instead of



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
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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 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[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[brk_lookback] else brk[1];
hi =  if bar1 then if close > open then close else open else
                 if dir[1] == -1 and close > brk[1] or (dir == 1 and close > hi[1]) then close else hi[1];
lo =  if bar1 then if close > open then open else open else
                 if dir[1] == 1 and close < brk[1] or dir == -1 and close < lo[1] then close else lo[1];
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[1] then 1 else if close<brk[1] then -1 else dir[1];


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

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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 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[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[brk_lookback] else brk[1];
hi =  if bar1 then if close > open then close else open else
                 if dir[1] == -1 and close > brk[1] or (dir == 1 and close > hi[1]) then close else hi[1];
lo =  if bar1 then if close > open then open else open else
                 if dir[1] == 1 and close < brk[1] or dir == -1 and close < lo[1] then close else lo[1];
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[1] then 1 else if close<brk[1] then -1 else dir[1];


def sum = sum(dir,9);
def sell_cond = close>close[1] and close[1]>close[2] and close[2]>close[3];
def buy_cond = close<close[1] and close[1]<close[2] and close[2]<close[3];

plot Buy = if buy_cond and sum==-9 and sum[1]!=-9 then low else Double.NaN;
Buy.AssignValueColor(color.green);
Buy.SetPaintingStrategy(PaintingStrategy.arrow_up);
Buy.SetLineWeight(5);
plot Sell = if sell_cond and sum==9 and sum[1]!=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);
 

Trading51

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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 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[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[brk_lookback] else brk[1];
hi =  if bar1 then if close > open then close else open else
                 if dir[1] == -1 and close > brk[1] or (dir == 1 and close > hi[1]) then close else hi[1];
lo =  if bar1 then if close > open then open else open else
                 if dir[1] == 1 and close < brk[1] or dir == -1 and close < lo[1] then close else lo[1];
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[1] then 1 else if close<brk[1] then -1 else dir[1];


def sum = sum(dir,9);
def sell_cond = close>close[1] and close[1]>close[2] and close[2]>close[3];
def buy_cond = close<close[1] and close[1]<close[2] and close[2]<close[3];

plot Buy = if buy_cond and sum==-9 and sum[1]!=-9 then low else Double.NaN;
Buy.AssignValueColor(color.green);
Buy.SetPaintingStrategy(PaintingStrategy.arrow_up);
Buy.SetLineWeight(5);
plot Sell = if sell_cond and sum==9 and sum[1]!=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.
 

Similar threads

Top