god /satan/r_test.php

Language PHP Lines 704
MD5 Hash c0ba29524862d684b0067b609a2dd90a Estimated Cost $9,377 (why?)
Repository https://bitbucket.org/samuelbond/god.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?php

/*
 * BACKGROUND:
 * There is a black box trading system.  In 2008, the system was fully functional.  A statistician
 * used SAS software to optimize the variable values that the system uses.  Since 2008, the system
 * has been rewritten several times.  New features and variables have been added, but the original
 * variables and basic functionality remain the same.  The previous statisical analysis determined
 * the best way to optimize the variables.  The analysis seems correct.  New random tests were run
 * and the results matched what the statistician predicted.  Now that the system has new features,
 * a new statistical analysis needs to be performed.  That will help determine the best values for
 * the newly added variables.  Also, more test data will be used.  The goal is to use R scripts to
 * optimize the variable values.  Moreover, since there are more variables (and 'too many' values)
 * R will have to guess the optimal values since brute force testing is not feasible.  Ultimately,
 * the goal is to design the R scripts so that statistical analysis tests can be run automatically
 * in the future every 6 months.
 *
 * This r_test.php file is designed to mimic the black box system.  Optimal variable values can be
 * manually set, and then tests can be run to make sure the R script successfully guesses the best
 * values for each variable. The "goodness value" that shows how the black box system performed is
 * stored in the database in a column named "rank".
 *
 *
 * INSTRUCTIONS:
 * . Review the previous statistical analysis at http://caLLLendar.com/pub/analysis/
 * The previous statistical analysis describes the various methods used to determine the optimal
 * variable values.  It also lists the important variables that have the most influence.  Please
 * use it as a guide when creating the new R scripts.  Also, note the behavior of the variables.
 *
 * . Review the PHP code below.
 * The purpose of this r_test.php file is to mimic the behavior of the black box system.  Briefly
 * review the code in the score() method below.  The score() method is used to calculate a "rank"
 * value based on the variable values.
 *
 * . Make recommendations for modifying r_test.php
 * r_test.php is designed to mimic the black box trading system. Please determine if the score is
 * calculated in such a way as to effectively mimic the system used in the previous analysis.  It
 * may be important to have the individual variables perform in the same way they did before.  If
 * so, please recommend changes that should be made to make r_test.php more authentic.
 *
 * Variable relationships
 *
 * 3. Write the R script that will optimize the variables
 * Use the previous statistical analysis (http://caLLLendar.com/pub/analysis/ ) as a guide. The
 * analisys was successful in predicting the performance of the variable settings that it chose.
 * Note: The idea is that the optimal values in r_test.php can be manually set. Then, the R code
 * will be run and it will be clear if it is able to find those manually set optimal values.
 *
 * Once the R script is integrated into the php below, r_test.php will be called.
 *
 * Here is the process.
 * . r_test is called
 * . The database and tables are set up.  Default values are added to the `settings` table.
 * . The R script (hereafter called "R") is called.
 * . R will find an empty `backtests` table.
 * . R will guess the variable settings to test.
 * . R will update the `settings` table with the variable values that should be tested.
 * . r_test will create rows in the `backtests` tables.
 * . r_test will mimic the black box system by calculating fake `rank` values.
 * . r_test repeats / loops.
 * . R is called again and finds a `backtest` table that includes results.
 * . R will guess what values to test based on the `rank` column in the `backtests` table.
 * . R will determine if the variables are sufficiently optimized.
 * . If R thinks the testing process is complete, it will update the `settings` table.
 * . r_test will stop looping.
 *
 * Here are the goals:
 * . Find the optimal values.
 * . Sharpe ratio
 * . Minimize / Maximize values
 * . How much money is needed (Kelly Criterion), how many times to profit?
 * . http://calllendar.com/pub/backtest-optimization.txt
 *
 */


// Run the R optimization test
$r_test = new r_test();

class r_test
{
  private $sett, $parent_insertq, $child_insertq;


  public function __construct()
  {

    do {
      $this->setup_database();

      $this->setup_settings();

      $this->call_r();

      $this->setup_testresults();

      $this->setup_positions();

      $this->rank();

      $brake = $this->stop_testing();

    } while( $brake == 'no' );

  }



  private function stop_testing()
  {
    /*
     * CONCEPT:
     * test_r->stop_testing() stops the optimization process
     * after R records "brake = yes" in the settings table.
     *
     * CAUTION:
     */

    list( $brake ) = mysql_fetch_row( mysql_query("SELECT `value` FROM `settings`
      WHERE `name` = 'backtestbrake' " ));

    if( $brake === 'yes' )
    {
      echo "Optimization is complete. ";
    }

    return $brake;
  }



  private function setup_database()
  {
    /*
     * CONCEPT:
     * r_test->setup_database() sets up the database and tables needed for R.
     *
     * CAUTION:
     */

    // Connect to the mySQL server
    $connect = mysql_connect( 'localhost', 'root', 'OlV734A' )
      or die( "Unable to connect to server!" );

    // Connect to the database
    $select = mysql_select_db( 'r_test', $connect )
      or die( "Could not select database ( `r_test` )
      using [ localhost ] connection code" );

    // Create the database that corresponds with the account set in  configure
    mysql_query( "CREATE DATABASE IF NOT EXISTS `r_test` " );

    // Create the `settings` table
    mysql_query( "CREATE TABLE `settings`
      (name char(24) NOT NULL,
        port int(3) NOT NULL,
        value char(64) NOT NULL,
        KEY `name` (`name`, `port`),
    KEY `port` (`port`,`name`) )
    ENGINE=InnoDB " );

    // Create the table that holds the test results
    mysql_query( "CREATE TABLE IF NOT EXISTS `backtests` ( " .
      // id to make it easy to update test results
      "`id` int(10) unsigned NOT NULL auto_increment, " .

      // godprofit - setprofit = rank
      "`rank` int(10) DEFAULT NULL, " .
      // The percentage profit (or loss) between the startcash and godresult
      "`godprofit` int(11) NOT NULL DEFAULT '0', " .
      // The percentage profit (or loss) between the startcash and setresult
      "`setprofit` int(11) NOT NULL DEFAULT '0', " .

      // The place to store the failure message
      "`fail` text NOT NULL, " .

      // The total amount of cash the client puts in the brokerage account before god runs.
      "`startcash` decimal(10,2) NOT NULL DEFAULT '0.00', " .
      // The actual amount of money the position ended with
      "`godresult` decimal(10,2) NOT NULL DEFAULT '0.00', " .

      // startdate is the day the symbols are seleted on. The test process walks forward in time from startdate.
      "`startdate` date NOT NULL DEFAULT '0000-00-00', " .
      // The number of days between the startdate and the last day of trading
      "`duration` int(5) NOT NULL DEFAULT '0', " .
      // enddate is startdate + duration (days) (plus any extra time backtest needs to sell off all positions)
      "`enddate` date NOT NULL DEFAULT '0000-00-00', " .

      // The lowest price a symbol can be on the startdate
      "`startquote` decimal(10,2) NOT NULL DEFAULT '0.00', " .

      // The position number of the test result
      "`position` int(3) NOT NULL DEFAULT '0', " .
      // Internal variable that may affect results
      "`selldegree` int(3) NOT NULL DEFAULT '0', " .

      // The broker commission schedule used for the test
      "`broker` varchar(3) NOT NULL DEFAULT '', " .
      // Use R to select symbols
      "`select_r` varchar(3) NOT NULL DEFAULT '', " .
      // The set of symbols picked by R (based on their price correlation)
      "`symbols` text NOT NULL, " .
      // The diversify=yes forces god to avoid buying a symbol for more than one position
      "`diversify` varchar(3) NOT NULL DEFAULT '', " .

      // The hold=yes temporarily prevents god from selling a symbol with a rising price
      "`hold` varchar(3) NOT NULL DEFAULT '', " .
      // The number of positions processed during the test
      "`positions` int(3) NOT NULL DEFAULT '0', " .
      // The number of symbols selected for the set
      "`quantity` int(3) NOT NULL DEFAULT '0', " .
      // The sharebuffer setting god uses solely
      "`sharebuffer` int(11) NOT NULL DEFAULT '0', " .
      // The number of transactions that god executed
      "`transactions`  int(11) NOT NULL DEFAULT '0', " .

      // The max amount of overall profit before triggering a stop
      "`profitlimitpercent` int(3) NOT NULL DEFAULT '0', " .
      // The max amount of overall loss before triggering a stop
      "`stoplosspercentage` int(3) NOT NULL DEFAULT '0', " .
      // The max percentage profit after buying a symbol before triggering a stop
      "`buystop_percentage` int(3) NOT NULL DEFAULT '0', " .
      // The max amount of loss after buying a symbol before triggering a stop
      "`sellstoppercentage` int(3) NOT NULL DEFAULT '0', " .
      // The max amount of loss from a high price before triggering a stop
      "`trailingpercentage` int(3) NOT NULL DEFAULT '0', " .
      // The percentage of slippage before triggering a stop
      "`pricebufferpercent` int(3) NOT NULL DEFAULT '0', " .

      // Setting KEYS increased performance
      "KEY `id` (`id`), " .

      "KEY `rank` (`rank`), " .
      "KEY `godprofit` (`godprofit`), " .
      "KEY `setprofit` (`setprofit`), " .

      "KEY `startcash` (`startcash`), " .
      "KEY `godresult` (`godresult`), " .

      // fail key not needed

      // startdate key not needed
      "KEY `startquote` (`startquote`), " .

      "KEY `position` (`position`), " .
      "KEY `selldegree` (`selldegree`), " .

      "KEY `broker` (`broker`), " .
      "KEY `select_r` (`select_r`), " .
      // symbols key not needed
      "KEY `diversify` (`diversify`), " .

      "KEY `duration` (`duration`), " .
      // enddate key not needed

      "KEY `hold` (`hold`), " .
      "KEY `positions` (`positions`), " .
      "KEY `quantity` (`quantity`), " .
      "KEY `sharebuffer` (`sharebuffer`), " .
      "KEY `transactions` (`transactions`), " .

      "KEY `profitlimitpercent` (`profitlimitpercent`), " .
      "KEY `stoplosspercentage` (`stoplosspercentage`), " .
      "KEY `buystop_percentage` (`buystop_percentage`), " .
      "KEY `sellstoppercentage` (`sellstoppercentage`), " .
      "KEY `trailingpercentage` (`trailingpercentage`), " .
      "KEY `pricebufferpercent` (`pricebufferpercent`) ) ENGINE=InnoDB" );

    echo "Database and tables are setup<br> ";
  }



  private function setup_positions()
  {
    /*
     * CONCEPT:
     *
     * CAUTION:
     */


    // Delete the tests
    mysql_query("DELETE FROM `backtests` " );

    // Insert the basic tests
    for( $i = 1; $i <= 9; $i++ )
    {
      mysql_query("$this->parent_insertq");
      mysql_query("$this->child_insertq");
      mysql_query("UPDATE `backtests` SET `position` = '$i' WHERE `position` = '' ");
    }

  }



  private function setup_testresults()
  {
    /*
     * CONCEPT:
     *
     * CAUTION:
     */


    $settingsq = mysql_query("SELECT name,value FROM `settings` ");

    $testarray = array();

    while( list( $name, $value ) = mysql_fetch_array( $settingsq ) )
    {
      $$name = $value;
    }

    // Make the parent
    $this->parent_insertq = "INSERT INTO `backtests` SET
      `startcash` = '1000',
      `startdate` = '2012-03-17',
      `duration` = '360',
      `startquote` = '1.00',
      `broker` = '0',
      `select_r` = 'yes',
      `symbols` = 'ASTC,RHAT',
      `diversify` = 'no',
      `hold` = 'no',
      `positions` = '9',
      `quantity` = '24',
      `sharebuffer` = '0',
      `profitlimitpercent` = '10000',
      `stoplosspercentage` = '1',
      `buystop_percentage` = '1000',
      `sellstoppercentage` = '1',
      `trailingpercentage` = '1',
      `pricebufferpercent` = '0' ";

    // Make a child
    $this->child_insertq = "INSERT INTO `backtests` SET
      `startcash` = '$startcash',
      `startdate` = '2012-03-17',
      `duration` = '$duration',
      `startquote` = '$startquote',
      `broker` = '$broker',
      `select_r` = '$select_r',
      `symbols` = 'ASTC,RHAT',
      `diversify` = '$diversify',
      `hold` = '$hold',
      `positions` = '9',
      `quantity` = '$quantity',
      `sharebuffer` = '$sharebuffer',
      `profitlimitpercent` = '$profitlimitpercent',
      `stoplosspercentage` = '$stoplosspercentage',
      `buystop_percentage` = '$buystop_percentage',
      `sellstoppercentage` = '$sellstoppercentage',
      `trailingpercentage` = '$trailingpercentage',
      `pricebufferpercent` = '$pricebufferpercent' ";
  }



  private function setup_settings()
  {
    /*
     * CONCEPT:
     *
     * CAUTION:
     * 'position' will NOT be stored or set in the `settings` table.
     */

    $this->sett = array(
      'broker_min' => '0',
      'broker_max' => '3',
      'broker_opt' => '1',
      'buystop_percentage_min' => '100',
      'buystop_percentage_max' => '1000',
      'buystop_percentage_opt' => '800',
      'diversify_min' => 'no',
      'diversify_max' => 'yes',
      'diversify_opt' => 'yes',
      'duration_min' => '1',
      'duration_max' => '360',
      'duration_opt' => '90',
      'hold_min' => 'no',
      'hold_max' => 'yes',
      'hold_opt' => 'no',
      'position_min' => '1',
      'position_max' => '9',
      'position_opt' => '3',
      'pricebufferpercent_min' => '1',
      'pricebufferpercent_max' => '25',
      'pricebufferpercent_opt' => '10',
      'profitlimitpercent_min' => '1',
      'profitlimitpercent_max' => '10000',
      'profitlimitpercent_opt' => '5555',
      'quantity_min' => '2',
      'quantity_max' => '24',
      'quantity_opt' => '12',
      'select_r_min' => 'no',
      'select_r_max' => 'yes',
      'select_r_opt' => 'yes',
      'selldegree_min' => 'no',
      'selldegree_max' => 'yes',
      'selldegree_opt' => 'yes',
      'sellstoppercentage_min' => '1',
      'sellstoppercentage_max' => '100',
      'sellstoppercentage_opt' => '51',
      'sharebuffer_min' => '0',
      'sharebuffer_max' => '3000',
      'sharebuffer_opt' => '444',
      'startcash_min' => '100',
      'startcash_max' => '1000',
      'startcash_opt' => '333',
      'startquote_min' => '0.10',
      'startquote_max' => '50',
      'startquote_opt' => '2.22',
      'stoplosspercentage_min' => '1',
      'stoplosspercentage_max' => '100',
      'stoplosspercentage_opt' => '75',
      'trailingpercentage_min' => '1',
      'trailingpercentage_max' => '100',
      'trailingpercentage_opt' => '11' );

    mysql_query("DELETE FROM `settings` " );
    // maximize / minimize
    $sett[] = array('broker', 'base' => 0, 'min' => 0, 'max' => 3, 'type' => 'int', 'opt' => '1');
    $sett[] = array('buystop_percentage', 'base' => 0, 'min' => 100, 'max' => 1000, 'type' => 'int', 'opt' => '800');
    $sett[] = array('diversify', 'base' => 'no', 'min' => 'no', 'max' => 'yes', 'type' => 'yesno', 'opt' => 'yes');
    $sett[] = array('duration', 'base' => 1, 'min' => 1, 'max' => 360, 'type' => 'int', 'opt' => '90');
    $sett[] = array('hold', 'base' => 'no', 'min' => 'no', 'max' => 'yes', 'type' => 'yesno', 'opt' => 'no');
    $sett[] = array('position', 'base' => 1, 'min' => 1, 'max' => 9, 'type' => 'int', 'opt' => '3');
    $sett[] = array('pricebufferpercent', 'base' => 0, 'min' => 0, 'max' => 25, 'type' => 'int', 'opt' => '10');
    $sett[] = array('profitlimitpercent', 'base' => 100, 'min' => 0, 'max' => 10000, 'type' => 'int', 'opt' => '5555');
    $sett[] = array('quantity', 'base' => 3, 'min' => 2, 'max' => 24, 'type' => 'int', 'opt' => '12');
    $sett[] = array('select_r', 'base' => 'no', 'min' => 'no', 'max' => 'yes', 'type' => 'yesno', 'opt' => 'yes');
    $sett[] = array('selldegree', 'base' => 1, 'min' => 1, 'max' => 9, 'type' => 'int', 'opt' => ''); // ???
    $sett[] = array('sellstoppercentage', 'base' => 36, 'min' => 1, 'max' => 100, 'type' => 'int', 'opt' => '51');
    $sett[] = array('sharebuffer', 'base' => 400, 'min' => 0, 'max' => 3000, 'type' => 'int', 'opt' => '444');
    $sett[] = array('startcash', 'base' => 200, 'min' => 100, 'max' => 1000, 'type' => 'int', 'opt' => '333');
    $sett[] = array('startquote', 'base' => 1, 'min' => 0.10, 'max' => 50, 'type' => 'int', 'opt' => '2.22');
    $sett[] = array('stoplosspercentage', 'base' => 10, 'min' => 1, 'max' => 100, 'type' => 'int', 'opt' => '75');
    $sett[] = array('trailingpercentage', 'base' => 36, 'min' => 1, 'max' => 100, 'type' => 'int', 'opt' => '11');

    // Set a class-wide variable for the settings
    foreach( $sett AS $index => $setting )
    {
      $r_value = $this->set_random( $setting );

      echo "setting = [ " . $setting[0] . " ] default = [ " . $r_value . " ]<br> ";

      mysql_query("INSERT INTO `settings` SET
        `name` = '" . $setting[0] . "',
        `value` = '" . $r_value . "' ");
    }

    mysql_query("INSERT INTO `settings` SET `name` = 'backtestbrake', `value` = 'no' ");
    mysql_query("INSERT INTO `settings` SET `name` = '', `value` = '' ");

    echo "Done inserting default settings into the `settings` table.<br> ";
  }



  private function call_r()
  {
    /*
     * CONCEPT:
     * test_r->call_r() calls R. Then, R will . . .
     * 1. Check the `backtests` table.
     * 2. Determine what variable settings to optimize.
     * 3. Update the values in the `settings` table.
     * 4. Determine if the settings are completely opitimized.
     * 5. IF the settings are optimized insert brake = 'yes' in the settings table.
     *
     * R should try to . . .
     * maximize buystop_percentage
     * minimize duration
     * maximize profitlimitpercent
     * maximize pricebufferpercent
     * minimize quantity
     * minimize sellstoppercentage
     * maximize sharebuffer
     * minimize startcash
     * minimize stoplosspercentage
     * minimize trailingpercentage
     *
     * CAUTION:
     * Make sure that R updates 'backtestbrake' to 'yes' in the `settings` table.
     */


    // Call R

    // Remove this line after R is able to set the backtestbrake
    mysql_query("UPDATE `settings` SET `value` = 'yes' WHERE `name` = 'backtestbrake' ");
  }



  private function score( $test_settings )
  {
    /*
     * CONCEPT:
     *
     * CAUTION:
     */


    // print_r($test_settings);
    // print_r($this->sett);
    foreach( $test_settings AS $setting => $value )
    {
      if( $setting !== 'id' )
      {
        echo "setting = $setting , value = $value <br>";

        $opt = $this->sett[$setting . '_opt'];
        $opt = $value; // ??? Remove this line
        $max = $this->sett[$setting . '_max'];

        // Convert yesno values
        if( $max === 'yes' )
        {
          $max = 2;
        }
        elseif( $max === 'no' )
        {
          $max = 1;
        }

        if( $opt === 'yes' )
        {
          $opt = 2;
        }
        elseif( $opt === 'no' )
        {
          $opt = 1;
        }

        if( $value === 'yes' )
        {
          $value = 2;
        }
        elseif( $value === 'no' )
        {
          $value = 1;
        }

        if( $opt > $value )
        {
          $difference = $opt - $value;
        }
        else
        {
          $difference = $value - $opt;
        }

        // echo "difference = $difference<br>";

        $subset = $max - $difference;
        $decimal = $subset / $max;
        $percent = floor( $decimal * 100 );

        // echo "value = $value<br>";
        // echo "decimal = $decimal<br>";
        echo "score = $percent<br>";

        // Tally the score
        $score = $score + $percent;
      }
    }

    return $score;
  }



  public function set_random( $setting )
  {
    /*
     * CONCEPT:
     * randomizer() picks a random setting between the min and max
     *
     * CAUTION:
     */


    // say($setting);
    // var_dump($setting);
    // Convert the 'no' and 'yes' to '0' and '1' for the mt_rand() function
    if( $setting['type'] == 'yesno' )
    {
      $setting['max'] = 1;
      $setting['min'] = 0;
    }
    elseif( $setting['type'] == 'string' )
    {
      $emergencies .= "randomizer() was asked to randomize a setting that is a 'string' type";
    }

    // Set the '' to 0 on the $settings that have 0 as the minimum value
    $setting['min'] = ( !$setting['min'] ) ? 0 : $setting['min'];

    // If the setting is startquote, use a minimum of 1
    if( $setting === 'startquote' )
    {
      // Multiply the max startquote by 100 to make room for change
      $maxed_quote = $setting['max'] * 100;

      // Make some random change (starting as low as $0.10)
      $change = mt_rand( 10, $maxed_quote );

      // Divide by 100 to get the dollars and cents
      $rand = $change / 100;

      // Convert to a string for easy comparison
      $rand = strval( $rand );
    }
    else
    {
      // Get a random number
      $rand = mt_rand( $setting['min'], $setting['max'] );
    }

    if( $setting['type'] == 'yesno' )
    {
      // If the type is 'yesno' convert the random number back to 'yes' or 'no'
      $rand = ( $setting['type'] == 'yesno' && $rand == 1 ) ? 'yes' : 'no';
    }

    // echo $rand;
    return $rand;
  }



  private function rank()
  {
    /*
     * CONCEPT:
     *
     * CAUTION:
     */

    // Calculate the rank for each position

    $resultq = "SELECT `id`,`startcash`,`duration`,`startquote`,`position`,`broker`,`select_r`,
      `diversify`,`hold`,`quantity`,`sharebuffer`,`transactions`,`profitlimitpercent`,`stoplosspercentage`,
      `buystop_percentage`,`sellstoppercentage`,`trailingpercentage`,`pricebufferpercent` FROM `backtests`
      WHERE `rank` IS NULL ";

    // echo "$resultq<br>";

    $result = mysql_query("$resultq");

    while( list( $id, $startcash, $duration, $startquote, $position, $broker, $select_r,
      $diversify, $hold, $quantity, $sharebuffer, $transactions, $profitlimitpercent, $stoplosspercentage,
      $buystop_percentage, $sellstoppercentage, $trailingpercentage, $pricebufferpercent ) =
      mysql_fetch_row( $result ) )
    {
      $test_settings = array(
        'id' => "$id",
        'startcash' => "$startcash",
        'duration' => "$duration",
        'startquote' => "$startquote",
        'position' => "$position",
        'broker' => "$broker",
        'select_r' => "$select_r",
        'diversify' => "$diversify",
        'hold' => "$hold",
        'quantity' => "$quantity",
        'sharebuffer' => "$sharebuffer",
        'pricebufferpercent' => "$pricebufferpercent",
        'profitlimitpercent' => "$profitlimitpercent",
        'stoplosspercentage' => "$stoplosspercentage",
        'buystop_percentage' => "$buystop_percentage",
        'sellstoppercentage' => "$sellstoppercentage",
        'trailingpercentage' => "$trailingpercentage",
        'pricebufferpercent' => "$pricebufferpercent" );

      // Calculate the score for the test
      $total_score = $this->score( $test_settings );
      echo "total_score = [ $total_score ]<br> ";

      // Alter the score randomly ?
      $t_score_min = $total_score - ( $total_score * 0.25 );
      $t_score_max = $total_score + ( $total_score * 0.20 );
      $rank = mt_rand( $t_score_min, $t_score_max );

      echo "t_score_min = [ $t_score_min ]<br>";
      echo "t_score_max = [ $t_score_max ]<br>";
      echo "rank = [ $rank ] ";

      // Record the rank
      mysql_query("UPDATE `backtests` SET `rank` = '" . $rank . "'
        WHERE `id` = '" . $id . "' ");
    }

  }


}

?>
Back to Top