12 #ifndef Python_for_stock
13 #define Python_for_stock
16 #include <python2.7/Python.h>
19 using namespace TimeScale;
54 std::cout <<
"Entry Stock::LoadDictionary dict " << dict << std::endl;
62 pModule = PyImport_ImportModule(
"DictionaryStocks" );
65 if (pModule == NULL) {
66 std::cout <<
"\n\tCannot load module DictionaryStocks\n" << std::endl;
71 std::cout <<
"DictionaryStocks imported " << std::endl;
75 pFunc_Init = PyObject_GetAttrString(pModule,
"DictionaryStocks");
77 if (pFunc_Init && PyCallable_Check(pFunc_Init)) {
79 PyObject_CallObject( pFunc_Init, pArgs );
86 Py_DECREF ( pModule );
87 Py_DECREF ( pFunc_Init );
119 std::cout <<
"\n\tEntry Stock::UpdateCSVFromParser source " << source << std::endl;
120 std::cout <<
"dates first " << opt_dates.first <<
" " <<
Utils::Time_tToString( opt_dates.first ) << std::endl;
121 std::cout <<
"dates last " << opt_dates.last <<
" " <<
Utils::Time_tToString( opt_dates.last ) << std::endl;
124 std::string namestock=GetName();
129 time_t first_time = 0;
142 PyObject *pStockName, *pSource;
144 PyObject *pFunc_Parser, *pInstance;
146 PyObject *pFunc_GetListCSV, *pReturn_ListCSV;
147 PyObject *pArgs=0, *pArg_lasttime;
153 if ( opt_dates.first == 0 ) {
154 first_time = header_dates.
last;
156 std::cout <<
"Set First time, get the last time from header " << first_time << std::endl;
161 if ( opt_dates.last == 0 ) {
162 opt_dates.last = time( NULL );
169 std::cout <<
"opt_dates.last " << opt_dates.last <<
" " <<
Utils::Time_tToString( opt_dates.last ) << std::endl;
170 std::cout <<
"Set first_time " << first_time <<
" " <<
Utils::Time_tToString( first_time ) << std::endl;
187 if ( header_dates.
last > in_memory_dates.
last ) {
188 std::cout <<
"CANNOT_UPDATE UpdateFromWeb, return -1" << std::endl;
194 first_time = in_memory_dates.
last;
203 status = LoadDictionary(
"");
206 std::cout <<
"Could not load the dictionary" << std::endl;
207 throw (
StockPythonException( namestock,
"Error in UpdateCSVFromWeb in loading Python dictionary" ) ) ;
212 pStockName = PyString_FromString( namestock.c_str() );
213 pSource = PyString_FromString( source.c_str() );
216 std::cout <<
"source " << source << std::endl;
217 std::cout <<
"namestock " << namestock << std::endl;
221 pModule = PyImport_ImportModule(
"ParserStocks" );
224 if ( pModule == NULL ) {
226 std::cout <<
"Error in loading module" << std::endl;
228 throw (
StockPythonException( namestock,
"Error in UpdateCSVFromWeb in loading Python module" ) ) ;
234 pFunc_Parser = PyObject_GetAttrString(pModule,
"ParserStocks");
238 if (pFunc_Parser && PyCallable_Check(pFunc_Parser)) {
240 pArgs = PyTuple_New(2);
251 PyTuple_SetItem(pArgs, 0, pStockName);
252 PyTuple_SetItem(pArgs, 1, pSource);
256 pInstance = PyObject_CallObject( pFunc_Parser, pArgs );
263 if ( pInstance == NULL ) {
264 Py_DECREF(pFunc_Parser);
269 std::cout <<
"Call failed return -1" << std::endl;
270 throw (
StockPythonException( namestock,
"Error in UpdateCSVFromWeb in loaing Python module" ) ) ;
282 pFunc_GetListCSV = PyObject_GetAttrString( pInstance,
"GetListCSV");
285 if ( pFunc_GetListCSV && PyCallable_Check(pFunc_GetListCSV) ) {
289 std::cout <<
"Call python with argument first time " <<
Utils::Time_tToString(first_time) << std::endl;
290 std::cout <<
"Call python with argument last_dates " <<
Utils::Time_tToString(opt_dates.last) << std::endl;
293 pArg_lasttime = Py_BuildValue(
"(ll)", (
long int) first_time, (
long int) opt_dates.last);
298 pReturn_ListCSV = PyObject_CallObject( pFunc_GetListCSV, pArg_lasttime);
299 Py_DECREF( pArg_lasttime );
301 std::cout <<
" Size of pReturn_ListCSV " << PyList_Size( pReturn_ListCSV ) << std::endl;
305 if ( PyList_Size( pReturn_ListCSV ) == 0 ) {
307 std::cout <<
"Size of updated stockCSV == 0 " << std::endl;
311 Py_DECREF(pInstance);
322 std::cout <<
"GetListCSV return with size " << PyList_Size( pReturn_ListCSV ) << std::endl;
328 std::cout <<
"error in calling pFunc_GetListCSV" << std::endl;
330 Py_DECREF(pInstance);
332 fprintf(stderr,
"Call failed\n");
336 std::cout <<
"Extract data from the list " << std::endl;
355 Py_DECREF ( pModule );
356 Py_DECREF ( pInstance );
357 Py_DECREF ( pFunc_Parser );
359 Py_DECREF ( pArg_lasttime );
368 Py_DECREF ( pFunc_GetListCSV );
369 Py_DECREF ( pReturn_ListCSV );
375 new_size = new_listdata_vol->
GetSize();
380 if ( new_listdata_vol->
GetSize() ) {
393 if ( AddListDataToMap(
ETime::DAY, new_listdata_csv ) == 1 ) {
394 delete new_listdata_csv; new_listdata_csv=0;
396 if ( AddListDataToMap(
ETime::DAY, new_listdata_vol ) == 1 ) {
397 delete new_listdata_vol; new_listdata_vol=0;
404 std::cout <<
"StockwrapPyStock::Update return new_size " << new_size << std::endl;
417 std::cout <<
"Entry Stock::GetListDataFromPyList" << std::endl;
424 PyObject *pyListData;
430 size=PyList_GET_SIZE(pyListCSV);
432 std::cout <<
"size data pyListCSV reserve for list" << size << std::endl;
435 new_listdata_csv->
Reserve( size );
436 new_listdata_vol->
Reserve( size );
438 for (
int i=0; i<size; i++ ) {
439 pyListData = PyList_GET_ITEM(pyListCSV,i);
441 Py_XINCREF(pyListData);
443 date = (time_t) PyInt_AsLong(PyList_GET_ITEM(pyListData,0));
444 op = PyFloat_AsDouble(PyList_GET_ITEM(pyListData,1));
445 pl = PyFloat_AsDouble(PyList_GET_ITEM(pyListData,2));
446 mo = PyFloat_AsDouble(PyList_GET_ITEM(pyListData,3));
447 cl = PyFloat_AsDouble(PyList_GET_ITEM(pyListData,4));
448 vol = PyFloat_AsDouble(PyList_GET_ITEM(pyListData,5));
452 data_csv.
SetData(date,op,pl,mo,cl);
455 new_listdata_csv ->
AddData ( data_csv );
456 new_listdata_vol ->
AddData ( data_vol );
458 data_csv.
SetData(0,0.,0.,-1000.0,0.);
462 Py_XDECREF(pyListData);
470 std::cout <<
"entry testwrappython " << std::endl;
472 std::string namestock=GetName();
473 std::cout <<
"name " << namestock <<
"source " << source << std::endl;
481 PyObject *pStockName;
492 pSource = PyString_FromString( source.c_str() );
494 pStockName = PyString_FromString( namestock.c_str() );
495 Py_INCREF (pStockName);
499 pModule = PyImport_ImportModule(
"ModTest" );
501 if (pModule != NULL) {
505 std::cout <<
" toto " << std::endl;
512 Py_DECREF ( pStockName );
513 Py_DECREF ( pSource );
526 std::cout <<
"entry testwrappython 2 " << std::endl;
528 std::string namestock=GetName();
529 std::cout <<
"name " << namestock <<
" source " << source << std::endl;
537 PyObject *pStockName;
539 PyObject *pModule = NULL;
548 pSource = PyString_FromString( source.c_str() );
550 pStockName = PyString_FromString( namestock.c_str() );
551 Py_INCREF (pStockName);
557 pModule = PyImport_ImportModule(
"ModTest2" );
558 std::cout <<
"pModule " << pModule << std::endl;
563 if (pModule != NULL) {
565 Py_INCREF ( pModule );
569 std::cout <<
" loaded ModTest2 " << std::endl;
573 std::cout <<
" Module not loaded " << std::endl;
575 Py_DECREF ( pStockName );
576 Py_DECREF ( pSource );
583 Py_DECREF ( pStockName );
584 Py_DECREF ( pSource );
585 Py_DECREF ( pModule );
ListDataStock< DataCSV, VecChronologic > ldata_candle
Define a default policy for candlestick.
virtual void SetData(const time_t d, const std::vector< type_value_data > &vec)
Set a full Data : date + value(s) in a vector.
void GetListDataFromPyList(PyObject *pyListCSV, ldata_candle *new_listdata_csv, ldata_volume *new_listdata_vol)
Utility function to fill ListDataStocks from a python tuple.
void AddData(DataStock &data)
Add a copy of the DataStock into the ListDataStock.
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).
unsigned int GetSize() const
Return the size of the vector.
ListDataStock< SimpleData, VecChronologic > ldata_volume
Volume has also one value.
int LoadDictionary(std::string dict="")
Load the dictionary of Stock, see ServerPortfolio.
Base Exception for Stock and StockManager, all src_cpp code.
Use a policy PolicyChronologic, default VecNoChronologic.
Data type to describe Japanese candlesticks : open, high, low and close values.
void Reserve(const unsigned int new_size)
Reserve new_size elements.
void Order(const EPChrono b_chronologic=EPChrono::CHRONO)
Function to reverse the internal vector in its original policy.
std::string Time_tToString(const time_t &time, const bool b_hour)
Return a string representing the date All times are expressed in the localtime.
virtual void SetData(const time_t d, const std::vector< type_value_data > &vec)
Set a full Data : date + value(s) in a vector.
void SetDates()
Set the correct first/last dates from the DataStock in the vector.
Derive class which contains only one value of type type_value_data (float or double).
static Indicator const StockCSV
int TestwrapPython(std::string source)
static SUPPRESS_NOT_USED_WARN firstlast_dates get_fldate_default()
need to be static for using default argument in a function
Specific the wrapper python.
int TestwrapPython2(std::string source)
general structure for dates in csv files