I am working on a study in which I would like to compare todays high or low to Thursday and Fridays high/low. Once the next thursday and friday pass the value becomes that high/low . Any idea how to just capture the previous thursday/friday high low numbers?
Announcement
Collapse
No announcement yet.
High/Low on Friday
Collapse
X
-
Hello Jeff,
What you need to do is create some global variables to store these values. Then on each bar you would get the date of the bar and then assign a local variable to the day of the week.
var myDate = getValue("time");
The obove creates a Date Object, which you would then use to look at the day of the week using it's .getDay() method.
var dayOfWeek = myDate.getDay();
The dayOfWeek variable will contain a number from 0 to 6. Sunday is 0, therefore Thursday is 4 and Friday is 5. Then add a condition that checks for the value of 4 and assign the daily high and low to the global variables.
PHP Code:if (dayOfWeek == 4) { // Thu
nThuHigh = high(0, inv("D"));
nThuLow = low(0, inv("D"));
}
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
-
Jason, thank you for your reply. With your help I have made progress. I am stumped on one detail: I want the High/Low of Thursday or Friday whichever day has the highest or lowest value. if thurs h=1200 fri h=1000 thurs low = 900 Friday low =800. I want the values of 1200 and 800. In addition what approach should I take to look back specifically to those 2 days. I understand the process of, if today is Thursday = true the value = x, but if today is Tuesday how do I look back to those two days to get the highest high and lowest low for the previous Thursday and Friday?
Comment
-
Hello Jeff,
To collect the max or min values between those two days you would only need two global variables. You would then use the Math Object's Math.max() and Math.min() functions to collect the highest and lowest values. You won't need to "look back" to get the values from the previous as long as the values are assigned to global variables (i.e. declared outside of main() ). They will remain available on subsequent days until the new Thursday and Friday arrive, at which point you will need to reset the variables at the beginning of the day on Thursday.
PHP Code:if (dayOfWeek == 4 || dayOfWeek == 5) { // Thu or Fri
if (dayOfWeek == 4 && day(-1) != day(0) ) {
// beginnig of day on thu, reset variables
nTFHigh = high(0, inv("D"));
nTFLow = low(0, inv("D"));
}
nTFHigh = Math.max(nTFHigh, high(0, inv("D")) );
nTFLow = Math.min(nTFLow, low(0, inv("D")) );
}
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
-
Resetting the variable
If we need to keep the values till after the present Friday has passed, that is keep the previous Thu Fri - Highest High and Lowest Low till after the present Thu Fri -Highest High and Lowest Low have been determined, how can the efs be modified.
Can that be done by changing the line where begining of Thu resets the variable
PHP Code:if (dayOfWeek == 4 && day(-1) != day(0) ) {
PHP Code:if (dayOfWeek == 1 && day(-1) != day(0) ) {
AK
Comment
-
Hello akiri,
One method would be to create two sets of global variables where one stores the previous week's high and low and the other stores the current weeks. When the Thursday of the current week is found, pass the values of the current week variables to the previous week variables. Then assign the new current weeks values to the current week variables.
Add the global variables, nTFHigh_1 and nTFLow_1.
Then, inside the condition that detects the beginning of the current Thursday, pass the current week variables to the previous week variables.
PHP Code:var nTFHigh = null;
var nTFLow = null;
// previous week global variables
var nTFHigh_1 = null;
var nTFLow_1 = null;
function main() {
...
if (dayOfWeek == 4 || dayOfWeek == 5) { // Thu or Fri
if (dayOfWeek == 4 && day(-1) != day(0)) {
// add the following
if (getBarState() == BARSTATE_NEWBAR) { // for real time processing
nTFHigh_1 = nTFHigh;
nTFLow_1 = nTFLow;
}
// beginnig of day on thu, reset variables
nTFHigh = high(0, inv("D"));
nTFLow = low(0, inv("D"));
}
nTFHigh = Math.max(nTFHigh, high(0, inv("D")) );
nTFLow = Math.min(nTFLow, low(0, inv("D")) );
}
...
return new Array(nTFHigh_1, nTFLow_1);
}
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
-
Code Check Please
JasonK thanks for the code I tried to implement a version and there is no error but no output either. Here is the code for it. Could anybody point me in the right direction
I am trying to draw the highest high and lowest low of 2 days Thu and Fri last week and retain that line till end of next Friday (that is till a new highest high and lowest low of current Thu Fri set in)
PHP Code:var t1 = addLibrary("shUtilities.efsLib");
function preMain() {
setPriceStudy(true);
setStudyTitle("Test");
}
var myDate = null;
var dayOfWeek = null;
var nTFHigh = null;
var nTFLow = null;
var nTFHigh_1 = null;
var nTFLow_1 = null;
function main() {
var myDate = getValue("Time");
var dayOfWeek = myDate.getDay();
if (dayOfWeek == 4 || dayOfWeek == 5) { // Sun=0 Mon=1 Tue=2 Wed=3 Thu=4 Fri=5
if (dayOfWeek == 4 && day(-1) != day(0) ) {
if (getBarState() == BARSTATE_NEWBAR) {
nTFHigh_1 = nTFHigh;
nTFLow_1 = nTFLow;
}
nTFHigh = high(0, inv("D"));
nTFLow = low(0, inv("D"));
}
nTFHigh = Math.max(nTFHigh, high(0, inv("D")) );
nTFLow = Math.min(nTFLow, low(0, inv("D")) );
}
//return new Array(nTFHigh_1, nTFLow_1);
var nBarIndex = null;
var nTime;
var nPrice;
//get the rawtime value for the current bar
nTime = getValue( "rawtime", 0 );
if ( nTime != null ) {
//get the bar index to the first bar of the date in nTime
nBarIndex = Math.abs(getFirstBarIndexOfDay( nTime ));
drawLineRelative(-nBarIndex,nTFHigh_1(0),3,nTFHigh_1(0),PS_DASH,3,Color.black,"nTFhigh_1");
drawTextAbsolute(3, nTFHigh_1, "nTFhigh_1=" +nTFHigh_1.round2(), Color.black, null, Text.FRAME | Text.VCENTER | Text.BOLD, "Arial", 14, "nTFhigh_1");
drawLineRelative(-nBarIndex,nTFLow_1,3,nTFLow_1,PS_DASH,3,Color.black,"nTFLow_1");
drawTextAbsolute(3, nTFLow_1, "nTFLow_1=" +nTFLow_1.round2(), Color.black, null, Text.FRAME | Text.VCENTER | Text.BOLD, "Arial", 14, "nTFLow_1");
return;
}
}
Comment
-
Hello akiri,
The first thing you need to add is an initial validation condition for the global variables, nTFHigh and nTFLow. It's not necessary to do this for nTFHigh, but is a good programming practice. Add the highlighted conditional block before your conditional block that assigns the values for these variables on Thursdays and Fridays.
This is particularly important for nTFLow because it is declared outside of main() as null. Therefore the Math.min() function will always assign null to the variable. When plotted on the chart, it will be null or 0.
The reason your current formula doesn't plot anything is caused by two issues. First your return statement in main() is not returning any values to be plotted. However, prior to the return statement at the end of main() you are executing some drawing functions that do not have valid parameters. You're making a function call out of a global variable, nTFHigh_1(0). With the (0) added to the end of the variable, EFS is trying execute a function with that name while passing 0 as a parameter. This is valid syntax but because the function does not exist in your formula EFS exits the formula. Change these parameters to remove the (0).
At this point your code should generate an error that nTFHigh_1 has no properties. This occurs at initialization or the beginning of processing because on the first bar that gets processed, nTFHigh_1 is still null. To correct this error you can add a conditional check for a non-null value of this variable in the associated if() statement that also validates nTime.
The next issue that you will encounter is the use of a method, .round2(). This function again, does not exist in the core functions of EFS and causes an exit. This may be a function defined within the shUtilities library that you've added at the top of your formula, but you are not using it correctly. You may want to review Steve's documentation on his library functions. One alternative you could use is the .toFixed(2) function in it's place.
At this point, you can return some values from the return statement to plot values. Try the following.
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
-
Thanks
JasonK
Thanks for the help and the explanations.
Its working now, instead of toFixed I am using priceFormatNumber and that way on bond and notes chart it shows correct formatting.
In version 10 the text flashes intermittently on the chart for some time, and then the flashing stops. The lines do not flash
I don't have any flashing codes in there. Any thoughts?
Anybody else experiencing that problem with their efs scripts.
Comment
-
Hello akiri,
You're most welcome. Good thinking on the formatPriceNumber().
Regarding the flashing graphics in 10.0, we are aware of that problem. It should be fixed in the next version. There may be a work-around we can explore for the time being. Please post your completed formula code that has the flashing. Alternatively you may revert back to 8.0 until the next release is available.Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
-
akiri
You are most welcome.
I would be curious to see the script you are using so as to run some tests on it because as far as I can see at my end drawTextRelative() should work. Just to confirm this see the enclosed animation showing two running copies of the script posted in this thread, one using drawTextAbsolute() - which as expected was flashing - and the other using drawTextRelative() which appeared to work fine.
Alex
PS. Note that the value os nTFHigh is different from what you may see at your end because I displaced it by 0.0052 to move it closer to where the market was trading
Comment
Comment