Skip to content

Commit

Permalink
Added the C reference source
Browse files Browse the repository at this point in the history
  • Loading branch information
jb-lopez committed Nov 28, 2023
1 parent 7548d4d commit a88e985
Show file tree
Hide file tree
Showing 345 changed files with 90,232 additions and 7 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Change Log

## 2.1.1
* Figured out how to use .gitattributes to exclude files from the composer package.
* Now I can add the TA-Lib C source files to the repository and not have them show up in the composer package. 🎉
## 2.1.0
* Added new functions from TA-Lib v0.6.0.
* [ACCBANDS](https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_ACCBANDS.c)
* [AVGDEV](https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_AVGDEV.c)
* [IMI](https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_IMI.c)
## 2.0.1
* Fixed [Issue #16](https://github.com/LupeCode/phpTraderNative/issues/16)
* Removed version from composer.json as per their spec.
## 2.0.0
* Cleaned up git repository.
* Drop support for PHP 7.X and 8.0.
* Updated dependencies.
* Cleaned up the code and use PSR-12 formatting.
* Changed the LICENSE to MIT.
* Updated PHPUnit.
* Added better testing data.
## 1.2.3
* Small updates and bug fixes.
* Added .deepsource.toml for DeepSource.io.
## 1.2.2
* GitLab CI and TravisCI no longer build this package
## 1.2.1
* Added PHP 7.0.x to the CI tests.
* Also various things to get this to work in the CI runners.
## 1.2.0
* Added the static variable to the test. Use this instead of a string just in case the string for the errors change.
* Test all of the MA types that stochrsi supports.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ cd phpTraderNative
composer install --dev
~~~

### Directory Structure

* `source` - The source code for the library.
* `tests` - The PHPUnit tests for the library.
* `pecl` - The PECL Trader extension source code.
* `talib` - The TA-LIB source code that the PECL Trader extension uses.

### Testing

Two PHPUnit XML files are included, one for testing and the other for coverage. This is due to the fact that when some tests are run with coverage, PHP hangs and never finishes.
Expand Down
86 changes: 86 additions & 0 deletions pecl/function.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright (c) 2012-2018, Anatol Belski <ab@php.net>
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.

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 COPYRIGHT
HOLDER 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.
*/

/* $Id$ */

#include "php.h"
#include "php_trader.h"

#include <ta_func.h>
#include <ta_common.h>

ZEND_EXTERN_MODULE_GLOBALS(trader)

/* {{{ proto array MY_FUNC_NAME_LOWER(MY_FUNC_DOC_PARAMS)
MY_FUNC_DESC */
PHP_FUNCTION(MY_FUNC_NAME_LOWER)
{
int optimalOutAlloc, lookback;
MY_IN_PHP_ARRAY_DEFS
MY_FUNC_ARRAY_PARA_DEFS
MY_FUNC_INT_PARA_DEFS
MY_IN_PHP_LONG_DEFS
MY_IN_PHP_DOUBLE_DEFS
#if PHP_MAJOR_VERSION >= 7
MY_ZEND_FAST_ZPP
#else
if (zend_parse_parameters(ZEND_NUM_ARGS(), MY_ZEND_PARAMS_STR, MY_ZEND_PARAM_LIST) == FAILURE) {
RETURN_FALSE;
}
#endif

MY_FUNC_CHECK_MA_TYPES
MY_FUNC_SET_BOUNDABLE

MY_FUNC_SET_MIN_END_IDX
MY_FUNC_SET_START_IDX

MY_FUNC_OPTIMAL_OUT_ALLOC
if (optimalOutAlloc > 0) {
MY_FUNC_ARRAY_PARA_ALLOCS
TRADER_G(last_error) = MY_FUNC_NAME(MY_FUNC_PARAMS);
if (TRADER_G(last_error) != TA_SUCCESS) {
MY_FUNC_ARRAY_PARA_DEALLOCS2
RETURN_FALSE;
}

MY_PHP_MAKE_RETURN

MY_FUNC_ARRAY_PARA_DEALLOCS1
} else {
/* The current input args combination would cause TA-Lib to produce
zero output, don't bother making any allocs or calls. */
TRADER_G(last_error) = TA_BAD_PARAM;
RETURN_FALSE;
}
}
/* }}} */
111 changes: 111 additions & 0 deletions pecl/trader_accbands.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright (c) 2012-2018, Anatol Belski <[email protected]>
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.
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 COPYRIGHT
HOLDER 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.
*/

/* $Id$ */

#include "php.h"
#include "php_trader.h"

#include <ta_func.h>
#include <ta_common.h>

ZEND_EXTERN_MODULE_GLOBALS(trader)

/* {{{ proto array trader_accbands(array high, array low, array close [, int timePeriod])
Acceleration Bands */
PHP_FUNCTION(trader_accbands)
{
int optimalOutAlloc, lookback;
zval *zinHigh, *zinLow, *zinClose;
double *inHigh, *inLow, *inClose, *outRealUpperBand, *outRealMiddleBand, *outRealLowerBand;
int startIdx = 0, endIdx = 0, outBegIdx = 0, outNBElement = 0;
zend_long optInTimePeriod = 2;


#if PHP_MAJOR_VERSION >= 7
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_ARRAY(zinHigh)
Z_PARAM_ARRAY(zinLow)
Z_PARAM_ARRAY(zinClose)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(optInTimePeriod)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
#else
if (zend_parse_parameters(ZEND_NUM_ARGS(), "aaa|l", &zinHigh, &zinLow, &zinClose, &optInTimePeriod) == FAILURE) {
RETURN_FALSE;
}
#endif


TRADER_LONG_SET_BOUNDABLE(2, 100000, optInTimePeriod);

TRADER_SET_MIN_INT3(endIdx, zend_hash_num_elements(Z_ARRVAL_P(zinHigh)),
zend_hash_num_elements(Z_ARRVAL_P(zinLow)),
zend_hash_num_elements(Z_ARRVAL_P(zinClose)))
endIdx--; /* it's <= in the ta-lib */


lookback = TA_ACCBANDS_Lookback((int)optInTimePeriod);
optimalOutAlloc = (lookback > endIdx) ? 0 : (endIdx - lookback + 1);
if (optimalOutAlloc > 0) {
outRealUpperBand = emalloc(sizeof(double)*optimalOutAlloc);
outRealMiddleBand = emalloc(sizeof(double)*optimalOutAlloc);
outRealLowerBand = emalloc(sizeof(double)*optimalOutAlloc);
TRADER_DBL_ZARR_TO_ARR(zinHigh, inHigh)
TRADER_DBL_ZARR_TO_ARR(zinLow, inLow)
TRADER_DBL_ZARR_TO_ARR(zinClose, inClose)

TRADER_G(last_error) = TA_ACCBANDS(startIdx, endIdx, inHigh, inLow, inClose, (int)optInTimePeriod, &outBegIdx, &outNBElement, outRealUpperBand, outRealMiddleBand, outRealLowerBand);
if (TRADER_G(last_error) != TA_SUCCESS) {
efree(inHigh);
efree(inLow);
efree(inClose);
efree(outRealUpperBand);
efree(outRealMiddleBand);
efree(outRealLowerBand);

RETURN_FALSE;
}

TRADER_DBL_ARR_TO_ZRET3(outRealUpperBand, outRealMiddleBand, outRealLowerBand, return_value, endIdx, outBegIdx, outNBElement)

efree(inHigh);
efree(inLow);
efree(inClose);
efree(outRealUpperBand);
efree(outRealMiddleBand);
efree(outRealLowerBand);
} else {
/* The current input args combination would cause TA-Lib to produce
zero output, don't bother making any allocs or calls. */
TRADER_G(last_error) = TA_BAD_PARAM;
RETURN_FALSE;
}
}
/* }}} */

93 changes: 93 additions & 0 deletions pecl/trader_acos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright (c) 2012-2018, Anatol Belski <[email protected]>
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.
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 COPYRIGHT
HOLDER 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.
*/

/* $Id$ */

#include "php.h"
#include "php_trader.h"

#include <ta_func.h>
#include <ta_common.h>

ZEND_EXTERN_MODULE_GLOBALS(trader)

/* {{{ proto array trader_acos(array real)
Vector Trigonometric ACos */
PHP_FUNCTION(trader_acos)
{
int optimalOutAlloc, lookback;
zval *zinReal;
double *inReal, *outReal;
int startIdx = 0, endIdx = 0, outBegIdx = 0, outNBElement = 0;



#if PHP_MAJOR_VERSION >= 7
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(zinReal)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
#else
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &zinReal) == FAILURE) {
RETURN_FALSE;
}
#endif




TRADER_SET_MIN_INT1(endIdx, zend_hash_num_elements(Z_ARRVAL_P(zinReal)))
endIdx--; /* it's <= in the ta-lib */


lookback = TA_ACOS_Lookback();
optimalOutAlloc = (lookback > endIdx) ? 0 : (endIdx - lookback + 1);
if (optimalOutAlloc > 0) {
outReal = emalloc(sizeof(double)*optimalOutAlloc);
TRADER_DBL_ZARR_TO_ARR(zinReal, inReal)

TRADER_G(last_error) = TA_ACOS(startIdx, endIdx, inReal, &outBegIdx, &outNBElement, outReal);
if (TRADER_G(last_error) != TA_SUCCESS) {
efree(inReal);
efree(outReal);

RETURN_FALSE;
}

TRADER_DBL_ARR_TO_ZRET1(outReal, return_value, endIdx, outBegIdx, outNBElement)

efree(inReal);
efree(outReal);
} else {
/* The current input args combination would cause TA-Lib to produce
zero output, don't bother making any allocs or calls. */
TRADER_G(last_error) = TA_BAD_PARAM;
RETURN_FALSE;
}
}
/* }}} */

Loading

0 comments on commit a88e985

Please sign in to comment.