SHARE
TWEET

Untitled

a guest May 26th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // if needed fetch 5 sec periods for the for the past running minute or so each time traed.php runs
  3.  
  4. /*
  5.  
  6.  
  7.  
  8. <?php
  9. Array
  10. (
  11.     //[0] => Array ( [0] => DateTime [1] => Currency [2] => Name [3] => Country [4] => Volatility [5] => Actual [6] => Previous [7] => Consensus )
  12.     [1] => Array ( [0] => 05/06/2019 00:00:00 [1] => GBP [2] => Early May [3] => United Kingdom [4] => 0 [5] => [6] => [7] => )
  13.     [2] => Array ( [0] => 05/06/2019 00:00:00 [1] => JPY [2] => Children\'s Day (Observed) [3] => Japan [4] => 0 [5] => [6] => [7] => )
  14.     [3] => Array ( [0] => 05/06/2019 01:00:00 [1] => NZD [2] => ANZ Commodity Price [3] => New Zealand [4] => 1 [5] => 2.500 [6] => 1.400 [7] => )
  15. )
  16.  
  17. $fundis = array_map('str_getcsv', file('fundis.txt'));
  18. array_shift($fundis);
  19. //$timenow = gmdate("m/d/Y H:i:s", time());
  20. $timenow = time();
  21. foreach ($fundis as $fundi)
  22. {
  23.     $fundt = explode('/', $fundi[0]);
  24.     $fundt = $fundt[1] . '-' . $fundt[0] . '-' . $fundt[2];
  25.     $fundt = strtotime($fundt);
  26.     if (
  27.         $fundt > ($timenow - 60) // depends on which periods are used
  28.         && $fundt < ($timenow + 300) // depends on which periods are used
  29.         && ($fundi[1] == 'GBP' || $fundi[1] == 'AUD')
  30.         && $fundi[4] > 1
  31.     )
  32.     {
  33.         $buy = 0;
  34.         $sell = 0;
  35.         break 1;
  36.         // maybe write somethig to some text file here
  37.     }
  38. }
  39. ?>
  40.  
  41.  
  42.  
  43.  
  44. */
  45.  
  46.  
  47.  
  48. if (isset($_POST['fetchy'])) // rename in traed.php
  49. {
  50.     include ('funcs.php');
  51.     ini_set('trader.real_precision', '8');
  52.     $cyc = file_get_contents('cyc.txt'); file_put_contents('cyc.txt', ($cyc + 1));
  53.     $trade = file_get_contents('trade.txt'); if ($trade != 0) $trade = explode('_', $trade);
  54.     $authorization = "Authorization: Bearer ";
  55.     $account = "";
  56.     $dt = microtime();
  57.     $dts = ('Y-m-d H:i:s');
  58.     $htm = date('H:i');
  59.     $mode = 'trade';
  60.  
  61.     $curr = "GBP_AUD";
  62.     $units = 100;
  63.     $takeprofit = 0.01000;
  64.     $stoploss = 0.00100;
  65.     $bound = 0.00025;
  66.     $spread =  0.00035;
  67.     $toty = 250;
  68.  
  69.     $close_vals = array();
  70.     $high_vals = array();
  71.     $low_vals = array();
  72.     $bb_vals1 = array();
  73.     $bb_vals2 = array();
  74.     $buy = 0;
  75.     $sell = 0;
  76.  
  77.     $atr_length = -2;
  78.     $atrval_l = 10;
  79.     $atrval_h = 1000000;
  80.     $adx_length = 15;
  81.     $adx_l = 1;
  82.     $adx_h = 20;
  83.     $bb_length1 = 14;
  84.     $sd_factor1 = 1.8;
  85.     $bb_length2 = 10;
  86.     $sd_factor2 = 1;
  87.    
  88.     $ch_rates = curl_init('https://api-fx' . $mode . '.oanda.com/v3/instruments/' . $curr . '/candles?count=' . $toty . '&price=MBA&granularity=M1');
  89.     curl_setopt($ch_rates, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));  
  90.     curl_setopt($ch_rates, CURLOPT_RETURNTRANSFER, TRUE);
  91.     curl_setopt($ch_rates, CURLOPT_SSL_VERIFYPEER, FALSE);
  92.     curl_setopt($ch_rates, CURLOPT_HEADER, TRUE);
  93.     $result_rates = curl_exec($ch_rates);
  94.     curl_close($ch_rates);
  95.     $result_rates1 = explode('{', $result_rates);
  96.     $header = array_shift($result_rates1);
  97.     if (substr($header, 0, 12) == 'HTTP/1.1 200')
  98.     {
  99.         $echostr = $dts . PHP_EOL;
  100.         file_put_contents('rates200/' . $cyc . '.txt', $dt . '-_-' . $result_rates);
  101.         $result_rates1 = '{' . implode('{', $result_rates1);
  102.         $result_rates1 = json_decode($result_rates1, true);
  103.         $candlerate = $result_rates1['candles'];
  104.         $current_rate = array_pop($candlerate);
  105.         $live_rate = $current_rate['mid']['c'];
  106.         $bid = $current_rate['bid']['c'];
  107.         $ask = $current_rate['ask']['c'];
  108.         if (($ask - $bid) > $spread) exit("spread");
  109.        
  110.         $candlerate = $api_rates;
  111.         foreach ($candlerate as $cr)
  112.         {
  113.             if (!is_numeric($cr['mid']['c'])) exit("nonum");
  114.             array_push($close_vals, $cr['mid']['c']);
  115.             array_push($high_vals, $cr['mid']['h']);
  116.             array_push($low_vals, $cr['mid']['l']);
  117.         }
  118.         if (count($close_vals) < ($toty - 1)) exit("toty");
  119.        
  120.         $close_vals = array_reverse($close_vals);
  121.         $c1 = $close_vals[0]; $c2 = $close_vals[1]; $c3 = $close_vals[2]; $c4 = $close_vals[3]; $c5 = $close_vals[4];
  122.         $close_vals = array_reverse($close_vals);
  123.         $high_vals = array_reverse($high_vals);
  124.         $h1 = $high_vals[0]; $h2 = $high_vals[1]; $h3 = $high_vals[2]; $h4 = $high_vals[3]; $h5 = $high_vals[4];
  125.         $high_vals = array_reverse($high_vals);
  126.         $low_vals = array_reverse($low_vals);
  127.         $l1 = $low_vals[0]; $l2 = $low_vals[1]; $l3 = $low_vals[2]; $l4 = $low_vals[3]; $l5 = $low_vals[4];
  128.         $low_vals = array_reverse($low_vals);
  129.        
  130.        
  131.        
  132.         // atr
  133.        
  134.         $bb_vals1 = array_slice($close_vals, ($bb_length1 * -1), $bb_length1);
  135.         $bb1 = bb($bb_vals1,$bb_length1,$sd_factor1);
  136.        
  137.         $adxc = $close_vals; $adxh = $high_vals; $adxl = $low_vals;
  138.         $adx = trader_adx($adxh,$adxl,$adxc,$adx_length);
  139.         $adx = array_pop($adx);
  140.        
  141.         for ($adxi = 1; $adxi <= $bb_length2; $adxi++)
  142.         {
  143.             $adx1 = trader_adx($adxh,$adxl,$adxc,$adx_length);
  144.             array_push($bb_vals2, array_pop($adx1));
  145.             array_pop($adxc); array_pop($adxh); array_pop($adxl);
  146.         }
  147.        
  148.         $bb2 = bb($bb_vals2,$bb_length2,$sd_factor2);
  149.        
  150.        
  151.        
  152.         if ($trade[1] > 0)
  153.         {
  154.             if ( (
  155.                     $trade[0] == 1
  156.                     &&
  157.                     &&
  158.                 )
  159.                 || (
  160.                     $trade[0] == 2
  161.                     &&
  162.                     &&
  163.                 )
  164.             )
  165.             {
  166.                 $ch_close = curl_init('https://api-fx' . $mode . '.oanda.com/v3/accounts/' . $account . '/trades/' . $trade[1] . '/close');
  167.                 curl_setopt($ch_close, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
  168.                 curl_setopt($ch_close, CURLOPT_RETURNTRANSFER, TRUE);
  169.                 curl_setopt($ch_close, CURLOPT_SSL_VERIFYPEER, FALSE);
  170.                 curl_setopt($ch_close, CURLOPT_HEADER, TRUE);
  171.                 curl_setopt($ch_close, CURLOPT_PUT, TRUE);
  172.                 // curl_setopt($ch_close, CURLOPT_CUSTOMREQUEST, "PUT");
  173.                 $result_close = curl_exec($ch_close);
  174.                 curl_close($ch_close);
  175.                 $result_close1 = explode('{', $result_close);
  176.                 $header_close = array_shift($result_close1);
  177.                 if (substr($header_close, 0, 12) == 'HTTP/1.1 200')
  178.                 {
  179.                     $trade = 0;
  180.                     file_put_contents('trade.txt', 0);
  181.                     file_put_contents('posclose200/' . $cyc . '.txt', $dt . '-_-' . $result_close);
  182.                     $echostr .= " -> Trade Closed: " . $trade[1] . PHP_EOL;
  183.                 }
  184.                 elseif (substr($header_close, 0, 12) == 'HTTP/1.1 400') { file_put_contents('posclose400/' . $cyc . '.txt', $dt . '-_-' . $result_close); exit("400"); }
  185.                 elseif (substr($header_close, 0, 12) == 'HTTP/1.1 404') { file_put_contents('posclose404/' . $cyc . '.txt', $dt . '-_-' . $result_close); exit("404"); }
  186.                 else { file_put_contents('posclose999/' . $cyc . '.txt', $dt . '-_-' . $result_close); exit("999"); }
  187.             }
  188.         }
  189.        
  190.         if ($trade == 0)
  191.         {
  192.             if (
  193.                 !strstr($htm, '23:4') && !strstr($htm, '23:5') && !strstr($htm, '00:0') && !strstr($htm, '00:1') && !strstr($htm, '00:2')
  194.                 &&
  195.                 &&
  196.             )
  197.             {
  198.                 $buy = 1;
  199.                 $takeprofit = round( ( (string)$live_rate + (string)$takeprofit ), 5 );
  200.                 $bound = round( ( (string)$live_rate + (string)$bound ), 5 );
  201.             }
  202.             elseif (
  203.                 !strstr($htm, '23:4') && !strstr($htm, '23:5') && !strstr($htm, '00:0') && !strstr($htm, '00:1') && !strstr($htm, '00:2')
  204.                 &&
  205.                 &&
  206.             )
  207.             {
  208.                 $sell = 1;
  209.                 $units *= -1;
  210.                 $takeprofit = round( ( (string)$live_rate - (string)$takeprofit ), 5 );
  211.                 $bound = round( ( (string)$live_rate - (string)$bound ), 5 );
  212.             }
  213.         }
  214.     }
  215.     else { file_put_contents('rates999/' . $cyc . '.txt', $dt . '-_-' . $result_rates); exit("rates"); }
  216.    
  217.     if ($buy == 1 || $sell == 1)
  218.     {
  219.         if ($live_rate < 0.5 || $takeprofit < 0.5  || $bound < 0.5 || !is_numeric($cyc) || strlen($bound) < 5 || !is_numeric($bound) || (strlen($takeprofit) < 5 || !is_numeric($takeprofit)))
  220.         {
  221.             file_put_contents('check.txt', 'check-/_-' . $dt . '-/_-' . $live_rate . '-/_-' . $takeprofit . '-/_-' . $bound . '-/_-' . $cyc . PHP_EOL, FILE_APPEND);
  222.             exit("check");
  223.         }
  224.        
  225.         $data = array(
  226.             "order" => array(
  227.                 "units" => (string)$units,
  228.                 "instrument" => $curr,
  229.                 "timeInForce" => "FOK",
  230.                 "type" => "MARKET",
  231.                 "positionFill" => "DEFAULT",
  232.                 "priceBound" => (string)$bound,
  233.                 "takeProfitOnFill" => array("price" => (string)$takeprofit, "timeInForce" => "GTC"),
  234.                 "stopLossOnFill" => array("distance" => (string)$stoploss, "timeInForce" => "GTC")
  235.             )
  236.         );
  237.         $payload = json_encode($data);
  238.         $ch_open = curl_init('https://api-fx' . $mode . '.oanda.com/v3/accounts/' . $account . '/orders');
  239.         curl_setopt($ch_open, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
  240.         curl_setopt($ch_open, CURLOPT_RETURNTRANSFER, TRUE);
  241.         curl_setopt($ch_open, CURLOPT_SSL_VERIFYPEER, FALSE);
  242.         curl_setopt($ch_open, CURLOPT_POSTFIELDS, $payload);
  243.         curl_setopt($ch_open, CURLOPT_HEADER, TRUE);
  244.         curl_setopt($ch_open, CURLOPT_POST, TRUE);
  245.         $result_open = curl_exec($ch_open);
  246.         curl_close($ch_open);
  247.         $result_open1 = explode('{', $result_open);
  248.         $header_open = array_shift($result_open1);
  249.         if (substr($header_open, 0, 12) == 'HTTP/1.1 201')
  250.         {
  251.             $result_open1 = '{' . implode('{', $result_open1);
  252.             $result_open1 = json_decode($result_open1, true);
  253.             $newpos = $result_open1['orderFillTransaction']['tradeOpened']['tradeID'];
  254.             if ($units > 0) file_put_contents('trade.txt', '1_' . $newpos);
  255.             else file_put_contents('trade.txt', '2_' . $newpos);
  256.             file_put_contents('posopen201/' . $cyc . '.txt', $newpos . '-_-' . $dt . '-_-' . $result_open);
  257.             $echostr .= " -> Trade Opened: " . $newpos . PHP_EOL;
  258.         }
  259.         else file_put_contents('posopen999/' . $cyc . '.txt', $dt . '-_-' . $result_open);
  260.     }
  261.     echo $echostr;
  262. }
  263. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top