Autofib from yesterday close to 5 min close today


Category:
0
0

Ughh, i feel like I’m so close but my code isn’t quite working. I’m trying to get an autofib from yesterdays close to the close of the first 5 minute bar today (so close of 1600 to close at 935). Not the range of this time but the 1600 close being the 0% fibb and the close at 935 being the 100% fib. Heres what I have

 

 

input targetTime = 1555;

input fibOne = 50.0;

def targetBar = SecondsFromTime(targetTime) == 0;

rec targetclose = if targetBar then close else targetclose[1];

plot data = targetclose;

input marketOpen = 1555;

input marketClose = 935;

def startCounter = SecondsFromTime(marketOpen);

def endCounter = SecondsTillTime(marketClose);

def targetHours = if startCounter >= 0 and endCounter >= 0 then 1 else 0;

rec targetPeriodClose = marketClose;

rec targetPeriodOpen = marketopen; 

plot today = marketClose; 

plot yesterday = marketopen; 

def range = if !targetHours then today – yesterday else Double.NaN; 

plot fibLevelOne = if range > 0 then (range * (fibOne * 0.01)) + yesterday else Double.NaN;

 

Marked as spam
Posted by (Questions: 6, Answers: 18)
Asked on August 21, 2019 8:37 pm
113 views
0
Private answer

The Desire to Learn

Since you have included your code and have given it your best shot I see that you are trying to learn something here. So let's begin by examining your code and see where you went wrong. From this, we'll explore methods we can use to patch things up.

Code Review

In the following lines of code you take the user input for targetTime and use it to grab and hold the close of the last bar on the 5 min chart. That's great. The problem is you don't use any of these variables for the rest of the code. You use that value to plot on the chart. But it's never used again for the rest of the code.

def targetBar = SecondsFromTime(targetTime) == 0;
rec targetClose = if targetBar then close else targetClose[1];
plot data = targetClose;

Next, I'll skip ahead a few lines to show you another place where you just barely missed hitting your goal:

rec targetPeriodClose = marketClose;
rec targetPeriodOpen = marketOpen;

You tagged these as 'rec' however there is no recursion used in their formation. It is clear from the variable names what you intended them to hold but you have only assigned them the value of the user inputs: marketClose and marketOpen. So what you end up with is this:

targetPeriodClose = 935

targetPeriodOpen = 1555

But that's really a moot point, because once again we see that neither of these two variables are being used later on in the code. Then you repeat this exact same thing in the next two lines:

plot today = marketClose;
plot yesterday = marketOpen;

But it's pretty clear you actually intended the two variables to contain the 16:00 close and the close of 9:35. We can see that from the very next line of code:

def range = if !targetHours then today – yesterday else Double.NaN;

You are trying to compute the range between the 16:00 and 9:35 close however this is really what you end up with:

range = if targetHours is true then 935 - 1555 otherwise N/A

If I remove all the lines of code that are not being used for the very last statement here is what remains:

input fibOne = 50.0;
input marketOpen = 1555;
input marketClose = 935;
def startCounter = SecondsFromTime(marketOpen);
def endCounter = SecondsTillTime(marketClose);
def targetHours = if startCounter >= 0 and endCounter >= 0 then 1 else 0;
plot today = marketClose;
plot yesterday = marketOpen;
def range = if !targetHours then today – yesterday else Double.NaN;
plot fibLevelOne = if range > 0 then (range * (fibOne * 0.01)) + yesterday else Double.NaN;

It still doesn't work but you can see how that makes things so much easier to diagnose. You don't have a bunch of unused lines of code cluttering things up. Always reduce things to their most simplistic form. Only once you get that part working should you add to it.

The Solution

To solve this, I took the elements of your code that was working and added a few lines to fill in the gaps. You already had yesterday's close. So I duplicated those lines and tweaked them to grab today's 9:35 close. Then I took those values and assigned them to your plots, yesterday and today. Which then trickled down into the remaining lines of code to compute your 50% fibonacci level.

input targetTime = 1555;
input fibOne = 50.0;
input marketOpen = 1555;
input marketClose = 935;
def targetBarStart = SecondsFromTime(targetTime) == 0;
def targetBarEnd = SecondsFromTime(marketClose) == 0;
rec targetCloseStart = if targetBarStart then close else targetCloseStart[1];
rec targetCloseEnd = if targetBarEnd then close else targetCloseEnd[1];
def startCounter = SecondsFromTime(marketOpen);
def endCounter = SecondsTillTime(marketClose);
def targetHours = if startCounter >= 0 and endCounter >= 0 then 1 else 0;
plot today = targetCloseEnd;
plot yesterday = targetCloseStart;
def range = if !targetHours then today – yesterday else Double.NaN;
plot fibLevelOne = if range > 0 then (range * (fibOne * 0.01)) + yesterday else Double.NaN;

So, you had it right at some point. But as you added lines of code you lost track of stuff that was crucial to your success. Screenshot below shows the result.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 22, 2019 1:13 pm
0
Dang Pete, you out did yourself. Obviously thanks for the code but the education was even better. Thanks!
( at August 22, 2019 1:39 pm)
0
Any direction on how to get this to work if it gaps down instead of up? I know I need some language that talks about yesterday vs today close and which is < or > than the other
( at August 22, 2019 6:56 pm)
0
sorry, this about as far as I go for posts in the Q&A forum. If you require a full featured professional level indicator we can do that. But there are fees involved. Here in the Q&A forum we just take care of simple requests.
( at August 22, 2019 8:34 pm)
0
Ok no problem, I'm going to play around with it as I 've had fun learning. Thanks!
( at August 23, 2019 9:09 am)