Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // if needed fetch 5 sec periods for the for the past running minute or so each time traed.php runs
- /*
- <?php
- Array
- (
- //[0] => Array ( [0] => DateTime [1] => Currency [2] => Name [3] => Country [4] => Volatility [5] => Actual [6] => Previous [7] => Consensus )
- [1] => Array ( [0] => 05/06/2019 00:00:00 [1] => GBP [2] => Early May [3] => United Kingdom [4] => 0 [5] => [6] => [7] => )
- [2] => Array ( [0] => 05/06/2019 00:00:00 [1] => JPY [2] => Children\'s Day (Observed) [3] => Japan [4] => 0 [5] => [6] => [7] => )
- [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] => )
- )
- $fundis = array_map('str_getcsv', file('fundis.txt'));
- array_shift($fundis);
- //$timenow = gmdate("m/d/Y H:i:s", time());
- $timenow = time();
- foreach ($fundis as $fundi)
- {
- $fundt = explode('/', $fundi[0]);
- $fundt = $fundt[1] . '-' . $fundt[0] . '-' . $fundt[2];
- $fundt = strtotime($fundt);
- if (
- $fundt > ($timenow - 60) // depends on which periods are used
- && $fundt < ($timenow + 300) // depends on which periods are used
- && ($fundi[1] == 'GBP' || $fundi[1] == 'AUD')
- && $fundi[4] > 1
- )
- {
- $buy = 0;
- $sell = 0;
- break 1;
- // maybe write somethig to some text file here
- }
- }
- ?>
- */
- if (isset($_POST['fetchy'])) // rename in traed.php
- {
- include ('funcs.php');
- ini_set('trader.real_precision', '8');
- $cyc = file_get_contents('cyc.txt'); file_put_contents('cyc.txt', ($cyc + 1));
- $trade = file_get_contents('trade.txt'); if ($trade != 0) $trade = explode('_', $trade);
- $authorization = "Authorization: Bearer ";
- $account = "";
- $dt = microtime();
- $dts = ('Y-m-d H:i:s');
- $htm = date('H:i');
- $mode = 'trade';
- $curr = "GBP_AUD";
- $units = 100;
- $takeprofit = 0.01000;
- $stoploss = 0.00100;
- $bound = 0.00025;
- $spread = 0.00035;
- $toty = 250;
- $close_vals = array();
- $high_vals = array();
- $low_vals = array();
- $bb_vals1 = array();
- $bb_vals2 = array();
- $buy = 0;
- $sell = 0;
- $atr_length = -2;
- $atrval_l = 10;
- $atrval_h = 1000000;
- $adx_length = 15;
- $adx_l = 1;
- $adx_h = 20;
- $bb_length1 = 14;
- $sd_factor1 = 1.8;
- $bb_length2 = 10;
- $sd_factor2 = 1;
- $ch_rates = curl_init('https://api-fx' . $mode . '.oanda.com/v3/instruments/' . $curr . '/candles?count=' . $toty . '&price=MBA&granularity=M1');
- curl_setopt($ch_rates, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
- curl_setopt($ch_rates, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch_rates, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch_rates, CURLOPT_HEADER, TRUE);
- $result_rates = curl_exec($ch_rates);
- curl_close($ch_rates);
- $result_rates1 = explode('{', $result_rates);
- $header = array_shift($result_rates1);
- if (substr($header, 0, 12) == 'HTTP/1.1 200')
- {
- $echostr = $dts . PHP_EOL;
- file_put_contents('rates200/' . $cyc . '.txt', $dt . '-_-' . $result_rates);
- $result_rates1 = '{' . implode('{', $result_rates1);
- $result_rates1 = json_decode($result_rates1, true);
- $candlerate = $result_rates1['candles'];
- $current_rate = array_pop($candlerate);
- $live_rate = $current_rate['mid']['c'];
- $bid = $current_rate['bid']['c'];
- $ask = $current_rate['ask']['c'];
- if (($ask - $bid) > $spread) exit("spread");
- $candlerate = $api_rates;
- foreach ($candlerate as $cr)
- {
- if (!is_numeric($cr['mid']['c'])) exit("nonum");
- array_push($close_vals, $cr['mid']['c']);
- array_push($high_vals, $cr['mid']['h']);
- array_push($low_vals, $cr['mid']['l']);
- }
- if (count($close_vals) < ($toty - 1)) exit("toty");
- $close_vals = array_reverse($close_vals);
- $c1 = $close_vals[0]; $c2 = $close_vals[1]; $c3 = $close_vals[2]; $c4 = $close_vals[3]; $c5 = $close_vals[4];
- $close_vals = array_reverse($close_vals);
- $high_vals = array_reverse($high_vals);
- $h1 = $high_vals[0]; $h2 = $high_vals[1]; $h3 = $high_vals[2]; $h4 = $high_vals[3]; $h5 = $high_vals[4];
- $high_vals = array_reverse($high_vals);
- $low_vals = array_reverse($low_vals);
- $l1 = $low_vals[0]; $l2 = $low_vals[1]; $l3 = $low_vals[2]; $l4 = $low_vals[3]; $l5 = $low_vals[4];
- $low_vals = array_reverse($low_vals);
- // atr
- $bb_vals1 = array_slice($close_vals, ($bb_length1 * -1), $bb_length1);
- $bb1 = bb($bb_vals1,$bb_length1,$sd_factor1);
- $adxc = $close_vals; $adxh = $high_vals; $adxl = $low_vals;
- $adx = trader_adx($adxh,$adxl,$adxc,$adx_length);
- $adx = array_pop($adx);
- for ($adxi = 1; $adxi <= $bb_length2; $adxi++)
- {
- $adx1 = trader_adx($adxh,$adxl,$adxc,$adx_length);
- array_push($bb_vals2, array_pop($adx1));
- array_pop($adxc); array_pop($adxh); array_pop($adxl);
- }
- $bb2 = bb($bb_vals2,$bb_length2,$sd_factor2);
- if ($trade[1] > 0)
- {
- if ( (
- $trade[0] == 1
- &&
- &&
- )
- || (
- $trade[0] == 2
- &&
- &&
- )
- )
- {
- $ch_close = curl_init('https://api-fx' . $mode . '.oanda.com/v3/accounts/' . $account . '/trades/' . $trade[1] . '/close');
- curl_setopt($ch_close, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
- curl_setopt($ch_close, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch_close, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch_close, CURLOPT_HEADER, TRUE);
- curl_setopt($ch_close, CURLOPT_PUT, TRUE);
- // curl_setopt($ch_close, CURLOPT_CUSTOMREQUEST, "PUT");
- $result_close = curl_exec($ch_close);
- curl_close($ch_close);
- $result_close1 = explode('{', $result_close);
- $header_close = array_shift($result_close1);
- if (substr($header_close, 0, 12) == 'HTTP/1.1 200')
- {
- $trade = 0;
- file_put_contents('trade.txt', 0);
- file_put_contents('posclose200/' . $cyc . '.txt', $dt . '-_-' . $result_close);
- $echostr .= " -> Trade Closed: " . $trade[1] . PHP_EOL;
- }
- elseif (substr($header_close, 0, 12) == 'HTTP/1.1 400') { file_put_contents('posclose400/' . $cyc . '.txt', $dt . '-_-' . $result_close); exit("400"); }
- elseif (substr($header_close, 0, 12) == 'HTTP/1.1 404') { file_put_contents('posclose404/' . $cyc . '.txt', $dt . '-_-' . $result_close); exit("404"); }
- else { file_put_contents('posclose999/' . $cyc . '.txt', $dt . '-_-' . $result_close); exit("999"); }
- }
- }
- if ($trade == 0)
- {
- if (
- !strstr($htm, '23:4') && !strstr($htm, '23:5') && !strstr($htm, '00:0') && !strstr($htm, '00:1') && !strstr($htm, '00:2')
- &&
- &&
- )
- {
- $buy = 1;
- $takeprofit = round( ( (string)$live_rate + (string)$takeprofit ), 5 );
- $bound = round( ( (string)$live_rate + (string)$bound ), 5 );
- }
- elseif (
- !strstr($htm, '23:4') && !strstr($htm, '23:5') && !strstr($htm, '00:0') && !strstr($htm, '00:1') && !strstr($htm, '00:2')
- &&
- &&
- )
- {
- $sell = 1;
- $units *= -1;
- $takeprofit = round( ( (string)$live_rate - (string)$takeprofit ), 5 );
- $bound = round( ( (string)$live_rate - (string)$bound ), 5 );
- }
- }
- }
- else { file_put_contents('rates999/' . $cyc . '.txt', $dt . '-_-' . $result_rates); exit("rates"); }
- if ($buy == 1 || $sell == 1)
- {
- 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)))
- {
- file_put_contents('check.txt', 'check-/_-' . $dt . '-/_-' . $live_rate . '-/_-' . $takeprofit . '-/_-' . $bound . '-/_-' . $cyc . PHP_EOL, FILE_APPEND);
- exit("check");
- }
- $data = array(
- "order" => array(
- "units" => (string)$units,
- "instrument" => $curr,
- "timeInForce" => "FOK",
- "type" => "MARKET",
- "positionFill" => "DEFAULT",
- "priceBound" => (string)$bound,
- "takeProfitOnFill" => array("price" => (string)$takeprofit, "timeInForce" => "GTC"),
- "stopLossOnFill" => array("distance" => (string)$stoploss, "timeInForce" => "GTC")
- )
- );
- $payload = json_encode($data);
- $ch_open = curl_init('https://api-fx' . $mode . '.oanda.com/v3/accounts/' . $account . '/orders');
- curl_setopt($ch_open, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
- curl_setopt($ch_open, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch_open, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch_open, CURLOPT_POSTFIELDS, $payload);
- curl_setopt($ch_open, CURLOPT_HEADER, TRUE);
- curl_setopt($ch_open, CURLOPT_POST, TRUE);
- $result_open = curl_exec($ch_open);
- curl_close($ch_open);
- $result_open1 = explode('{', $result_open);
- $header_open = array_shift($result_open1);
- if (substr($header_open, 0, 12) == 'HTTP/1.1 201')
- {
- $result_open1 = '{' . implode('{', $result_open1);
- $result_open1 = json_decode($result_open1, true);
- $newpos = $result_open1['orderFillTransaction']['tradeOpened']['tradeID'];
- if ($units > 0) file_put_contents('trade.txt', '1_' . $newpos);
- else file_put_contents('trade.txt', '2_' . $newpos);
- file_put_contents('posopen201/' . $cyc . '.txt', $newpos . '-_-' . $dt . '-_-' . $result_open);
- $echostr .= " -> Trade Opened: " . $newpos . PHP_EOL;
- }
- else file_put_contents('posopen999/' . $cyc . '.txt', $dt . '-_-' . $result_open);
- }
- echo $echostr;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement