Thursday, May 12, 2022

Forex Sample and Coding

ExSan++ High Perfomance C++ Computing _V22_17.2.0@05.06

iTweet#ibxsn    iTweet#xsncode    iTweet#xsn

FOREX TRADING    My Portfolio   

 
 
 

  1 
    ExSan++ High Perfomance C++ Computing _V22_17.2.0@05.06

  2                                                         Thu May 12 06:51:08 2022

  7 

  8    ==  F O R E X  =========

  9    INPUT (file)

 10    2

 11 

 12    2.0

 13 

 14    0.45

 15    =======================

 16 

 17     Generate Exsan ( 10 ,  10 )

 18 

 19     WORKSHEET 2  B[2, 2] FLOAT

 20                    A          B 

 21         >----------------------<

 22      1:         1          2 

 23      2:      0.45          1 

 24         <---------------------->

 25 

 26    Row 1: Forex Trading Sequence

 27    Row 2: Forex Trading Exchange

 28     WORKSHEET 0  @[2, 3] FLOAT

 29              A       B       C 

 30         >---------------------<

 31      1:      1       2       0 

 32      2:      2    0.45       0 

 33         <--------------------->

 34 

 35    

 36    ENDS  arbtrg5108   Elapsed Time: 0.02  sec

 37    Boost version: 1.59.0

 38 

 39    EXIT FROM EXSAN 


  8    =======================

  9    INPUT (file)

 10    3

 11 

 12    1.2 .89

 13 

 14    .88 5.1

 15 

 16    1.1 0.15

 17    =======================

 21     WORKSHEET 3  C[3, 3] FLOAT

 22                    A          B          C 

 23         >---------------------------------<

 24      1:         1        1.2       0.89 

 25      2:      0.88          1        5.1 

 26      3:       1.1       0.15          1 

28 

 29    Row 1: Forex Trading Sequence

 30    Row 2: Forex Trading Exchange

 31     WORKSHEET 0  @[2, 4] FLOAT

 32              A       B       C       D 

 33         >----------------------------<

 34      1:      1       2       3       0 

 35      2:    1.2     5.1     1.1       0 

 36         <---------------------------->

 37 

 40    Boost version: 1.59.0

 41 

 42    EXIT FROM EXSAN 


  8    =======================

  9    INPUT (file)

 10    4

 11 

 12    3.1    0.0023    0.35

 13 

 14    0.21   0.00353   8.13

 15 

 16    200    180.559   10.339

 17 

 18    2.11   0.089     0.06111

 19    =======================

 21     Generate Exsan ( 10 ,  10 )

 22 

 23     WORKSHEET 4  D[4, 4] FLOAT

 24                    A          B          C          D 

 25         >--------------------------------------------<

 26      1:         1        3.1     0.0023       0.35 

 27      2:      0.21          1    0.00353       8.13 

 28      3:       200      180.6          1      10.34 

 29      4:      2.11      0.089    0.06111          1 

 30         <-------------------------------------------->

 31 

 32    Row 1: Forex Trading Sequence

 33    Row 2: Forex Trading Exchange

 34     WORKSHEET 0  @[2, 5] FLOAT

 35              A       B       C       D       E 

 36         >-----------------------------------<

 37      1:      1       2       4       1       0 

 38      2:    3.1    8.13    2.11     3.1       0 

 39         

 44 

 45    EXIT FROM EXSAN 


   8    =======================

  9    INPUT (file)

 10    5

 11 

 12                 1.3    2.3   0.14   0.17

 13 

 14        0.94           0.42    2.6    3.1

 15 

 16        0.33   0.44           3.1    2.2

 17 

 18         95     34    3.1             13

 19 

 20         3.4    6.1    3.5    3.6      

 21    =======================

 25     WORKSHEET 5  E[5, 5] FLOAT

 26                    A          B          C          D          E 

 27         >-------------------------------------------------------<

 28      1:         1        1.3        2.3       0.14       0.17 

 29      2:      0.94          1       0.42        2.6        3.1 

 30      3:      0.33       0.44          1        3.1        2.2 

 31      4:        95         34        3.1          1         13 

 32      5:       3.4        6.1        3.5        3.6          1 

 33         <------------------------------------------------------->

 34 

 35    no !!! -TRADING SEQUENCE-  !!! no

 36    Row 1: Forex Trading Sequence

 37    Row 2: Forex Trading Exchange

 38     WORKSHEET 0  @[2, 6] FLOAT

 39              A       B       C       D       E       F 

 40         >------------------------------------------<

 41      1:      1       3       4       1       3       0 

 42      2:    2.3     3.1      95     2.3       0       0 

 43         

 49    EXIT FROM EXSAN 


  8    =======================

  9    INPUT (file)

 10    8

 11 

 12       1.3908 2475.0000 1.4304 3.4519 1.4394 0.9982 2.1677 

 13 

 14     0.7047   1761.7050 1.0182 2.4571 1.0245 0.7105 1.5430 

 15 

 16     0.0004 0.0006   0.0006 0.0014 0.0006 0.0004 0.0009 

 17 

 18     0.6852 0.9626 1712.9475   2.3890 0.9962 0.6908 1.5003 

 19 

 20     0.2839 0.3989 709.8300 0.4102   0.4128 0.2863 0.6217 

 21 

 22     0.6809 0.9566 1702.3050 0.9838 2.3742   0.6866 1.4910 

 23 

 24     0.9819 1.3794 2454.7050 1.4187 3.4236 1.4276   2.1499 

 25 

 26     0.4521 0.6352 1130.3325 0.6533 1.5765 0.6574 0.4559   

 27    =======================

 28 

 29     Generate Exsan ( 10 ,  10 )

 30 

 31     WORKSHEET 8  H[8, 8] FLOAT

 32                    A          B          C          D          E          F          G          H 

 33         >----------------------------------------------------------------------------<

 34      1:         1      1.391       2475       1.43      3.452      1.439     0.9982      2.168 

 35      2:    0.7047          1       1762      1.018      2.457      1.024     0.7105      1.543 

 36      3:    0.0004     0.0006          1     0.0006     0.0014     0.0006     0.0004     0.0009 

 37      4:    0.6852     0.9626       1713          1      2.389     0.9962     0.6908        1.5 

 38      5:    0.2839     0.3989      709.8     0.4102          1     0.4128     0.2863     0.6217 

 39      6:    0.6809     0.9566       1702     0.9838      2.374          1     0.6866      1.491 

 40      7:    0.9819      1.379       2455      1.419      3.424      1.428          1       2.15 

 41      8:    0.4521     0.6352       1130     0.6533      1.577     0.6574     0.4559          1 

 42         <----------------------------------------------------------------------->

 43 

 44    no !!! -TRADING SEQUENCE-  !!! no

 45    Row 1: Forex Trading Sequence

 46    Row 2: Forex Trading Exchange

 47     WORKSHEET 0  @[2, 5] FLOAT

 48              A       B       C       D       E 

 49         >-----------------------------------<

 50      1:      1       3       5       3       0 

 51      2:   2475  0.0014   709.8       0       0 

 58    EXIT FROM EXSAN 

 8    ==  F O R E X  =========

 8 /***********START***************/

    9 void main(void){

   10    setJobID("exsanForex");

   11    unsigned short int i{0}, j{ 0 };

   12    enum FLOAT   { pp_float = 10 };

   13    enum BOOL   { pp_bool };/// dos paginas????

   14    enum USHORT { pp_us };

   15    enum STRING { pp_string };

   16    enum CHAR   { pp_char };

   17 

   18    NETPTR net(nullptr);//

   19    CELLPTR ptr(nullptr); // , auxPtrLast(nullptr);

   20a    CELLPTR rateExPtr(nullptr), tracptr(nullptr), aux_min(nullptr);

   20b    CELLPTR  aux_max(nullptr), checkptr(nullptr);

   21    ROWPTR rowPtr(nullptr);

   22    unsigned short rows{10}, cols{10};

   23    bool done(0);

   24       

   25    net = net->exsan(rows, cols, pp_float, pp_bool, pp_us, pp_string, pp_char);   

   26    

   27    fin.open(fileData, ios::in);

   28    if (!fin) {//donde esta la data que se lee de los distintos archivos

   29       printf("Error! I can't find the file: ", fileData);

   30       fout << "This file does not exist " << fileData;      

   31       return;

   32       exit(1); 

   33    }

   34    else {  

   35       fin >> ppdata;  

   36       rowIn = ppdata;

   37    } 

   38    

   39    double data_do{0};   

   40    rowPtr = net->goto_row(net, ppdata, 1, 'f'); 

   41    //fout << "\n\tConversion Rate  Currency ";

   42    for (i = 1; i <= ppdata; i++) {

   43       ptr = rowPtr->get_forward_ptr();

   44       for (j = 1; j <= ppdata; j++) {

   45          if (i == j){ ptr->set_data(ppdata, 1.0);  ptr = ptr->get_next_ptr(); continue; }

   46          fin >> data_do;

   47          //fout << "\n\tCurrency " << i << " vs. Currency " << j << " -> " << data_do;

   48          ptr->set_data(ppdata, data_do);

   49          ptr = ptr->get_next_ptr();

   50       }

   51       rowPtr = rowPtr->get_down_ptr();

   52    }

   53    fin.close();            

   54    

   55    rowIn = 1;  // entry point

   56    

   57    rateExPtr = net->point_to(net, 0, 2, 1); // track exchange

   58    tracptr = net->point_to(net, 0, 1, 1);   // 

   59    ptr = net->point_to(net, ppdata, 1, 1);  // start with this forex entry

   60    aux_max = net->point_to(net, ppdata, 1, 1);  // start with this forex entry

   61    for (i = 1;  i <= ppdata; i++) {// row traversal

   62       net->DiscarDB(net, HolDB);  //for each row      

   63       tracptr->set_data(0, ptr->get_row());

   64       checkptr = tracptr->get_previous_ptr();

   65       tracptr = tracptr->get_next_ptr();      

   66       while (checkptr && tracptr->get_col() != 1) {         

   67          if (checkptr->get_data(0) == 1) {

   68             //fout << "\n\tOK TRADING SEQUENCE";   

   69             //break; 

   70          }

   71          else if (ptr->get_row() == checkptr->get_data(0)) {

   72              fout << "\n\tno !!! -TRADING SEQUENCE-  !!! no"; 

   73              done = 1;

   74              //break; 

   75          }

   76          checkptr = checkptr->get_previous_ptr();

   77       }

   78       if (done) goto sequence;      

   79       for (j = 1; j <= ppdata; j++) {                  

   80          ptr = net->point_to(net, ppdata, aux_max->get_col(), j);

   81          if (ptr->get_data(ppdata) <= 0 || ptr->get_row() == ptr->get_col() ) { 

   82             continue;

   83          }         

   84          net->dB(net, rootRowHolDB,  ptr, ppdata);  

   85       }      

   86       aux_max = net->RB_max(net, rootRowHolDB, crptr);      

   87       rateExPtr->set_data(0, aux_max->get_data(ppdata));

   88       rateExPtr = rateExPtr->get_next_ptr();

   89       if (!aux_max){ 

   90          fout << "\n\tNO CYCLE - !!!";

   91          i = ppdata + 1;         

   92       }

   93       else{         

   94          ptr = net->point_to(net, ppdata, aux_max->get_col(), 1, 'f');

   95       }      

   96    }  

   97 sequence:

   98 

   99    net->set_work_sheet(net, 0, 2, tracptr->get_col());  //page i, (i * i)   

  

100    fout << "\n\tRow 1: Forex Trading Sequence";

  

101    fout << "\n\tRow 2: Forex Trading Exchange";

  

102    net->show_page(net, 0, 'f', 0, 2, 7, 4);

  

103    net->kill_exsan(net);

  

104    return;

  

105 }

  

106 /***********END Forex ExSan*****************/

Trading of one currency for another with the hopes of taking advantage of small differences in conversion rates among several currencies in order to achieve a profit. For example, if 1.00 in U.S. currency buys 0.7 British pounds currency, 1.00 in British currency buys 9.5 French francs, and 1 French franc buys 0.16 in U.S. dollars, then a forex trader can start with 1.00 USD and earn 1 * 0.7 * 9.5 * 0.16 = 1.064 dollars thus earning a profit of 6.4 percent.

The problem consist to write a program that determines whether a sequence of currency exchanges can yield a profit as described above.

To result in successful trade, a sequence of exchanges must begin and end with the same currency, but any starting currency may be considered.

The input file consists of one or more conversion tables. You must solve the trading problem for each of the tables in the input file. Each table is preceded by an integer n on a line by itself giving the dimensions of the table. The maximum dimension is 20. The minimum dimension is 2.

The table then follows in row major order but with the diagonal elements of the table missing (these are assumed to have value 1.0). Thus the first row of the table represents the conversion rates between country 1 and n - 1 other countries, i.e. the amount of currency of country i constrained to 2 eq i eq n that can be purchased with one unit of the currency of country 1.

Thus each table consists of n + 1 lines in the input file: 1 line containing n and lines representing the conversion table.

Output

For each table in the input file, you must determine whether a sequence of exchanges exists that results in a profit of more than 1 percent (0.01). If a sequence exists you must print the sequence of exchanges that results in a profit. If there is more than one sequence that results in a profit of more than 1 percent you must print a sequence of minimal length, i.e. one of the sequences that uses the fewest exchanges of currencies to yield a profit.

Because the IRS (United States Internal Revenue Service) taxes long transaction sequences with a high rate, all profitable sequences must consist of n or fewer transactions where n is the dimension of the table giving conversion rates. The sequence 1 2 1 represents two conversions.

If a profitable sequence exists you must print the sequence of exchanges that results in a profit. The sequence is printed as a sequence of integers with the integer i representing the i-th line of the conversion table (country i). The first integer in the sequence is the country from which the profitable sequence starts. This integer also ends the sequence.

If no profiting sequence of n or fewer transactions exists, then the line

no trading sequence exists

should be printed.

Sample Input

3
1.2 .89
.88 5.1
1.1 0.15
4
3.1    0.0023    0.35
0.21   0.00353   8.13
200    180.559   10.339
2.11   0.089     0.06111
2
2.0
0.45

Sample Output

1 2 1
1 2 4 1
no trading sequence exists

Analysis

For a given problem, the conversion table corresponds to a graph and the solution corresponds to a shortest profitable cycle in that graph. Consider the following conversion table over USD, MXN, and EUR.

3
1.004987562112089 1.004987562112089
0.99503719020999 1.004987562112089
1.004987562112089 0.99503719020999

The table corresponds to the following interpretation.

     | USD         | MXN         | EUR
----------------------------------------------
 USD | 0           | 1.01^(1/2)  | 1.01^(1/2)
 MXN | 1.01^(-1/2) | 0           | 1.01^(1/2)
 EUR | 1.01^(1/2)  | 1.01^(-1/2) | 0

The corresponding graph is the following.


When we interpret the conversion table, we write 0 for the rates in the diagonal to indicate that we do not consider edges that start and end in the same vertex. The reason is that those edges are redundant. A lasso is an edge that starts and ends in the same vertex. Lassos are redundant because from a given sequence that includes a lasso you obtain a shorter sequence with the same rate by removing the lasso. For example, sequence USD USD EUR USD yields profit 1.01, the same profit that the shorter sequence USD EUR USD yields.

A sequence of exchanges may yield a profit only if the sequence is a cycle. For example, the cycle USD -> MXN -> EUR -> USD yields profit of 1.01^{3/2}. Given that the number of vertices in the graph is 3, we do not consider cycles longer than 3. Consider the cycles of length 3 or less.

CYCLE                    : RATE

USD -> MXN -> USD        : 1
USD -> EUR -> USD        : 1.01
EUR -> MXN -> EUR        : 1
USD -> MXN -> EUR -> USD : 1.01^(3/2)
USD -> EUR -> MXN -> USD : 1.01^(-1/2)


A cycle is profitable when its rate is greater or equal to 1.01. Out of the five cycles, only the following two are profitable.

USD -> EUR -> USD
USD -> MXN -> EUR -> USD

Out of the two profitable cycles, USD -> EUR -> USD is the only solution because it is the shortest cycle.

A solution may not be a simple cycle like in the previous example. For example, consider the following conversion table and its corresponding graph.

4
1 0 0
1.005 0 0
0 0 0
0 0 0

For the graph, the cycles of length 4 or less are the following

CYCLE     : RATE
1 2 1     : 1.005
1 2 1 2 1 : 1.010025

The only profitable cycle is 1 2 1 2 1 and therefore it is the only solution. The cycle is a solution regardless of the fact that it consists of the repetition of simple cycle 1 2 1.

A solution that is not a simple cycle is not necessarily the repetition of a simple cycle like in the previous example. For example, consider the following conversion table and its corresponding graph.

5
1.001992047666533 0 0 1.001992047666533
1.001992047666533 0 0 0
1.001992047666533 0 0 0
0 0 0 0
0 0 1.001992047666533 0

For the graph, the cycles of length 5 or less are the following.

CYCLE       : RATE
1 2 1       : 1.01^(2/5)
1 5 3 1     : 1.01^(3/5)
1 2 1 5 3 1 : 1.01

The only profitable cycle is 1 2 1 5 3 1 and therefore it is the only solution. The cycle consists of simple cycles 1 2 1 and 1 5 3 1.

ExSan++ High Perfomance C++ Computing _V22_17.2.0@05.06

iTweet#ibxsn    iTweet#xsncode    iTweet#xsn

FOREX TRADING    My Portfolio   

Flag Counter

Monday, May 9, 2022

Forex 001

      Previous Post       Next Post YET

Exsan High Performance C++ Computing_V22_17.1.6@05.06

iTwit         exsan.plusplus@gmail.com   Mon May 9 13:24:34 2022         x: 18.10.25       c:\exsan   JOB ID:

arbtrg2434

FOREX TRADING ExSan at Work -Current Project-

Trading of one currency for another with the hopes of taking advantage of small differences in conversion rates among several currencies in order to achieve a profit. For example, if 1.00 in U.S. currency buys 0.7 British pounds currency, 1.00 in British currency buys 9.5 French francs, and 1 French franc buys 0.16 in U.S. dollars, then a forex trader can start with 1.00 USD and earn 1 * 0.7 * 9.5 * 0.16 = 1.064 dollars thus earning a profit of 6.4 percent.

The problem consist to write a program that determines whether a sequence of currency exchanges can yield a profit as described above.

To result in successful trade, a sequence of exchanges must begin and end with the same currency, but any starting currency may be considered.

The input file consists of one or more conversion tables. You must solve the trading problem for each of the tables in the input file. Each table is preceded by an integer $@n@$ on a line by itself giving the dimensions of the table. The maximum dimension is 20. The minimum dimension is 2.

The table then follows in row major order but with the diagonal elements of the table missing (these are assumed to have value 1.0). Thus the first row of the table represents the conversion rates between country 1 and $@n - 1@$ other countries, i.e. the amount of currency of country $@i@$ constrained to $@2 \leq i \leq n@$ that can be purchased with one unit of the currency of country 1.

Thus each table consists of $@n + 1@$ lines in the input file: 1 line containing $@n@$ and $@n@$ lines representing the conversion table.

Output

For each table in the input file, you must determine whether a sequence of exchanges exists that results in a profit of more than 1 percent (0.01). If a sequence exists you must print the sequence of exchanges that results in a profit. If there is more than one sequence that results in a profit of more than 1 percent you must print a sequence of minimal length, i.e. one of the sequences that uses the fewest exchanges of currencies to yield a profit.

Because the IRS (United States Internal Revenue Service) taxes long transaction sequences with a high rate, all profitable sequences must consist of $@n@$ or fewer transactions where $@n@$ is the dimension of the table giving conversion rates. The sequence 1 2 1 represents two conversions.

If a profitable sequence exists you must print the sequence of exchanges that results in a profit. The sequence is printed as a sequence of integers with the integer $@i@$ representing the $@i@$-th line of the conversion table (country $@i@$). The first integer in the sequence is the country from which the profitable sequence starts. This integer also ends the sequence.

If no profiting sequence of $@n@$ or fewer transactions exists, then the line

no trading sequence exists

should be printed.

Sample Input

3
1.2 .89
.88 5.1
1.1 0.15
4
3.1    0.0023    0.35
0.21   0.00353   8.13
200    180.559   10.339
2.11   0.089     0.06111
2
2.0
0.45

Sample Output

1 2 1
1 2 4 1
no trading sequence exists

Analysis

For a given problem, the conversion table corresponds to a graph and the solution corresponds to a shortest profitable cycle in that graph. Consider the following conversion table over USD, MXN, and EUR.

3
1.004987562112089 1.004987562112089
0.99503719020999 1.004987562112089
1.004987562112089 0.99503719020999

The table corresponds to the following interpretation.

     | USD         | MXN         | EUR
----------------------------------------------
 USD | 0           | 1.01^(1/2)  | 1.01^(1/2)
 MXN | 1.01^(-1/2) | 0           | 1.01^(1/2)
 EUR | 1.01^(1/2)  | 1.01^(-1/2) | 0

The corresponding graph is the following.

When we interpret the conversion table, we write 0 for the rates in the diagonal to indicate that we do not consider edges that start and end in the same vertex. The reason is that those edges are redundant. A lasso is an edge that starts and ends in the same vertex. Lassos are redundant because from a given sequence that includes a lasso you obtain a shorter sequence with the same rate by removing the lasso. For example, sequence USD USD EUR USD yields profit 1.01, the same profit that the shorter sequence USD EUR USD yields.

A sequence of exchanges may yield a profit only if the sequence is a cycle. For example, the cycle USD -> MXN -> EUR -> USD yields profit of $@1.01^{3/2}@$. Given that the number of vertices in the graph is 3, we do not consider cycles longer than 3. Consider the cycles of length 3 or less.

CYCLE                    : RATE

USD -> MXN -> USD        : 1
USD -> EUR -> USD        : 1.01
EUR -> MXN -> EUR        : 1
USD -> MXN -> EUR -> USD : 1.01^(3/2)
USD -> EUR -> MXN -> USD : 1.01^(-1/2)


A cycle is profitable when its rate is greater or equal to 1.01. Out of the five cycles, only the following two are profitable.

USD -> EUR -> USD
USD -> MXN -> EUR -> USD

Out of the two profitable cycles, USD -> EUR -> USD is the only solution because it is the shortest cycle.

A solution may not be a simple cycle like in the previous example. For example, consider the following conversion table and its corresponding graph.

4
1 0 0
1.005 0 0
0 0 0
0 0 0

For the graph, the cycles of length 4 or less are the following

CYCLE     : RATE
1 2 1     : 1.005
1 2 1 2 1 : 1.010025

The only profitable cycle is 1 2 1 2 1 and therefore it is the only solution. The cycle is a solution regardless of the fact that it consists of the repetition of simple cycle 1 2 1.

A solution that is not a simple cycle is not necessarily the repetition of a simple cycle like in the previous example. For example, consider the following conversion table and its corresponding graph.

5
1.001992047666533 0 0 1.001992047666533
1.001992047666533 0 0 0
1.001992047666533 0 0 0
0 0 0 0
0 0 1.001992047666533 0

For the graph, the cycles of length 5 or less are the following.

CYCLE       : RATE
1 2 1       : 1.01^(2/5)
1 5 3 1     : 1.01^(3/5)
1 2 1 5 3 1 : 1.01

The only profitable cycle is 1 2 1 5 3 1 and therefore it is the only solution. The cycle consists of simple cycles 1 2 1 and 1 5 3 1.

Searching for a solution is difficult because there may be many cycles for a given problem. The reason is that a conversion table of length $@n@$ corresponds to complete graph $@K_n@$. Even if there are edges with weight 0, we consider them because considering a complete graph makes for a simpler solution. The exception are lassos, which we do not consider because they are redundant. For complete graphs of size $@2 \leq n \leq 20@$, the number of cycles of length $@n@$ or less is the following.

 K2:                                       1
 K3:                                       5
 K4:                                      42
 K5:                                     384
 K6:                                   4,665
 K7:                                  69,537
 K8:                               1,230,124
 K9:                              25,140,552
K10:                             582,508,305
K11:                          15,084,077,381
K12:                         431,646,196,806
K13:                      13,525,545,361,080
K14:                     460,576,563,322,057
K15:                  16,935,036,272,292,001
K16:                 668,691,718,661,091,000
K17:              28,220,125,532,003,984,176
K18:           1,267,597,789,008,779,578,401
K19:          60,381,304,029,673,985,693,205
K20:       3,040,239,935,992,309,703,757,730

Approach

We approach the problem by searching for a shortest profitable cycle amongst a limited number of candidates. We guarantee that the profitable cycle we find is shortest by considering candidates in order of length.

Consider the following input graph.

The candidates of length 2 are the cycles of length 2. Thus, we consider the cycles of length 2 from each one of the vertices as illustrated in the following diagram. These are all the cycles of length 2 because we consider all paths that start and end in each given vertex.

We search for a profitable candidate of length 2 by considering each root and each corresponding child. For example, for root 1 and child 2, we consider the following candidate.

The rate of the candidate is 1.005 which is not profitable and therefore not a solution. We search the rest of the candidates by repeating the process for each root and child. We find no profitable candidate of length 2.

Half of the candidates of length 2 are repeated but we consider them anyway. For example, candidate for root 2 and child 1 (2 -> 1 -> 2) is the same cycle and candidate for root 1 and child 2 (1 -> 2 -> 1). The reason we consider repetitions is that when we apply the process to longer candidates, the number of candidates we consider for each length remains 12 while the number of cycles for the length increases. For example, when we consider candidates of length 4 for the input graph, we consider 12 candidates instead of the 28 cycles of length 4 that exist.

Given that there is no solution of length 2, we search for a profitable candidate of length 3. The candidates of length 3 are most beneficial cycles of length 3. The structure of each candidate consists of a prefix edge i -> j and a suffix path j -> k -> i. For example, the candidate for root 1 and child 2 is the following.

The prefix edge 1 -> 2 is the edge from 1 to 2 given by the input graph. The suffix path 2 -> 3 -> 1 is a most beneficial path of length 2 from 2 to 1. In this case the suffix is the most beneficial path of length 2 from 2 to 1. The rate of the candidate is 1 which is not profitable and therefore not a solution. Given that there is a candidate for each root and child, we search the rest of the candidates by repeating the process for each root and child. We find no profitable candidate of length 3.

Candidates are most beneficial paths, for that reason we construct candidates by constructing most beneficial paths.

The construction of most beneficial paths is determined by their structure. Consider the structure a most beneficial path.

p = i -> k -> ... -> j
    ------------------
             |
          m edges

The candidate consists of a prefix edge i -> k and a suffix path k -> ... -> j. The suffix path is a most beneficial path of length 2 from k to j because p is a most beneficial path. If the suffix were not most beneficial, p would not be a most beneficial path from i to j because there would be a suffix from k to j with a higher rate. The rate B'[i,j] of path p is determined by the rate of its edge W[i,k] and the rate of its suffix B[k,j] as follows.

B'[i,j] = W[i,k] * B[k,j]


The construction of a most beneficial path corresponds to the calculation of its rate. For given origin i and destination j vertices, we obtain the rate B[m][i,j] of most beneficial path of length m from the rates of most beneficial paths of length m - 1 as follows.

B[m][i,j] = max { W[i,k] * B[m - 1][k,j] | k in 1 ... n }

For the matrix of weights W corresponding to the input graph, the rate of most beneficial paths of length 1 B[1] is W.

It is possible to construct most beneficial paths on demand instead of upfront together with candidates. We do not explain that approach because the time complexity of the end-to-end algorithm is the same either way. We do provide an implementation of the approach for reference.

When we construct most beneficial paths, we consider many more paths than the count of candidates. While we consider 12 candidates for each length, we consider 48 paths when we compute most beneficial paths (4 roots times 3 children times 4 destination vertices). We do so because for a given input graph, the number of candidates is constant for each length and thus the total count of paths that we consider is much less than the count of cycles in the graph. Compare the overapproximation $@n^4@$ of count of paths we consider to the number of cycles in the input graph.

                NUMBER OF CYCLES                   NUMBER OF PATHS WE CONSIDER
 K2:                                       1   <                16    
 K3:                                       5   <                81    
 K4:                                      42   <               256   
 K5:                                     384   <               625   
 K6:                                   4,665   >              1296  
 K7:                                  69,537   >              2401  
 K8:                               1,230,124   >              4096  
 K9:                              25,140,552   >              6561  
K10:                             582,508,305   >             10000 
K11:                          15,084,077,381   >             14641 
K12:                         431,646,196,806   >             20736 
K13:                      13,525,545,361,080   >             28561 
K14:                     460,576,563,322,057   >             38416 
K15:                  16,935,036,272,292,001   >             50625 
K16:                 668,691,718,661,091,000   >             65536 
K17:              28,220,125,532,003,984,176   >             83521 
K18:           1,267,597,789,008,779,578,401   >            104976
K19:          60,381,304,029,673,985,693,205   >            130321
K20:       3,040,239,935,992,309,703,757,730   >            160000

The overapproximation corresponds to our nested iteration of $@n@$ lengths, $@n@$ roots, $@n@$ children, and $@n@$ destination vertices.

Given that there is no solution of length 3, we search for a profitable candidate of length 4. For root 1 and child 2, the candidate is the following.

The prefix edge 1 -> 2 is given by the input graph. The suffix path is the most beneficial path of length 3 from 2 to 1. In this case there is only one most beneficial path, 2 1 2 1. The rate of the candidate is 1.010025 which is profitable and thus a solution. We stop and return the solution. If we repeated the process for the other candidates, we would find no other solution. Thus, 1 2 1 2 1 is the only solution.

Implementation

The following C program is the ExSan implementation of my own algorithm. For a given weight matrix rate of size n \times n, function S executes algorithm and prints a solution if there is one. If there is no solution, function S prints NO Sequence Exists.

EXIT FROM EXSAN

Boost version: 1.59.0© Registed TRADEMARK ExSan
      Previous Post       Next Post YET
Flag Counter

Saturday, February 19, 2022

JOB: hft3106

  1     ExSan++ High Perfomance C++ Computing _V22_17.1.0@11.08.a_
  2    NOT VALID FOR DISTRIBUTION ---BE---careful
  3                                                         Sat Feb 19 09:31:06 2022
  4 
  5 
  6     exsan.plusplus@gmail.com   https://twitter.com/#!/ExSan_com
  7     JOB:  hft3106
  8                GLUTo Sun May 15, 2022
  9    License Grant for: 14 days more      StockCounterDB Number of files: 7
 10 
 11     Read From File myInvestment:  1000   Savings:  0 
 12 
 13    stFrom : 7 <---> 10000110 stTo   =  .77 * myInvestment /  minDB : 500 / 7   I 
 14    pp_lastIN  0   ppnt_time  1   ppnt_data  2    ... -> prev 1, last 0
 15     ... -> prev 1, last 0
 16 
 17    stockCounterDB:   7   mu:  0.33     Limit Trading / Savings:  10000  FactoRun:  2
 18    ******************
 19    <--------e-x-s-a-n---------->
 20     Generate Exsan ( 21 ,  21 )
 21 
 22    Total Number of assets allocated:  20
 23 
 24    PARAMETERS
 25    Investment:   1000    min Inv/Stock:  10 # Ticks -cols-:  10  beta:  0.02   ep(little ball):  0.0001
 26    HedgeBand   0.45   <-- nbsp="">  0.55  R(  0.93377  ;  0.97733 )r Lineal #Ticks:   3
 27    Expected Return:   3  %  minDB:  7   timesLapse:  1    timeZoomFactor:  1   retFactor:  1.5
 28    Trigger Check Counter:  2  Number of times dice is thrown:  1110  EndLess:  0
 29    Run:   3   onlyInteger 0 hedge:  1  ntNodes:  7 limitWeightFactor:  0.33  tickLimitToClear:  33
 30     |<--------e-x-s-a-n---hft------->|
 31    :1409: CHECK---- either a wrap around or no room for new data in DB    geNSTOCKS: 611   nTicksLineal 3   :nTL 3
 32    :1400: As it is *A ppDataIn
 33     WORKSHEET 9  I[4, 10] FLOAT
 34             A       B       C       D       E       F       G       H       I       J 
 35            >----------------------------------------------------------------------<
 36MSFT    1:  25.17   28.36   26.68   22.48   22.25   22.42   22.62   22.81   23.33   23.88 
 37TFSM    2:  71.77   69.49   69.78   70.27   71.57   69.63   68.11   68.56   68.00   65.05 
 38NFLX    3:  24.81   24.50   18.15   18.15   18.61   20.76   21.44   23.00   21.79   22.83 
 39AAPL    4:  20.52   20.32   21.89   22.69   22.07   19.98   17.34   15.06   12.35   12.68 
 40   <---------------------------------------------------------------------->
 41       1       2       3       4       5       6       7       8       9      10
 42 
 43    :Begin --- cluster traversal----- nStocks: 5
 44    : (1, 4)-> 27.02   : (2, 1)-> 71.77   : (3, 3)-> 18.15   : (4, 1)-> 20.52   : (5, 1)-> 0.00
 45    : (1, 5)-> 22.25   : (2, 2)-> 69.49   : (3, 4)-> 18.15   : (4, 2)-> 20.32   : (5, 2)-> 0.00
 46    : (1, 6)-> 22.42   : (2, 3)-> 69.78   : (3, 5)-> 18.61   : (4, 3)-> 21.89   : (5, 3)-> 0.00
 47    : (1, 7)-> 22.62   : (2, 4)-> 70.27   : (3, 6)-> 20.76   : (4, 4)-> 22.69   : (5, 4)-> 0.00
 48    : (1, 8)-> 22.81   : (2, 5)-> 71.57   : (3, 7)-> 21.44   : (4, 5)-> 22.07   : (5, 5)-> 0.00
 49    : (1, 9)-> 23.33   : (2, 6)-> 69.63   : (3, 8)-> 23.00   : (4, 6)-> 19.98   : (5, 6)-> 0.00
 50    : (1, 10)-> 23.88   : (2, 7)-> 68.11   : (3, 9)-> 21.79   : (4, 7)-> 17.34   : (5, 7)-> 0.00
 51    : (1, 1)-> 25.17   : (2, 8)-> 68.56   : (3, 10)-> 22.83   : (4, 8)-> 15.06   : (5, 8)-> 0.00
 52    : (1, 2)-> 28.36   : (2, 9)-> 68.00   : (3, 1)-> 24.81   : (4, 9)-> 12.35   : (5, 9)-> 0.00
 53    : (1, 3)-> 26.68   : (2, 10)-> 65.05   : (3, 2)-> 24.50   : (4, 10)-> 12.68   : (5, 10)-> 0.00
 54 
 55    Data RANGE <-0 .00="" nbsp="">
 56    :1589: pp_openVal
 57     WORKSHEET 17  Q[4, 10] FLOAT
 58                                   A       B       C       D       E       F       G       H       I       J 
 59                                    >----------------------------------------------------------------------<
 60    13           0.0000000  MSFT    1:  23.69   27.92   26.48   27.24   22.18   21.93   22.33   22.51   23.13   23.73 
 61    10           0.0000000  TFSM    2:  65.11   71.15   68.93   70.27   69.78   70.10   68.45   68.07   68.91   66.02 
 62    12           0.0000000  NFLX    3:  25.37   23.97   16.90   19.00   17.98   20.26   20.07   23.57   21.47   22.25 
 63    10           0.0000000  AAPL    4:  20.52   20.92   20.73   23.14   22.26   20.80   18.11   16.61   12.52   13.43 
 64                                    <---------------------------------------------------------------------->
 65                                             1       2       3       4       5       6       7       8       9      10
 66 
 67    :1589: pp_lowVal
 68     WORKSHEET 19  S[4, 10] FLOAT
 69                                   A       B       C       D       E       F       G       H       I       J 
 70                                    >----------------------------------------------------------------------<
 71    13           0.0000000  MSFT    1:  23.69   27.27   26.21   21.83   22.04   21.93   22.33   22.31   23.12   23.48 
 72    10           0.0000000  TFSM    2:  -0.00   68.17   68.49   69.90   69.78   68.68   67.48   68.07   67.15   64.58 
 73    12           0.0000000  NFLX    3:  23.91   23.97   16.70   17.33   17.59   20.26   20.07   22.09   21.38   21.33 
 74    10           0.0000000  AAPL    4:  -0.00   19.77   20.73   22.39   21.44   19.22   16.37   12.92   11.45   11.80 
 75                                    <---------------------------------------------------------------------->
 76                                             1       2       3       4       5       6       7       8       9      10
 77 
 78    :1589: pp_highVal
 79     WORKSHEET 18  R[4, 10] FLOAT
 80                                   A       B       C       D       E       F       G       H       I       J 
 81                                    >----------------------------------------------------------------------<
 82    13           0.0000000  MSFT    1:  27.50   29.09   27.14   27.24   22.62   22.70   22.89   23.21   23.63   24.46 
 83    10           0.0000000  TFSM    2:23325.25   71.15   71.21   70.87   72.52   70.41   69.41   68.91   69.01   66.02 
 84    12           0.0000000  NFLX    3:  25.50   25.27   19.60   19.00   19.78   21.13   23.17   24.38   22.20   24.67 
 85    10           0.0000000  AAPL    4:6668.73   20.92   23.13   23.14   22.54   20.80   18.18   17.02   14.23   13.43 
 86                                    <---------------------------------------------------------------------->
1182    :1580:End --- cluster traversal-----