📄 test_rsi.c
字号:
/* TA-LIB Copyright (c) 1999-2007, Mario Fortier * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither name of author nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* List of contributors: * * Initial Name/description * ------------------------------------------------------------------- * MF Mario Fortier * * * Change history: * * MMDDYY BY Description * ------------------------------------------------------------------- * 112400 MF First version. * 112605 MF Add CMO test. *//* Description: * Test RSI/CMO function. *//**** Headers ****/#include <stdio.h>#include <string.h>#include "ta_test_priv.h"#include "ta_test_func.h"#include "ta_utility.h"/**** External functions declarations. ****//* None *//**** External variables declarations. ****//* None *//**** Global variables definitions. ****//* None *//**** Local declarations. ****/typedef enum {TA_RSI_TEST,TA_CMO_TEST} TA_TestId;typedef struct{ TA_Integer doRangeTestFlag; TA_TestId theFunction; TA_Integer unstablePeriod; TA_Integer startIdx; TA_Integer endIdx; TA_Integer optInTimePeriod; TA_Integer compatibility; TA_RetCode expectedRetCode; TA_Integer oneOfTheExpectedOutRealIndex0; TA_Real oneOfTheExpectedOutReal0; TA_Integer expectedBegIdx; TA_Integer expectedNbElement;} TA_Test;typedef struct{ const TA_Test *test; const TA_Real *close;} TA_RangeTestParam;/**** Local functions declarations. ****/static ErrorNumber do_test( const TA_History *history, const TA_Test *test );/**** Local variables definitions. ****/static TA_Test tableTest[] ={ /**********************/ /* RSI TEST */ /**********************/ { 1, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 252-14 }, /* First Value */ { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 1, 52.32, 14, 252-14 }, { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 2, 46.07, 14, 252-14 }, { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 252-15, 49.63, 14, 252-14 }, /* Last Value */ /* No output value. */ { 0, TA_RSI_TEST, 0, 1, 1, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 0, 0, 0}, /* One value tests. */ { 0, TA_RSI_TEST, 0, 14, 14, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 1}, /* Index too low test. */ { 0, TA_RSI_TEST, 0, 0, 15, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 2}, { 0, TA_RSI_TEST, 0, 1, 15, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 2}, { 0, TA_RSI_TEST, 0, 2, 16, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 3}, { 0, TA_RSI_TEST, 0, 2, 16, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 1, 52.32, 14, 3}, { 0, TA_RSI_TEST, 0, 2, 16, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 2, 46.07, 14, 3}, { 0, TA_RSI_TEST, 0, 0, 14, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 1}, { 0, TA_RSI_TEST, 0, 0, 13, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 49.14, 14, 0}, /* Test with 1 unstable price bar. Test for period 1, 2, 14 */ { 0, TA_RSI_TEST, 1, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 52.32, 15, 252-(14+1) }, { 0, TA_RSI_TEST, 1, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 1, 46.07, 15, 252-(14+1) }, { 0, TA_RSI_TEST, 1, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 252-(15+1), 49.63, 15, 252-(14+1) }, /* Last Value */ /* Test with 2 unstable price bar. Test for period 1, 2, 14 */ { 0, TA_RSI_TEST, 2, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, 46.07, 16, 252-(14+2) }, { 0, TA_RSI_TEST, 2, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 252-(15+2), 49.63, 16, 252-(14+2) }, /* Last Value */ /**********************/ /* RSI Metastock TEST */ /**********************/ { 1, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 252-13 }, /* First Value */ { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 1, 49.14, 13, 252-13 }, { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 2, 52.32, 13, 252-13 }, { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 3, 46.07, 13, 252-13 }, { 0, TA_RSI_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 252-14, 49.63, 13, 252-13 }, /* Last Value */ /* No output value. */ { 0, TA_RSI_TEST, 0, 1, 1, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 0, 0, 0}, /* One value tests. */ { 0, TA_RSI_TEST, 0, 13, 13, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 1}, { 0, TA_RSI_TEST, 0, 13, 13, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 1}, /* Index too low test. */ { 0, TA_RSI_TEST, 0, 0, 15, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 3}, { 0, TA_RSI_TEST, 0, 1, 15, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 3}, { 0, TA_RSI_TEST, 0, 2, 16, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 4}, { 0, TA_RSI_TEST, 0, 2, 16, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 1, 49.14, 13, 4}, { 0, TA_RSI_TEST, 0, 2, 16, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 2, 52.32, 13, 4}, { 0, TA_RSI_TEST, 0, 0, 14, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 2}, { 0, TA_RSI_TEST, 0, 0, 13, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 1}, { 0, TA_RSI_TEST, 0, 0, 12, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 47.11, 13, 0}, /* Test with 1 unstable price bar. Test for period 1, 2, 14 */ { 0, TA_RSI_TEST, 1, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 49.14, 14, 252-(13+1) }, { 0, TA_RSI_TEST, 1, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 1, 52.32, 14, 252-(13+1) }, { 0, TA_RSI_TEST, 1, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 252-(14+1), 49.63, 14, 252-(13+1) }, /* Last Value */ /* Test with 2 unstable price bar. Test for period 1, 2, 14 */ { 0, TA_RSI_TEST, 2, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, 52.32, 15, 252-(13+2) }, { 0, TA_RSI_TEST, 2, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 252-(14+2), 49.63, 15, 252-(13+2) }, /* Last Value */ /**********************/ /* CMO TEST */ /**********************/ { 1, TA_CMO_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_DEFAULT, TA_SUCCESS, 0, -1.70, 14, 252-14 }, { 1, TA_CMO_TEST, 0, 0, 251, 14, TA_COMPATIBILITY_METASTOCK, TA_SUCCESS, 0, -5.76, 13, 252-13 },};#define NB_TEST (sizeof(tableTest)/sizeof(TA_Test))/**** Global functions definitions. ****/ErrorNumber test_func_rsi( TA_History *history ){ unsigned int i; ErrorNumber retValue; /* Re-initialize all the unstable period to zero. */ TA_SetUnstablePeriod( TA_FUNC_UNST_ALL, 0 ); for( i=0; i < NB_TEST; i++ ) { if( (int)tableTest[i].expectedNbElement > (int)history->nbBars ) { printf( "%s Failed Bad Parameter for Test #%d (%d,%d)\n", __FILE__, i, tableTest[i].expectedNbElement, history->nbBars ); return TA_TESTUTIL_TFRR_BAD_PARAM; } retValue = do_test( history, &tableTest[i] ); if( retValue != 0 ) { printf( "%s Failed Test #%d (Code=%d)\n", __FILE__, i, retValue ); return retValue; } } /* Re-initialize all the unstable period to zero. */ TA_SetUnstablePeriod( TA_FUNC_UNST_ALL, 0 ); /* All test succeed. */ return TA_TEST_PASS; }/**** Local functions definitions. ****/static TA_RetCode rangeTestFunction( TA_Integer startIdx, TA_Integer endIdx, TA_Real *outputBuffer, TA_Integer *outputBufferInt, TA_Integer *outBegIdx, TA_Integer *outNbElement, TA_Integer *lookback, void *opaqueData, unsigned int outputNb, unsigned int *isOutputInteger ){ TA_RetCode retCode; TA_RangeTestParam *testParam; (void)outputNb; (void)outputBufferInt; *isOutputInteger = 0; testParam = (TA_RangeTestParam *)opaqueData; switch( testParam->test->theFunction ) { case TA_RSI_TEST: retCode = TA_RSI( startIdx, endIdx, testParam->close, testParam->test->optInTimePeriod,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -