Support Board
Date/Time: Sat, 23 Nov 2024 18:10:21 +0000
[Programming Help] - Determing Day of week for each bar on the chart
View Count: 258
[2024-08-11 02:46:18] |
Dryheat31 - Posts: 13 |
I have spent the last 2 hours trying to work this out on my own and can't find a way to get the day of week for each bar on the chart. For each bar on the chart, I just want to know which day of the week it is. Then I want to be able to use that as a condition for resetting various metrics in my code. I tried using GetDayOfWeek(), but that is not working at all. Even after I built an if/then/else structure to convert SUNDAY to an int of zero. Nothing I have tried has worked. Here is the reference I found for GetDayOfWeek(): Working with the SCDateTime Variables and Values: GetDayOfWeek() But this does not work the way I would expect to work given the information provided in that link: #include "sierrachart.h" SCDLLName("Test Day Of Week") SCSFExport scsf_TestDayOfWeek(SCStudyInterfaceRef sc) { SCSubgraphRef testPlot = sc.Subgraph[0]; SCFloatArrayRef weekDay = sc.Subgraph[1]; if (sc.SetDefaults) { sc.GraphName = "Test Day Of Week"; sc.ValueFormat = 2; testPlot.Name = "Day Of Week"; testPlot.DrawStyle = DRAWSTYLE_LINE; testPlot.DrawZeros = true; sc.FreeDLL = 1; return; } sc.DataStartIndex = 1; // indicator metrics begin here... SCDateTime scWeekDayString; if (scWeekDayString.GetDayOfWeek() == SUNDAY) { weekDay[sc.Index] = 0; } else if (scWeekDayString.GetDayOfWeek() == MONDAY) { weekDay[sc.Index] = 1; } else if (scWeekDayString.GetDayOfWeek() == TUESDAY) { weekDay[sc.Index] = 2; } else if (scWeekDayString.GetDayOfWeek() == WEDNESDAY) { weekDay[sc.Index] = 3; } else if (scWeekDayString.GetDayOfWeek() == THURSDAY) { weekDay[sc.Index] = 4; } else if (scWeekDayString.GetDayOfWeek() == FRIDAY) { weekDay[sc.Index] = 5; } else { weekDay[sc.Index] = 6; } testPlot[sc.Index] = weekDay[sc.Index]; } When you try to plot that it only gives a value for the last bar on the chart. And the value it plots is the current day of week (based on computer date/time) and NOT the day of week for the last bar on the chart. It seem that I need an equivalent to GetDayOfWeek that works as an SCFloatArrayRef ??? But where is that found? Or does it even exist? This is so simple to do in other trading platforms. I really appreciate all the features that Sierra Chart offers. But trying to write custom tools for Sierra Chart always requires at least 10 times the time and effort as any other trading platform I work with. Ugh! Please help. |
[2024-08-11 03:36:01] |
Tony - Posts: 516 |
I assume your code should look something like this: for (int IndexCount=0; IndexCount<=sc.Index; IndexCount++) { testPlot[IndexCount] = sc.BaseDateTimeIn[IndexCount].GetDayOfWeek(); } SUNDAY is the value 0, and MONDAY is the value 1, etc. your original code had unnecessary steps. A bit off topic and my personal experience, I have written a few indicators/studies with ThinkScrip, NinjaScript, PineScript and ACSIL. ACSIL is THE most powerful yet the easiest tool. I was a bit surprised that you didn't understand how indexes work, because all other trading platforms follow the exact same principle. Date Time Of Last Edit: 2024-08-11 03:44:44
|
[2024-08-11 04:13:45] |
Dryheat31 - Posts: 13 |
just by reading the code, it seems that you do have a lot of misunderstanding about ACSIL, your code would probably look like this:
Nope, that does not work either. I appreciate your efforts but the solution you suggested does not work. But you did give me an idea that helped me to solve it. I have developed many solutions for Sierra Chart and every time I have to work in this trading platform I absolutely dread it. I find it easier to build a trading tool in a language and trading platform I have never seen before. ACSIL is just about as complicated as NinjaTrader. The reason I had those "extra steps" was because the documentation for "GetDayOfWeek()" does not tell us whether it returns a String or an Integer. It merely tells us that it returns SUNDAY... well... what exactly is that? Is it a constant? If so, what is the value of those constants? Is it a string? If so, why is SUNDAY not in quotes. There is lot of essential details lacking in the language reference for this function. So I thank you for pointing out that we can trust that the output is integers of 0 through 6. If that language reference had stated that clearly it would have saved me two hours of wasted effort (that's a note to Sierra Chart engineers to update that section). But if that function does indeed return integers of 0 through 6, then my original solution would have worked, and I would have eventually reduced the complexity of the code to only the essential elements. It seems the fix that actually made this work was to replace SCDateTime.GetDayOfWeek() with sc.BaseDateTimeIn[sc.Index].GetDayOfWeek(). But where is the documentation for this? Here is the final solution that works, based on the idea I got from Tony's suggestion (thank you Tony): #include "sierrachart.h" SCDLLName("Test Day Of Week") SCSFExport scsf_TestDayOfWeek(SCStudyInterfaceRef sc) { SCSubgraphRef testPlot = sc.Subgraph[0]; SCFloatArrayRef weekDay = sc.Subgraph[1]; if (sc.SetDefaults) { sc.GraphName = "Test Day Of Week"; sc.ValueFormat = 2; testPlot.Name = "Day Of Week"; testPlot.DrawStyle = DRAWSTYLE_LINE; testPlot.DrawZeros = true; sc.AutoLoop = 1; sc.FreeDLL = 1; return; } sc.DataStartIndex = 1; // indicator metrics begin here... testPlot[sc.Index] = sc.BaseDateTimeIn[sc.Index].GetDayOfWeek(); } |
[2024-08-11 09:22:05] |
User431178 - Posts: 541 |
The reason I had those "extra steps" was because the documentation for "GetDayOfWeek()" does not tell us whether it returns a String or an Integer.
Read the documentation again, two lines below the heading it says int GetDayOfWeek();
the return type is documented as integer, using standard c++ function declaration format. But if that function does indeed return integers of 0 through 6, then my original solution would have worked, and I would have eventually reduced the complexity of the code to only the essential elements.
sc.DataStartIndex = 1; // indicator metrics begin here... SCDateTime scWeekDayString; if (scWeekDayString.GetDayOfWeek() == SUNDAY) { weekDay[sc.Index] = 0; How would your original solution would have worked? What is your expectation here? You are calling GetDayOfWeek() on a default constructed (i.e. empty) SCDateTime object, so how that can possibly do anything useful? It seems the fix that actually made this work was to replace SCDateTime.GetDayOfWeek() with sc.BaseDateTimeIn[sc.Index].GetDayOfWeek(). But where is the documentation for this?
sc.BaseDateTimeIn in an array of SCDateTime objects, so all you are doing call the GetDayOfWeek function on the SCDateTime element stored at sc.Index. ACSIL Interface Members - Variables and Arrays: sc.BaseDateTimeIn[] There is lot of essential details lacking in the language reference for this function.
If I am unsure of anything from the reference docs, the next step is to inspect the files provided in the ACS_Source folder.For example you could look at SCDateTime.h to find out more the SCDateTime member function GetDayOfWeek. |
[2024-08-11 16:41:01] |
Dryheat31 - Posts: 13 |
User431178: Read the documentation again, two lines below the heading it says
int GetDayOfWeek();
the return type is documented as integer, using standard c++ function declaration format. Thanks. I completely missed that. If the example provided with that section had used a value of 1 instead of MONDAY, this would have been easier to understand. I will provide a bit more detail on my thought processes while trying to work through this on my, in the hopes it may help others to avoid getting stuck like I did: When I am looking for solutions in the language reference, this is the very first thing my eye is drawn to: if (SCDateTimeVariable.GetDayOfWeek() == MONDAY)
{ // SCDateTimeVariable is a Monday } I tend to skip the description and go straight to the code snippet to understand how to use each function. Then I immediately model my own solution based on the example given. And once I get something working, I tweak it to fit my specific needs. Clearly I missed an important detail. But the description should have made it perfectly clear that: SUNDAY == 0
MONDAY == 1 TUESDAY == 2 .... But since it did not, I was lost in an endless loop of desperately trying to figure out how to use the example code snippet in my own solution. Which is exactly why I structured my original solution to model that same exact layout: "Check if SCDateTimeVariable.GetDayOfWeek() == MONDAY " then assign a value of 1. However that never worked. There was something else preventing it from working And I believe that was due to the difference between the example snippet provided in the documentation.... SCDateTimeVariable.GetDayOfWeek()
And the solution which finally worked as I expected.... sc.BaseDateTimeIn[sc.Index].GetDayOfWeek()
So for the benefit of those trying to learn how to navigate the language reference, please help us understand how we can make the jump from "SCDateTimeVariable.GetDayOfWeek()" to "sc.BaseDateTimeIn[sc.Index].GetDayOfWeek()", given the examples and descriptions provided in the following language reference: Working with the SCDateTime Variables and Values: GetDayOfWeek() It seems to me there needs to a link or explanation that there are other member of the SCDateTime class to which this function may be applied. And I really appreciate your very detailed response. I have learned a great deal about how to navigate the complexity of the language reference provided by Sierra Chart engineers. It is very detailed, but it's also very easy to stuck in a dead end or chasing rabbits down a hole. I don't think I ever would have made the jump to... ACSIL Interface Members - Variables and Arrays: sc.BaseDateTimeIn[] ... on my own. So thank for that. And I hope that others who run across this forum post will have a better understanding of how to navigate the language reference more effectively. Such as examining the SCDateTime.h file for more details when the language reference itself is lacking. Thanks again to Tony and User431178 for steering to the solution, but more importantly, for showing me a better way to work through this sort issue more effectively in the future. I am very grateful. |
[2024-08-11 19:37:36] |
Sawtooth - Posts: 4118 |
You can use any of the native spreadsheet studies using the WEEKDAY function, where Sunday=1...Saturday=7: =WEEKDAY(A3) You can also use the Spreadsheet Formula study, which despite its name, uses Alert syntax: =WEEKDAY(BARDATE) Date Time Of Last Edit: 2024-08-11 19:41:27
|
To post a message in this thread, you need to log in with your Sierra Chart account: