12 #include "../utils/Indicator.h"
20 cout <<
"Constructor TAlibAPI " << endl;
23 res_ptr = TAResource::CreateResource();
26 cout <<
"res_ptr.use_count() " << res_ptr.use_count() << endl;
27 cout <<
"res_ptr " << res_ptr << endl;
33 cout <<
"Destructor TAlibAPI " << endl;
35 cout <<
"res_ptr use_count() " << res_ptr.use_count() << endl;
44 std::cout <<
"TAResource init()" << std::endl;
47 TA_RetCode rc = TA_Initialize();
48 if ( rc == TA_SUCCESS ) {
50 std::cout <<
"Ok TA_Initialize" << std::endl;
55 std::cout <<
"Error in TA_Initialize: " << rc << std::endl;
63 std::cout <<
"TAResource Shutdown()" << endl;
66 TA_RetCode rc = TA_Shutdown();
67 if (rc != TA_SUCCESS) {
68 std::cout <<
"TALib Error on TA_Shutdown" << rc << std::endl;
73 std::cout <<
"Ok TALIB TA_Shutdown properly" << std::endl;
82 cout <<
"Constructor TAResource, init TAlib" << endl;
90 cout <<
"Destructor TAResource" << endl;
92 TAResource::Shutdown();
99 cout <<
"TAResource CreateResource " << endl;
113 static std::weak_ptr<TAResource> instance;
119 if(
const auto result = instance.lock() )
return result;
127 return ( instance = std::shared_ptr<TAResource>(
new TAResource() ) ).lock();
139 const std::vector< ListDataStockBase *>& vec_ldata_in,
ListDataStockBase *ldata_out)
const
143 std::cout <<
"\nEntry TALib::Compute new_indic " << new_indic.label() << std::endl;
144 std::cout <<
"Name of out " << ldata_out->GetName() <<
" " << ldata_out->Capacity() << std::endl;
149 unsigned int size_data_in;
153 float **arraydata_c_in = 0;
157 double **arraydata_c_out = 0;
159 int nb_array_out = 0;
173 std::cout <<
"Size vec_ldata_in " << vec_ldata_in.size() << std::endl;
175 if ( vec_ldata_in.size() >= 1 ) {
178 ldata_csv_in = vec_ldata_in[0];
180 size_data_in = (
unsigned int) ldata_csv_in->
GetSize();
183 if ( vec_ldata_in.size() >= 2 ) {
184 ldata_volume_in = vec_ldata_in[1];
185 std::cout <<
"Volume is present as input " << std::endl;
189 std::cout <<
"Error in size of vec_ldata_in in TAlibAPI::Compute, throw Exception" << std::endl;
190 throw (
TALibException( new_indic, vec_param,
"TALibException: Size of Lists input is null") );
195 nb_array_in = new_indic.nb_input();
196 nb_array_out = new_indic.nb_output();
198 std::cout <<
"nb array in/out " << nb_array_in <<
" " << nb_array_out << std::endl;
202 if ( (
unsigned int)*std::max_element(vec_param.cbegin(), vec_param.cend()) > size_data_in )
203 throw TALibException( new_indic, vec_param,
"Size of the input data is too small for theses parameters");
207 arraydata_c_out =
new double*[ nb_array_out ];
208 for (
int i = 0; i< nb_array_out; i++ ) {
209 arraydata_c_out[i] =
new double[size_data_in];
211 for (
unsigned int j = 0; j < size_data_in; j++ )
212 arraydata_c_out[i][j] = 0.;
216 arraydata_c_in =
new float*[ nb_array_in ];
219 if ( ( new_indic.label().compare(
"MACD") == 0 ) || ( new_indic.label().compare(
"BOLL") == 0 ) ||
220 ( new_indic ==
Indicator::RSI ) || ( new_indic.label().compare(
"STOCHF") == 0 ) ) {
223 std::cout <<
"Compute MACD/BOLL/STOCHF/RSI: " << new_indic.label() << std::endl;
242 std::cout <<
"new indic STOCHF nb_array_int" << nb_array_in << std::endl;
245 throw TALibException( new_indic, vec_param,
"Cannot compute STOCHF with a TimeScale INST");
254 std::cout <<
"\n\tCompute indicator not implemented !!\n" << std::endl;
255 throw TALibException( new_indic, vec_param,
"Indicator no implemented");
258 retour = ComputeMACD ( arraydata_c_in ,size_data_in, new_indic.label(),
259 vec_param, arraydata_c_out, nb_array_out, offset_begin, out_nb_data);
261 std::cout <<
"\n\tRetour ComputeMACD" << std::endl;
262 std::cout <<
"out_nb_data " << out_nb_data <<
" offset_begin " << offset_begin << std::endl;
263 std::cout <<
"arraydata_c_out " << arraydata_c_out[0][0] <<
" " << arraydata_c_out[1][0] <<
" " << arraydata_c_out[2][0] << std::endl;
264 std::cout <<
"arraydata_c_out " << arraydata_c_out[0][1] <<
" " << arraydata_c_out[1][1] <<
" " << arraydata_c_out[2][1] << std::endl;
271 else if ( new_indic.label().compare(1,2,
"MA",0,2) == 0 ) {
273 std::cout <<
"Compute a MA: " << new_indic.label() << std::endl;
294 retour = ComputeMA ( arraydata_c_in ,size_data_in, new_indic.label(),
295 vec_param[0], arraydata_c_out, offset_begin, out_nb_data);
297 std::cout <<
"offset begin " << offset_begin << std::endl;
304 std::cout <<
"In TAlib::Compute Compute AD " << std::endl;
305 std::cout <<
"new indic AD nb_array_in " << nb_array_in << std::endl;
309 throw TALibException( new_indic, vec_param,
"Cannot compute AD with a TimeScale INST");
316 retour = ComputeMACD ( arraydata_c_in ,size_data_in, new_indic.label(),
317 vec_param, arraydata_c_out, nb_array_out, offset_begin, out_nb_data);
321 std::cout <<
"Not found this indicator in computeTA " << std::endl;
322 throw TALibException( new_indic, vec_param,
"Indicator no implemented");
328 std::cout <<
"Ok for ComputeMA or other offset_begin " << offset_begin <<
" copy back data " << std::endl;
329 std::cout <<
"Call PutCArray nb_array_out " << nb_array_out << std::endl;
333 ldata_out->PutCArray( *ldata_csv_in, out_nb_data, nb_array_out, arraydata_c_out,
339 std::cout <<
"Error in computation, delete the listdatastock " << std::endl;
343 for (
int i = 0; i< nb_array_in; i++ ) {
344 delete[] arraydata_c_in[i];
346 delete[] arraydata_c_in; arraydata_c_in = 0;
348 for (
int i = 0; i< nb_array_out; i++ ) {
349 delete[] arraydata_c_out[i];
351 delete[] arraydata_c_out; arraydata_c_out = 0;
353 throw TALibException( new_indic, vec_param,
"Error in TA-lib calculation");
357 for (
int i = 0; i< nb_array_in; i++ ) {
358 delete[] arraydata_c_in[i];
360 delete[] arraydata_c_in; arraydata_c_in = 0;
362 for (
int i = 0; i< nb_array_out; i++ ) {
363 delete[] arraydata_c_out[i];
365 delete[] arraydata_c_out; arraydata_c_out = 0;
368 std::cout <<
"return from TAlib:compute ok" << std::endl;
374 int param,
double **arraydata_c_out,
int & offset_begin,
int & out_nb_data)
const
377 std::cout <<
"Entry ComputeMA str_label " << str_label << std::endl;
380 TA_RetCode rc = TA_SUCCESS;
385 int endIdx=size_input-1;
388 std::cout <<
"startIdx " << startIdx << std::endl;
389 std::cout <<
"endIdx " << endIdx << std::endl;
390 std::cout <<
"array[0[0] " << arraydata_c_in[0][0] << std::endl;
391 std::cout <<
"array[0][size_input-1]" << arraydata_c_in[0][size_input-1] << std::endl;
392 std::cout <<
"param " << param << std::endl;
393 std::cout <<
"str_label " << str_label << std::endl;
394 std::cout <<
"arraydata_c_out[0] " << arraydata_c_out[0] <<
" " << arraydata_c_out[0][0] << std::endl;
400 if ( str_label ==
"SMA" ) {
403 rc = TA_S_MA( startIdx, endIdx, arraydata_c_in[0], param,TA_MAType_SMA, &offset_begin, &out_nb_data, arraydata_c_out[0]);
405 }
else if ( str_label ==
"EMA" ) {
406 rc = TA_S_MA( startIdx, endIdx, arraydata_c_in[0], param,TA_MAType_EMA, &offset_begin, &out_nb_data, arraydata_c_out[0]);
408 }
else if ( str_label ==
"WMA" ) {
409 rc = TA_S_MA( startIdx, endIdx, arraydata_c_in[0], param,TA_MAType_WMA, &offset_begin, &out_nb_data, arraydata_c_out[0]);
412 std::cout <<
"Not defined, error " << std::endl;
416 if (rc != TA_SUCCESS) {
417 std::cout <<
"error in TA_S_MA rc: " << rc << std::endl;
419 std::cout <<
"offset_begin " << offset_begin <<
", out_nb_data " << out_nb_data << std::endl;
427 std::vector<int> param,
double **arraydata_c_out,
unsigned int nb_array_out,
int & offset_begin,
int & out_nb_data )
const
430 std::cout <<
"Entry ComputeMACD str_label " << str_label << std::endl;
432 std::cout <<
"size_input " << size_input <<
" nb_array_out " << nb_array_out << std::endl;
435 TA_RetCode rc = TA_SUCCESS;
437 int endIdx=size_input-1;
440 std::cout <<
"startIdx " << startIdx << std::endl;
441 std::cout <<
"endIdx " << endIdx << std::endl;
460 if ( str_label.compare(
"MACD") == 0 ) {
462 std::cout <<
"Compute MACD " << std::endl;
464 rc = TA_S_MACD ( startIdx, endIdx, arraydata_c_in[0], param[0], param[1], param[2], &offset_begin, &out_nb_data,
465 arraydata_c_out[0], arraydata_c_out[1], arraydata_c_out[2] );
466 }
else if ( str_label.compare(
"BOLL") == 0 ) {
468 std::cout <<
"Compute BOLL " << std::endl;
470 rc = TA_S_BBANDS ( startIdx, endIdx, arraydata_c_in[0], param[0], (
double)param[1], (
double)param[2], TA_MAType_SMA,
471 &offset_begin, &out_nb_data,
472 arraydata_c_out[0], arraydata_c_out[1], arraydata_c_out[2] );
474 }
else if ( str_label.compare(
"STOCHF") == 0 ) {
476 std::cout <<
"Compute STOCHF" << std::endl;
478 rc = TA_S_STOCHF ( startIdx, endIdx, arraydata_c_in[0], arraydata_c_in[1], arraydata_c_in[2], param[0], param[1], TA_MAType_SMA,
479 &offset_begin, &out_nb_data, arraydata_c_out[0], arraydata_c_out[1] );
481 }
else if ( str_label.compare(
"RSI") == 0 ) {
483 std::cout <<
"Compute RSI" << std::endl;
485 rc = TA_S_RSI ( startIdx, endIdx, arraydata_c_in[0], param[0],
486 &offset_begin, &out_nb_data, arraydata_c_out[0] );
488 }
else if ( str_label.compare(
"AD") == 0 ) {
490 std::cout <<
"TAlib::ComputeMACD Compute AD" << std::endl;
492 rc = TA_S_AD ( startIdx, endIdx, arraydata_c_in[0], arraydata_c_in[1], arraydata_c_in[2], arraydata_c_in[3],
493 &offset_begin, &out_nb_data, arraydata_c_out[0] );
496 std::cout <<
"label not known " << std::endl;
501 std::cout <<
"rc " << rc << std::endl;
502 std::cout <<
" offset " << offset_begin <<
" out_nb_data " << out_nb_data << std::endl;
513 if (rc != TA_SUCCESS) {
514 std::cout <<
"error in TA_MACD or other: " << rc << std::endl;
515 std::cout <<
"offset_begin " << offset_begin <<
", out_nb_data " << out_nb_data << std::endl;
524 std::cout <<
"Entry getListFunctions " << std::endl;
528 typedef std::map< std::string, std::vector<std::string> > type_map_func;
529 typedef type_map_func::iterator it_map;
530 type_map_func map_func;
534 TA_StringTable *table;
538 retCode = TA_GroupTableAlloc( &table );
540 if( retCode == TA_SUCCESS ) {
541 for( i=0; i < table->size; i++ )
543 map_func.insert( std::pair<std::string,
544 std::vector<std::string> >(table->string[i], std::vector<std::string>() ));
546 TA_GroupTableFree( table );
549 for (it_map it = map_func.begin(); it != map_func.end(); ++it ) {
551 retCode = TA_FuncTableAlloc( ((*it).first).c_str(), &table );
553 if( retCode == TA_SUCCESS ) {
554 for( i=0; i < table->size; i++ )
556 (*it).second.push_back( table->string[i] );
558 TA_FuncTableFree( table );
565 for (it_map it = map_func.begin(); it != map_func.end(); ++it ) {
567 std::cout <<
"Group " << (*it).first << std::endl;
568 std::vector<std::string>::iterator it_v;
569 for ( it_v = (*it).second.begin(); it_v != (*it).second.end(); ++it_v ) {
570 std::cout <<
" " << *it_v << std::endl;
574 const TA_FuncHandle *handle;
575 const TA_FuncInfo *theInfo;
577 retCode = TA_GetFuncHandle(
"CDL2CROWS", &handle );
579 if( retCode == TA_SUCCESS ) {
580 retCode = TA_GetFuncInfo( handle, &theInfo );
581 if( retCode == TA_SUCCESS ) {
582 printf(
"Nb Input = %d\n", theInfo->nbInput );
583 printf(
"flags = %d\n", theInfo->flags );
586 if ( theInfo->flags & TA_FUNC_FLG_CANDLESTICK ) {
587 std::cout <<
"I am a CandleStick " << std::endl;
589 std::cout <<
"I am not a CandleStick " << std::endl;
592 if ( theInfo->flags == TA_FUNC_FLG_OVERLAP ) {
593 std::cout <<
"I also scale with input " << std::endl;
620 std::cout <<
"End GetListFunctions" << std::endl;
Define a list of DataStock objects.
static Indicator const STOCHF
TAlibAPI()
Default constructor.
"Clever" enumeration of the ListDataStock type : StockCSV, Volume, EMA, BOLL,...
virtual ~TAlibAPI()
Destructor.
int ComputeMA(float **arraydata_c_in, unsigned int size_input, std::string str_label, int param, double **arraydata_c_out, int &offset_begin, int &out_nb_data) const
Compute Moving Averges : SMA, EMA, WMA.
static void GetListFunctions()
bool Compute(const ETime tmscl, const Indicator new_indic, const std::vector< int > &vec_param, const std::vector< ListDataStockBase * > &vec_ldata_in, ListDataStockBase *ldata_out) const
Compute an indicator.
static std::shared_ptr< TAResource > CreateResource()
Return a shared pointer on the current resource.
static Indicator const MACD
static Indicator const AD
static Indicator const RSI
auto as_uint(ETime const value) -> unsigned int
Provides explicit conversion to unsigned int.
int ComputeMACD(float **arraydata_c_in, unsigned int size_input, std::string str_label, std::vector< int > params, double **arraydata_c_out, unsigned int nb_array_out, int &offset_begin, int &out_nb_data) const
Compute others indicators : MACD, AD, RSI, STOCHF.
ETime
Enumeration for the different time representation, from instantaneous (INST) to year(YEAR) ...
TAResource()
Constructor made private, to force use of CreateResource()
Centralize all exception coming form the TALib library interface.
virtual unsigned int GetSize() const =0
Return the size of the vector.
static Indicator const BOLL
Abstract base class for the ListDataStock, for storing base pointers in vectors.
Base class for exception coming from talibapi interface.
API for using TA_lib of X in ROOT_Application.
virtual unsigned int GetCArray(var_types arraydata_c, const EPChrono b_order, const unsigned int no=0) const =0
Extract the data into a one dimensional C-array.
virtual ~TAResource()
Destructor.