Price Bands not printing correctly at end of chart
I have begun to delve into the world of programming with a bit of fear and trepidation. My goal has been to put together a custom indicator called “Price Bands” that will always maintain an equal distance between the top and bottom bands throughout, with price always in between. What will make the bands begin to move up or down is if price tries to break through one of the bands. If it breaks to the downside, both the top and bottom bands will begin to drop maintaining an equal distance in between. Price always stays in between the bands.
I have been able to take the simple Donchian channel study and modify it to fit what I want it to do, and my “Price Bands” work perfectly as the chart is loaded or refreshed. However, at the very end of the chart, as it adds more bars, the indicator does not maintain the equal distance. In fact, the indicators start to mimic the movement of price (Look at my two examples, one that is correct, the other incorrect).
(Sorry, I was going to post two pictures here showing what was happening, one working correctly, one incorrectly, but I guess I am supposed to have posted more posts to this site before being allowed to post any pictures. I didn't know that. Sorry! If anyone can give still give any advice, that would be appreciated.)
I have tried everything I could think of, but to no avail. It seems like when I turn “sc.FreeDLL = 0”, that makes it work better, but even then, not all the time. I am at a loss. Is there anyone who could give me some pointers on what I must do to make it perform correctly? I have included two different versions of code I have tried in attempt to solve the problem. Both work on a refresh, but not as new bars are added. I would greatly appreciate any help you could give me… Dan
Not sure. I may have solved my own problem. Both code examples work, though the first is simpler. But they seem to work only if I have one occurrence of the study in a chartbook. I'm not sure if by defining and using a global variable that when there is more than one occurrence of the study within the same chartbook but on different time frames, that the global variable information gets muddled between the two charts that the indicator is trying to print on??? Is that possible? Or does the global variable still maintain its independence from other occurrences that may be found in a chartbook. Any thoughts from a more experience programmer? Thanks in advance!
I have just got round to looking at your code. I don't know how it works by putting global variables outside of the routine. In ACSIL there is a way of defining persistent variables. I have fixed your code by using them. The way I do it is to load the variables with your minimum and maximum values in the set defaults block. Then, every time the routine is run, Highest and Lowest are loaded with the persistent variables. Just before exiting from the code, you then load the vairables with the current values of Highest and Lowest.
I have tested it and it works. There may be neater solutions, and there are alternatives that work, but this is how I do it. See the revised code below.
Hope this helps.
// The top of every source code file must include this line
// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies.
SCDLLName("Price Bands DLL")
sc.GraphName = "Price Bands";
// During development set this flag to 1, so the DLL can be rebuilt without restarting Sierra Chart.
// When development is completed, set it to 0 to improve performance.
sc.FreeDLL = 0;
sc.GraphRegion = 0;
sc.AutoLoop = true;
Yes! It works! Thank you swandro! I didn't realize there was such a thing as "persistent variables" that would carry values across iterations. I thought global variables were the key, but they kept giving me problems, so I learned something here. So, again, thank you!
I had seen an indicator like this used elsewhere and really grew to like it because of how it helps me to see when price is consolidating or is trending. I have also now added a second price band to overlay the first. The outer bands I "ignore", but the mid -band, I made just three points wide, with color based on slope--blue if going up and red if going down. It is clean, simple, and sweet.
The following user says Thank You to vdhoek for this post: