30 Star 78 Fork 79

liuyinghua/FastTrader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
KDJ.cpp 4.23 KB
一键复制 编辑 原始数据 按行查看 历史
liuyinghua 提交于 2018-05-08 01:05 +08:00 . 整个项目的编译
#include "KDJ.h"
#include "Tech.h"
#include "../FacilityBaseLib/KData.h"
//////////////////////////////////////////////////////////////////////
// CKDJ
CKDJ::CKDJ( )
{
SetDefaultParameters( );
}
CKDJ::CKDJ( KdataContainer * pKData )
: TechnicalIndicator( pKData )
{
SetDefaultParameters( );
}
CKDJ::~CKDJ()
{
clear( );
}
void CKDJ::SetDefaultParameters( )
{
m_nRSVDays = 9;
m_nKDays = 3;
m_nDDays = 3;
m_nJ = 1;
m_KDTrustValue.first = 25;
m_KDTrustValue.second = 75;
m_itsGoldenFork = ITS_BUY;
m_itsDeadFork = ITS_SELL;
}
void CKDJ::attach( CKDJ & src )
{
m_nRSVDays = src.m_nRSVDays;
m_nKDays = src.m_nKDays;
m_nDDays = src.m_nDDays;
m_nJ = src.m_nJ;
m_itsGoldenFork = src.m_itsGoldenFork;
m_itsDeadFork = src.m_itsDeadFork;
}
bool CKDJ::IsValidParameters( )
{
return ( VALID_DAYS(m_nRSVDays) && VALID_DAYS(m_nKDays)
&& VALID_DAYS(m_nDDays) && VALID_DAYS(m_nJ)
&& VALID_ITS(m_itsGoldenFork) && VALID_ITS(m_itsDeadFork) );
}
void CKDJ::clear( )
{
TechnicalIndicator::clear( );
}
// 计算RSV值
bool CKDJ::CalculateRSV(double * pValue, size_t nIndex)
{
STT_ASSERT_CALCULATE1( m_pKData, nIndex );
double dH = 0, dL = 0, dRSV = 0;
int nCount = 0;
for( int k=nIndex; k>=0; k-- )
{
if( nIndex == k )
{
dH = m_pKData->at(k).HighestPrice;
dL = m_pKData->at(k).LowestPrice;
}
if( dH < m_pKData->at(k).HighestPrice )
dH = m_pKData->at(k).HighestPrice;
if( dL > m_pKData->at(k).LowestPrice )
dL = m_pKData->at(k).LowestPrice;
nCount ++;
if( nCount == m_nRSVDays )
{
if( dH-dL < 1e-4 )
dRSV = 100;
else
dRSV = (m_pKData->at(nIndex).ClosePrice - dL) * 100 / (dH - dL);
if( pValue ) *pValue = dRSV;
return true;
}
}
return false;
}
int CKDJ::signal( size_t nIndex, uint32_t * pnCode )
{
if( pnCode ) *pnCode = ITSC_NOTHING;
if( !prepare_cache( 0, -1, false ) )
return ITS_NOTHING;
double dK, dD, dJ;
if( !calc( &dK, &dD, &dJ, nIndex, false ) )
return ITS_NOTHING;
if (dK < m_KDTrustValue.first && dD < m_KDTrustValue.first && is_golden_fork(nIndex, m_pdCache1, m_pdCache2))
{ // 低位金叉;
if( pnCode ) *pnCode = ITSC_GOLDENFORK;
return m_itsGoldenFork;
}
if (dK > m_KDTrustValue.second && dD > m_KDTrustValue.second && is_dead_fork(nIndex, m_pdCache1, m_pdCache2))
{ // 高位死叉;
if( pnCode ) *pnCode = ITSC_DEADFORK;
return m_itsDeadFork;
}
return ITS_NOTHING;
}
bool CKDJ::min_max_info(size_t nStart, size_t nEnd, double *pdMin, double *pdMax)
{
return AfxGetMinMaxInfo3( nStart, nEnd, pdMin, pdMax, this );
}
/***
AX = 今天的收盘价 - N天中的最低价
BX = N天中的最高价 - N天中的最低价
RSV = (AX ÷ BX)× 100%
NK = K计算天数,一般取3
ND = D计算天数,一般取3
K = 前一日K×(NK-1)/NK + RSV×1/NK
D = 前一日D×(ND-1)/ND + K×1/3
J = 3D - 2K (或 J = 3K - 2D)
第一次计算时,前一日的K、D值皆以50代替。
*/
bool CKDJ::calc(double *pValue1, double *pValue2, double *pValue3, size_t nIndex, bool bUseLast)
{
STT_ASSERT_CALCULATE1( m_pKData, nIndex );
if( m_nRSVDays > nIndex+1 )
return false;
if( load_from_cache( nIndex, pValue1, pValue2, pValue3 ) )
return true;
double dRSV = 0;
double dK = 50, dD = 50, dJ = 50;
if( bUseLast && pValue1 && pValue2 )
{
if( CalculateRSV( &dRSV, nIndex ) )
{
dK = (m_nKDays-1)*(*pValue1)/m_nKDays + dRSV/m_nKDays;
dD = (m_nDDays-1)*(*pValue2)/m_nDDays + dK/m_nDDays;
if( mode3K2D == m_nJ )
dJ = 3 * dK - 2 * dD;
else
dJ = 3 * dD - 2 * dK;
if( pValue1 ) *pValue1 = dK;
if( pValue2 ) *pValue2 = dD;
if( pValue3 ) *pValue3 = dJ;
store_to_cache( nIndex, pValue1, pValue2, pValue3 );
return true;
}
}
else
{
double factor1 = 1, factor2 = 1;
int k;
for( k=nIndex; k > 0; k-- )
{
factor1 *= ((double)(m_nKDays-1))/m_nKDays;
factor2 *= ((double)(m_nDDays-1))/m_nDDays;
if( factor1 < 0.001 && factor2 < 0.001 )
break;
}
for( ; k<=nIndex; k++ )
{
if( CalculateRSV( &dRSV, k ) )
{
dK = (m_nKDays-1)*(dK)/m_nKDays + dRSV/m_nKDays;
dD = (m_nDDays-1)*(dD)/m_nDDays + dK/m_nDDays;
if( mode3K2D == m_nJ )
dJ = 3 * dK - 2 * dD;
else
dJ = 3 * dD - 2 * dK;
if( nIndex == k )
{
if( pValue1 ) *pValue1 = dK;
if( pValue2 ) *pValue2 = dD;
if( pValue3 ) *pValue3 = dJ;
store_to_cache( nIndex, pValue1, pValue2, pValue3 );
return true;
}
}
}
}
return false;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mongodb_client/FastTrader.git
[email protected]:mongodb_client/FastTrader.git
mongodb_client
FastTrader
FastTrader
master

搜索帮助