19 April 2010 - 09:00
I am now at the stage where I am going back and learning about how to work with date/time values in Sierra Chart. This is necessary for several of the studies I need to transition from TradeStation to Sierra Chart. I will add to this posting as I "learn" and develop small code segments.
Monday, April 19, 2010
Wednesday, April 14, 2010
Todays Project - Volume Oscillator
Here is what I am working on today. This is a screenshot from my Tradestation platform.
This is an indicator which takes price action and volume into account. Plots include overbought / oversold lines, zero line showing bias long / short or overbought/oversold, normal range lines, indicator plot with trigger line. An option in the Tradestation version allows plotting standard deviations instead of fixed overbought/oversold lines. Not sure I'll build that into the first version for Sierra Chart or not.
Update 16:11 CT
Finished the first version of the volume oscillator for Sierra Chart. I included the calculation for the standard deviations but I did not add the option of plotting them at this time. Much like the eSignal and NinjaTrader versions the values are different from the values in Tradestion on tick charts. This may be an artifact of the tick charts themselves not having the exact starting points. What I learned - on the oscillator the zero line will not be plotted unless you use allow it in the code. My quick work around was to set the value to 0.000001 and use that. I learned if you set the subgraph value to 0 it does not plot. I guess that matches the boolean logic from the paint bar studies.
Here are screen shots of Tradestation, Sierra Chart, and NinjaTrader versions.
Update 16:11 CT
Finished the first version of the volume oscillator for Sierra Chart. I included the calculation for the standard deviations but I did not add the option of plotting them at this time. Much like the eSignal and NinjaTrader versions the values are different from the values in Tradestion on tick charts. This may be an artifact of the tick charts themselves not having the exact starting points. What I learned - on the oscillator the zero line will not be plotted unless you use allow it in the code. My quick work around was to set the value to 0.000001 and use that. I learned if you set the subgraph value to 0 it does not plot. I guess that matches the boolean logic from the paint bar studies.
Here are screen shots of Tradestation, Sierra Chart, and NinjaTrader versions.
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
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
Tuesday, April 13, 2010
Todays Project - DMI Ratio Indicator
Todays project is converting the DMI Ratio indicator from Tradestation easy language to ASCIL.
Update - 13:06
Ok finished the indicator. I had the usual problems with the variable definitions. Worked through them again. Today I "learned" how to set primary and secondary color for a plot. I also learned how to use the autocoloring.
sc.Subgraph[2].Name = "DMI Ratio";
sc.Subgraph[2].DrawStyle = DRAWSTYLE_BAR;
sc.Subgraph[2].PrimaryColor = RGB(0,255,0);
sc.Subgraph[2].SecondaryColor = RGB(255,128,128);
sc.Subgraph[2].SecondaryColorUsed = 1;
sc.Subgraph[2].LineWidth = 3;
sc.Subgraph[2].AutoColoring = AUTOCOLOR_POSNEG;
This indicator took me 2 hours to code up. I will get faster and better at this in time. I used the DMI function in Sierra Chart and then defined my DMI+ and DMI- to output or Subgraphs from function. Calculated my ratio variable and defined the output subgraph to the calculated ratio variable.
Here is a screenshot
Update - 13:06
Ok finished the indicator. I had the usual problems with the variable definitions. Worked through them again. Today I "learned" how to set primary and secondary color for a plot. I also learned how to use the autocoloring.
sc.Subgraph[2].Name = "DMI Ratio";
sc.Subgraph[2].DrawStyle = DRAWSTYLE_BAR;
sc.Subgraph[2].PrimaryColor = RGB(0,255,0);
sc.Subgraph[2].SecondaryColor = RGB(255,128,128);
sc.Subgraph[2].SecondaryColorUsed = 1;
sc.Subgraph[2].LineWidth = 3;
sc.Subgraph[2].AutoColoring = AUTOCOLOR_POSNEG;
This indicator took me 2 hours to code up. I will get faster and better at this in time. I used the DMI function in Sierra Chart and then defined my DMI+ and DMI- to output or Subgraphs from function. Calculated my ratio variable and defined the output subgraph to the calculated ratio variable.
Here is a screenshot
Thursday, April 8, 2010
Progress Update - Syntax
I have converted one of my TradeStation indicators - Advanced Moving Average into Sierra Charts. I coded everything up and compiled the study. When I applied it to the chart it did not match the TradeStation chart.
After loading Microsoft Visual C ++ Express Edition so I could debug the code (that is what Sierra Chart recommends) then trying to figure out how to use the debugger (I would not say I figured it out - but got it to give me the information I wanted) I still did not get the displays to match. At this point I went into brute force mode and starting breaking the individual components out and plotting them. This approach led me to the answer. Here is where I messed up ...
In Tradestation I can use Close[5] to give me the data for the Close 5 bars back. So in Sierra Chart ACSIL I did this
sc.BaseDataIn[SC_HIGH][Value5] where Value5 was the number of bars back I wanted the High. Well - that just reference the High value that was stored in the array in position Value5. Now if one looks at the documentation one would realize that. However, I am so accustom to TradeStation Easy Language syntax that I could not "see" the error. Of course the code compiled ok so... after several hours and the brute force approach I realized I needed to write the formula as
sc.BaseDataIn[SC_HIGH][sc.Index-Value5]
instead. When I did that everything worked just like it was supposed to. It appears I have to use the sc.Index on anything that is an array.
I am going in search of how to define color as an input variable.
After loading Microsoft Visual C ++ Express Edition so I could debug the code (that is what Sierra Chart recommends) then trying to figure out how to use the debugger (I would not say I figured it out - but got it to give me the information I wanted) I still did not get the displays to match. At this point I went into brute force mode and starting breaking the individual components out and plotting them. This approach led me to the answer. Here is where I messed up ...
In Tradestation I can use Close[5] to give me the data for the Close 5 bars back. So in Sierra Chart ACSIL I did this
sc.BaseDataIn[SC_HIGH][Value5] where Value5 was the number of bars back I wanted the High. Well - that just reference the High value that was stored in the array in position Value5. Now if one looks at the documentation one would realize that. However, I am so accustom to TradeStation Easy Language syntax that I could not "see" the error. Of course the code compiled ok so... after several hours and the brute force approach I realized I needed to write the formula as
sc.BaseDataIn[SC_HIGH][sc.Index-Value5]
instead. When I did that everything worked just like it was supposed to. It appears I have to use the sc.Index on anything that is an array.
I am going in search of how to define color as an input variable.
Sierra Chart - Advance Custom Study Development - The Learning Curve
I am starting this blog to chronicle my learning curve on programming studies for Sierra Chart. I have been programming indicators for TradeStation® for around seven years and have several custom indicators that help in my trading. I have a plan to translate the key indicators to other platforms like Sierra Charts, NinjaTrader, eSignal, Open eCry, etc.
Now I must admit I am not a programmer. Showing my age - I had to take Fortran IV when I was in college as part of my degree requirements for my engineering degree. Other than a few other classes which required programming I have no formal education. I just "brute force" my way through it all.
So here is what I see will be some of the key challenges:
I will post "solutions" to this blog as I work through them.
Now I must admit I am not a programmer. Showing my age - I had to take Fortran IV when I was in college as part of my degree requirements for my engineering degree. Other than a few other classes which required programming I have no formal education. I just "brute force" my way through it all.
So here is what I see will be some of the key challenges:
- Learning the C ++ language and the "structure" of programming with C ++ in the Sierra Chart
- Learning how to "display" the indicators as I want to see them
- Changing plot colors based upon condition
- Changing plot size based upon condition
- Only plotting when a condition is satisfied
- Filling in areas between two plots or shading background
- Putting text on the screen that stays in the location I placed it and / or dynamically adjusts
- Placing horizontal lines on the screen that either extend left/right or are fixed length
- Placing vertical lines on the screen that either extend up/down or are fixed length
- Calculating indicators/studies based upon time of day - example 1st hour high / 1st hour low, globex session etc.
- Calculating indicators based on different time frames - weekly, daily, hourly, etc
- Calculating indicators based on multiple inputs - multiple time frame like 5, 30 min data and several instruments
- How to color the price bar based upon a condition being met and let the bars be normal when it is not.
I will post "solutions" to this blog as I work through them.
Subscribe to:
Posts (Atom)