ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FactoryObjectStock.cxx
Go to the documentation of this file.
1 
9 #include "FactoryObjectStock.h"
10 
11 #include "TypeObjectLine.h"
12 
13 // needed for Add ObjectLine to THStock
14 #include "THStock.h"
15 //#include "ObjectLine.h"
16 #include "ListObjectStock.h"
17 
18 
19 
20 // mandatory ?? should static must be initialized
22 
24 
37 //AbstractObjectStock* FactoryObjectStock::CreateObjectStock( Indicator indic, UInt_t size, THStock* parent )
38 IndicatorObjectStock* FactoryObjectStock::CreateObjectStock( Indicator indic, std::string full_name_indic, unsigned int size,
39  THStock * parent, std::string opt_string )
40 {
41 #ifdef DEBUG_OBJSTOCK
42  std::cout << "Entry FactoryObjectStock::CreateObjectStock " << std::endl;
43  //std::cout << "name " << listdata_stock->GetName() << std::endl;
44  //Indicator indic = Indicator::GetIndicator ( listdata_stock->GetName() );
45  std::cout << "Indicator " << indic.label() << " " << indic.GetDescription() << std::endl;
46  std::cout << "Full_name_indic " << full_name_indic << std::endl;
47  std::cout << "size " << size << std::endl;
48 
49  //std::cout << "opt_pair " << opt_pair.first << " , " << opt_pair.second << std::endl;
50  std::cout << "opt_string :" << opt_string << "!!" << std::endl;
51 #endif
52 
53  // used for Collection, can contain ObjectLine
54  //AbstractObjectStock * sub_object = 0;
55  // problem with collection and DistancetoPrimitive
56  IndicatorObjectStock * sub_object = 0;
57  IndicatorObjectStock * object = 0;
58 
59  // type L = ListObjectStock, K = Collection
60  std::string type;
61 
62  // stores information : type, (object), color
63  //std::vector< std::pair<std::string, std::string> > obj_color;
64  std::vector<std::string> obj_color;
65  //std::vector< std::vector<std::string> > obj_color;
66 
67  std::vector<std::string> descp;
68 
69  if ( opt_string.size() > 0 ) {
70 #ifdef DEBUG_OBJSTOCK
71  std::cout << " An optional opt_string was given " << std::endl;
72 #endif
73  // need to set descp
74  descp.push_back ( opt_string );
75 #ifdef DEBUG_OBJSTOCK
76  std::cout << "update descp[0] " << descp[0] << std::endl;
77 #endif
78 
79  } else {
80 #ifdef DEBUG_OBJSTOCK
81  std::cout << "Will call parse description" << std::endl;
82 #endif
83  // should return a string "LC0" / "P0R"
84  descp = indic.parse_description();
85  }
86 
87 #ifdef DEBUG_OBJSTOCK
88  std::cout << "\tPrint Factory, get description of the indicator " << std::endl;
89  std::cout << " summary in Factory " << std::endl;
90  //std::cout << " type " << type << std::endl;
91  for ( unsigned int i = 0; i < descp.size(); i ++ ) {
92  std::cout << "descp " << descp[i] << std::endl;
93  }
94 #endif
95  //exit(1);
96 
97  // make a ListDataStock
98  if ( descp[0][0] == 'L' ) {
99 #ifdef DEBUG_OBJSTOCK
100  std::cout << "case L" << std::endl;
101 #endif
102  // list of candles
103  if ( descp[0][1] == 'C' ) {
104 #ifdef DEBUG_OBJSTOCK
105  std::cout << " case C for Candle " << std::endl;
106 #endif
107  object = new ListObjectStock<CandleStock> ( size, full_name_indic );
108  // list of bar
109  } else if ( descp[0][1] == 'B' ) {
110 #ifdef DEBUG_OBJSTOCK
111  std::cout << " case B for Bar " << std::endl;
112 #endif
113  // problem with name...
114  object = new ListObjectStock<BarStock> ( size, full_name_indic );
115  //set color, more tricky here
116  //object descp[0][2] contains color
117 
118  }
119  // PStockLine
120  } else if ( descp[0][0] == 'P' ) {
121 #ifdef DEBUG_OBJSTOCK
122  std::cout << "case P" << std::endl;
123 #endif
124  object = new PStockLine( full_name_indic, size );
125  //std::cout << " kBlue" << kBlue << std::endl;
126  // object->SetLineColor ( kBlue );
127 #ifdef DEBUG_OBJSTOCK
128  std::cout << "get color " << descp[0][2] << std::endl;
129 #endif
130 
131  // works directly with SetLineColor
132  //object->SetLineColor( FactoryObjectStock::MakeColor( descp[0][2]) );
133  object->SetLineColor( indic.MakeEColor( descp[0][2]) );
134 
135  // Collection need to loop over all elements
136  } else if ( descp[0][0] == 'K' ) {
137 #ifdef DEBUG_OBJSTOCK
138  std::cout << "case K" << std::endl;
139 #endif
140 
141  object = new CollectionObjectStock( size, full_name_indic );
142  // need at least 3 LBR for instance
143  // goal Create with string ( "LBR" / "P0B" / "LC0" ...)
144  for ( unsigned int i = 1; i < descp.size(); i++ ) {
145  //std::cout << "obj " << obj_color[i].first << " color " << obj_color[i].second << std::endl;
146  //sub_pair.first = obj_color[i].first;
147  //sub_pair.second = obj_color[i].second;
148  // here the trick, should be in indicator
149 #ifdef DEBUG_OBJSTOCK
150  std::cout << "Call Factory::CreateObject descp[i] " << descp[i] << std::endl;
151 #endif
152  sub_object = FactoryObjectStock::CreateObjectStock( indic, full_name_indic, size, parent, descp[i] );
153  // maybe name to give
154  // can be in an other array in Indicator
155  //dynamic_cast<>object->AddObject( sub_object );
156  // function defined in IndicatorObjectStock
157  object->AddToVecObject( sub_object );
158  // variant make directly because no want to loose the type
159  // not working better, always call base class, missing virtual in base class
160  //(dynamic_cast<CollectionObjectStock *>(object))->vec_object.push_back( sub_object );
161  }
162  } else {
163  std::cout <<"descriptor not implemented " << descp[0][0] << std::endl;
164 }
165 
167 
168  // need additional descriptor in Indicator
169  if ( indic == Indicator::RSI ) {
170 #ifdef DEBUG_OBJSTOCK
171  std::cout << "indic == RSI add ObjectLineIndicator " << std::endl;
172 #endif
173  ObjectLineIndicator * line1 = new ObjectLineIndicator("RSI 30", 30 );
174  line1->SetLineColor ( kBlack );
175  ObjectLineIndicator * line2 = new ObjectLineIndicator("RSI 70", 70 );
176  line2->SetLineColor ( kBlack );
177 
178  // need explicit, not contained in a collection ?
179  line1->SetParent( parent );
180  line2->SetParent( parent );
181 
182  //std::cout << "Add to parent THStock " << std::endl;
183  // Can choose, add to parent ( THStock ) send by argument
184  // call complexe function originally, done for all lines
185  // RSI PStockLine, need to assign to THStock, or make RSI a collection..
186  parent->Add ( line1 );
187  parent->Add ( line2 );
188  // or add to Collection, both are acceptable
189  // simple and logic, belong to the indicator
190  // RSI is not a Collection, it is a simple PStockLine
191  // cannot do this ! why ?
192  //object->AddToVecObject ( line1 );
193  //object->AddToVecObject ( line2 );
194 
195  } //else if ( indic == XXX )
196 
197  /*
198  } else {
199  std::cout << " FactoryObjectStock::CreateObjectStock indicator not known " << indic.label() << std::endl;
200  return 0;
201  }
202  */
203 
204 #ifdef DEBUG_OBJSTOCK
205  std::cout << "Exit FactoryObjectStock::CreateObjectStock object" << object << std::endl;
206  std::cout << "GetSize of new object " << object->GetSize() << std::endl;
207  std::cout << "Return object ObjectName" << object->GetNameObject() << std::endl;
208 #endif
209 
210  return object;
211 }
212 
213 
214 // could add THStock as parameter for anchor, anyway THStock known by all lines...
215 // to see later
224 ObjectLine * FactoryObjectStock::CreateDrawableObjectLine( int stage_draw, double valueX, double valueY, TypeLine mode_obj )
225 {
226 #ifdef DEBUG_OBJSTOCK
227  std::cout << "\n\tEntry FactoryObjectStock::CreateDrawableObjectLine " << std::endl;
228  std::cout << "stage_draw " << stage_draw << std::endl;
229  std::cout << "valueX " << valueX << " valueY " << valueY << std::endl;
230 #endif
231 
232 #ifdef DEBUG_OBJSTOCK
233  if ( obj != nullptr ) {
234  //std::cout << "obj different of nullptr get x0,Y0 " << obj->GetX0() << " " << obj->GetY0 << std::endl;
235  std::cout << "obj different of nullptr" << std::endl;
236  } else
237  std::cout << "obj is nullptr " << std::endl;
238 #endif
239  // creates the line
240  if ( stage_draw == kBeginDraw ) {
241 
242  // test not needed, because send to ObjectLineTendance...,
243  // maybe for later
244  if ( ( mode_obj== TypeLine::kSupport ) || ( mode_obj== TypeLine::kTendance ) ) {
245 #ifdef DEBUG_OBJSTOCK
246  std::cout << "Draw a support or a tendance at stage 1 creation " << std::endl;
247 #endif
248 
249  // should take care for deletion, or re-initialisation
250  obj = new ObjectLineTendance ( mode_obj, valueX, valueY );
251 
252  if ( obj != nullptr ) {
253  obj->SetLineColor(4); //Blue, can use function
254  return obj;
255  } else {
256  std::cout << "Error in creating ObjectLineTendance, stage begin object null" << std::endl;
257  return 0; //nullptr;
258  }
259  }
260 
261  // move the line, if only one factory, need to test kSupport or kTendance
262  } else if ( stage_draw == kMoveDraw ) {
263 #ifdef DEBUG_OBJSTOCK
264  std::cout << "move a support or a tendance " << std::endl;
265  //std::cout << "move to " << gPad->AbsPixeltoX( px ) << " " << gPad->AbsPixeltoY( py ) << std::endl;
266  std::cout << "move to " << valueX << " " << valueY << std::endl;
267  /*
268  std::cout << "gPadXtoAbsPixel " << gPad->XtoAbsPixel(obj->GetX0())
269  << " " << gPad->YtoAbsPixel(obj->GetY0())
270  << " " << gPad->XtoAbsPixel(obj->GetX1())
271  << " " << gPad->YtoAbsPixel(obj->GetY1()) << std::endl;
272  */
273 #endif
274  // works here, not in TPadStock ?? why ??
275  // DrawLine must be in pixel
276  // act like if draw 2 times, it deletes the line !! both are necessary !! ??
277  gVirtualX->DrawLine( gPad->XtoAbsPixel(obj->GetX0()),
278  gPad->YtoAbsPixel(obj->GetY0()),
279  gPad->XtoAbsPixel(obj->GetX1()),
280  gPad->YtoAbsPixel(obj->GetY1()) );
281 
282  // works if moved here !! just between
283  // set X1 value
284  obj->SetX1( valueX );
285 
286  // set Y1 value
287  // here use object, could use parameter ?? no if reset !!
288  if ( obj->GetTypeLine() == TypeLine::kSupport ) {
289  obj->SetY1( obj->GetY0() );
290  } else if ( obj->GetTypeLine() == TypeLine::kTendance ) {
291  obj->SetY1( valueY );
292  }
293 
294  // creates the new line
295  gVirtualX->DrawLine( gPad->XtoAbsPixel(obj->GetX0()),
296  gPad->YtoAbsPixel(obj->GetY0()),
297  gPad->XtoAbsPixel(obj->GetX1()),
298  gPad->YtoAbsPixel(obj->GetY1()) );
299 
300  //return obj;
301 
302  // finalize the creation of a line
303  } else if ( stage_draw == kEndDraw ) {
304 #ifdef DEBUG_OBJSTOCK
305  std::cout << "finalize a support or a tendance " << std::endl;
306  std::cout << "get X0 X1 " << obj->GetX0() << " " << obj->GetX1() << std::endl;
307  std::cout << "get Y0 Y1 " << obj->GetY0() << " " << obj->GetY1() << std::endl;
308  //std::cout << "SetX1/Y1 " << gPad->AbsPixeltoX( px ) << " " << gPad->AbsPixeltoY( py ) << std::endl;
309  std::cout << "SetX1/Y1 " << valueX << " " << valueY << std::endl;
310 #endif
311 
312  if ( obj->GetTypeLine() == TypeLine::kSupport ) {
313  obj->SetY1( obj->GetY0() );
314  } else if ( obj->GetTypeLine() == TypeLine::kTendance ) {
315  obj->SetY1( valueY );
316  } else {
317  std::cout << "Mode is not known, make default behaviour setX1 " << std::endl;
318  obj->SetX1 ( valueX );
319  }
320 
321  // here works ?? yes, but try in TPadStock
322  // works in TPad, to check all and delete
323  /*
324  if ( obj->InheritsFrom("ObjectLineTendance") ) {
325 #ifdef DEBUG_OBJSTOCK
326  std::cout << "Inherits from ObjTendance, call Finalise " << std::endl;
327 #endif
328  dynamic_cast<ObjectLineTendance *>(obj)->FinaliseDrawableLine( obj->GetParent()->GetCommonXAxis() );
329  }
330 #ifdef DEBUG_OBJSTOCK
331  else {
332  std::cout << "Do not inherits from ObjectLineTendance, nothing to do for finalize " << std::endl;
333  }
334 #endif
335  */
336  // not used anyway
337  return obj;
338  }
339  // retrun null when moving line
340  return nullptr;
341 }
342 
343 //will deal with particular List or Collection,
344 //certainly better come back to previous one "general list" with aggregate or not, not sure problem of recursivity
345 // goal of factory call approprite method, cannot avoid this and point of it !!
346 
347 //void FactoryObjectStock::Resize( ListDataStockBase * list_stock, UInt_t new_size )
348 //consider no problem of update last value
349 
350 /* implements for each
351 //listdta already cu, and checked for update of last ( deleted before not sure )
352 //can keep new_size to check if delete/replace the last old data, checked beforie
353 //void FactoryObjectStock::Resize( IndicatorObjectStock* obj, Indicator indic, UInt_t new_size )
354 void FactoryObjectStock::Resize( AbstractObjectStock* obj, Indicator indic, UInt_t new_size )
355 {
356 #ifdef DEBUG_OBJSTOCK
357  std::cout << "FactoryObjectStock::Resize() new size " << new_size << std::endl;
358  std::cout << "Indicator " << indic.label() << std::endl;
359  //std::cout << "new_size - ArrayObject.GetEntriesFast() " << new_size - ArrayObject.GetEntriesFast() << std::endl;
360  //std::cout << "listdata_new name " << listdata->GetName() << " " << listdata->GetSize() << std::endl;
361  std::cout << " obj " << obj << "ClassName " << obj->ClassName() << std::endl;
362  //std::cout << " obj->GetSize() " << obj->GetSize() << std::endl;
363 #endif
364 
365  //call the function the most general, need to reimplement
366  //obj->Resize( indic, new_size );
367 
368  //only for indicator ?? could be independent because ListObjectStock stores only Candle and Bar
369  //Indicator indic = Indicator::GetIndicator ( list_stock->GetName() );
370  //need to save old_size
371 }
372 */
373 
375 /*
376 enum EColor { kWhite =0, kBlack =1, kGray=920,
377  kRed =632, kGreen =416, kBlue=600, kYellow=400, kMagenta=616, kCyan=432,
378  kOrange=800, kSpring=820, kTeal=840, kAzure =860, kViolet =880, kPink=900 };
379 */
380 EColor FactoryObjectStock::MakeColor( const char str_color)
381 {
382  //if ( str_color == "0" ) return 0;
383  //else
384  if ( str_color == 'B') return kBlue;
385  else if ( str_color == 'R' ) return kRed;
386  else if ( str_color == 'V' ) return kViolet;
387  else return kPink;
388 }
std::vector< std::string > parse_description() const
Utility function to parse the description.
Definition: Indicator.cpp:112
double GetY1() const
Definition: ObjectLine.h:98
virtual TypeLine GetTypeLine() const
Definition: ObjectLine.h:133
Derive from TH1 will draw the graph.
Definition: THStock.h:59
"Clever" enumeration of the ListDataStock type : StockCSV, Volume, EMA, BOLL,...
Definition: Indicator.h:29
static ObjectLine * obj
no direct access to internal ObjectLine
static IndicatorObjectStock * CreateObjectStock(Indicator indic, std::string full_name_indic, unsigned int size, THStock *parent, std::string opt_string=std::string())
create indicator object
virtual void SetParent(THStock *thstock)
virtual void Add(ObjectLine *obj_stock)
call by Factory, specific to drawable line
Definition: THStock.cxx:231
int MakeEColor(const char str_color)
Definition: Indicator.cpp:351
static Indicator const RSI
Definition: Indicator.h:44
double GetX0() const
wrapper functions to fline, need public for all ??
Definition: ObjectLine.h:95
Define a base class for drawing indicator only, they are sorted in ListDataStock object.
Define the template ListObjectStock<T> in a separate header.
static EColor MakeColor(const char str_color)
translate name to integer color in root
static ObjectLine * CreateDrawableObjectLine(int stage_draw, double valueX, double valueY, TypeLine mode_obj)
create specifically ObjectLine
virtual void SetLineColor(Color_t color)
Definition: ObjectLine.h:114
class for drawing graph
double GetY0() const
Definition: ObjectLine.h:97
Define a vector for all identical Object ( Candle, Bar...)
Used for free drawing of support/tendance Add a general line description with coeef_d and origin : y ...
Definition: ObjectLine.h:230
std::string GetDescription() const
Graphical description.
Definition: Indicator.cpp:102
void SetY1(double y1)
Definition: ObjectLine.h:103
Factory for all Object.
double GetX1() const
Definition: ObjectLine.h:96
for all ObjectLines
Definition: ObjectLine.h:33
TypeLine
c++ 11 style, used by both EditorObjectStock and ObjectLine cannot make dictionary with enum class ! ...
void SetX1(double x1)
Definition: ObjectLine.h:101
Line for drawing with indicator, eg, RSI 30-70 Could be in Collection or in THStock They are fixed : ...
Definition: ObjectLine.h:178
std::string const label() const
Get the label.
Definition: Indicator.cpp:51