NexusFi: Find Your Edge


Home Menu

 





Price Bands not printing correctly at end of chart


Discussion in Sierra Chart

Updated
    1. trending_up 1,302 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Price Bands not printing correctly at end of chart

  #1 (permalink)
 vdhoek 
Grand Rapids MI / USA
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Stage 5 / Advantage Futures
Trading: Futures ES
Posts: 4 since Nov 2014
Thanks Given: 7
Thanks Received: 1

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


First attempt:

 
Code
// The top of every source code file must include this line
#include "sierrachart.h"

// 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")

float Lowest = FLT_MAX;
float Highest = -FLT_MAX;
int BarIndex;
	
	
SCSFExport scsf_PriceBands(SCStudyInterfaceRef sc)
{
	SCSubgraphRef HighestHigh = sc.Subgraph[0];
	SCSubgraphRef LowestLow = sc.Subgraph[1];
	SCSubgraphRef Midline = sc.Subgraph[2];
	SCInputRef Length = sc.Input[3];
	
	
	if (sc.SetDefaults)
	{
		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;

		HighestHigh.Name= "Highest High";
		HighestHigh.DrawStyle=  DRAWSTYLE_LINE;
		HighestHigh.PrimaryColor = RGB(255,206,206);
		HighestHigh.LineWidth = 48;
		HighestHigh.DrawZeros= true;
		HighestHigh.GraphicalDisplacement= 1;

		LowestLow.Name = "Lowest Low";
		LowestLow.DrawStyle=  DRAWSTYLE_LINE;
		LowestLow.PrimaryColor = RGB(183,255,183);
		LowestLow.LineWidth = 48;
		LowestLow.DrawZeros= true;
		LowestLow.GraphicalDisplacement= 1;

		Midline.Name = "Mid Line";
		Midline.DrawStyle=  DRAWSTYLE_LINE;
		Midline.PrimaryColor = RGB(232,232,232);
		Midline.LineWidth = 56;
		Midline.DrawZeros= true;
		Midline.GraphicalDisplacement= 1;

		Length.Name = "Band Width in Ticks";
		Length.SetInt(48);
		Length.SetIntLimits(1, MAX_STUDY_LENGTH);

		return;
	}
	
	float BandWidth = Length.GetInt() * sc.TickSize;
	float VertOffsetTop = sc.TickSize * (HighestHigh.LineWidth / 16.0f);
	float VertOffsetBot = sc.TickSize * (LowestLow.LineWidth / 16.0f);
	
	
	BarIndex = sc.Index - 1;
		
	if(sc.High[BarIndex] > Highest)
	{	
		Highest = sc.High[BarIndex];
		Lowest = Highest - BandWidth;
	}
	
	if(sc.Low[BarIndex] < Lowest)
	{	
		Lowest = sc.Low[BarIndex];
		Highest = Lowest + BandWidth;
	}
	
	HighestHigh[sc.Index] = Highest - VertOffsetTop;
	LowestLow[sc.Index] = Lowest + VertOffsetBot;
	Midline[sc.Index] = (Highest + Lowest) / 2.0f;
	
	//Following lines added for debugging, as needed
	//SCString Buffer;	
	//Buffer.Format("%d -- %f -- %f -- %f -- %f", sc.Index, sc.High[sc.Index], sc.Low[sc.Index], Highest, Lowest);
	//sc.AddMessageToLog(Buffer,1);
}


Second attempt:

 
Code
// The top of every source code file must include this line
#include "sierrachart.h"

// 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")

float Lowest = FLT_MAX;
float Highest = -FLT_MAX;
int BarIndex;
	
	
SCSFExport scsf_PriceBands(SCStudyInterfaceRef sc)
{
	SCSubgraphRef HighestHigh = sc.Subgraph[0];
	SCSubgraphRef LowestLow = sc.Subgraph[1];
	SCSubgraphRef Midline = sc.Subgraph[2];
	SCInputRef Width = sc.Input[3];
	
	
	if (sc.SetDefaults)
	{
		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;

		HighestHigh.Name= "Highest High";
		HighestHigh.DrawStyle=  DRAWSTYLE_LINE;
		HighestHigh.PrimaryColor = RGB(255,206,206);
		HighestHigh.LineWidth = 48;
		HighestHigh.DrawZeros= true;
		HighestHigh.GraphicalDisplacement= 1;

		LowestLow.Name = "Lowest Low";
		LowestLow.DrawStyle=  DRAWSTYLE_LINE;
		LowestLow.PrimaryColor = RGB(183,255,183);
		LowestLow.LineWidth = 48;
		LowestLow.DrawZeros= true;
		LowestLow.GraphicalDisplacement= 1;

		Midline.Name = "Mid Line";
		Midline.DrawStyle=  DRAWSTYLE_LINE;
		Midline.PrimaryColor = RGB(232,232,232);
		Midline.LineWidth = 56;
		Midline.DrawZeros= true;
		Midline.GraphicalDisplacement= 1;

		Width.Name = "Band Width in Ticks";
		Width.SetInt(48);
		Width.SetIntLimits(1, MAX_STUDY_LENGTH);

		return;
	}
	
	float BandWidth = Width.GetInt() * sc.TickSize;
	float VertOffsetTop = sc.TickSize * (HighestHigh.LineWidth / 16.0f);
	float VertOffsetBot = sc.TickSize * (LowestLow.LineWidth / 16.0f);
	
		
	for(BarIndex = sc.Index - 1; BarIndex <= sc.Index; BarIndex++)
	{		
		if(sc.BaseDataIn[SC_HIGH][BarIndex] > Highest)
		{	
			Highest = sc.BaseDataIn[SC_HIGH][BarIndex];
			Lowest = Highest - BandWidth;
		}
	
		if(sc.BaseDataIn[SC_LOW][BarIndex] < Lowest)
		{	
			Lowest = sc.BaseDataIn[SC_LOW][BarIndex];
			Highest = Lowest + BandWidth;
		}
	}
	
	HighestHigh[sc.Index] = Highest - VertOffsetTop;
	LowestLow[sc.Index] = Lowest + VertOffsetBot;
	Midline[sc.Index] = (Highest + Lowest) / 2.0f;
	
	//Following lines added for debugging, as needed
	//SCString Buffer;	
	//Buffer.Format("%d -- %f -- %f -- %f -- %f", sc.Index, sc.High[sc.Index], sc.Low[sc.Index], Highest, Lowest);
	//sc.AddMessageToLog(Buffer,1);
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Better Renko Gaps
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Spoo-nalysis ES e-mini futures S&P 500
28 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 vdhoek 
Grand Rapids MI / USA
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Stage 5 / Advantage Futures
Trading: Futures ES
Posts: 4 since Nov 2014
Thanks Given: 7
Thanks Received: 1


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!

Started this thread Reply With Quote
  #4 (permalink)
 swandro 
England
 
Experience: Advanced
Platform: SierraChart
Posts: 70 since Jul 2009
Thanks Given: 9
Thanks Received: 80

Hi

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
#include "sierrachart.h"

// 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")

int BarIndex;


SCSFExport scsf_PriceBands(SCStudyInterfaceRef sc)
{
SCSubgraphRef HighestHigh = sc.Subgraph[0];
SCSubgraphRef LowestLow = sc.Subgraph[1];
SCSubgraphRef Midline = sc.Subgraph[2];
SCInputRef Width = sc.Input[3];

if (sc.SetDefaults)
{
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;

HighestHigh.Name= "Highest High";
HighestHigh.DrawStyle= DRAWSTYLE_LINE;
HighestHigh.PrimaryColor = RGB(255,206,206);
HighestHigh.LineWidth = 48;
HighestHigh.DrawZeros= true;
HighestHigh.GraphicalDisplacement= 1;

LowestLow.Name = "Lowest Low";
LowestLow.DrawStyle= DRAWSTYLE_LINE;
LowestLow.PrimaryColor = RGB(183,255,183);
LowestLow.LineWidth = 48;
LowestLow.DrawZeros= true;
LowestLow.GraphicalDisplacement= 1;

Midline.Name = "Mid Line";
Midline.DrawStyle= DRAWSTYLE_LINE;
Midline.PrimaryColor = RGB(232,232,232);
Midline.LineWidth = 56;
Midline.DrawZeros= true;
Midline.GraphicalDisplacement= 1;

Width.Name = "Band Width in Ticks";
Width.SetInt(48);
Width.SetIntLimits(1, MAX_STUDY_LENGTH);

sc.PersistVars->f1 = FLT_MAX;
sc.PersistVars->f2 = -FLT_MAX;

return;
}

float BandWidth = Width.GetInt() * sc.TickSize;
float VertOffsetTop = sc.TickSize * (HighestHigh.LineWidth / 16.0f);
float VertOffsetBot = sc.TickSize * (LowestLow.LineWidth / 16.0f);

float Highest = sc.PersistVars->f1;
float Lowest = sc.PersistVars->f2;

for(BarIndex = sc.Index - 1; BarIndex <= sc.Index; BarIndex++)
{
if(sc.BaseDataIn[SC_HIGH][BarIndex] > Highest)
{
Highest = sc.BaseDataIn[SC_HIGH][BarIndex];
Lowest = Highest - BandWidth;
}

if(sc.BaseDataIn[SC_LOW][BarIndex] < Lowest)
{
Lowest = sc.BaseDataIn[SC_LOW][BarIndex];
Highest = Lowest + BandWidth;
}
}

HighestHigh[sc.Index] = Highest - VertOffsetTop;
LowestLow[sc.Index] = Lowest + VertOffsetBot;
Midline[sc.Index] = (Highest + Lowest) / 2.0f;

sc.PersistVars->f1 = Highest;
sc.PersistVars->f2 = Lowest;


//Following lines added for debugging, as needed
//SCString Buffer;
//Buffer.Format("%d -- %f -- %f -- %f -- %f", sc.Index, sc.High[sc.Index], sc.Low[sc.Index], Highest, Lowest);
//sc.AddMessageToLog(Buffer,1);
}

Reply With Quote
Thanked by:
  #5 (permalink)
 vdhoek 
Grand Rapids MI / USA
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Stage 5 / Advantage Futures
Trading: Futures ES
Posts: 4 since Nov 2014
Thanks Given: 7
Thanks Received: 1

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.

Started this thread Reply With Quote
Thanked by:




Last Updated on July 2, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts