Skip to content

Commit

Permalink
Merge pull request #4 from JulesText/dev
Browse files Browse the repository at this point in the history
format
  • Loading branch information
JulesVertex authored Mar 29, 2021
2 parents 05fb1d9 + 7e7036a commit 4832793
Show file tree
Hide file tree
Showing 16 changed files with 1,853 additions and 1,594 deletions.
15 changes: 12 additions & 3 deletions config_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
date_default_timezone_set('UTC');

if (version_compare(phpversion(),"8.0.0",'>=')
|| version_compare(phpversion(),"7.0.0",'<'))
|| version_compare(phpversion(),"7.0.0",'<'))
die ('requires php version 7.x.x, running ' . phpversion());

ini_set('memory_limit', '64M'); # memory limit, set '-1' for unlimited, default 32M
Expand Down Expand Up @@ -33,8 +33,10 @@

/* telegram settings */

$config['chatId'] = 'chatId'; # default telegram chat id
$config['chatPath'] = 'https://api.telegram.org/bot132'; # bot API path and key
// bot name aaaaaa
$config['chatPath'] = 'https://api.telegram.org/bot000000000:AAAAAAAAA_AAAAAAAA_AAAAAAAA'; # bot API path and key
// user name aaaaaa
$config['chatId'] = '111111111'; # chat id for bot and user
$config['chatText'] = 'message failed';

/* database arrays */
Expand Down Expand Up @@ -226,6 +228,13 @@ function config_exchange($config) {
$config['hist_pairs'] = array('BTC/USDT', 'ETH/USDT', 'LTC/USDT', 'ETC/USDT', 'XRP/USDT', 'EOS/USDT', 'BCH/USDT', 'BSV/USDT', 'TRX/USDT');
break;

case 'etherscan':
$config['method'] = 'GET';
$config['api_key'] = '000';
$config['url'] = 'https://api.etherscan.io/api?';
$config['address_eth'] = '0x000';
break;

case 'uniswap':
$config['url'] = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2';
$config['address_uniswap'] = '0x0000000000000000000000000000000000000000';
Expand Down
252 changes: 131 additions & 121 deletions functions.analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
function technical_analysis($config, $pair_id = FALSE) {

$query = "SELECT DISTINCT pair, period, source, currency, history_start FROM asset_pairs WHERE analyse";
if($pair_id) $query .= " AND pair_id = " . $pair_id;
if ($pair_id) $query .= " AND pair_id = " . $pair_id;
$pairs = query($query, $config);

$stop = $config['timestamp'];
Expand All @@ -18,145 +18,155 @@ function technical_analysis($config, $pair_id = FALSE) {
$rsios = 25; // oversold, 'rsi'.$n.'os'
$ema = array(6, 12, 26, 50, 100, 200); // 'ema'.$n, crossover upwards 'ema'.$n.'cu', crossover downwards 'ema'.$n.'cd'
$roc = array(1, 2, 4, 6, 12, 24); // 'roc'.$n
$corr = array('BTC' => array(50),
'ETH' => array(50)); // 'corr'.$n.'btc'
$corr = array(
'BTC' => array(50),
'ETH' => array(50)
); // 'corr'.$n.'btc'

foreach ($pairs as $pair) {

$period_txt = $config['period'][$pair['period']];
$period = $pair['period'] * 60000;
$start = $stop - $period * ($points + $points_buffer);

$query = "SELECT history_id, timestamp, close FROM price_history
WHERE pair = '" . $pair['pair'] . "'
AND source = '" . $pair['source'] . "'
AND period = '" . $period_txt . "'
AND timestamp >= " . $start . "
AND timestamp <= " . $stop . "
ORDER BY timestamp ASC";
$hist = query($query, $config);
if($config['debug']) echo $query . PHP_EOL;
if(empty($hist)) continue 1;
//var_dump($hist);
//var_dump($query);

$last = $hist[0]['timestamp'] - $period;
$missing_data = FALSE;
$i = 0;
foreach ($hist as $h) {
$diff_obs = $h['timestamp'] - $last;
if ($period !== $diff_obs) {
$missing_data = TRUE;
$missing_obs = $h['timestamp'] - $period;
$missing_array = $i;
}
$last = $h['timestamp'];
$i++;
}
if($missing_data) {
echo 'missing data at array position ' . $missing_array . ', <br>time ' . date('Y-m-d H:i T', $missing_obs / 1000) . ', <br>timestamp ' . $missing_obs . ', <br>period ' . $period . '<br>';
var_dump($hist[$missing_array - 1]);
var_dump($hist[$missing_array]);
continue 1;
}

$count = count($hist);
$dc = array_column($hist, 'close');

foreach($rsi as $n) {
$r = trader_rsi($dc, $n);
$j = 0;
for($i = 0; $i < $count; $i++) {
if(isset($r[$i])) {
$hist[$i]['rsi'.$n] = $r[$i];
if(isset($r[$j])) {
if($r[$j] <= $rsiob && $r[$i] > $rsiob)
$hist[$i]['rsi'.$n.'ob'] = 1;
else
$hist[$i]['rsi'.$n.'ob'] = 0;
if($r[$j] >= $rsios && $r[$i] < $rsios)
$hist[$i]['rsi'.$n.'os'] = 1;
else
$hist[$i]['rsi'.$n.'os'] = 0;
}
$period_txt = $config['period'][$pair['period']];
$period = $pair['period'] * 60000;
$start = $stop - $period * ($points + $points_buffer);

$query = "
SELECT history_id, timestamp, close FROM price_history
WHERE pair = '" . $pair['pair'] . "'
AND source = '" . $pair['source'] . "'
AND period = '" . $period_txt . "'
AND timestamp >= " . $start . "
AND timestamp <= " . $stop . "
ORDER BY timestamp ASC
";
$hist = query($query, $config);
if ($config['debug']) echo $query . PHP_EOL;
if (empty($hist)) continue 1;

$last = $hist[0]['timestamp'] - $period;
$missing_data = FALSE;
$i = 0;
foreach ($hist as $h) {
$diff_obs = $h['timestamp'] - $last;
if ($period !== $diff_obs) {
$missing_data = TRUE;
$missing_obs = $h['timestamp'] - $period;
$missing_array = $i;
}
$last = $h['timestamp'];
$i++;
}
if ($missing_data) {
echo '
missing data at array position ' . $missing_array . ', <br>
time ' . date('Y-m-d H:i T', $missing_obs / 1000) . ', <br>
timestamp ' . $missing_obs . ', <br>
period ' . $period . '<br>
';
var_dump($hist[$missing_array - 1]);
var_dump($hist[$missing_array]);
continue 1;
}

$count = count($hist);
$dc = array_column($hist, 'close');

foreach ($rsi as $n) {
$r = trader_rsi($dc, $n);
$j = 0;
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i])) {
$hist[$i]['rsi'.$n] = $r[$i];
if (isset($r[$j])) {
if ($r[$j] <= $rsiob && $r[$i] > $rsiob)
$hist[$i]['rsi'.$n.'ob'] = 1;
else
$hist[$i]['rsi'.$n.'ob'] = 0;
if ($r[$j] >= $rsios && $r[$i] < $rsios)
$hist[$i]['rsi'.$n.'os'] = 1;
else
$hist[$i]['rsi'.$n.'os'] = 0;
}
$j = $i;
}
$j = $i;
}

foreach($ema as $n) {
$r = trader_ema($dc, $n);
$j = 0;
for($i = 0; $i < $count; $i++) {
if(isset($r[$i])) {
$hist[$i]['ema'.$n] = $r[$i];
if(isset($r[$j])) {
if($dc[$j] <= $r[$j] && $dc[$i] > $r[$i]) {
$hist[$i]['ema'.$n.'cu'] = 1; // crossover upwards
} else {
$hist[$i]['ema'.$n.'cu'] = 0;
}
if($dc[$j] >= $r[$j] && $dc[$i] < $r[$i]) {
$hist[$i]['ema'.$n.'cd'] = 1; // crossover downwards
} else {
$hist[$i]['ema'.$n.'cd'] = 0;
}
}

foreach ($ema as $n) {
$r = trader_ema($dc, $n);
$j = 0;
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i])) {
$hist[$i]['ema'.$n] = $r[$i];
if (isset($r[$j])) {
if ($dc[$j] <= $r[$j] && $dc[$i] > $r[$i]) {
$hist[$i]['ema'.$n.'cu'] = 1; // crossover upwards
} else {
$hist[$i]['ema'.$n.'cu'] = 0;
}
if ($dc[$j] >= $r[$j] && $dc[$i] < $r[$i]) {
$hist[$i]['ema'.$n.'cd'] = 1; // crossover downwards
} else {
$hist[$i]['ema'.$n.'cd'] = 0;
}
}
$j = $i;
}
$j = $i;
}
}

foreach($roc as $n) {
$r = trader_roc($dc, $n);
for($i = 0; $i < $count; $i++) {
if(isset($r[$i]))
$hist[$i]['roc'.$n] = $r[$i];
}
foreach ($roc as $n) {
$r = trader_roc($dc, $n);
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i]))
$hist[$i]['roc'.$n] = $r[$i];
}

foreach($corr as $key => $array) {
$query = "SELECT DISTINCT pair_id, pair, source, currency, history_start FROM asset_pairs
WHERE analyse
AND period = " . $pair['period'] . "
AND pair LIKE '%" . $key . "/USD%'";
}

foreach ($corr as $key => $array) {
$query = "
SELECT DISTINCT pair_id, pair, source, currency, history_start FROM
WHERE analyse
AND period = " . $pair['period'] . "
AND pair LIKE '%" . $key . "/USD%'
";
$res = query($query, $config);
if (!empty($res)) {
$query = "
SELECT timestamp, close FROM price_history
WHERE pair = '" . $res[0]['pair'] . "'
AND source = '" . $res[0]['source'] . "'
AND period = '" . $period_txt . "'
AND timestamp >= " . $hist[0]['timestamp'] . "
AND timestamp <= " . $hist[$count-1]['timestamp'] . "
ORDER BY timestamp ASC
";
$res = query($query, $config);
if(!empty($res)) {
$query = "SELECT timestamp, close FROM price_history
WHERE pair = '" . $res[0]['pair'] . "'
AND source = '" . $res[0]['source'] . "'
AND period = '" . $period_txt . "'
AND timestamp >= " . $hist[0]['timestamp'] . "
AND timestamp <= " . $hist[$count-1]['timestamp'] . "
ORDER BY timestamp ASC";
$res = query($query, $config);
if(!empty($res) && count($res) == $count) {
$cc = array_column($res, 'close');
foreach($array as $n) {
$r = trader_correl($dc, $cc, $n);
for($i = 0; $i < $count; $i++) {
if(isset($r[$i]))
$hist[$i]['corr'.$n.$key] = $r[$i];
}
if (!empty($res) && count($res) == $count) {
$cc = array_column($res, 'close');
foreach ($array as $n) {
$r = trader_correl($dc, $cc, $n);
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i]))
$hist[$i]['corr'.$n.$key] = $r[$i];
}
}
}
}

$omit = array('history_id', 'timestamp', 'open', 'high', 'low', 'close', 'volume');
foreach($hist as $h) {
$sep = '';
$query = "UPDATE price_history SET ";
foreach($h as $key => $val) {
if(!in_array($key, $omit, TRUE)) {
$query .= $sep . $key . " = '" . $val . "'";
$sep = ',';
}
}

$omit = array('history_id', 'timestamp', 'open', 'high', 'low', 'close', 'volume');
foreach ($hist as $h) {
$sep = '';
$query = "UPDATE price_history SET ";
foreach ($h as $key => $val) {
if (!in_array($key, $omit, TRUE)) {
$query .= $sep . $key . " = '" . $val . "'";
$sep = ',';
}
$query .= " WHERE history_id = " . $h['history_id'];
query($query, $config);
//die;
}
$query .= " WHERE history_id = " . $h['history_id'];
query($query, $config);
}

}

Expand Down
Loading

0 comments on commit 4832793

Please sign in to comment.