Doesn't anybody know by any chance (because I'm no programmer and I suppose will never be..) how to correct/modify/adapt the below (I don't even know a name for programme letters) to implement it to NT?
It's supposed to be volatility stop study found on Joe Ross' pages.
I don't expect anyone will rewrite it for me for free, but any advises on corrections at least?
Thank anyone in advance. Zdena.
float a, fN, loclose, hiclose, ma, b;
int x, xstrt, i, revCount=1;
a = b = 0.0; /* SET INITIAL AVERAGE VAL TO 0 */
fN = (float)n;
for (x=0; x<=end+5; x++)
{ /* zero out any possible garbage */
pVolstop[x].lstop = 0.0;
pVolstop[x].sstop = 0.0;
pVolstop[x].range = 0.0;
}
x = xstrt = 0;
/* protect against whatever 0's there might be at start of a tick file */
while (m_pChart->prices[x].cl == 0 && x <= end)
{
x++;
xstrt++;
}
if (xstrt >= end)
return; /* no data in this data base so return now */
// Set initial hiclose and loclose to something real for starters.
hiclose = loclose = m_pChart->prices[x].cl;
while (x < xstrt+n && x+voloff <= end+5)
{
a += m_pChart->prices[x].hi - m_pChart->prices[x].lo;
b += m_pChart->prices[x].cl;
x++;
if (m_pChart->prices[x].cl > hiclose) // init high and low close refs
hiclose = m_pChart->prices[x].cl;
if (m_pChart->prices[x].cl < loclose)
loclose = m_pChart->prices[x].cl;
}
//Initial movave. It is needed to determine whether he first N period trend is a long trend
// or a short trend. If the N close is above this value, it's a long trend. If below this
// point, it's a short trend.
ma = b / fN;
// FIRST Average range value FOR N DAY AVERAGE
pVolstop[xstrt+n-1+voloff].range = a / fN;
for (x = x; x+voloff <= end+5; x++)
{ // Determine the average values across the entire database basis the fN Period parameter.
// Each of these range values is calculated before the lstop and sstop values are calculated,
// and are saved
pVolstop[x+voloff].range = (pVolstop[x-1+voloff].range) +
(m_pChart->prices[x].hi - m_pChart->prices[x].lo -
(m_pChart->prices[x-n].hi - m_pChart->prices[x-n].lo))/fN;
}
if (ma <= m_pChart->prices[xstrt+n-1].cl)
{
// If initial n bar Moving Average is lower or equal to n day's close,
// calc the long stop to be below this close price.
// Also initialize the short stop to same value for making the short and long
// term trend test below possible.
pVolstop[xstrt+n-1+voloff].lstop =
hiclose - pVolstop[xstrt+n-1+voloff].range*mult;
x = xstrt+n;
revCount=1; // reversal count
goto lstop;
}
else
{
// If initial n bar Moving Average is higher than n day's close,
// calc the short stop to be above this close price.
// Also initialize the short stop to same value for making the short and long
// term trend test below possible.
pVolstop[xstrt+n-1+voloff].sstop =
loclose + pVolstop[xstrt+n-1+voloff].range*mult;
x = xstrt+n;
revCount=1; // reversal count
goto sstop;
}
// lstop represents a current long trend up, so the stop is drawn under the price bars
lstop:
for (x = x; x <= end; x++)
{
// This is the initial trend change close price equate that starts this
// long trend.
hiclose = m_pChart->prices[x].cl;
i = x;
while (pVolstop[i+voloff].sstop == 0.0f && i != -1)
// The sstop values equal 0 during a long lstop trend. This routine backward
// tests for the last sstop real value that signaled the end of the previous
// short trend and the start of this long trend. The hiclose must be the
// highest close detected in this long trend to satisfy the VS formula.
// At that trend change price bar, both the sstop and the lstop had non
// zero values. Thus, this while loop exits before this earlier trend change
// bar's close price can be processed by this while loop. However, be aware
// that the trend change bar's close price was in fact processed and included
// in the calculation of the highest hiclose value for this long trend by
// virtue of the fact that it is the first equate just below the for loop above.
// Remember, this while loop goes backward in time as the long trend continues
// from here across the database, causing more and more bars to be included
// in this long trend test.
{
if (m_pChart->prices[i].cl > hiclose)
hiclose = m_pChart->prices[i].cl;
i--;
}
if (x+voloff <= end+5)
// Calculate the lstops and test for a volatility stop reversal here
{
pVolstop[x+voloff].lstop = hiclose - pVolstop[x+voloff].range*mult;
if (m_pChart->prices[x].cl < pVolstop[x+voloff].lstop)
{
revCount++;
if (revCount > 5000)
// test for an impossible count to detect a infinite loop condition
// This would only occur when the VOLSTOP structure members were created as float values.
// Since late 2004 they were redefined as doubles, and that fixed this problem.
goto TimeoutExit;
goto sstop;
}
}
}
return;
sstop:
for (x = x; x <= end; x++)
{
// This is the initial trend change close price equate that starts this
// short trend.
loclose = m_pChart->prices[x].cl;
i = x;
while (pVolstop[i+voloff].lstop == 0.0f && i != -1)
// The lstop values equal 0 during a short sstop trend. This routine backward
// tests for the last lstop real value that signaled the end of the previous
// long trend and the start of this short trend. The loclose must be the
// lowest close detected in this short trend to satisfy the VS formula.
// At that trend change price bar, both the lstop and the sstop had non
// zero values. Thus, this while loop exits before this earlier trend change
// bar's close price can be processed by this while loop. However, be aware
// that the trend change bar's close price was in fact processed and included
// in the calculation of the lowest loclose value for this short trend by
// virtue of the fact that it is the first equate just below the for loop above.
// Remember, this while loop goes backward in time as the short trend continues
// from here across the database, causing more and more bars to be included
// in this short trend test.
{
if (m_pChart->prices[i].cl < loclose)
loclose = m_pChart->prices[i].cl;
i--;
}
if (x+voloff <= end+5)
// Test for a volatility stop reversal here
{
pVolstop[x+voloff].sstop = loclose + pVolstop[x+voloff].range*mult;
if (m_pChart->prices[x].cl > pVolstop[x+voloff].sstop)
{
revCount++;
if (revCount > 5000)
// test for an impossible count to detect a infinite loop condition
// This would only occur when the VOLSTOP structure members were created as float values.
// Since late 2004 they were redefined as doubles, and that fixed this problem.
goto TimeoutExit;
goto lstop;
}
}
}
return;
TimeoutExit:
// test for an impossible count to detect a infinite loop condition
CString sMsg = "The Volatility Stop calculation for this chart cannot be completed\n"
"because of a mathematical deadlock condition caused by extremely\n"
"low volatility at date ";
char sDate[10];
long lDate = m_pChart->prices[x].date;
if (lDate < 91232) // satisfies test for date 20091231
// This must be a 2000 to 2010 year, so add 20000000 to this number.
lDate = 20000000 + m_pChart->prices[x].date;
ltoa (lDate, sDate, 10);
sMsg += sDate;
sMsg += ". Use the Chart menu command\n"
"- Data Load Option, or change the Multiplier to a lower value\n"
"as a work-around for this problem...";
MessageBox (sMsg, "Volatility Stop timed out!", MB_ICONSTOP | MB_OK);
}
Comment