ExSan & Interactive Brokers C++ API CODING 003

      Previous Post       Next Post

ExSan & Interactive Brokers C++ API

    1 /*****************************************************************************/
    2 // exsan.plusplus@gmail.com
    3 // ACTUAL CODE 
    4 //
    5 int main(void){
    6    
    7    // basic parameters 
    8    unsigned short rows(2), cols(3);// main matrix 
    9    unsigned short pp_float(5);     // 5 worksheets, could be in this range[0 ... 536870911] 
   10    unsigned short pp_bool(8);      // 8 worksheets UNSIGNED SHORT
   11    unsigned short pp_us(5);        // 5 worksheets UNSIGNED SHORT
   12    unsigned short pp_string(2);    // 2 worksheets STRING
   13    unsigned short pp_char(0);      // 0 worksheets CHAR
   14    
   15    Net* net(nullptr);
   16     net = net->exsan(rows, cols, pp_float, pp_bool, pp_us, pp_string, pp_char);
   17    // 
   18    // HERE ALL CODE YOU CAN IMAGINE, sky is the limit
   19    ///   EXSAN LIBRARY
   20    /// Console Oriented Worksheet coded in C++
   21    /// +100K lines of source code
   22    /// A novel data structure build upon Binary Tree
   23    /// A master template supports the whole ExSan
   24    /// This master template (BINARY TREE) has been coded ONLY ONCE
   25    ///  more info in my blog    
   26     /// youtube channel  ExSan PlusPlus
   27    ///   
   28    
   29    net->kill_exsan(net);
   30    
   31    return 0;
   32 }
   33 /****************************** END main *************************************/
   34 
   35 
   36 /*********************************************************************/
   37 // ref: Introduction to Algorithms
   38 // Cormen, Leiserson, Rivest Ed. 1990
   39 // Tree-Insert pp 268
   40 void Cell::RB_Insert(CELLPTR t, NODEPTR &root, NODEPTR x){  
   41    t->Tree_Insert_nt(root, x);      
   42    NODEPTR y(nullptr);
   43    x->set_nt_color_red();                              //2   
   44    while (x != root && x->get_nt_color_p(x) == red){
   45       if(x->get_nt_p_ptr() == x->get_nt_p_ptr()->get_nt_p_ptr()->get_nt_left_ptr()){ //4
   46          //cerr << "\n ...........if ";  
   47          y = x->get_nt_p_ptr()->get_nt_p_ptr()->get_nt_right_ptr();
   48          if (y->get_nt_color(y) == red){   
   49             x->get_nt_p_ptr()->set_nt_color_black();               
   50             y->set_nt_color_black();               //8
   51             x->get_nt_p_ptr()->get_nt_p_ptr()->set_nt_color_red();
   52             x = x->get_nt_p_ptr()->get_nt_p_ptr();   
   53          }
   54          else{
   55             //cerr << "      *if else";            
   56             if (x == x->get_nt_p_ptr()->get_nt_right_ptr()){
   57                x = x->get_nt_p_ptr();
   58                //cerr << "\n      antes left rot"; 
   59                //Cell->Tree_Inorder(root, id, page);
   60                t->leftRotate_nt(root, x);                  
   61                //cerr << "\n      after left rotate"; 
   62                //Cell->Tree_Inorder(root, id, page);
   63             }               
   64             x->get_nt_p_ptr()->set_nt_color_black();               
   65             x->get_nt_p_ptr()->get_nt_p_ptr()->set_nt_color_red();               
   66             //cerr << "\nbefore right rotate";  
   67             //Cell->Tree_Inorder(root, id, page);
   68             t->rightRotate_nt(root, x->get_nt_p_ptr()->get_nt_p_ptr());
   69             //cerr << "\nafter right rotate"; 
   70             //Cell->Tree_Inorder(root, id, page);
   71          }            
   72       }
   73       else{// same as then right<->left              //17
   74          //cerr << "\n      en else ***r<>l**"; 
   75          y = x->get_nt_p_ptr()->get_nt_p_ptr()->get_nt_left_ptr();         
   76          if (y->get_nt_color(y) == red){
   77             //cerr << "\n      if --- "; //system("pause");
   78             x->get_nt_p_ptr()->set_nt_color_black();               
   79             y->set_nt_color_black();            //8
   80             x->get_nt_p_ptr()->get_nt_p_ptr()->set_nt_color_red();
   81             x = x->get_nt_p_ptr()->get_nt_p_ptr();
   82          }
   83          else{
   84             //cerr << "\n        "; //system("pause");
   85             if (x == x->get_nt_p_ptr()->get_nt_left_ptr()){
   86                x = x->get_nt_p_ptr();
   87                //cerr << "\n  before right  rotate ";
   88                t->rightRotate_nt(root, x);
   89                //cerr << "\n  after right rotate ";
   90             }               
   91             //cerr << "\n      ------*****---------->";
   92             x->get_nt_p_ptr()->set_nt_color_black();      
   93             //cerr << "\n      ---------------->";
   94             x->get_nt_p_ptr()->get_nt_p_ptr()->set_nt_color_red();
   95             //cerr << "\n      before left rotate ";
   96             t->leftRotate_nt(root, x->get_nt_p_ptr()->get_nt_p_ptr());
   97             //cerr << "\n      after left rotate ";               
   98          }
   99       }         
  100    }
  101    root->set_nt_color_black();            //18   
  102    //*/
  103 }
  104 /************************* RB_Insert ***********************************/
  105  
  106  
  107 /*******************************/
  108 // ref: Introduction to Algorithms
  109 // Cormen, Leiserson, Rivest Ed. 1990
  110 // Tree-Insert pp 251
  111 void Cell::Tree_Insert_nt(
  112             NODEPTR & root, 
  113             NODEPTR node) {
  114                
  115    NODEPTR x(root), y(nullptr);
  116    
  117    while (x) {
  118       y = x;  // 
  119       node->get_key_nt() < x->get_key_nt() ? 
  120          x = x->get_nt_left_ptr() : 
  121          x = x->get_nt_right_ptr();
  122    }
  123    
  124    node->set_nt_p_ptr(y); 
  125    
  126    if (!y) root = node;
  127    else
  128       node->get_key_nt() < y->get_key_nt() ? 
  129          y->set_nt_left_ptr(node) : 
  130          y->set_nt_right_ptr(node);   
  131 }
  132 /*** END Tree_Insert_nt ***********/
  133 
  134 /*************************************************/
  135 // https://twitter.com/ExSan_com 
  136 // exsan.plusplus@gmail.com
  137 // ref: Introduction to ALGORITHMS The MIT Press
  138 // Cormen, Leiserson, Rivest Ed. 1990
  139 // pseudoCode RB Tree - Left Rotate  pp 266
  140 //---------------------------
  141 //        LEFT-ROTATE(T, x)
  142 //01   y ← right [x]  ➤ set y  
  143 //02   right [x]  ← left [y]
  144 //03   if left [y]  ≠ NIL
  145 //04   then p[left[y]] ← x
  146 //05   p[y] ← [x] ➤link x's parent to y
  147 //06   if p[x] = NIL   
  148 //07      then root[T] ← y  
  149 //08      else if x = left[p[x]]
  150 //09            then left[p[x]] ← y
  151 //10            else right[p[x]] ← y
  152 //11   left[y] ← x
  153 //12   p[x] ← y
  154 //---------------------------
  155 // 
  156 void Cell::leftRotate_nt(NODEPTR& root, NODEPTR x) {
  157    
  158    NODEPTR temp(nullptr);
  159    NODEPTR y(nullptr);      
  160    
  161    y = x->get_nt_right_ptr();
  162    x->set_nt_right_ptr(y->get_nt_left_ptr());   //02      
  163    
  164    if (y->get_nt_left_ptr())  
  165       y->get_nt_left_ptr()->set_nt_p_ptr(x);  //03               
  166    
  167    y->set_nt_p_ptr(x->get_nt_p_ptr());       
  168    //exsan.plusplus@gmail.com
  169    if (!x->get_nt_p_ptr())      
  170       root = y;
  171    else {
  172       if (x == x->get_nt_p_ptr()->get_nt_left_ptr()) 
  173          x->get_nt_p_ptr()->set_nt_left_ptr(y);
  174       
  175       else x->get_nt_p_ptr()->set_nt_right_ptr(y);
  176    }
  177    
  178    y->set_nt_left_ptr(x); //11
  179    x->set_nt_p_ptr(y);      //12
  180 }
  181 /************ leftRotate_nt  ********************/
  182 
  183 
  184 /***********START InOrderData  3356 ***************/
  185 void Net::InOrderDataAndCountMain(NETPTR net, 
  186                                   CELLPTR head, 
  187                           unsigned short& inTreeCounter, 
  188                           unsigned short ppData,  
  189                           unsigned short * ary) {
  190    
  191    unsigned short static counter(0);
  192    unsigned short static counterLine(0);
  193 
  194    if (head) {
  195       net->InOrderDataAndCountMain(net, head->get_db_left_ptr(), inTreeCounter, ppData, ary);
  196       ++ary[head->get_row()]; 
  197       ++ary[head->get_col()];
  198       if (!(++counter % 10)) {
  199          counterLine++;   
  200       }
  201       inTreeCounter++;      
  202       net->InOrderDataAndCountMain(net,  head->get_db_right_ptr(), inTreeCounter, ppData, ary);
  203    }
  204 }
  205 /*********************InOrderData****************************/
  206 
  207 
  208 /**********************************************/
  209 PTR searchStock(ROWPTR root, string stockName) {
  210    NODEPTR ptr = x;
  211    
  212    static float e_p =  0.00001;
  213    static float e_n = -0.00001;
  214    
  215    if (puntero == NULL || 
  216       ((ptr->get_data() - k > e_n) && 
  217       (ptr->get_data() - k < e_p)))
  218       return x;
  219 
  220    if(root->get_stock_name() == stockName) return root;
  221 
  222    if (stockName < root->get_stock_name())
  223       return searchStock(root->get_row_db_left_ptr(), k);
  224    else
  225       return searchStock(root->get_root_db_right_ptr(), k);
  226 }
  227 /***********   end searchStock***************/
  228 
  229 
  230 /***********BEGIN***************/
  231 //ExSan is build up on Red-Black Tree
  232 //This complex function uses InOrder Traversal
  233 void Net::TreeInorderCorrelationPlusNoHedge(...) {
  234    
  235    ROWPTR static  stockPtr(nullptr);
  236    ROWPTR static  stockPtr2(nullptr);
  237    ROWPTR static  auxStockPtr(nullptr);
  238 
  239    CELLPTR static ptRow(nullptr);
  240    CELLPTR static ptRow2(nullptr);
  241    CELLPTR ptr(nullptr), ptr2(nullptr);
  242    //CELLPTR static ptr(nullptr);   
  243    double static  i(0), SCxy(0), SCxx(0), SCyy(0), SCxy2(0), SCyy2(0);
  244    double static SCxy_2(0), SCxx_2(0), SCyy_2(0), SCxy2_2(0), SCyy2_2(0);
  245    double u_row(0), u_row2(0), Rho(0), Rho2(0), slope1(0), slope2(0);
  246 
  247    gnuplot static plot;
  248    string  static time_series_data;
  249    fstream static fts;
  250    string  static titlePlot;
  251    unsigned short static  j(0), id(0);
  252    bool static firsTime(0);
  253 
  254    if (head) {
  255       net->TreeInorderCorrelationPlusNoHedge(...,  head->get_db_left_ptr(), ...);
  256       if (!firsTime) {
  257          id = 0;
  258          firsTime = 1;
  259       }
  260       //ppU
  261       stockPtr  = net->goto_row(net, ppMean, head->get_row());  //pp y fila
  262       stockPtr2 = net->goto_row(net, ppMean, head->get_col());
  263       /// it was previously reset before the call -  ppMean !!! verificar      
  264       if (stockPtr->get_forward_ptr()->get_next_ptr()->get_next_ptr()->get_data(ppMean) ||  //no this slope
  265          stockPtr2->get_forward_ptr()->get_next_ptr()->get_next_ptr()->get_data(ppMean) ||
  266          stockPtr->get_forward_ptr()->get_next_ptr()->get_next_ptr()->get_next_ptr()->get_data(ppMean) ||  
  267          stockPtr2->get_forward_ptr()->get_next_ptr()->get_next_ptr()->get_next_ptr()->get_data(ppMean)) {
  268       }
  269       else {         
  270          if (!stockPtr->get_forward_ptr()->get_data(ppMean)) {
  271             ptRow = net->point_to(net, ppData, head->get_row(), net->get_cols_in_page(ppData)); // 
  272             u_row = 0;
  273             for (j = 1; j <= k; j++) {               
  274                u_row += ptRow->get_data(ppData);
  275                ptRow = ptRow->get_previous_ptr();  
  276             }
  277             u_row /= k;
  278             ptRow = net->point_to(net, ppMean, head->get_row(), 1);
  279             ptRow->set_data(ppMean, u_row);            
  280             ptRow  = net->point_to(net, ppData, head->get_row(), net->get_cols_in_page(ppData) - k + 1); // 
  281             ptRow2 = net->point_to(net, ppData, head->get_col(), net->get_cols_in_page(ppData) - k + 1); //          
  282             SCxy = SCxx = SCyy = 0;  
  283             for (i = 1; i <= k; i++) {               
  284                Rho = i - (double)xx; // usado como temporal
  285                SCxx += pow(Rho, 2);               
  286                SCyy += pow(ptRow->get_data(ppData) - u_row, 2);
  287                SCxy += (Rho) * (ptRow->get_data(ppData) - u_row);
  288                ptRow = ptRow->get_next_ptr();
  289             }//ok            
  290 
  291             stockPtr->get_forward_ptr()->set_data(ppMean, SCxy / sqrt(SCxx * SCyy));     // 1 rho
  292             stockPtr->get_forward_ptr()->get_next_ptr()->set_data(ppMean, SCxy / SCxx);  //2 slope
  293 
  294             //3 DISCARD SLOPE     //4 DISCARD RHO            
  295             Rho = SCxy / sqrt(SCxx * SCyy);
  296             slope1 = SCxy / SCxx;            
  297          }
  298          Rho    = stockPtr->get_forward_ptr()->get_data(ppMean);
  299          slope1 = stockPtr->get_forward_ptr()->get_next_ptr()->get_data(ppMean);
  300          if(slope1 >= slope && Rho >= factor_linear_correlation) { 
  301             ary[head->get_row()] = 1;   
  302          }
  303          if (!stockPtr2->get_forward_ptr()->get_data(ppMean)) {
  304             ptRow2 = net->point_to(net, ppData, head->get_col(), net->get_cols_in_page(ppData)); 
  305             for (j = 1; j <= k; j++) {                           
  306                u_row2 += ptRow2->get_data(ppData);
  307                ptRow2  = ptRow2->get_previous_ptr();  
  308             }
  309             u_row2 /= k;
  310             ptRow2 = net->point_to(net, ppMean, head->get_col(), 1); //         
  311             ptRow2->set_data(ppMean, u_row2);            
  312             ptRow2 = net->point_to(net, ppData, head->get_col(), net->get_cols_in_page(ppData) - k + 1); 
  313             SCxx = SCxy_2 = SCxx_2 = SCyy_2 = 0; // SCxy2_2 = SCyy2_2 = 0;
  314             for (i = 1; i <= k; i++) {               
  315                Rho = i - (double)xx; // usado como temporal
  316                SCxx += pow(Rho, 2);
  317                SCyy_2 += pow(ptRow2->get_data(ppData) - u_row2, 2);
  318                SCxy_2 += (Rho) * (ptRow2->get_data(ppData) - u_row2);
  319                ptRow2 = ptRow2->get_next_ptr();
  320             }//ok
  321             stockPtr2->get_forward_ptr()->set_data(ppMean, SCxy_2 / sqrt(SCxx * SCyy_2));  //rho lineal
  322             stockPtr2->get_forward_ptr()->get_next_ptr()->set_data(ppMean, SCxy_2 / SCxx); //slope            
  323          }
  324          Rho2   = stockPtr2->get_forward_ptr()->get_data(ppMean);
  325          slope2 = stockPtr2->get_forward_ptr()->get_next_ptr()->get_data(ppMean);         
  326          if (fabs(Rho)  <= factor_linear_correlation ||
  327             fabs(Rho2) <= factor_linear_correlation ||
  328             slope1 >= slope ||
  329             slope2 >= slope ) {
  330          }
  331       }
  332       net->TreeInorderCorrelationPlusNoHedge(...,  head->get_db_right_ptr(), ...);
  333    }
  334 }
  335 /*********************Net::TreeInorderCorrelationPlusNoHedge****************************/
  336 
  337 
  338 /***********START***************/
  339 //This code calculates RETURN / CLUSTER BASED SCENARIO
  340 void Cell::Tree_inOrder_nt_Return(PTR t, NODEPTR head, 
  341                                   unsigned short ppDataIn, 
  342                           bool first , 
  343                           double & carryStockVal, 
  344                           double& retVal) {
  345    static NODEPTR ptrNode(nullptr);
  346    
  347    if (head) {
  348       t->Tree_inOrder_nt_Return(t, head->get_nt_left_ptr(), ppDataIn, first, carryStockVal, retVal);      
  349       if (head->get_key_nt() != 1 && first) {
  350          t->inOrderSearch_nt_ptr(t, t->get_ntHead(), head->get_key_nt() - 1, ptrNode);         
  351          retVal += (head->get_nt_data(ppDataIn) - ptrNode->get_nt_data(ppDataIn)) / 
  352                    ptrNode->get_nt_data(ppDataIn);         
  353       }   
  354       else{
  355          if(!first)
  356             if (head->get_key_nt() == 1) {                              
  357                retVal += (head->get_nt_data(ppDataIn) - carryStockVal) / carryStockVal;               
  358             }
  359             else {
  360                t->inOrderSearch_nt_ptr(t, t->get_ntHead(), head->get_key_nt() - 1, ptrNode);               
  361                retVal += (head->get_nt_data(ppDataIn) - ptrNode->get_nt_data(ppDataIn)) / 
  362                           ptrNode->get_nt_data(ppDataIn);
  363             }            
  364       }      
  365       if (head->get_key_nt() == ntNodes) {
  366          carryStockVal = head->get_nt_data(ppDataIn);         
  367       }
  368       t->Tree_inOrder_nt_Return(t, head->get_nt_right_ptr(), ppDataIn, first, carryStockVal, retVal);
  369    }
  370 }
  371 /********************* Tree_backOrder_nt_Return ****************************/
  372 

https://robertosantanderprofessionalprofile.blogspot.com/
      Previous Post       Next Post
Flag Counter

Comments

Popular posts from this blog

Interactive Brokers tick by tick Data Market - C++ API & ExSan jobID 106391

Systematic Forex Trading

Interactive Brokers C++ API & ExSan xsn 279947