Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- Raspberry Pi or some Linux box
- php5 or up
- [composer](https://getcomposer.org/)
- (Optional) A Rocket.Chat or Slack webhook which will inform you whenever BTC/ETH are sold or bought
- (Optional) A Rocket.Chat, Slack or Pushover webhook which will inform you whenever BTC/ETH are sold or bought

## Install
1. Download the repo by using ```git clone https://github.com/chrisiaut/phptrader.git``` or download as [ZIP file](https://github.com/chrisiaut/phptrader/archive/master.zip)
Expand Down Expand Up @@ -45,4 +45,18 @@
The heart of the bot is an infinite loop that checks periodically for price changes.
You can start it yourself or use the ```start.sh``` script which will put the process in background and log to ```/var/log/phptrader.log```

```./start.sh```
```./start.sh```

### Using SystemD
Instead of the nohup start Script you can add the phpTrader to SystemD. For this purpose just copy the example.systemd.service File.

```cp example.systemd.service /lib/systemd/system/phpTrader.service```

After this please edit the phpTrader.service File and change the Lines "user / group / ExecStart" fitting to your Setup needs, when done reload the systemd and start:

```
systemctl daemon-reload
systemctl enable phpTrader
systemctl start phpTrader
```

7 changes: 6 additions & 1 deletion example.config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
define('SLEEPTIME',10);

// chat system output (rocket.chat and Slack supported)
define('ROCKETCHAT_REPORTING',true); //enables/disables rocketchat reporting
define('ROCKETCHAT_REPORTING',false); //enables/disables rocketchat reporting
define('ROCKETCHAT_WEBHOOK',''); // The URL of the webhook. Also works with a Slack webhook!

// secure mobile notifications by pushover (https://pushover.net)
define('PUSHOVER_REPORTING',false); //enables/disables rocketchat reporting
define('PUSHOVER_TOKEN',''); // Pushover App token
define('PUSHOVER_USER',''); // Pushover User API Key

// Coinbase
define('COINBASE_KEY','');
define('COINBASE_SECRET','');
Expand Down
36 changes: 36 additions & 0 deletions example.systemd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# name: phpTrader.service
# 1. copy/rename this file into: '/lib/systemd/system/phpTrader.service'
# 2. change user/group and ExecStart matching your setup
# 3. then run: 'systemctl daemon-reload'
# 4. Enable by 'systemctl enable phpTrader'
# 5. start by 'systemctl start phpTrader'
# status by 'systemctl status phpTrader'

[Unit]
Description=phpTrader Service
After=local-fs.target network.target

[Service]
Type=simple
User=myphptraderuser
Group=myphptradergroup
SyslogIdentifier=phpTrader
StandardOutput=syslog
ExecStart=/usr/bin/php /opt/phptrader/trader.php watchdog 2>&1
ReadOnlyDirectories=/etc
ReadOnlyDirectories=/usr
ReadOnlyDirectories=/proc
ReadOnlyDirectories=/sys
ReadOnlyDirectories=/sbin
ReadOnlyDirectories=/lib
ReadOnlyDirectories=/lib32
ReadOnlyDirectories=/lib64
InaccessibleDirectories=/boot
InaccessibleDirectories=/home
Nice=10
KillMode=process
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
66 changes: 56 additions & 10 deletions trader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
use Coinbase\Wallet\Resource\Buy;
use Coinbase\Wallet\Value\Money;

if(empty($argv[1])) $argv[1] = false;
$t = new trader(($argv[1]==='debug'?true:false));

$myname = $argv[0];




switch($argv[1])
{
Expand Down Expand Up @@ -78,6 +82,11 @@
$t->debug();
break;

case 'pushovertest':
var_dump(sendToPushover("test"));
break;


default:
echo "Usage info\n---------------\n";
echo "php $myname buy <amount in ".CURRENCY."> <sell when price increases by ".CURRENCY.">\n";
Expand Down Expand Up @@ -325,7 +334,7 @@ function addBuyTransaction($eur,$buyat,$sellat)
$id = @max(array_keys($this->transactions))+1;
$this->transactions[$id] = array('eur'=>$eur,'buyprice'=>$buyat,'sellat'=>$sellat);
$this->saveTransactions();
if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Added BUY order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$buyat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($this->sellPrice-$buyat).' '.CURRENCY.'* to go',':raised_hands:');
sendMessage("Added BUY order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$buyat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($this->sellPrice-$buyat).' '.CURRENCY.'* to go',':raised_hands:');
}

function addSellTransaction($eur,$sellat)
Expand All @@ -335,7 +344,7 @@ function addSellTransaction($eur,$sellat)
$id = @max(array_keys($this->transactions))+1;
$this->transactions[$id] = array('eur'=>$eur,'sellat'=>$sellat);
$this->saveTransactions();
if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Added SELL order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$sellat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($sellat-$this->sellPrice).' '.CURRENCY.'* to go',':raised_hands:');
sendMessage("Added SELL order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$sellat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($sellat-$this->sellPrice).' '.CURRENCY.'* to go',':raised_hands:');
}

/*
Expand Down Expand Up @@ -394,8 +403,7 @@ function buyBTC($amount,$sellat,$btc=false)

echo "[B #$id] Buying $eur €\t=\t$btc ".CRYPTO."\n";

if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Buying *$btc ".CRYPTO."* for *$eur ".CURRENCY."*",':moneybag:');

sendMessage("Buying *$btc ".CRYPTO."* for *$eur ".CURRENCY."*",':moneybag:');
$this->saveTransactions();

return $id;
Expand All @@ -410,7 +418,7 @@ function sellBTCID($id)

$profit = round(($data['btc']*$this->sellPrice)-($data['btc']*$data['buyprice']),2);

if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Selling *".$data['btc']." ".CRYPTO."* for *".$data['eur']." ".CURRENCY."*. Profit: *$profit ".CURRENCY."*",':money_with_wings:');
sendMessage("Selling *".$data['btc']." ".CRYPTO."* for *".$data['eur']." ".CURRENCY."*. Profit: *$profit ".CURRENCY."*",':money_with_wings:');
}

function sellBTC($amount,$btc=false)
Expand All @@ -429,6 +437,7 @@ function sellBTC($amount,$btc=false)
if($this->checkBalanceOfAccount($this->account)<$btc)
{
echo " [ERR] You don't have enough ".CRYPTO." in your '".$this->account->getName()."'. Cancelling sell\n";
sendMessage(" [ERR] You don't have enough ".CRYPTO." in your '".$this->account->getName()."'. Cancelling sell\n",":no_entry:");
return;
}
else
Expand Down Expand Up @@ -477,7 +486,7 @@ function listTransactions()

function watchdog()
{
if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Starting watchdog",':wave:');
sendMessage("Starting watchdog",':wave:');
while(1)
{
$this->mainCheck();
Expand Down Expand Up @@ -519,7 +528,7 @@ function mainCheck()
$btc = (1/$this->sellPrice) * $eur;
$this->deleteTransaction($id);
$this->sellBTC($btc,true);
if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Selling *".$btc." ".CRYPTO."* for *".$eur." ".CURRENCY."*. Forefilling and deleting this sell order.",':money_with_wings:');
sendMessage("Selling *".$btc." ".CRYPTO."* for *".$eur." ".CURRENCY."*. Forefilling and deleting this sell order.",':money_with_wings:');
}
else
echo " [#$id] Watching SELL order for \t$eur ".CURRENCY.". Will sell when ".CRYPTO." price reaches ".$td['sellat']." ".CRYPTO.".\n";
Expand Down Expand Up @@ -559,7 +568,7 @@ function report()
$out = ob_get_contents();
ob_end_clean();

sendToRocketchat($out,':information_source:');
sendMessage($out,':information_source:');
}

function autotrade($stake=10,$sellpercent=115)
Expand Down Expand Up @@ -611,11 +620,20 @@ function autotrade($stake=10,$sellpercent=115)

}

// a general message function to handle different Message protocols
function sendMessage($message,$icon=':ghost:')
{
if(!empty($message)) {
if(ROCKETCHAT_REPORTING===true) sendToRocketchat($message,$icon);
if(PUSHOVER_REPORTING===true) sendToPushover($message);
}
}


//rocketchat
function sendToRocketchat($message,$icon=':ghost:')
{
$username = CURRENCY.' - '.CRYPTO.' trader'.(SIMULATE===true?' (simulation)':'');
$username = CURRENCY.' - '.CRYPTO.' trader'.(SIMULATE===true?' (simulation)':'');
$data = array("icon_emoji"=>$icon,
"username"=>$username,
"text"=>$message);
Expand All @@ -636,4 +654,32 @@ function makeRequest($url,$data,$headers=false,$post=true)
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
return json_decode($result,true);
}
}


//pushover
function sendToPushover($message)
{
if( empty(PUSHOVER_TOKEN) || empty(PUSHOVER_USER) || empty($message) ) return false;

$username = CURRENCY.' - '.CRYPTO.' trader'.(SIMULATE===true?' (simulation)':'');
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token"=>PUSHOVER_TOKEN,
"user"=>PUSHOVER_USER,
"priority"=>-1,
"title"=>$username,
"message"=>$message),
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_RETURNTRANSFER => true,
));
$RET = curl_exec($ch);
curl_close($ch);
return $RET;
}