stc指标源码
int start()
{
if (Bars <= BarsRequired) return(0);
int counted_bars = IndicatorCounted();
double LLV, HHV;
int shift, n = 1, i;
// Static variables are used to flag that we already have calculated curves from the previous indicator run
static bool st1_pass = false;
static bool st2_pass = false;
int st1_count = 0;
bool check_st1 = false, check_st2 = false;
if (counted_bars < BarsRequired)
{
for (i = 1; i <= BarsRequired; i++) ST2[Bars - i] = 0;
for (i = 1; i <= BarsRequired; i++) ST[Bars - i] = 0;
}
if (counted_bars > 0) counted_bars--;
shift = Bars - counted_bars + BarsRequired - MALong;
if (shift > Bars - 1) shift = Bars - 1;
while (shift >= 0)
{
double MA_Short = iMA(NULL, 0, MAShort, 0, MODE_EMA, PRICE_CLOSE, shift);
double MA_Long = iMA(NULL, 0, MALong, 0, MODE_EMA, PRICE_CLOSE, shift);
MACD[shift] = MA_Short - MA_Long;
if (n >= Cycle) check_st1 = true;
else n++;
if (check_st1)
{
// Finding Max and Min on Cycle of MA differrences (MACD)
for (i = 0; i < Cycle; i++)
{
if (i == 0)
{
LLV = MACD[shift + i];
HHV = MACD[shift + i];
}
else
{
if (LLV > MACD[shift + i]) LLV = MACD[shift + i];
if (HHV < MACD[shift + i]) HHV = MACD[shift + i];
}
}
// Calculating first Stochastic
if (HHV - LLV != 0) ST[shift] = ((MACD[shift] - LLV) / (HHV - LLV)) * 100;
else {ST[shift] = ST[shift + 1];}
// Smoothing first Stochastic
if (st1_pass) ST[shift] = Factor * (ST[shift] - ST[shift + 1]) + ST[shift + 1];
st1_pass = true;
// Have enough elements of first Stochastic to proceed to second
if (st1_count >= Cycle) check_st2 = true;
else st1_count++;
if (check_st2)
{
// Finding Max and Min on Cycle of first smoothed Stoch
for (i = 0; i < Cycle; i++)
{
if (i == 0)
{
LLV = ST[shift + i];
HHV = ST[shift + i];
}
else
{
if (LLV > ST[shift + i]) LLV = ST[shift + i];
if (HHV < ST[shift + i]) HHV = ST[shift + i];
}
}
// Calculating second Stochastic
if (HHV - LLV != 0) ST2[shift] = ((ST[shift] - LLV) / (HHV - LLV)) * 100;
else {ST2[shift] = ST2[shift + 1];}
// Smoothing second Stochastic
if (st2_pass) ST2[shift] = Factor * (ST2[shift] - ST2[shift + 1]) + ST2[shift + 1];
st2_pass = true;
}
}
shift--;
}