ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Indicator.cpp
Go to the documentation of this file.
1 
7 #include "Indicator.h"
8 #include "Utils.h"
9 
10 //only for gcc and ACLiC
11 // still needed, complains from libstock.so not known
12 // #ifndef __CINT__
13 Indicator const
24 Indicator::AD (Indicator::ad_l), //accumulation/disribution use volume
25 Indicator::STOCHF (Indicator::stochf_l); //2 versions stock and stockf
26 // #endif
27 
28 
30 namespace
31 {
32  //std::string indicator_labels_[] = {"Null","StockCSV", "SimpleData", "Volume","SMA","EMA","WMA","MACD"};
33  std::string indicator_labels_[] = {"Null","StockCSV", "StockInst", "Volume", "SMA","EMA","WMA", "MACD", "BOLL","RSI", "AD","STOCHF"};
34  //number of parameters for computing the TA
35  int indicator_nb_param_[] = { 0, 0, 0, 0, 1, 1, 1, 3, 3, 1, 0, 2};
36 // number of input
37  int indicator_nb_input_[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 3};
38  //number of output, used both b TAlib and ListData
39  int indicator_nb_output_[] = { 0 , 4 , 1 , 1, 1, 1 , 1, 3, 3, 1, 1, 2};
40  bool bool_default_[] = { false, false, false, false, false,false,false, true, true, true,false, true};
41  // fast/low
42  int default_value_[][3] = { {}, {}, {}, {}, {}, {}, {},{12,26,9}, {5,2,2},{14},{}, {5,3}};
43  //BOLL expect double for 2
44  // desription Candle,Line,Bar,Kollection, and color (Blue,Red,Violet)
45  char descp_[][12] = { "", "LC0", "LC0", "LBR", "P0B","P0B","P0B","KP0RP0BLBR","KP0VP0BP0V", "P", "P", "PP"};
46  //shift [0,..,nblabel) taken into account in function
47  unsigned int nb_label=12;
48 }
49 
50 
51 const std::string Indicator::label () const
52 {
53  return indicator_labels_[v_];
54 }
55 
56 //compile finally, take example of GetIndicator
57 std::vector<std::string> Indicator::GetListAllIndicator()
58 {
59  std::vector<std::string> tmp_vec;
60  for(unsigned int i=1; i<nb_label; i++)
61  {
62  //std::cout << "Alllabel " << indicator_labels_[i] << std::endl;
63  tmp_vec.push_back( indicator_labels_[i] );
64  }
65  return tmp_vec;
66 }
67 
68 int Indicator::nb_param () const
69 {
70  return indicator_nb_param_[v_];
71 }
72 
73 int Indicator::nb_input () const
74 {
75  return indicator_nb_input_[v_];
76 }
77 
79 {
80  return indicator_nb_output_[v_];
81 }
82 
84 {
85  return bool_default_[v_];
86 }
87 
88 std::vector<int> Indicator::GetDefaultValues() const
89 {
90 #ifdef DEBUG_UTIL
91  std::cout << "nb_output in GetDefaultValues " << nb_output() << std::endl;
92 #endif
93 
94  std::vector<int> vec_return;
95  vec_return.resize( nb_output() );
96  for ( int i=0; i< nb_output(); i++ ) {
97  vec_return[i] = default_value_[v_][i];
98  }
99  return vec_return;
100 }
101 
102 std::string Indicator::GetDescription() const
103 {
104 #ifdef DEBUG_UTIL
105  std::cout << "Entry Indicator::GetDescription " << Utils::toString<std::string>(descp_[v_]) << std::endl;
106 #endif
107  //return Utils::toString<std::string>(descp_[v_]);
108  return Utils::toString<char*>(descp_[v_]);
109 }
110 
111 // new version, really use vector of string, each string LC0, P0R, "K"
112 std::vector<std::string> Indicator::parse_description() const
113 {
114 #ifdef DEBUG_UTIL
115  std::cout << "\nEntry Indicator::parse_description\n" << std::endl;
116 #endif
117  // in fact vector of character, stupid it is a string !
118  std::vector<std::string> desc_string;
119  std::string charact, charact2, charact3;
120 
121  std::string full_descp_str;
122  std::string sub_string;
123  full_descp_str = GetDescription();
124 #ifdef DEBUG_UTIL
125  std::cout << "full descp " << full_descp_str << std::endl;
126 #endif
127  for ( unsigned int i = 0; i < full_descp_str.size(); ++i) {
128  charact.clear();
129  charact2.clear();
130  charact3.clear();
131 
132 
133  // read L = List, K = Collection
134  //if ( ( i == 0 ) ) {//& ( full_descp_str[0] == "K") ) {
135  // very important '' and not "" !!
136  if ( ( i == 0 ) && ( (full_descp_str[0]) == 'K') ) {
137  std::string local_str;
138  local_str = full_descp_str[0];
139  //charact = descp_str[i];
140 #ifdef DEBUG_UTIL
141  std::cout << "set special for K charact [0]" << full_descp_str[0] << std::endl;
142 #endif
143  // trick for compilation
144 
145  //desc_string.push_back( full_descp_str[0] );
146  desc_string.push_back( local_str );
147 
148  // read by two C = Candle, B = Bar, P = PStockLime
149  // second color 0= default ( for Candle)
150  // R = Red, B = Blue, M = Mauve
151  } else {
152  // read two obj and color
153  //std::cout << " make something else " << std::endl;
154  //string substr (size_t pos = 0, size_t len = npos) const;
155 
156  sub_string = full_descp_str.substr( i, 3 );
157  /*
158  charact = full_descp_str[i];
159  charact2 = full_descp_str[++i];
160  charact3 = full_descp_str[++i];
161  std::cout << charact << std::endl;
162  std::cout << charact2 << std::endl;
163  std::cout << charact3 << std::endl;
164  */
165  //std::cout << " sub_string " << sub_string << std::endl;
166 
167  i += 2;
168  //std::cout << " i " << i << std::endl;
169  //if ( charact.size() > 0 ) desc_string.push_back( charact );
170  //if ( charact2.size() > 0 ) desc_string.push_back( charact2 );
171  //if ( charact3.size() > 0 ) desc_string.push_back( charact3 );
172  //desc_string.push_back( charact + charact2 + charact3 );
173  desc_string.push_back( sub_string );
174  }
175 
176  // can regroup the push back after
177  //if ( charact.size() > 0 ) desc_string.push_back( charact );
178  //if ( charact2.size() > 0 ) desc_string.push_back( charact2 );
179  //if ( charact3.size() > 0 ) desc_string.push_back( charact3 );
180  }
181 #ifdef DEBUG_UTIL
182  std::cout << " summary parse_description " << std::endl;
183  for ( unsigned int i = 0; i < desc_string.size(); ++i ) {
184  std::cout << " i " << i << " " << desc_string[i] << std::endl;
185  }
186 #endif
187 
188  return desc_string;
189 }
190 
191 // basic test, need to extend with a field: NULL, TA, Other (enum)
193 {
194  if ( ( indicator_nb_param_[v_] > 0 ) ||
195  ( indicator_labels_[v_] == "AD" ) )
196  return true;
197 
198  return false;
199 }
200 
201 
202 //Bug with size of one indicator, should be 8 octets, not 4 !
203 //Replaced by similar GetIndicator, but still different
204 Indicator::Value Indicator::FindValue(std::string str_name) const
205 {
206  //std::cout << "size labels" << sizeof(indicator_labels_) << std::endl;
207  int size_int=sizeof(int(1)); //machine dependent
208  //do not work, send back 4
209  //Indicator indic (Indicator::StockCSV);
210  //int size_int=sizeof( indic );
211 
212  //std::cout << "sizeof indicator " << sizeof( indicator_labels_) << std::endl;
213  //need two times the size, do not know why !!!
214  //different output from ordiwg and asus
215  //if ordiwg 64 bits
216  //int size=sizeof( indicator_labels_ ) / (2*size_int);
217  //if asus ?? work at HITS ??
218  int size=sizeof( indicator_labels_ ) / (size_int);
219 #ifdef DEBUG_UTIL
220  std::cout << "machine dependent size_int " << size_int << "size value " << size << std::endl;
221 #endif
222  for (int i=0;i<size;i++)
223  {//std::cout << " str_name " << str_name << " labels " << indicator_labels_[i] << std::endl;
224  if (str_name == indicator_labels_[i])
225  return Value(i);
226  }
227 
228  //std::cout << "not found" << std::endl;
229  //std::cout << "Value(0)" << Value(0) << std::endl;
230  return Value(0);
231 }
232 
233 // const what the point here ?? No it is a static function
234 Indicator Indicator::GetIndicator(const std::string& str_name)
235 {
236  std::vector<std::string> vec_substr;
237  Utils::splitline(str_name, "_", vec_substr);
238  //std::cout << "GetIndicator 1 " << vec_substr[0] << std::endl;
239 
240  //intermadiate because want a integer for switch, to see later
241  //switch better
242  if ( vec_substr[0] == "StockCSV" ) {
243  //std::cout << "GetIndicator StockCSV" << std::endl;
244  return ( Indicator::StockCSV );
245  }
246  // to add Volume
247  else if ( vec_substr[0] == "StockInst" ) {
248  return ( Indicator::StockInst );
249  }
250  else if ( vec_substr[0] == "SMA" ) {
251  //std::cout << "GetIndicator SMA" << std::endl;
252  return ( Indicator::SMA );
253  //return ( Indicator::StockCSV );
254  }
255  else if ( vec_substr[0] == "EMA" ) {
256  //std::cout << "GetIndicator EMA" << std::endl;
257  return ( Indicator::EMA );
258  }
259  else if ( vec_substr[0] == "WMA" ) {
260  //std::cout << "GetIndicator EMA" << std::endl;
261  return ( Indicator::WMA );
262  }
263  //stupid name ?? used ?? to delete ?? to see later
264  else if ( vec_substr[0] == "SimpleData" ) {
265  //std::cout << "GetIndicator SimpleData" << std::endl;
266  return ( Indicator::StockInst );
267  }
268  else if ( vec_substr[0] == "Volume" ) {
269  //std::cout << "GetIndicator Volume" << std::endl;
270  return ( Indicator::Volume );
271  }
272  else if ( vec_substr[0] == "MACD" ) {
273  //std::cout << "GetIndicator MACD" << std::endl;
274  return ( Indicator::MACD );
275  }
276  else if ( vec_substr[0] == "BOLL" ) {
277  //std::cout << "GetIndicator Bollinger" << std::endl;
278  return ( Indicator::BOLL );
279  }
280  else if ( vec_substr[0] == "RSI" ) {
281  //std::cout << "GetIndicator RSI" << std::endl;
282  return ( Indicator::RSI );
283  }
284  else if ( vec_substr[0] == "STOCHF" ) {
285  //std::cout << "GetIndicator StochF" << std::endl;
286  return ( Indicator::STOCHF );
287  }
288  else if ( vec_substr[0] == "AD" ) {
289  //std::cout << "GetIndicator AD" << std::endl;
290  return ( Indicator::AD );
291  }
292  else
293  std::cout << "Error No Match in Indicator::GetIndicator, return Indicator::Null " << std::endl;
294 
295  return ( Indicator::Null );
296 }
297 
298 std::ostream& operator<< (std::ostream& o, Indicator c)
299 {
300  return o << c.label ();
301 }
302 
303 // never used ?? no point ?
304 void Indicator::Set(std::string str_name, std::vector<int> & param )
305 {
306 #ifdef DEBUG_UTIL
307  std::cout << "Indicator::Set " << str_name << std::endl;
308 #endif
309  std::string str_indic;
310  int tmp_int;
311  std::string tmp_str;
312 
313  char delimiters='_';
314  // Skip delimiters at beginning.
315  std::string::size_type lastPos = str_name.find_first_not_of(delimiters, 0);
316  // Find first "non-delimiter".
317  std::string::size_type pos = str_name.find_first_of(delimiters, lastPos);
318 
319  int count=0;
320  while (std::string::npos != pos || std::string::npos != lastPos) {
321  // Found a token, add it to the vector
322  // tokens.push_back(str.substr(lastPos, pos - lastPos));
323  if (count == 0) {
324  std::cout << str_name.substr(lastPos, pos - lastPos) << std::endl;
325  tmp_str=str_name.substr(lastPos, pos - lastPos);
326  //this->v_=volume_l;
327  //this->v_=Indicator::Value(2);
328  v_=FindValue(tmp_str);
329  //std::cout << "retour FindValue(" << tmp_str << ")" << v_ << std::endl;
330  //else for param
331  } else {
332  std::cout << "param " << str_name.substr(lastPos, pos - lastPos) << std::endl;
333  tmp_str=str_name.substr(lastPos, pos - lastPos);
334  tmp_int= Utils::fromString<int>(tmp_str);
335  param.push_back(tmp_int);
336  }
337  // Skip delimiters. Note the "not_of"
338  lastPos = str_name.find_first_not_of(delimiters, pos);
339  // Find next "non-delimiter"
340  pos = str_name.find_first_of(delimiters, lastPos);
341  count++;
342  }
343 
344 }
345 
346 
347 
348 //enum EColor { kWhite =0, kBlack =1, kGray=920,
349 // kRed =632, kGreen =416, kBlue=600, kYellow=400, kMagenta=616, kCyan=432,
350 // kOrange=800, kSpring=820, kTeal=840, kAzure =860, kViolet =880, kPink=900 };
351 int Indicator::MakeEColor( const char str_color)
352 {
353  //if ( str_color == "0" ) return 0;
354  //else
355  if ( str_color == 'B') return 600; //kBlue;
356  else if ( str_color == 'R' ) return 632; //kRed;
357  else if ( str_color == 'V' ) return 880; //kViolet;
358  else return 900;//1; // kBlackkPink;
359 }
360 
361 
362 int Indicator::MakeColor( const char str_color)
363 {
364  //if ( str_color == "0" ) return 0;
365  //else
366  if ( str_color == 'B') return 255; //4;
367  else if ( str_color == 'R' ) return 16711680; //2;
368  else if ( str_color == 'V' ) return 9568511; //9;
369  else return 16711680; //0; // Blue, Black
370 }
371 
372 // test can modify, so can register ??
373 void Indicator::Change( int new_value ) {
374 
375  std::cout << "v_ " << v_ << std::endl;
376  indicator_nb_output_[v_] = new_value;
377 }
378 
379 
380 /* example d utilisation, to be in unit_test
381 using std::cerr;
382 using std::endl;
383 
384 void f (Indicator)
385 {
386  cerr << "color" << endl;
387 }
388 
389 void f (int)
390 {
391  cerr << "int" << endl;
392 }
393 
394 int
395 main ()
396 {
397  // 1
398  {
399  // Color c (red); // error: red undeclared
400  Indicator c (Indicator::StockCSV);
401  }
402 
403  // 2
404  {
405  // Color c; // error: no default c-tor
406  Indicator c (Indicator::StockCSV);
407  c = Indicator::StockdCSV;
408  }
409 
410  // 3
411  {
412  Indicator c (Indicator::StockdCSV);
413  cerr << c << " " << Indicator::Volume << endl;
414  }
415 
416  // 4
417  {
418  Indicator c (Indicator::StockCSV);
419 
420  f (c);
421  f (Indicator::Volume);
422 
423  // c + Color::red; // error: no operator+
424 
425  // if (c) {} // error: no conversion from Color to bool
426 
427  if (c == Indicator::StockCSV && c != Indicator::Volume)
428  {
429  cerr << "ok" << endl;
430  }
431  }
432 
433  // 5
434  {
435  Color c (Color::red);
436 
437  switch (c.integral ())
438  {
439  case Color::red_l:
440  cerr << "r" << endl;
441  break;
442 
443  case Color::green_l:
444  cerr << "g" << endl;
445  break;
446 
447  case Color::blue_l:
448  cerr << "b" << endl;
449  break;
450  }
451  }
452 
453 }
454 */
Value FindValue(std::string) const
Definition: Indicator.cpp:204
static Indicator const STOCHF
Definition: Indicator.h:44
std::vector< std::string > parse_description() const
Utility function to parse the description.
Definition: Indicator.cpp:112
static std::vector< std::string > GetListAllIndicator()
Get a list of all indicator's label.
Definition: Indicator.cpp:57
"Clever" enumeration of the ListDataStock type : StockCSV, Volume, EMA, BOLL,...
Definition: Indicator.h:29
int nb_param() const
Nb of parameters necessary to be computed.
Definition: Indicator.cpp:68
void Set(std::string str_name, std::vector< int > &param)
Definition: Indicator.cpp:304
bool HasDefaultValues() const
Test functions if default values are provided.
Definition: Indicator.cpp:83
static Indicator const Null
Predefined static instances of the class.
Definition: Indicator.h:44
static Indicator const WMA
Definition: Indicator.h:44
int MakeEColor(const char str_color)
Definition: Indicator.cpp:351
bool NeedToComputeTA() const
Test function if this indicator is computed with TALib interface.
Definition: Indicator.cpp:192
static Indicator const MACD
Definition: Indicator.h:44
Group general functions to convert to/from string, split line...
static Indicator const AD
Definition: Indicator.h:44
static Indicator const RSI
Definition: Indicator.h:44
static Indicator GetIndicator(const std::string &str_name)
Constructor with a string label.
Definition: Indicator.cpp:234
int MakeColor(const char str_color)
Definition: Indicator.cpp:362
static Indicator const Volume
Definition: Indicator.h:44
int nb_output() const
Nb of ListDataStock output of TALib computation.
Definition: Indicator.cpp:78
void Change(int new_value)
Definition: Indicator.cpp:373
static Indicator const StockInst
Definition: Indicator.h:44
Value v_
Definition: Indicator.h:36
static Indicator const EMA
Definition: Indicator.h:44
Value
Internally define an enumeration.
Definition: Indicator.h:34
static Indicator const StockCSV
Definition: Indicator.h:44
int nb_input() const
Nb of ListDataStock input to be computed.
Definition: Indicator.cpp:73
static Indicator const BOLL
Definition: Indicator.h:44
std::string GetDescription() const
Graphical description.
Definition: Indicator.cpp:102
Declare all indicators (historical data and technical indicators) which can be computed and visualize...
std::vector< int > GetDefaultValues() const
Get the default values, return an empty vector if none are provided.
Definition: Indicator.cpp:88
static Indicator const SMA
Definition: Indicator.h:44
std::ostream & operator<<(std::ostream &o, Indicator c)
Definition: Indicator.cpp:298
void splitline(const std::string &str, const std::string &delimiters, std::vector< std::string > &tokens)
splitline
Definition: Utils.cpp:56
std::string const label() const
Get the label.
Definition: Indicator.cpp:51