Scan Help RSI Fractal indicator

M

Miket

Member
The RSI Fractal indicator is really solid when you have an up or down arrow. I've tried to create a scan to show me stocks that have a up or down arrow within the last bar but have failed. I'm trying to scan for stocks at the end of the day primarily for swing trade candidates. Any help would be greatly appreciated.

Code:
# RSIwithFractalPivots_Amalia

# reposted to chatroom 04.18.2018



########START  Code



declare lower;



input length = 14;

input over_Bought = 70;

input over_Sold = 30;

input price = close;

input averageType = AverageType.WILDERS;

input New_HI_LO = 30;





def NetChgAvg = MovingAverage(averageType, price - price[1], length);

def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);

def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;



plot RSI = 50 * (ChgRatio + 1);

plot OverSold = over_Sold;

plot OverBought = over_Bought;



RSI.DefineColor("OverBought", GetColor(5));

RSI.DefineColor("Normal", GetColor(7));

RSI.DefineColor("OverSold", GetColor(1));

RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));

OverSold.SetDefaultColor(GetColor(8));

OverBought.SetDefaultColor(GetColor(8));



plot NEWHIGH = if RSI == highest(RSI, new_HI_LO) then RSI else Double.NAN; 

NEWHIGH.setpaintingStrategy(paintingStrategy.POINTS);

NEWHIGH.setdefaultColor(color.light_green);

NewHIGH.setlineWeight(1);



plot NEWLOW = if RSI == lowest(RSI, new_HI_LO) then RSI else Double.NAN; 

NEWLOW.setpaintingStrategy(paintingStrategy.POINTS);

NEWLOW.setdefaultColor(color.light_RED);

NEWLOW.setlineWeight(1);



# Fractals

# 1/5/17 Amalia added Aggregation periods?



def H = RSI; 

def L = RSI; 



input sequenceCount = 10;



def maxSideLength = sequenceCount + 10;

def upRightSide = fold i1 = 1 to maxSideLength + 1 

                  with count1 

                  while count1 != sequenceCount and 

                        count1 != -1 

                  do if GetValue(H, -i1) > H or 

                       (GetValue(H, -i1) == H and

                        count1 == 0) 

                     then -1

                     else if GetValue(H, -i1) < H 

                          then count1 + 1 

                          else count1;

def upLeftSide = fold i2 = 1 to maxSideLength + 1 

                 with count2 

                 while count2 != sequenceCount and 

                       count2 != -1 

                 do if GetValue(H, i2) > H or 

                      (GetValue(H, i2) == H and 

                       count2 >= 1) 

                    then -1

                    else if GetValue(H, i2) < H 

                         then count2 + 1 

                         else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 

                    with count3 

                    while count3 != sequenceCount and 

                          count3 != -1 

                    do if GetValue(L, -i3) < L or 

                         (GetValue(L, -i3) == L and 

                          count3 == 0) 

                          then -1

                        else if GetValue(H, -i3) > L 

                             then count3 + 1 

                             else count3;

def downLeftSide = fold i4 = 1 to maxSideLength + 1 

                   with count4 

                   while count4 != sequenceCount and 

                         count4 != -1

                   do if GetValue(L, i4) < L or 

                        (GetValue(L, i4) == L and 

                         count4 >= 1) 

                      then -1

                      else if GetValue(L, i4) > L 

                      then count4 + 1 

                      else count4;



plot UpFractal = if upRightSide == sequenceCount and 

                    upLeftSide == sequenceCount 

                 then RSI 

                 else Double.NaN;

plot DownFractal = if downRightSide == sequenceCount and 

                      downLeftSide == sequenceCount 

                   then RSI

                   else Double.NaN;



UpFractal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

UpFractal.SetDefaultColor(color.RED);

UpFractal.SetLineWeight(2);

DownFractal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownFractal.SetDefaultColor(color.GREEN);

DownFractal.SetLineWeight(2);





def Pre_hi = if !isnan(upfractal) then upfractal else pre_hi[1];



plot high_line = pre_hi;

high_line.setpaintingStrategy(paintingStrategy.DASHES);

high_line.setdefaultColor(color.CYAN);



def Pre_lo = if !isnan(downFractal) then downFractal else pre_lo[1];



plot lo_line = pre_lo;

lo_line.setpaintingStrategy(paintingStrategy.DASHES);

lo_line.setdefaultColor(color.MAGENTA);



addlabel(yes, 

if RSI >= OverBought && RSI >= RSI[1]

    then "RSI OB & Rising: " +round(RSI, 2)

else if RSI >= OverBought && RSI < RSI[1]

    then "RSI OB & Falling: " +round(RSI, 2)

else if RSI <= OverSold && RSI < RSI[1]

    then "RSI OS & Falling: " +round(RSI, 2)

else if RSI <= OverSold && RSI >= RSI[1]

    then "RSI OS & Rising: " +round(RSI, 2)

else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]

    then "RSI Rising: " +round(RSI, 2)

else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]

    then "RSI Falling: " +round(RSI, 2)

else "",

if RSI >= OverBought && RSI >= RSI[1]

    then color.dark_orange

else if RSI >= OverBought && RSI < RSI[1]

    then color.yellow

else if RSI <= OverSold && RSI < RSI[1]

    then createcolor(000, 100, 100)

else if RSI <= OverSold && RSI >= RSI[1]

    then color.cyan

else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]

    then color.green

else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]

    then color.red

else color.gray);





plot Buffer = if RSI <= OverSold then RSI - 10 else if RSI >= OverBought then RSI + 10 else double.nan;

Buffer.setdefaultColor(color.black);

Buffer.hidetitle();

Buffer.hidebubble();



####END Code
 
M

Miket

Member
Did that. The scan is giving me over 13000 stocks of which they don't have the red arrow or green arrow trigger. It's the trigger I am looking for within 1 bar of a daily timeframe.
 
T

tomsk

Well-known member
VIP
Warehouse
The RSI Fractal indicator is really solid when you have an up or down arrow. I've tried to create a scan to show me stocks that have a up or down arrow within the last bar but have failed. I'm trying to scan for stocks at the end of the day primarily for swing trade candidates. Any help would be greatly appreciated.

# RSIwithFractalPivots_Amalia

# reposted to chatroom 04.18.2018



########START Code



declare lower;



input length = 14;

input over_Bought = 70;

input over_Sold = 30;

input price = close;

input averageType = AverageType.WILDERS;

input New_HI_LO = 30;





def NetChgAvg = MovingAverage(averageType, price - price[1], length);

def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);

def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;



plot RSI = 50 * (ChgRatio + 1);

plot OverSold = over_Sold;

plot OverBought = over_Bought;



RSI.DefineColor("OverBought", GetColor(5));

RSI.DefineColor("Normal", GetColor(7));

RSI.DefineColor("OverSold", GetColor(1));

RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));

OverSold.SetDefaultColor(GetColor(8));

OverBought.SetDefaultColor(GetColor(8));



plot NEWHIGH = if RSI == highest(RSI, new_HI_LO) then RSI else Double.NAN;

NEWHIGH.setpaintingStrategy(paintingStrategy.POINTS);

NEWHIGH.setdefaultColor(color.light_green);

NewHIGH.setlineWeight(1);



plot NEWLOW = if RSI == lowest(RSI, new_HI_LO) then RSI else Double.NAN;

NEWLOW.setpaintingStrategy(paintingStrategy.POINTS);

NEWLOW.setdefaultColor(color.light_RED);

NEWLOW.setlineWeight(1);



# Fractals

# 1/5/17 Amalia added Aggregation periods?



def H = RSI;

def L = RSI;



input sequenceCount = 10;



def maxSideLength = sequenceCount + 10;

def upRightSide = fold i1 = 1 to maxSideLength + 1

with count1

while count1 != sequenceCount and

count1 != -1

do if GetValue(H, -i1) > H or

(GetValue(H, -i1) == H and

count1 == 0)

then -1

else if GetValue(H, -i1) < H

then count1 + 1

else count1;

def upLeftSide = fold i2 = 1 to maxSideLength + 1

with count2

while count2 != sequenceCount and

count2 != -1

do if GetValue(H, i2) > H or

(GetValue(H, i2) == H and

count2 >= 1)

then -1

else if GetValue(H, i2) < H

then count2 + 1

else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1

with count3

while count3 != sequenceCount and

count3 != -1

do if GetValue(L, -i3) < L or

(GetValue(L, -i3) == L and

count3 == 0)

then -1

else if GetValue(H, -i3) > L

then count3 + 1

else count3;

def downLeftSide = fold i4 = 1 to maxSideLength + 1

with count4

while count4 != sequenceCount and

count4 != -1

do if GetValue(L, i4) < L or

(GetValue(L, i4) == L and

count4 >= 1)

then -1

else if GetValue(L, i4) > L

then count4 + 1

else count4;



plot UpFractal = if upRightSide == sequenceCount and

upLeftSide == sequenceCount

then RSI

else Double.NaN;

plot DownFractal = if downRightSide == sequenceCount and

downLeftSide == sequenceCount

then RSI

else Double.NaN;



UpFractal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

UpFractal.SetDefaultColor(color.RED);

UpFractal.SetLineWeight(2);

DownFractal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownFractal.SetDefaultColor(color.GREEN);

DownFractal.SetLineWeight(2);





def Pre_hi = if !isnan(upfractal) then upfractal else pre_hi[1];



plot high_line = pre_hi;

high_line.setpaintingStrategy(paintingStrategy.DASHES);

high_line.setdefaultColor(color.CYAN);



def Pre_lo = if !isnan(downFractal) then downFractal else pre_lo[1];



plot lo_line = pre_lo;

lo_line.setpaintingStrategy(paintingStrategy.DASHES);

lo_line.setdefaultColor(color.MAGENTA);



addlabel(yes,

if RSI >= OverBought && RSI >= RSI[1]

then "RSI OB & Rising: " +round(RSI, 2)

else if RSI >= OverBought && RSI < RSI[1]

then "RSI OB & Falling: " +round(RSI, 2)

else if RSI <= OverSold && RSI < RSI[1]

then "RSI OS & Falling: " +round(RSI, 2)

else if RSI <= OverSold && RSI >= RSI[1]

then "RSI OS & Rising: " +round(RSI, 2)

else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]

then "RSI Rising: " +round(RSI, 2)

else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]

then "RSI Falling: " +round(RSI, 2)

else "",

if RSI >= OverBought && RSI >= RSI[1]

then color.dark_orange

else if RSI >= OverBought && RSI < RSI[1]

then color.yellow

else if RSI <= OverSold && RSI < RSI[1]

then createcolor(000, 100, 100)

else if RSI <= OverSold && RSI >= RSI[1]

then color.cyan

else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]

then color.green

else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]

then color.red

else color.gray);





plot Buffer = if RSI <= OverSold then RSI - 10 else if RSI >= OverBought then RSI + 10 else double.nan;

Buffer.setdefaultColor(color.black);

Buffer.hidetitle();

Buffer.hidebubble();



####END Code

@Miket This is a rather complex study. The formatting of this study seems to be off, I spent some time to put this into a format that makes reading of code easier. Based on the code logic of the original author, I converted the study into a scan.

Note that there are two plot statements in the scan. I have configured this for the bearish case.
If you'd like to use the bullish case scan, just comment out the bearish scan and uncomment the bullish scan. All this is listed at the end of the file

In case you're not sure, please read the following post.


I have tested this in the scanner, currently there are no results so you'll need to wait until there is a buy/sell signal.
Note that I used the author's code logic for the scan engine, i.e. I made no logic changes, just converted this to a scan
Additionally I have removed all extraneous statements .g. intermediate plots, AddLabels that the scan engine does not require

Here then is your converted SCAN. All the best

Code:
# RSI with Fractal Pivots Scan
# tomsk
# 12.2.2019

# Converted this to a scan based on the original author's code logic

# RSI with Fractal Pivots
# Amalia
# reposted to chatroom 04.18.2018

input length = 14;
input price = close;
input averageType = AverageType.WILDERS;
input New_HI_LO = 30;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def NEWHIGH = if RSI == highest(RSI, new_HI_LO) then RSI else Double.NAN;
def NEWLOW = if RSI == lowest(RSI, new_HI_LO) then RSI else Double.NAN;

# Fractals
# 1/5/17 Amalia added Aggregation periods?

def H = RSI;
def L = RSI;

input sequenceCount = 10;

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1
                  with count1
                  while count1 != sequenceCount and
                        count1 != -1
                  do if GetValue(H, -i1) > H or
                       (GetValue(H, -i1) == H and
                        count1 == 0)
                     then -1
                     else if GetValue(H, -i1) < H
                          then count1 + 1
                          else count1;

def upLeftSide = fold i2 = 1 to maxSideLength + 1
                 with count2
                 while count2 != sequenceCount and
                       count2 != -1
                 do if GetValue(H, i2) > H or
                      (GetValue(H, i2) == H and
                       count2 >= 1)
                    then -1
                    else if GetValue(H, i2) < H
                         then count2 + 1
                         else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1
                    with count3
                    while count3 != sequenceCount and
                          count3 != -1
                    do if GetValue(L, -i3) < L or
                         (GetValue(L, -i3) == L and
                          count3 == 0)
                       then -1
                       else if GetValue(H, -i3) > L
                            then count3 + 1
                            else count3;

def downLeftSide = fold i4 = 1 to maxSideLength + 1
                   with count4
                   while count4 != sequenceCount and
                         count4 != -1
                   do if GetValue(L, i4) < L or
                        (GetValue(L, i4) == L and
                         count4 >= 1)
                      then -1
                      else if GetValue(L, i4) > L
                           then count4 + 1
                           else count4;

def bearish = upRightSide == sequenceCount and upLeftSide == sequenceCount;     # UpFractal
def bullish = downRightSide == sequenceCount and downLeftSide == sequenceCount; # DownFractal

# Comment out (#) the PLOT not needed
plot scan = bearish;
#plot scan = bullish;

# End RSI with Fractal Pivots Scan
 
M

Miket

Member
@tomsk Thanks again. Currently, I have a few hundred stocks on my grid looking for a signal. It gets tiring after awhile.
 
T

tomsk

Well-known member
VIP
Warehouse
Should anyone be interested in the reformatted version of the study from @Miket , I'll be happy to post
 
M

Miket

Member
Should we get some results if we query the database using a weekly time frame? Currently I am getting nothing.
 
T

tomsk

Well-known member
VIP
Warehouse
@Miket READ my note above regarding the fact that you'll need to wait for the scans to produce results
Signals don't happen all the time. You asked for assistance to make it scanable - i did just that
 
M

Miket

Member
Roger that. I was hopeful that something would pop up on a longer time frame from the scan so that way its confirmed that the scan will work on a shorter time frame. I know a few of my stocks had signals a few weeks ago and was curious to see of they would pop up. When they didn't that's when I posted. [email protected] I appreciate the help.
 
markos

markos

Well-known member
VIP
Warehouse
The author, Amalia, day trade trades /YM. That's only one symbol. It may be that he waits for a trigger on that.
Then again, maybe not. Sorry I can't me of more help.
 

Top