| 23 | | const int emlig_size = 2; |
| 24 | | const int utility_constant = 5; |
| | 26 | //const int emlig_size = 2; |
| | 27 | //const int utility_constant = 5; |
| | 28 | |
| | 29 | const int max_model_order = 2; |
| | 30 | |
| | 31 | |
| | 32 | |
| | 33 | HDDEDATA CALLBACK DdeCallback( |
| | 34 | UINT uType, // Transaction type. |
| | 35 | UINT uFmt, // Clipboard data format. |
| | 36 | HCONV hconv, // Handle to the conversation. |
| | 37 | HSZ hsz1, // Handle to a string. |
| | 38 | HSZ hsz2, // Handle to a string. |
| | 39 | HDDEDATA hdata, // Handle to a global memory object. |
| | 40 | DWORD dwData1, // Transaction-specific data. |
| | 41 | DWORD dwData2) // Transaction-specific data. |
| | 42 | { |
| | 43 | return 0; |
| | 44 | } |
| | 45 | |
| | 46 | void DDERequest(DWORD idInst, HCONV hConv, char* szItem) |
| | 47 | { |
| | 48 | HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); |
| | 49 | HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, |
| | 50 | XTYP_REQUEST,TIMEOUT_ASYNC , NULL); |
| | 51 | if (hData==NULL) |
| | 52 | { |
| | 53 | printf("Request failed: %s\n", szItem); |
| | 54 | } |
| | 55 | |
| | 56 | if (hData==0) |
| | 57 | { |
| | 58 | printf("Request failed: %s\n", szItem); |
| | 59 | } |
| | 60 | } |
| | 61 | |
| | 62 | class model |
| | 63 | { |
| | 64 | |
| | 65 | |
| | 66 | public: |
| | 67 | list<pair<int,int>> ar_components; |
| | 68 | |
| | 69 | // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally |
| | 70 | // problematic. |
| | 71 | RARX* my_rarx; |
| | 72 | ARXwin* my_arx; |
| | 73 | |
| | 74 | bool has_constant; |
| | 75 | int window_size; |
| | 76 | int predicted_channel; |
| | 77 | mat* data_matrix; |
| | 78 | |
| | 79 | model(list<pair<int,int>> ar_components, |
| | 80 | bool robust, |
| | 81 | bool has_constant, |
| | 82 | int window_size, |
| | 83 | int predicted_channel, |
| | 84 | mat* data_matrix) |
| | 85 | { |
| | 86 | this->ar_components.insert(this->ar_components.begin(),ar_components.begin(),ar_components.end()); |
| | 87 | this->has_constant = has_constant; |
| | 88 | this->window_size = window_size; |
| | 89 | this->predicted_channel = predicted_channel; |
| | 90 | this->data_matrix = data_matrix; |
| | 91 | |
| | 92 | if(robust) |
| | 93 | { |
| | 94 | if(has_constant) |
| | 95 | { |
| | 96 | my_rarx = new RARX(ar_components.size()+1,window_size,true); |
| | 97 | my_arx = NULL; |
| | 98 | } |
| | 99 | else |
| | 100 | { |
| | 101 | my_rarx = new RARX(ar_components.size(),window_size,false); |
| | 102 | my_arx = NULL; |
| | 103 | } |
| | 104 | } |
| | 105 | else |
| | 106 | { |
| | 107 | my_rarx = NULL; |
| | 108 | my_arx = new ARXwin(); |
| | 109 | mat V0; |
| | 110 | |
| | 111 | if(has_constant) |
| | 112 | { |
| | 113 | V0 = 0.0001 * eye(ar_components.size()+2); |
| | 114 | //V0(0,0) = 0.1; |
| | 115 | my_arx->set_constant(true); |
| | 116 | |
| | 117 | } |
| | 118 | else |
| | 119 | { |
| | 120 | |
| | 121 | V0 = 0.0001 * eye(ar_components.size()+1); |
| | 122 | //V0(0,0) = 0.1; |
| | 123 | my_arx->set_constant(false); |
| | 124 | |
| | 125 | } |
| | 126 | |
| | 127 | my_arx->set_statistics(1, V0); |
| | 128 | my_arx->set_parameters(window_size); |
| | 129 | my_arx->validate(); |
| | 130 | } |
| | 131 | } |
| | 132 | |
| | 133 | void data_update(int time) |
| | 134 | { |
| | 135 | vec data_vector; |
| | 136 | for(list<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) |
| | 137 | { |
| | 138 | data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second)); |
| | 139 | } |
| | 140 | |
| | 141 | if(my_rarx!=NULL) |
| | 142 | { |
| | 143 | data_vector.ins(0,(*data_matrix).get(predicted_channel,time)); |
| | 144 | my_rarx->bayes(data_vector); |
| | 145 | } |
| | 146 | else |
| | 147 | { |
| | 148 | vec pred_vec; |
| | 149 | pred_vec.ins(0,(*data_matrix).get(predicted_channel,time)); |
| | 150 | my_arx->bayes(pred_vec,data_vector); |
| | 151 | } |
| | 152 | } |
| | 153 | |
| | 154 | static list<list<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels) |
| | 155 | { |
| | 156 | list<list<pair<int,int>>> created_model_types; |
| | 157 | |
| | 158 | if(max_order == 1) |
| | 159 | { |
| | 160 | for(int channel = 0;channel<number_of_channels;channel++) |
| | 161 | { |
| | 162 | list<pair<int,int>> returned_type; |
| | 163 | returned_type.push_back(pair<int,int>(channel,1)); |
| | 164 | created_model_types.push_back(returned_type); |
| | 165 | } |
| | 166 | |
| | 167 | return created_model_types; |
| | 168 | } |
| | 169 | else |
| | 170 | { |
| | 171 | created_model_types = possible_models_recurse(max_order-1,number_of_channels); |
| | 172 | list<list<pair<int,int>>> returned_types; |
| | 173 | |
| | 174 | for(list<list<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++) |
| | 175 | { |
| | 176 | |
| | 177 | for(int order = 1; order<=max_order; order++) |
| | 178 | { |
| | 179 | for(int channel = 0;channel<number_of_channels;channel++) |
| | 180 | { |
| | 181 | list<pair<int,int>> returned_type; |
| | 182 | pair<int,int> new_pair = pair<int,int>(channel,order); |
| | 183 | if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) |
| | 184 | { |
| | 185 | returned_type.insert(returned_type.begin(),(*model_ref).begin(),(*model_ref).end()); |
| | 186 | returned_type.push_back(new_pair); |
| | 187 | returned_types.push_back(returned_type); |
| | 188 | } |
| | 189 | } |
| | 190 | } |
| | 191 | } |
| | 192 | |
| | 193 | created_model_types.insert(created_model_types.end(),returned_types.begin(),returned_types.end()); |
| | 194 | |
| | 195 | return created_model_types; |
| | 196 | } |
| | 197 | } |
| | 198 | }; |
| | 199 | |
| | 200 | |
| 29 | | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
| 30 | | |
| 31 | | WORD wConvNo; |
| 32 | | char szType[] = "request"; |
| 33 | | char szData[21]; |
| 34 | | char szItem[] = "EURUSD"; |
| 35 | | char szService[] = "MT4"; |
| 36 | | char szTopic[] = "BID"; |
| 37 | | char szFormat[] = "CF_TEXT"; |
| 38 | | DWORD dwTimeout = 0; |
| 39 | | //char szAccess[] = "string"; |
| 40 | | |
| 41 | | if(!DCInit()) |
| 42 | | { |
| 43 | | cout << "DDE doesn't work." << endl; |
| 44 | | } |
| 45 | | else |
| 46 | | { |
| 47 | | // The following if-block shows a complete conversation with a |
| 48 | | // single transaction. You therefore do not need to free any memory |
| 49 | | // explicitly. |
| 50 | | |
| 51 | | // connect to server |
| 52 | | if (!DCConnect(&wConvNo,szService, szTopic)) |
| 53 | | { |
| 54 | | cout << "Couldn't connect DDE." << endl; |
| 55 | | } |
| 56 | | else |
| 57 | | { |
| 58 | | // do synchronous request transaction, wait max. 1000 ms, |
| 59 | | // return data as string |
| 60 | | if(!DCRequestString(wConvNo,szItem,100000)) |
| 61 | | { |
| 62 | | cout << "No data available." << endl; |
| 63 | | } |
| 64 | | else |
| 65 | | { |
| 66 | | if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) |
| 67 | | { |
| 68 | | cout << "Asynchronous transaction error." << endl; |
| 69 | | } |
| 70 | | else |
| 71 | | { |
| 72 | | // output data to console if transaction complete |
| 73 | | // DCDA[wConvNo]->pszData is the pointer to the data string |
| 74 | | cprintf(DCDA[wConvNo]->pszData); |
| 75 | | DCFreeDdeMem(wConvNo); |
| 76 | | } |
| 77 | | } |
| 78 | | // end conversation |
| 79 | | if(!DCDisconnect(wConvNo)) |
| 80 | | { |
| 81 | | cout << "Couldn't disconnect DDE." << endl; |
| 82 | | } |
| 83 | | } |
| 84 | | } |
| 85 | | |
| | 205 | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
| | 206 | |
| | 207 | /* |
| | 208 | char szApp[] = "MT4"; |
| | 209 | char szTopic[] = "ASK"; |
| | 210 | char szItem1[] = "EURUSD"; |
| | 211 | |
| | 212 | //DDE Initialization |
| | 213 | DWORD idInst=0; |
| | 214 | UINT iReturn; |
| | 215 | iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, |
| | 216 | APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 ); |
| | 217 | if (iReturn!=DMLERR_NO_ERROR) |
| | 218 | { |
| | 219 | printf("DDE Initialization Failed: 0x%04x\n", iReturn); |
| | 220 | Sleep(1500); |
| | 221 | return 0; |
| | 222 | } |
| | 223 | |
| | 224 | /* |
| | 225 | //Start DDE Server and wait for it to become idle. |
| | 226 | HINSTANCE hRet = ShellExecute(0, "open", szTopic, 0, 0, SW_SHOWNORMAL); |
| | 227 | if ((int)hRet < 33) |
| | 228 | { |
| | 229 | printf("Unable to Start DDE Server: 0x%04x\n", hRet); |
| | 230 | Sleep(1500); DdeUninitialize(idInst); |
| | 231 | return 0; |
| | 232 | } |
| | 233 | Sleep(1000); |
| | 234 | */ |
| | 235 | |
| | 236 | /* |
| | 237 | //DDE Connect to Server using given AppName and topic. |
| | 238 | HSZ hszApp, hszTopic; |
| | 239 | HCONV hConv; |
| | 240 | hszApp = DdeCreateStringHandle(idInst, szApp, 0); |
| | 241 | hszTopic = DdeCreateStringHandle(idInst, szTopic, 0); |
| | 242 | hConv = DdeConnect(idInst, hszApp, hszTopic, NULL); |
| | 243 | DdeFreeStringHandle(idInst, hszApp); |
| | 244 | DdeFreeStringHandle(idInst, hszTopic); |
| | 245 | if (hConv == NULL) |
| | 246 | { |
| | 247 | printf("DDE Connection Failed.\n"); |
| | 248 | Sleep(1500); DdeUninitialize(idInst); |
| | 249 | return 0; |
| | 250 | } |
| | 251 | |
| | 252 | //Execute commands/requests specific to the DDE Server. |
| | 253 | |
| | 254 | DDERequest(idInst, hConv, szItem1); |
| | 255 | |
| | 256 | //DDE Disconnect and Uninitialize. |
| | 257 | //DdeDisconnect(hConv); |
| | 258 | //DdeUninitialize(idInst); |
| | 259 | |
| | 260 | Sleep(300000); |
| | 261 | Sleep(3000); |
| | 262 | */ |
| | 263 | |
| 220 | | } |
| 221 | | |
| 222 | | strings.push_back(parsed_line); |
| 223 | | |
| 224 | | myfile.close(); |
| 225 | | } |
| 226 | | } |
| 227 | | |
| 228 | | |
| 229 | | |
| 230 | | for(int j = 0;j<strings.size();j++) |
| 231 | | { |
| | 400 | } |
| | 401 | |
| | 402 | data_matrix.ins_row(data_matrix.rows(),data_vector); |
| | 403 | } |
| | 404 | |
| | 405 | myfile.close(); |
| | 406 | } |
| | 407 | else |
| | 408 | { |
| | 409 | cout << "Can't open data file!" << endl; |
| | 410 | } |
| | 411 | |
| | 412 | list<list<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); |
| | 413 | |
| | 414 | list<model*> models; |
| | 415 | for(list<list<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
| | 416 | { |
| | 417 | models.push_back(new model((*model_type),true,false,30,0,&data_matrix)); |
| | 418 | models.push_back(new model((*model_type),false,false,30,0,&data_matrix)); |
| | 419 | } |
| | 420 | |
| | 421 | mat result_lognc; |
| | 422 | |
| | 423 | for(int time = max_model_order;20;time++) //time<data_matrix.cols() |
| | 424 | { |
| | 425 | vec cur_res_lognc; |
| | 426 | |
| | 427 | for(list<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
| | 428 | { |
| | 429 | (*model_ref)->data_update(time); |
| | 430 | if((*model_ref)->my_rarx!=NULL) |
| | 431 | { |
| | 432 | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->log_nc); |
| | 433 | } |
| | 434 | else |
| | 435 | { |
| | 436 | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc()); |
| | 437 | } |
| | 438 | } |
| | 439 | |
| | 440 | result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); |
| | 441 | |
| | 442 | cout << "Updated." << endl; |
| | 443 | |
| | 444 | /* |