Wednesday, April 14, 2010

Coloring the Price Bar

After completing the DMI Ratio study yesterday I began investigating how to color the price bars based upon an indicator or some other criteria.

On the Sierra Chart forum I found some code that colored price bars based upon the MACD and code that simply colored bars based upon Close > previous High or Close < previous Low.  Here is a snippet of code I found:

sg.GraphRegion = 0;


sg.Subgraph[0].Name = "Close > previous H";
sg.Subgraph[0].DrawStyle = DRAWSTYLE_COLORBAR;
sg.Subgraph[0].PrimaryColor = RGB(0,255,0);

This is the code in the data processing area:
 
float lastprice = sg.BaseDataIn[SC_LAST][sg.Index];

float priorhigh = sg.BaseDataIn[SC_HIGH][sg.Index-1];
float priorlow = sg.BaseDataIn[SC_LOW][sg.Index-1];

sg.Subgraph[0][sg.Index] = 0;
sg.Subgraph[1][sg.Index] = 0;

if (lastprice > priorhigh)
{
     sg.Subgraph[0][sg.Index] = 1;
     sg.Subgraph[0].DataColor[sg.Index] = sg.Subgraph[0].PrimaryColor;
}
else if (lastprice < priorlow)
{
     sg.Subgraph[1][sg.Index] = 1;
     sg.Subgraph[1].DataColor[sg.Index] = sg.Subgraph[1].PrimaryColor;
}

I don't know exactly why the sg.Subgraph[0][sg.Index] is set to 0 prior to the conditional statement and then set to 1 if the condition is satisfied.  I only assume this is like binary for true / false.  I will have to find out about that. 

Found this post explaining what is going on:  https://www.sierrachart.com/supportboard/archive/index.php/t-19637.html and some commented code that says:

if (sg.SetDefaults)

{
     sg.GraphName = "xyz";
     sg.StudyDescription = "xyz";
     sg.GraphRegion = 0;
     sg.Subgraph[0].Name = "xyz";

// Special flag that works with sg.Subgraph[n][i] when sg.GraphRegion=0 and
// DrawStyle is COLORBAR or COLORBARHOLLOW.
// In that case sg.Subgraph[n][i] does not behave as a "subgraph" but as a
// color control switch that gets incremented through the loop (0=OFF, 1=ON);
// see "Data processing" section below.

     sg.Subgraph[0].DrawStyle = DRAWSTYLE_COLORBAR;
     sg.Subgraph[0].SecondaryColorUsed = 1; // true
     sg.Subgraph[0].PrimaryColor = RGB(0,185,47); // green
     sg.Subgraph[0].SecondaryColor = RGB(255,0,0); // red
     sg.Subgraph[1].DrawStyle = DRAWSTYLE_COLORBARHOLLOW;

// your code here

// Data processing

for (int i = sg.UpdateStartIndex; i < sg.ArraySize; i++)
{
     // you code here

if (CloseVal > MovAvg)
{
//   bar color control switch
     sg.Subgraph[0][i] = 0; // colour bars are OFF
     sg.Subgraph[1][i] = 1; // colour bars are ON
     sg.Subgraph[1].DataColor[i] = sg.Subgraph[1].PrimaryColor;


Notice the sg.GraphRegion = 0; line. GraphRegion 0 is the same as Region 1 for the display as the array counts from zero not one.  This places the study in Region 1 of the chart.

I have also found some code snippet that uses sg.GraphDrawType = GDT_OHLCBAR.

This creates OHLC bars - looking at this as an overlay maybe??


Here is a screen shot of the DMI Ratio Study and the DMI Ratio Paintbar Study

No comments:

Post a Comment