📄 test_stoch.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 * ------------------------------------------------------------------- * 122101 MF First version. * 111603 MF Add test of TA_STOCHRSI *//* Description: * Test the Stochastic function. *//**** Headers ****/#include <stdio.h>#include <string.h>#include "ta_test_priv.h"#include "ta_test_func.h"#include "ta_utility.h"#include "ta_memory.h"/**** External functions declarations. ****//* None *//**** External variables declarations. ****//* None *//**** Global variables definitions. ****//* None *//**** Local declarations. ****/typedef enum{ TEST_STOCH, TEST_STOCHF, TEST_STOCHRSI} TestId;typedef struct{ TestId testId; TA_Integer doRangeTestFlag; /* One will do a call to doRangeTest */ TA_Integer unstablePeriod; TA_Integer startIdx; TA_Integer endIdx; TA_Integer optInPeriod_0; TA_Integer optInPeriod_1; TA_Integer optInMAType_1; TA_Integer optInPeriod_2; TA_Integer optInMAType_2; TA_RetCode expectedRetCode; TA_Integer expectedBegIdx; TA_Integer expectedNbElement; TA_Integer oneOfTheExpectedOutRealIndex0; TA_Real oneOfTheExpectedOutReal0; TA_Integer oneOfTheExpectedOutRealIndex1; TA_Real oneOfTheExpectedOutReal1;} TA_Test;typedef struct{ const TA_Test *test; const TA_Real *high; const TA_Real *low; const TA_Real *close;} TA_RangeTestParam;/**** Local functions declarations. ****/static ErrorNumber do_test( const TA_History *history, const TA_Test *test );static TA_RetCode referenceStoch( TA_Integer startIdx, TA_Integer endIdx, const TA_Real inHigh[], const TA_Real inLow[], const TA_Real inClose[], TA_Integer optInPeriod_0, /* From 1 to TA_INTEGER_MAX */ TA_Integer optInPeriod_1, /* From 1 to TA_INTEGER_MAX */ TA_Integer optInMAType_1, TA_Integer optInPeriod_2, /* From 1 to TA_INTEGER_MAX */ TA_Integer optInMAType_2, TA_Integer *outBegIdx, TA_Integer *outNbElement, TA_Real outSlowK_0[], TA_Real outSlowD_1[] );/**** Local variables definitions. ****/static TA_Test tableTest[] ={ /**************/ /* STOCH TEST */ /**************/ { TEST_STOCH, 1, 0, 9, 9, 5, 3, TA_MAType_SMA, 4, TA_MAType_SMA, TA_SUCCESS, 9, 1, 0, 38.139, 0, 36.725 }, /* Test one value */ { TEST_STOCH, 0, 0, 0, 251, 5, 3, TA_MAType_SMA, 3, TA_MAType_SMA, TA_SUCCESS, 8, 252-8, 0, 24.0128, 0, 36.254, }, /* First Value */ { TEST_STOCH, 0, 0, 0, 251, 5, 3, TA_MAType_SMA, 4, TA_MAType_SMA, TA_SUCCESS, 9, 252-9, 252-10, 30.194, 252-10, 46.641, }, /* Last Value */ { TEST_STOCH, 0, 0, 0, 251, 5, 3, TA_MAType_SMA, 3, TA_MAType_SMA, TA_SUCCESS, 8, 252-8, 252-9, 30.194, 252-9, 43.69, }, /* Last Value */ /*****************/ /* STOCHRSI TEST */ /*****************/ { TEST_STOCHRSI, 0, 0, 27, 27, 14, 14, -1, 1, TA_MAType_SMA, TA_SUCCESS, 27, 1, 0, 94.156709, 0, 94.156709 }, /* Test one Value */ { TEST_STOCHRSI, 1, 0, 0, 251, 14, 14, -1, 1, TA_MAType_SMA, TA_SUCCESS, 27, 252-27, 0, 94.156709, 0, 94.156709 }, /* First Value */ { TEST_STOCHRSI, 0, 0, 0, 251, 14, 14, -1, 1, TA_MAType_SMA, TA_SUCCESS, 27, 252-27, 251-27, 0.0, 251-27, 0.0 }, /* Last Value */ { TEST_STOCHRSI, 0, 0, 0, 251, 14, 45, -1, 1, TA_MAType_SMA, TA_SUCCESS, 58, 252-58, 0, 79.729186, 0, 79.729186 }, /* First Value */ { TEST_STOCHRSI, 0, 0, 0, 251, 14, 45, -1, 1, TA_MAType_SMA, TA_SUCCESS, 58, 252-58, 251-58, 48.1550743, 251-58, 48.1550743 }, /* Last Value */ { TEST_STOCHRSI, 1, 0, 0, 251, 11, 13, -1, 16, TA_MAType_SMA, TA_SUCCESS, 38, 252-38, 0, 5.25947, 0, 57.1711}, /* First Value */ { TEST_STOCHRSI, 0, 0, 0, 251, 11, 13, -1, 16, TA_MAType_SMA, TA_SUCCESS, 38, 252-38, 251-38, 0.0, 251-38, 15.7303 }, /* Last Value */ /* More test needed!!! */};#define NB_TEST (sizeof(tableTest)/sizeof(TA_Test))/**** Global functions definitions. ****/ErrorNumber test_func_stoch( 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; TA_Real *dummyOutput; (void)outputBufferInt; *isOutputInteger = 0; retCode = TA_NOT_SUPPORTED; testParam = (TA_RangeTestParam *)opaqueData; dummyOutput = TA_Malloc( (endIdx-startIdx+1) * sizeof(TA_Real) ); switch( testParam->test->testId ) { case TEST_STOCH: if( outputNb == 0 ) { retCode = TA_STOCH( startIdx, endIdx, testParam->high, testParam->low, testParam->close, testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, (TA_MAType)testParam->test->optInMAType_1, testParam->test->optInPeriod_2, (TA_MAType)testParam->test->optInMAType_2, outBegIdx, outNbElement, outputBuffer, dummyOutput ); } else { retCode = TA_STOCH( startIdx, endIdx, testParam->high, testParam->low, testParam->close, testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, (TA_MAType)testParam->test->optInMAType_1, testParam->test->optInPeriod_2, (TA_MAType)testParam->test->optInMAType_2, outBegIdx, outNbElement, dummyOutput, outputBuffer ); } *lookback = TA_STOCH_Lookback( testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, (TA_MAType)testParam->test->optInMAType_1, testParam->test->optInPeriod_2, (TA_MAType)testParam->test->optInMAType_2 ); break; case TEST_STOCHF: if( outputNb == 0 ) { retCode = TA_STOCHF( startIdx, endIdx, testParam->high, testParam->low, testParam->close, testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, (TA_MAType)testParam->test->optInMAType_1, outBegIdx, outNbElement, outputBuffer, dummyOutput ); } else { retCode = TA_STOCHF( startIdx, endIdx, testParam->high, testParam->low, testParam->close, testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, (TA_MAType)testParam->test->optInMAType_1, outBegIdx, outNbElement, dummyOutput, outputBuffer ); } *lookback = TA_STOCHF_Lookback( testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, (TA_MAType)testParam->test->optInMAType_1 ); break; case TEST_STOCHRSI: if( outputNb == 0 ) { retCode = TA_STOCHRSI( startIdx, endIdx, testParam->close, testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, testParam->test->optInPeriod_2, (TA_MAType)testParam->test->optInMAType_2, outBegIdx, outNbElement, outputBuffer, dummyOutput ); } else { retCode = TA_STOCHRSI( startIdx, endIdx, testParam->close, testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, testParam->test->optInPeriod_2, (TA_MAType)testParam->test->optInMAType_2, outBegIdx, outNbElement, dummyOutput, outputBuffer ); } *lookback = TA_STOCHRSI_Lookback( testParam->test->optInPeriod_0, testParam->test->optInPeriod_1, testParam->test->optInPeriod_2, (TA_MAType)testParam->test->optInMAType_2 ); break; } TA_Free( dummyOutput ); return retCode;}static ErrorNumber do_test( const TA_History *history, const TA_Test *test ){ TA_RetCode retCode; ErrorNumber errNb; TA_Integer outBegIdx; TA_Integer outNbElement; TA_RangeTestParam testParam; retCode = TA_NOT_SUPPORTED; /* Set to NAN all the elements of the gBuffers. */ clearAllBuffers(); /* Build the input. */ setInputBuffer( 0, history->high, history->nbBars ); setInputBuffer( 1, history->low, history->nbBars ); setInputBuffer( 2, history->close, history->nbBars ); /* Re-initialize all the unstable period to zero. */ TA_SetUnstablePeriod( TA_FUNC_UNST_ALL, 0 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -