ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stock.h
Go to the documentation of this file.
1 
8 #ifndef STOCK_H_
9 #define STOCK_H_
10 
11 #include <map>
12 
13 #include "../utils/FullName.h"
14 #include "../utils/Utils.h"
16 #include "StockException.h"
17 
18 // faster for compilation, no problem of recursive link
19 class DataStock;
20 class ListDataStockBase;
21 class TAlibAPI;
22 //class WaveletBase;
23 
24 // Maybe too much responsability in this class, all function as data-members
25 // Computation may be separate ? already TALib
26 // Stock may logically contain other data : fundamental, limits to check (permanently in MySQL DB), whatever needed for Portfolio construction
27 // More info could be retrieved from Python dictstocks / DataBase info
28 
29 
56 class Stock {
57 
58  private:
60  //@[
61  typedef std::map<ETime, std::vector<ListDataStockBase *> >::iterator it_map;
62  typedef std::map<ETime, std::vector<ListDataStockBase *> >::const_iterator cit_map;
63  typedef std::vector<ListDataStockBase *>::iterator it_listdata;
64  typedef std::vector<ListDataStockBase *>::const_iterator cit_listdata;
66 
71  std::string name;
73  // if python wrapper available, should use the info from dictstocks.txt
74  // if used only with python, can be deleted (symbol will be retrieved from the name in dictstocks.txt)
75  // if need DB access, same : name to symbol stored there.
76  // Certainly not needed anymore
77  std::string symbol;
78 
79  // problem if different paths,
80  // maybe better to use only one global one, with care
81  // extra parameter only for script use is needed
82  // certainly to delete also.. not sure
86  const std::string path;
87 
90 
92  std::map <ETime, std::vector<ListDataStockBase *> > mapTime_ListData;
93 
99  //warning To call if sure than it_map is not at end(), no check performed inside
100  // can delete it_m and performs find inside ! only to check if not at the end, but what to return ?
101  cit_listdata GetItList( cit_map& it_m, const Indicator& indic,
102  const std::vector<int>& param = std::vector<int>() ) const ;
103  cit_listdata GetItList( cit_map& it_m, const std::string& sname ) const ;
105  it_listdata GetItList( it_map& it_m, const Indicator& indic,
106  const std::vector<int>& param = std::vector<int>() );
108  it_listdata GetItList( it_map& it_m, const std::string& sname );
110 
111 
112  // Add other internal methods as private, cleaner interface
113 
122  // suppress tmscl, given in input, may add later because present in header
123  // need to be static to be called by Read_HeaderCSV_static
124  //int ReadLineHeader( const std::string& line, Utils::firstlast_dates& dates ) const;
125  static int ReadLineHeader( const std::string& line, Utils::firstlast_dates& dates );
126 
136  //void WriteLineHeader( const ETime& tmscl, const Utils::firstlast_dates& dates, std::ostringstream& ss_header ) const;
137  static void WriteLineHeader( const ETime& tmscl, const Utils::firstlast_dates& dates, std::ostringstream& ss_header );
139 
140 
155  void LoadCSVData(const ETime& tmscl, Utils::firstlast_dates& dates ) throw (StockException);
156 
163  // few check, should be called by LoadListData, moved to public for Transform_TimeCSV
164  // could make Transform_TimeCSV friend as well
165  int Transform_TimeScale ( const ETime& new_tmscl );
166 
168  // Call to external function, need interface ?, friend ?
169  // does really need to be member ?
170  // - python makes sense deal wih Stock on Web
171  // could be a composition with a separate class ?
172  // - Compute, only TA for now, but a separate class or namespace is possible
173  // Other libraries, other indicators to use
174 
175  // interaction with TAlib, both could be private ?? only called by LoadListData
176  // more private less public better
182  int ComputeTA( const ETime& tmscl, const std::string& name_indic )
183  throw(TALibException) ;
185 
190  // used for gui only ?
191  void SetLDataFullName( ListDataStockBase *p_listdata ) const;
192 
193  public:
194 
198  Stock() {p_talib = nullptr;}
199  // Constructor with name, set symbol == name and optional path to find the data
201  Stock( const std::string iname, const std::string isymbol, const std::string ipath);
202  // better constructor, to keep, ipath useful/better for tests, scripts ?
204  Stock( const std::string iname, const std::string ipath = Utils::GetPathData() );
205  virtual ~Stock();
207 
210  std::string GetName() const {return name;}
211  std::string GetSymbol() const {return symbol;}
212  std::string GetPath() const {return path;}
214 
217  // function do not see internal ListStockDataBase, should be public
218  // makes sense this function with tmscl only, intern ?
219  bool CheckLoaded( const ETime& tmscl) const;
221  bool CheckLoaded( const ETime& tmscl, const Indicator& indic, const std::vector<int>& param = std::vector<int>() ) const;
223  bool CheckLoaded( const ETime& tmscl, const std::string& sname) const;
225  // Only for StockManager to delete the entry...or other ?
226  bool IsEmpty() const;
227 
234  // possible to make the dynamic cast in the return ?? avoid from the code ??
235  // Indicator not necessary for Historic data (could implement)
236  ListDataStockBase* GetListData ( const ETime& tmscl, const Indicator& indic, const std::vector<int> param= std::vector<int>() ) const;
243  ListDataStockBase* GetListData ( const ETime& tmscl, const std::string& sname) const;
245 
283  int LoadListData( const ETime& tmscl, const Indicator& indic = Indicator::StockCSV, const std::vector<int>& param= std::vector<int>(),
286 
290  void DeleteListData( const ETime& tmscl, const Indicator& indic,std::vector<int> param = std::vector<int>() );
292  void DeleteListData( const ETime& tmscl, const std::string& sname );
294  void DeleteAllListData ( const ETime& tmscl );
296 
321  unsigned int ReadCSV( const ETime tmscl, Utils::firstlast_dates dates = Utils::get_fldate_default() )
322  throw ( StockFileError );
323 
324  // tmp default for testing, later move to normal, add opt_name ? */
336  void WriteCSV(const ETime tmscl, bool opt_tmp= false ) const throw ( StockException );
337 
348  int SaveCSV ( const ETime tmscl, bool opt_tmp =false ) const;
349 
361  int Read_HeaderCSV( ETime tmscl, Utils::firstlast_dates& dates ) const throw ( StockFileError );
362 
376  // certainly keep only the static version later
377  static int Read_HeaderCSV_static( std::string namestock, ETime tmscl, Utils::firstlast_dates & dates )
378  throw ( StockFileError );
379 
395  static int UpdateCSV_Inst( std::string namestock, DataStock& inst_value, DataStock& inst_volume,
396  ETime tmscl, bool opt_tmp = false ) throw ( StockFileError );
398 
401 
430  int AddListDataToMap( const ETime& tmscl, ListDataStockBase* p_listdata );
431 
433 
453  // indicator has been deleted, never used until now, always hist.data
454  // ok compile without
455  //time_t LastCSVUpdate(ETime tmscl = ETime::DAY, Indicator indic=Indicator::StockCSV) throw(StockException);
456  //time_t LastCSVUpdate(ETime tmscl = ETime::DAY, bool load_list = false) throw(StockException);
457 
469  void Updated( const ETime tmscl );
470 
483  void UpdateAllTimeScale( const ETime tmscl, Utils::firstlast_dates opt_dates = Utils::get_fldate_default(),
484  bool b_readfile = true );
485 
486  // C++ version, same role update DAY or dcsv; from python parser or from reading file
487  // normal dcsv read file if real time and DAY use python parser
488  // but could be mixed, dcsv force update of python (easy call Server_Client FORCE )
489  // DAY: csv could be updated by python, end of the day for instance, so need to read the file
490  // change name, only read from file
491  // was not bad idea to have a single interface to other update...
492  //int UpdateStocks( Indicator, TimeScale );
493 
509  // deleted call LastCSVUpdate, so should throw Exception ? or do not matter if throw by called function ?
510  int UpdateCSVFromFile( const ETime tmscl );
511 
512  //addtogroup python, not working here, for a single function
528  // important to add StockPythonException AND StockFileError, otherwise not caught. Works with StockException
529  // not clear if exception for all possible python errors, difficult to unit_test also
530  int UpdateCSVFromWeb( const std::string& source, Utils::firstlast_dates opt_dates = Utils::get_fldate_default() )
531  throw(StockException);
532 
539  // written to solve problem with GUI, where a temporary Stock is convenient
540  void MergeStock( const Stock *new_stock, bool /*to_replace*/);
541 
543 
552  void PrintMapChrono( const unsigned int verbose = 0 ) const;
554 
555  // could be in ListDataStock, no need to mix close, volume, open
556  // more centered, if needed StockManager can make the transition
557  // normally apply to close, here can try on every derivative !!
558  // WaveletBase* ComputeWavelet ( TimeScale tmscl ); //, std::string name_indic );
559 
560 
563  // addtogroup works here, ingroup for UpdateCSVFromParser..
569  // used internally by embedded python function, could be private
570  // Certainly much more to do with this functionality, access more data
580  int LoadDictionary ( std::string dict = "" );
581 
583  int TestwrapPython( std::string source );
584  // extend first version to produce error like UpdateCSVFromFile
585  int TestwrapPython2( std::string source ); //throw (std::exception);
587 
588 };
589 
590 #endif /* STOCK_H_ */
int SaveCSV(const ETime tmscl, bool opt_tmp=false) const
Save Historical data to file.
Definition: Stock.cpp:1357
Specific, inherit from FileError from utils.
"Clever" enumeration of the ListDataStock type : StockCSV, Volume, EMA, BOLL,...
Definition: Indicator.h:29
int UpdateCSVFromFile(const ETime tmscl)
Load the last historical data from the filesystem into a List in memory.
Definition: Stock.cpp:2068
int UpdateCSVFromWeb(const std::string &source, Utils::firstlast_dates opt_dates=Utils::get_fldate_default())
Update data from the Python parser (and save into the filesystem).
void SetLDataFullName(ListDataStockBase *p_listdata) const
Function to complete the fullname of a listdatastock.
Definition: Stock.cpp:258
std::map< ETime, std::vector< ListDataStockBase * > >::iterator it_map
Definition: Stock.h:61
static int ReadLineHeader(const std::string &line, Utils::firstlast_dates &dates)
Extract dates from the header of the historic data files.
Definition: Stock.cpp:116
int LoadDictionary(std::string dict="")
Load the dictionary of Stock, see ServerPortfolio.
std::vector< ListDataStockBase * >::iterator it_listdata
Definition: Stock.h:63
const std::string path
File path for the data directory.
Definition: Stock.h:86
Base Exception for Stock and StockManager, all src_cpp code.
std::map< ETime, std::vector< ListDataStockBase * > >::const_iterator cit_map
Definition: Stock.h:62
bool IsEmpty() const
Test if some ListData are still present, whatever the timescale.
Definition: Stock.cpp:567
void UpdateAllTimeScale(const ETime tmscl, Utils::firstlast_dates opt_dates=Utils::get_fldate_default(), bool b_readfile=true)
Update all indicators of the given TimeScale.
Definition: Stock.cpp:1892
Main class to deal with one stock.
Definition: Stock.h:56
int AddListDataToMap(const ETime &tmscl, ListDataStockBase *p_listdata)
Insert a ListDataStock to the map.
Definition: Stock.cpp:622
Wrapper/Adapter to the library ta-lib.
Definition: TAlibAPI.h:74
std::vector< ListDataStockBase * >::const_iterator cit_listdata
Definition: Stock.h:64
void DeleteListData(const ETime &tmscl, const Indicator &indic, std::vector< int > param=std::vector< int >())
Delete a list given the TimeScale, Indicator and param.
Definition: Stock.cpp:872
static void WriteLineHeader(const ETime &tmscl, const Utils::firstlast_dates &dates, std::ostringstream &ss_header)
Write the header in a stream.
Definition: Stock.cpp:159
ETime
Enumeration for the different time representation, from instantaneous (INST) to year(YEAR) ...
Definition: TimeScale.h:48
int Transform_TimeScale(const ETime &new_tmscl)
Transform history values dcsv or DAY to M5,M10..H1 or WEEK, MONTH..YEAR.
Definition: Stock.cpp:273
ListDataStockBase * GetListData(const ETime &tmscl, const Indicator &indic, const std::vector< int > param=std::vector< int >()) const
Get the ListDataStock from a TimeScale, Indicator and vector of parameters.
Definition: Stock.cpp:581
void LoadCSVData(const ETime &tmscl, Utils::firstlast_dates &dates)
Get Indicator::StockCSV or StockdCSV with the correct TimeScale Call functions to read and load data ...
Definition: Stock.cpp:177
std::string GetSymbol() const
Definition: Stock.h:211
void WriteCSV(const ETime tmscl, bool opt_tmp=false) const
Write historical data into files.
Definition: Stock.cpp:1247
bool CheckLoaded(const ETime &tmscl) const
Definition: Stock.cpp:529
Stock()
Default constructor.
Definition: Stock.h:198
virtual ~Stock()
Definition: Stock.cpp:47
void MergeStock(const Stock *new_stock, bool)
Merge entries with an other stock.
Definition: Stock.cpp:730
unsigned int ReadCSV(const ETime tmscl, Utils::firstlast_dates dates=Utils::get_fldate_default())
Read DAY or INST file depending on the TimeScale.
Definition: Stock.cpp:957
std::string GetPathData()
Definition: Utils.cpp:42
int ComputeTA(const ETime &tmscl, const std::string &name_indic)
Compute an indicator using the TAlib library.
Definition: Stock.cpp:352
Exception to be used within stock_cpp.
static Indicator const StockCSV
Definition: Indicator.h:44
std::string GetPath() const
Definition: Stock.h:212
Abstract base class for the ListDataStock, for storing base pointers in vectors.
Definition: ListDataStock.h:50
void Updated(const ETime tmscl)
Return the last available date of historical data.
Definition: Stock.cpp:1799
void DeleteAllListData(const ETime &tmscl)
Delete all data of the given TimeScale.
Definition: Stock.cpp:916
int TestwrapPython(std::string source)
TAlibAPI * p_talib
pointer to TA-lib API object, use a lazy initialization of the library
Definition: Stock.h:89
void PrintMapChrono(const unsigned int verbose=0) const
Print map content to the console.
Definition: Stock.cpp:2127
Base class for exception coming from talibapi interface.
static SUPPRESS_NOT_USED_WARN firstlast_dates get_fldate_default()
need to be static for using default argument in a function
Definition: Utils.h:60
std::map< ETime, std::vector< ListDataStockBase * > > mapTime_ListData
map TimeScale : vector of ListDataStockBase pointers
Definition: Stock.h:92
static int Read_HeaderCSV_static(std::string namestock, ETime tmscl, Utils::firstlast_dates &dates)
Static version of Read_HeaderCSV.
Definition: Stock.cpp:1519
int LoadListData(const ETime &tmscl, const Indicator &indic=Indicator::StockCSV, const std::vector< int > &param=std::vector< int >(), Utils::firstlast_dates dates=Utils::get_fldate_default())
Load or/and compute an indicator in the Stock.
Definition: Stock.cpp:801
cit_listdata GetItList(cit_map &it_m, const Indicator &indic, const std::vector< int > &param=std::vector< int >()) const
Definition: Stock.cpp:72
int Read_HeaderCSV(ETime tmscl, Utils::firstlast_dates &dates) const
Read the dates from the header file.
Definition: Stock.cpp:1487
static int UpdateCSV_Inst(std::string namestock, DataStock &inst_value, DataStock &inst_volume, ETime tmscl, bool opt_tmp=false)
Update file with TimeScale INST.
Definition: Stock.cpp:1555
int TestwrapPython2(std::string source)
general structure for dates in csv files
Definition: Utils.h:54
std::string symbol
symbol refers to the code, i.e., CAC40
Definition: Stock.h:77
std::string GetName() const
Definition: Stock.h:210
Abstract base class for all Data to be stored in ListDataStock.
Definition: DataStock.h:90
std::string name
Name of the stock.
Definition: Stock.h:71