PageRenderTime 54ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/satan/r_test.php

https://bitbucket.org/samuelbond/god
PHP | 703 lines | 367 code | 124 blank | 212 comment | 17 complexity | c0ba29524862d684b0067b609a2dd90a MD5 | raw file
  1. <?php
  2. /*
  3. * BACKGROUND:
  4. * There is a black box trading system. In 2008, the system was fully functional. A statistician
  5. * used SAS software to optimize the variable values that the system uses. Since 2008, the system
  6. * has been rewritten several times. New features and variables have been added, but the original
  7. * variables and basic functionality remain the same. The previous statisical analysis determined
  8. * the best way to optimize the variables. The analysis seems correct. New random tests were run
  9. * and the results matched what the statistician predicted. Now that the system has new features,
  10. * a new statistical analysis needs to be performed. That will help determine the best values for
  11. * the newly added variables. Also, more test data will be used. The goal is to use R scripts to
  12. * optimize the variable values. Moreover, since there are more variables (and 'too many' values)
  13. * R will have to guess the optimal values since brute force testing is not feasible. Ultimately,
  14. * the goal is to design the R scripts so that statistical analysis tests can be run automatically
  15. * in the future every 6 months.
  16. *
  17. * This r_test.php file is designed to mimic the black box system. Optimal variable values can be
  18. * manually set, and then tests can be run to make sure the R script successfully guesses the best
  19. * values for each variable. The "goodness value" that shows how the black box system performed is
  20. * stored in the database in a column named "rank".
  21. *
  22. *
  23. * INSTRUCTIONS:
  24. * . Review the previous statistical analysis at http://caLLLendar.com/pub/analysis/
  25. * The previous statistical analysis describes the various methods used to determine the optimal
  26. * variable values. It also lists the important variables that have the most influence. Please
  27. * use it as a guide when creating the new R scripts. Also, note the behavior of the variables.
  28. *
  29. * . Review the PHP code below.
  30. * The purpose of this r_test.php file is to mimic the behavior of the black box system. Briefly
  31. * review the code in the score() method below. The score() method is used to calculate a "rank"
  32. * value based on the variable values.
  33. *
  34. * . Make recommendations for modifying r_test.php
  35. * r_test.php is designed to mimic the black box trading system. Please determine if the score is
  36. * calculated in such a way as to effectively mimic the system used in the previous analysis. It
  37. * may be important to have the individual variables perform in the same way they did before. If
  38. * so, please recommend changes that should be made to make r_test.php more authentic.
  39. *
  40. * Variable relationships
  41. *
  42. * 3. Write the R script that will optimize the variables
  43. * Use the previous statistical analysis (http://caLLLendar.com/pub/analysis/ ) as a guide. The
  44. * analisys was successful in predicting the performance of the variable settings that it chose.
  45. * Note: The idea is that the optimal values in r_test.php can be manually set. Then, the R code
  46. * will be run and it will be clear if it is able to find those manually set optimal values.
  47. *
  48. * Once the R script is integrated into the php below, r_test.php will be called.
  49. *
  50. * Here is the process.
  51. * . r_test is called
  52. * . The database and tables are set up. Default values are added to the `settings` table.
  53. * . The R script (hereafter called "R") is called.
  54. * . R will find an empty `backtests` table.
  55. * . R will guess the variable settings to test.
  56. * . R will update the `settings` table with the variable values that should be tested.
  57. * . r_test will create rows in the `backtests` tables.
  58. * . r_test will mimic the black box system by calculating fake `rank` values.
  59. * . r_test repeats / loops.
  60. * . R is called again and finds a `backtest` table that includes results.
  61. * . R will guess what values to test based on the `rank` column in the `backtests` table.
  62. * . R will determine if the variables are sufficiently optimized.
  63. * . If R thinks the testing process is complete, it will update the `settings` table.
  64. * . r_test will stop looping.
  65. *
  66. * Here are the goals:
  67. * . Find the optimal values.
  68. * . Sharpe ratio
  69. * . Minimize / Maximize values
  70. * . How much money is needed (Kelly Criterion), how many times to profit?
  71. * . http://calllendar.com/pub/backtest-optimization.txt
  72. *
  73. */
  74. // Run the R optimization test
  75. $r_test = new r_test();
  76. class r_test
  77. {
  78. private $sett, $parent_insertq, $child_insertq;
  79. public function __construct()
  80. {
  81. do {
  82. $this->setup_database();
  83. $this->setup_settings();
  84. $this->call_r();
  85. $this->setup_testresults();
  86. $this->setup_positions();
  87. $this->rank();
  88. $brake = $this->stop_testing();
  89. } while( $brake == 'no' );
  90. }
  91. private function stop_testing()
  92. {
  93. /*
  94. * CONCEPT:
  95. * test_r->stop_testing() stops the optimization process
  96. * after R records "brake = yes" in the settings table.
  97. *
  98. * CAUTION:
  99. */
  100. list( $brake ) = mysql_fetch_row( mysql_query("SELECT `value` FROM `settings`
  101. WHERE `name` = 'backtestbrake' " ));
  102. if( $brake === 'yes' )
  103. {
  104. echo "Optimization is complete. ";
  105. }
  106. return $brake;
  107. }
  108. private function setup_database()
  109. {
  110. /*
  111. * CONCEPT:
  112. * r_test->setup_database() sets up the database and tables needed for R.
  113. *
  114. * CAUTION:
  115. */
  116. // Connect to the mySQL server
  117. $connect = mysql_connect( 'localhost', 'root', 'OlV734A' )
  118. or die( "Unable to connect to server!" );
  119. // Connect to the database
  120. $select = mysql_select_db( 'r_test', $connect )
  121. or die( "Could not select database ( `r_test` )
  122. using [ localhost ] connection code" );
  123. // Create the database that corresponds with the account set in configure
  124. mysql_query( "CREATE DATABASE IF NOT EXISTS `r_test` " );
  125. // Create the `settings` table
  126. mysql_query( "CREATE TABLE `settings`
  127. (name char(24) NOT NULL,
  128. port int(3) NOT NULL,
  129. value char(64) NOT NULL,
  130. KEY `name` (`name`, `port`),
  131. KEY `port` (`port`,`name`) )
  132. ENGINE=InnoDB " );
  133. // Create the table that holds the test results
  134. mysql_query( "CREATE TABLE IF NOT EXISTS `backtests` ( " .
  135. // id to make it easy to update test results
  136. "`id` int(10) unsigned NOT NULL auto_increment, " .
  137. // godprofit - setprofit = rank
  138. "`rank` int(10) DEFAULT NULL, " .
  139. // The percentage profit (or loss) between the startcash and godresult
  140. "`godprofit` int(11) NOT NULL DEFAULT '0', " .
  141. // The percentage profit (or loss) between the startcash and setresult
  142. "`setprofit` int(11) NOT NULL DEFAULT '0', " .
  143. // The place to store the failure message
  144. "`fail` text NOT NULL, " .
  145. // The total amount of cash the client puts in the brokerage account before god runs.
  146. "`startcash` decimal(10,2) NOT NULL DEFAULT '0.00', " .
  147. // The actual amount of money the position ended with
  148. "`godresult` decimal(10,2) NOT NULL DEFAULT '0.00', " .
  149. // startdate is the day the symbols are seleted on. The test process walks forward in time from startdate.
  150. "`startdate` date NOT NULL DEFAULT '0000-00-00', " .
  151. // The number of days between the startdate and the last day of trading
  152. "`duration` int(5) NOT NULL DEFAULT '0', " .
  153. // enddate is startdate + duration (days) (plus any extra time backtest needs to sell off all positions)
  154. "`enddate` date NOT NULL DEFAULT '0000-00-00', " .
  155. // The lowest price a symbol can be on the startdate
  156. "`startquote` decimal(10,2) NOT NULL DEFAULT '0.00', " .
  157. // The position number of the test result
  158. "`position` int(3) NOT NULL DEFAULT '0', " .
  159. // Internal variable that may affect results
  160. "`selldegree` int(3) NOT NULL DEFAULT '0', " .
  161. // The broker commission schedule used for the test
  162. "`broker` varchar(3) NOT NULL DEFAULT '', " .
  163. // Use R to select symbols
  164. "`select_r` varchar(3) NOT NULL DEFAULT '', " .
  165. // The set of symbols picked by R (based on their price correlation)
  166. "`symbols` text NOT NULL, " .
  167. // The diversify=yes forces god to avoid buying a symbol for more than one position
  168. "`diversify` varchar(3) NOT NULL DEFAULT '', " .
  169. // The hold=yes temporarily prevents god from selling a symbol with a rising price
  170. "`hold` varchar(3) NOT NULL DEFAULT '', " .
  171. // The number of positions processed during the test
  172. "`positions` int(3) NOT NULL DEFAULT '0', " .
  173. // The number of symbols selected for the set
  174. "`quantity` int(3) NOT NULL DEFAULT '0', " .
  175. // The sharebuffer setting god uses solely
  176. "`sharebuffer` int(11) NOT NULL DEFAULT '0', " .
  177. // The number of transactions that god executed
  178. "`transactions` int(11) NOT NULL DEFAULT '0', " .
  179. // The max amount of overall profit before triggering a stop
  180. "`profitlimitpercent` int(3) NOT NULL DEFAULT '0', " .
  181. // The max amount of overall loss before triggering a stop
  182. "`stoplosspercentage` int(3) NOT NULL DEFAULT '0', " .
  183. // The max percentage profit after buying a symbol before triggering a stop
  184. "`buystop_percentage` int(3) NOT NULL DEFAULT '0', " .
  185. // The max amount of loss after buying a symbol before triggering a stop
  186. "`sellstoppercentage` int(3) NOT NULL DEFAULT '0', " .
  187. // The max amount of loss from a high price before triggering a stop
  188. "`trailingpercentage` int(3) NOT NULL DEFAULT '0', " .
  189. // The percentage of slippage before triggering a stop
  190. "`pricebufferpercent` int(3) NOT NULL DEFAULT '0', " .
  191. // Setting KEYS increased performance
  192. "KEY `id` (`id`), " .
  193. "KEY `rank` (`rank`), " .
  194. "KEY `godprofit` (`godprofit`), " .
  195. "KEY `setprofit` (`setprofit`), " .
  196. "KEY `startcash` (`startcash`), " .
  197. "KEY `godresult` (`godresult`), " .
  198. // fail key not needed
  199. // startdate key not needed
  200. "KEY `startquote` (`startquote`), " .
  201. "KEY `position` (`position`), " .
  202. "KEY `selldegree` (`selldegree`), " .
  203. "KEY `broker` (`broker`), " .
  204. "KEY `select_r` (`select_r`), " .
  205. // symbols key not needed
  206. "KEY `diversify` (`diversify`), " .
  207. "KEY `duration` (`duration`), " .
  208. // enddate key not needed
  209. "KEY `hold` (`hold`), " .
  210. "KEY `positions` (`positions`), " .
  211. "KEY `quantity` (`quantity`), " .
  212. "KEY `sharebuffer` (`sharebuffer`), " .
  213. "KEY `transactions` (`transactions`), " .
  214. "KEY `profitlimitpercent` (`profitlimitpercent`), " .
  215. "KEY `stoplosspercentage` (`stoplosspercentage`), " .
  216. "KEY `buystop_percentage` (`buystop_percentage`), " .
  217. "KEY `sellstoppercentage` (`sellstoppercentage`), " .
  218. "KEY `trailingpercentage` (`trailingpercentage`), " .
  219. "KEY `pricebufferpercent` (`pricebufferpercent`) ) ENGINE=InnoDB" );
  220. echo "Database and tables are setup<br> ";
  221. }
  222. private function setup_positions()
  223. {
  224. /*
  225. * CONCEPT:
  226. *
  227. * CAUTION:
  228. */
  229. // Delete the tests
  230. mysql_query("DELETE FROM `backtests` " );
  231. // Insert the basic tests
  232. for( $i = 1; $i <= 9; $i++ )
  233. {
  234. mysql_query("$this->parent_insertq");
  235. mysql_query("$this->child_insertq");
  236. mysql_query("UPDATE `backtests` SET `position` = '$i' WHERE `position` = '' ");
  237. }
  238. }
  239. private function setup_testresults()
  240. {
  241. /*
  242. * CONCEPT:
  243. *
  244. * CAUTION:
  245. */
  246. $settingsq = mysql_query("SELECT name,value FROM `settings` ");
  247. $testarray = array();
  248. while( list( $name, $value ) = mysql_fetch_array( $settingsq ) )
  249. {
  250. $$name = $value;
  251. }
  252. // Make the parent
  253. $this->parent_insertq = "INSERT INTO `backtests` SET
  254. `startcash` = '1000',
  255. `startdate` = '2012-03-17',
  256. `duration` = '360',
  257. `startquote` = '1.00',
  258. `broker` = '0',
  259. `select_r` = 'yes',
  260. `symbols` = 'ASTC,RHAT',
  261. `diversify` = 'no',
  262. `hold` = 'no',
  263. `positions` = '9',
  264. `quantity` = '24',
  265. `sharebuffer` = '0',
  266. `profitlimitpercent` = '10000',
  267. `stoplosspercentage` = '1',
  268. `buystop_percentage` = '1000',
  269. `sellstoppercentage` = '1',
  270. `trailingpercentage` = '1',
  271. `pricebufferpercent` = '0' ";
  272. // Make a child
  273. $this->child_insertq = "INSERT INTO `backtests` SET
  274. `startcash` = '$startcash',
  275. `startdate` = '2012-03-17',
  276. `duration` = '$duration',
  277. `startquote` = '$startquote',
  278. `broker` = '$broker',
  279. `select_r` = '$select_r',
  280. `symbols` = 'ASTC,RHAT',
  281. `diversify` = '$diversify',
  282. `hold` = '$hold',
  283. `positions` = '9',
  284. `quantity` = '$quantity',
  285. `sharebuffer` = '$sharebuffer',
  286. `profitlimitpercent` = '$profitlimitpercent',
  287. `stoplosspercentage` = '$stoplosspercentage',
  288. `buystop_percentage` = '$buystop_percentage',
  289. `sellstoppercentage` = '$sellstoppercentage',
  290. `trailingpercentage` = '$trailingpercentage',
  291. `pricebufferpercent` = '$pricebufferpercent' ";
  292. }
  293. private function setup_settings()
  294. {
  295. /*
  296. * CONCEPT:
  297. *
  298. * CAUTION:
  299. * 'position' will NOT be stored or set in the `settings` table.
  300. */
  301. $this->sett = array(
  302. 'broker_min' => '0',
  303. 'broker_max' => '3',
  304. 'broker_opt' => '1',
  305. 'buystop_percentage_min' => '100',
  306. 'buystop_percentage_max' => '1000',
  307. 'buystop_percentage_opt' => '800',
  308. 'diversify_min' => 'no',
  309. 'diversify_max' => 'yes',
  310. 'diversify_opt' => 'yes',
  311. 'duration_min' => '1',
  312. 'duration_max' => '360',
  313. 'duration_opt' => '90',
  314. 'hold_min' => 'no',
  315. 'hold_max' => 'yes',
  316. 'hold_opt' => 'no',
  317. 'position_min' => '1',
  318. 'position_max' => '9',
  319. 'position_opt' => '3',
  320. 'pricebufferpercent_min' => '1',
  321. 'pricebufferpercent_max' => '25',
  322. 'pricebufferpercent_opt' => '10',
  323. 'profitlimitpercent_min' => '1',
  324. 'profitlimitpercent_max' => '10000',
  325. 'profitlimitpercent_opt' => '5555',
  326. 'quantity_min' => '2',
  327. 'quantity_max' => '24',
  328. 'quantity_opt' => '12',
  329. 'select_r_min' => 'no',
  330. 'select_r_max' => 'yes',
  331. 'select_r_opt' => 'yes',
  332. 'selldegree_min' => 'no',
  333. 'selldegree_max' => 'yes',
  334. 'selldegree_opt' => 'yes',
  335. 'sellstoppercentage_min' => '1',
  336. 'sellstoppercentage_max' => '100',
  337. 'sellstoppercentage_opt' => '51',
  338. 'sharebuffer_min' => '0',
  339. 'sharebuffer_max' => '3000',
  340. 'sharebuffer_opt' => '444',
  341. 'startcash_min' => '100',
  342. 'startcash_max' => '1000',
  343. 'startcash_opt' => '333',
  344. 'startquote_min' => '0.10',
  345. 'startquote_max' => '50',
  346. 'startquote_opt' => '2.22',
  347. 'stoplosspercentage_min' => '1',
  348. 'stoplosspercentage_max' => '100',
  349. 'stoplosspercentage_opt' => '75',
  350. 'trailingpercentage_min' => '1',
  351. 'trailingpercentage_max' => '100',
  352. 'trailingpercentage_opt' => '11' );
  353. mysql_query("DELETE FROM `settings` " );
  354. // maximize / minimize
  355. $sett[] = array('broker', 'base' => 0, 'min' => 0, 'max' => 3, 'type' => 'int', 'opt' => '1');
  356. $sett[] = array('buystop_percentage', 'base' => 0, 'min' => 100, 'max' => 1000, 'type' => 'int', 'opt' => '800');
  357. $sett[] = array('diversify', 'base' => 'no', 'min' => 'no', 'max' => 'yes', 'type' => 'yesno', 'opt' => 'yes');
  358. $sett[] = array('duration', 'base' => 1, 'min' => 1, 'max' => 360, 'type' => 'int', 'opt' => '90');
  359. $sett[] = array('hold', 'base' => 'no', 'min' => 'no', 'max' => 'yes', 'type' => 'yesno', 'opt' => 'no');
  360. $sett[] = array('position', 'base' => 1, 'min' => 1, 'max' => 9, 'type' => 'int', 'opt' => '3');
  361. $sett[] = array('pricebufferpercent', 'base' => 0, 'min' => 0, 'max' => 25, 'type' => 'int', 'opt' => '10');
  362. $sett[] = array('profitlimitpercent', 'base' => 100, 'min' => 0, 'max' => 10000, 'type' => 'int', 'opt' => '5555');
  363. $sett[] = array('quantity', 'base' => 3, 'min' => 2, 'max' => 24, 'type' => 'int', 'opt' => '12');
  364. $sett[] = array('select_r', 'base' => 'no', 'min' => 'no', 'max' => 'yes', 'type' => 'yesno', 'opt' => 'yes');
  365. $sett[] = array('selldegree', 'base' => 1, 'min' => 1, 'max' => 9, 'type' => 'int', 'opt' => ''); // ???
  366. $sett[] = array('sellstoppercentage', 'base' => 36, 'min' => 1, 'max' => 100, 'type' => 'int', 'opt' => '51');
  367. $sett[] = array('sharebuffer', 'base' => 400, 'min' => 0, 'max' => 3000, 'type' => 'int', 'opt' => '444');
  368. $sett[] = array('startcash', 'base' => 200, 'min' => 100, 'max' => 1000, 'type' => 'int', 'opt' => '333');
  369. $sett[] = array('startquote', 'base' => 1, 'min' => 0.10, 'max' => 50, 'type' => 'int', 'opt' => '2.22');
  370. $sett[] = array('stoplosspercentage', 'base' => 10, 'min' => 1, 'max' => 100, 'type' => 'int', 'opt' => '75');
  371. $sett[] = array('trailingpercentage', 'base' => 36, 'min' => 1, 'max' => 100, 'type' => 'int', 'opt' => '11');
  372. // Set a class-wide variable for the settings
  373. foreach( $sett AS $index => $setting )
  374. {
  375. $r_value = $this->set_random( $setting );
  376. echo "setting = [ " . $setting[0] . " ] default = [ " . $r_value . " ]<br> ";
  377. mysql_query("INSERT INTO `settings` SET
  378. `name` = '" . $setting[0] . "',
  379. `value` = '" . $r_value . "' ");
  380. }
  381. mysql_query("INSERT INTO `settings` SET `name` = 'backtestbrake', `value` = 'no' ");
  382. mysql_query("INSERT INTO `settings` SET `name` = '', `value` = '' ");
  383. echo "Done inserting default settings into the `settings` table.<br> ";
  384. }
  385. private function call_r()
  386. {
  387. /*
  388. * CONCEPT:
  389. * test_r->call_r() calls R. Then, R will . . .
  390. * 1. Check the `backtests` table.
  391. * 2. Determine what variable settings to optimize.
  392. * 3. Update the values in the `settings` table.
  393. * 4. Determine if the settings are completely opitimized.
  394. * 5. IF the settings are optimized insert brake = 'yes' in the settings table.
  395. *
  396. * R should try to . . .
  397. * maximize buystop_percentage
  398. * minimize duration
  399. * maximize profitlimitpercent
  400. * maximize pricebufferpercent
  401. * minimize quantity
  402. * minimize sellstoppercentage
  403. * maximize sharebuffer
  404. * minimize startcash
  405. * minimize stoplosspercentage
  406. * minimize trailingpercentage
  407. *
  408. * CAUTION:
  409. * Make sure that R updates 'backtestbrake' to 'yes' in the `settings` table.
  410. */
  411. // Call R
  412. // Remove this line after R is able to set the backtestbrake
  413. mysql_query("UPDATE `settings` SET `value` = 'yes' WHERE `name` = 'backtestbrake' ");
  414. }
  415. private function score( $test_settings )
  416. {
  417. /*
  418. * CONCEPT:
  419. *
  420. * CAUTION:
  421. */
  422. // print_r($test_settings);
  423. // print_r($this->sett);
  424. foreach( $test_settings AS $setting => $value )
  425. {
  426. if( $setting !== 'id' )
  427. {
  428. echo "setting = $setting , value = $value <br>";
  429. $opt = $this->sett[$setting . '_opt'];
  430. $opt = $value; // ??? Remove this line
  431. $max = $this->sett[$setting . '_max'];
  432. // Convert yesno values
  433. if( $max === 'yes' )
  434. {
  435. $max = 2;
  436. }
  437. elseif( $max === 'no' )
  438. {
  439. $max = 1;
  440. }
  441. if( $opt === 'yes' )
  442. {
  443. $opt = 2;
  444. }
  445. elseif( $opt === 'no' )
  446. {
  447. $opt = 1;
  448. }
  449. if( $value === 'yes' )
  450. {
  451. $value = 2;
  452. }
  453. elseif( $value === 'no' )
  454. {
  455. $value = 1;
  456. }
  457. if( $opt > $value )
  458. {
  459. $difference = $opt - $value;
  460. }
  461. else
  462. {
  463. $difference = $value - $opt;
  464. }
  465. // echo "difference = $difference<br>";
  466. $subset = $max - $difference;
  467. $decimal = $subset / $max;
  468. $percent = floor( $decimal * 100 );
  469. // echo "value = $value<br>";
  470. // echo "decimal = $decimal<br>";
  471. echo "score = $percent<br>";
  472. // Tally the score
  473. $score = $score + $percent;
  474. }
  475. }
  476. return $score;
  477. }
  478. public function set_random( $setting )
  479. {
  480. /*
  481. * CONCEPT:
  482. * randomizer() picks a random setting between the min and max
  483. *
  484. * CAUTION:
  485. */
  486. // say($setting);
  487. // var_dump($setting);
  488. // Convert the 'no' and 'yes' to '0' and '1' for the mt_rand() function
  489. if( $setting['type'] == 'yesno' )
  490. {
  491. $setting['max'] = 1;
  492. $setting['min'] = 0;
  493. }
  494. elseif( $setting['type'] == 'string' )
  495. {
  496. $emergencies .= "randomizer() was asked to randomize a setting that is a 'string' type";
  497. }
  498. // Set the '' to 0 on the $settings that have 0 as the minimum value
  499. $setting['min'] = ( !$setting['min'] ) ? 0 : $setting['min'];
  500. // If the setting is startquote, use a minimum of 1
  501. if( $setting === 'startquote' )
  502. {
  503. // Multiply the max startquote by 100 to make room for change
  504. $maxed_quote = $setting['max'] * 100;
  505. // Make some random change (starting as low as $0.10)
  506. $change = mt_rand( 10, $maxed_quote );
  507. // Divide by 100 to get the dollars and cents
  508. $rand = $change / 100;
  509. // Convert to a string for easy comparison
  510. $rand = strval( $rand );
  511. }
  512. else
  513. {
  514. // Get a random number
  515. $rand = mt_rand( $setting['min'], $setting['max'] );
  516. }
  517. if( $setting['type'] == 'yesno' )
  518. {
  519. // If the type is 'yesno' convert the random number back to 'yes' or 'no'
  520. $rand = ( $setting['type'] == 'yesno' && $rand == 1 ) ? 'yes' : 'no';
  521. }
  522. // echo $rand;
  523. return $rand;
  524. }
  525. private function rank()
  526. {
  527. /*
  528. * CONCEPT:
  529. *
  530. * CAUTION:
  531. */
  532. // Calculate the rank for each position
  533. $resultq = "SELECT `id`,`startcash`,`duration`,`startquote`,`position`,`broker`,`select_r`,
  534. `diversify`,`hold`,`quantity`,`sharebuffer`,`transactions`,`profitlimitpercent`,`stoplosspercentage`,
  535. `buystop_percentage`,`sellstoppercentage`,`trailingpercentage`,`pricebufferpercent` FROM `backtests`
  536. WHERE `rank` IS NULL ";
  537. // echo "$resultq<br>";
  538. $result = mysql_query("$resultq");
  539. while( list( $id, $startcash, $duration, $startquote, $position, $broker, $select_r,
  540. $diversify, $hold, $quantity, $sharebuffer, $transactions, $profitlimitpercent, $stoplosspercentage,
  541. $buystop_percentage, $sellstoppercentage, $trailingpercentage, $pricebufferpercent ) =
  542. mysql_fetch_row( $result ) )
  543. {
  544. $test_settings = array(
  545. 'id' => "$id",
  546. 'startcash' => "$startcash",
  547. 'duration' => "$duration",
  548. 'startquote' => "$startquote",
  549. 'position' => "$position",
  550. 'broker' => "$broker",
  551. 'select_r' => "$select_r",
  552. 'diversify' => "$diversify",
  553. 'hold' => "$hold",
  554. 'quantity' => "$quantity",
  555. 'sharebuffer' => "$sharebuffer",
  556. 'pricebufferpercent' => "$pricebufferpercent",
  557. 'profitlimitpercent' => "$profitlimitpercent",
  558. 'stoplosspercentage' => "$stoplosspercentage",
  559. 'buystop_percentage' => "$buystop_percentage",
  560. 'sellstoppercentage' => "$sellstoppercentage",
  561. 'trailingpercentage' => "$trailingpercentage",
  562. 'pricebufferpercent' => "$pricebufferpercent" );
  563. // Calculate the score for the test
  564. $total_score = $this->score( $test_settings );
  565. echo "total_score = [ $total_score ]<br> ";
  566. // Alter the score randomly ?
  567. $t_score_min = $total_score - ( $total_score * 0.25 );
  568. $t_score_max = $total_score + ( $total_score * 0.20 );
  569. $rank = mt_rand( $t_score_min, $t_score_max );
  570. echo "t_score_min = [ $t_score_min ]<br>";
  571. echo "t_score_max = [ $t_score_max ]<br>";
  572. echo "rank = [ $rank ] ";
  573. // Record the rank
  574. mysql_query("UPDATE `backtests` SET `rank` = '" . $rank . "'
  575. WHERE `id` = '" . $id . "' ");
  576. }
  577. }
  578. }
  579. ?>