Fibonacci Scan between levels within X percentage


Category:
0
0

Hi Pete

I might be violating aggregation period, but if you can please review and advise.  Goal is to Scan this on 5 min (including extended hours checked) and result if the percent difference between these 2 fib levels meet. I can see the lines the on the chart at market open after first 5 min candle, but would like to run the as scan also.

 

def vClose = close;
def nan = Double.NaN;
input fib1val=1.272;
input fib2val=0.618;
input percentDiff=10;
input aggregationPeriod = AggregationPeriod.fIVE_MIN;
def showPrevClose = 0;
def showOpen = 0;
# logic
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or
(isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
(afterEnd[-1] == 1 and afterEnd == 0) or
(isRollOver[-1] and firstBarOfDay[-1])
then 1
else 0;
def dayOpen = if firstBarOfDay then vClose else dayOpen[1];
def prevDayClose = if lastBarOfDay then vClose else prevDayClose[1];
# fibs
def delta = if firstBarOfDay then dayOpen – prevDayClose else delta[1];
def fib1 = if firstBarOfDay then prevDayClose + delta * fib1val else fib1[1];
def fib2 = if firstBarOfDay then prevDayClose + delta * fib2val else fib2[1];
def fib =  if  firstBarOfDay then nan else fib1;
def pfib2=if  firstBarOfDay then nan else fib2;
def Diff = pfib2 – fib;
def tenPercentLower = fib * (1 – percentDiff * .01);
def tenPercentHigher = fib * (1 + percentDiff * .01);
def withinTenPercent = (pfib2 >= tenPercentLower and pfib2 < fib) or (pfib2 <= tenPercentHigher and pfib2 > fib);
plot scan = withinTenPercent and !withinTenPercent[1];

Thanks

Marked as spam
Posted by (Questions: 3, Answers: 3)
Asked on April 16, 2020 6:51 pm
227 views
0
Private answer

Ouch, definitely not the first puzzle I wanted to try to unravel first thing in the morning. Not sure if this does the trick but this is what I came up with for the final scan statement in your code:

plot scan = AbsValue(fib2 - fib1) / fib1 > percentDiff * 0.01;

For the ticker symbol I tested (AAPL) I had to reduce the user input for "percent diff" down to a value of 1 from it's default value of 10. This code ignores the first bar of the day (the plots you were trying to use report Double.NaN on the first bar of each day). So I had to use the fib1 and fib2 variables for the scan signal instead of the ones you tried to use (fib and pfib2). In this way the scan will work on the first bar of the day.

The aggregation period is never used in your code so you can just take that out completely. And the signal is the same for every bar on the chart (except for the opening bar). So you can run this on a 30 time frame and get the same exact results as on the 5 min time frame. It does not work on the 1 hour time frame but I didn't take the time to understand why.

I spent way to much time trying to unravel this.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 17, 2020 7:36 am