PHP Bittrex alert bot

Here’s a a little script I wrote to send me alerts when the prices of certain alt coins pass defined points. I have it running on a cron job and it creates a log file instead of using sessions in order to limit repeat alerts to once an hour. The directory will need to be writable for the log file. Obviously you have to set the alert points manually in the PHP but if anyone is interested I can set this up as a service with a web interface. It also sends an alert when new markets are added to Bittrex, never miss a pump and dump again!

https://github.com/ekittell/bittrex_alert


$BittrexAlerts = new BittrexAlerts();
$BittrexAlerts->run();

Class BittrexAlerts {
	private $sms = "2125551212@txt.att.net"; //check with your mobile carrier
	private $email = "your@email.com"; // alert sent to
	private $from_email = "your@domain.com"; // from address for all alerts
	private $identitifier = "BITTREX-ALERT"; // use to identify messages for smart mail boxes

	private $alerts = array(
		//bittrex
		"BTC-AERO" 		=> array(.00004200,.00005000),
		// c-cex
		"dcn-btc"		=> array(.00000200, .00000200)
	); 



	private $log = array();
	private $log_file;

	public function __construct() {
		// create log file with empty array
		$this->log_file = dirname(__FILE__) . "/alerts.log";

		if(!file_exists($this->log_file))
			file_put_contents($this->log_file, serialize(array())) or die("can't create file");

		// load current log
		$this->log = unserialize(file_get_contents($this->log_file));

	}

	private function send_alert($subject, $message) {
		$headers = 'From: ' . $this->from_email . "\r\n" .
	    	'X-Mailer: PHP/' . phpversion();

		$subject = $this->identitifier . " : " . $subject;

		mail($this->sms, $subject, $message, $headers) or die('cannot send sms');
		mail($this->email, $subject, $message, $headers) or die('cannot send mail');
	}

	private function log_alert($alert_name, $repeat = true) {
		// log alert and return true if last alert was over an hour ago
		$limit = time() - (60 * 60);
		if(!isset($this->log[$alert_name]) || ($this->log[$alert_name] < $limit && $repeat)) {
			$this->log[$alert_name] = time();
			file_put_contents($this->log_file, serialize($this->log));
			return true;
		}

		return false;
	}

	public function run() {
		$markets = array();
		$lt_alerts = array();
		$gt_alerts = array();

		foreach($this->alerts as $market => $alert) {
			$markets[] = $market;
	    	$lt_alerts[$market] = $alert[0];
	    	$gt_alerts[$market] = $alert[1];
	    }


		$bittrex_summaries = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getmarketsummaries')); // BITTREX API call

		foreach($bittrex_summaries->result as $summary) {
			// Check for new Markets
			$limit = time() - (60 * 60);
			if(strtotime($summary->Created) >= $limit) {
				echo $summary->Created . "
"; $subject = "NEW market!"; $message = $summary->MarketName; if($this->log_alert("NEW-".$summary->MarketName, false)) $this->send_alert($subject, $message); } if(in_array($summary->MarketName, $markets)) { // Check if markets have droppped below alert points if($summary->Last <= $lt_alerts[$summary->MarketName]) { $subject = "market LOW!"; $message = $summary->MarketName ." at " . number_format($summary->Last, 8); if($this->log_alert("LOW-".$summary->MarketName)) $this->send_alert($subject, $message); } // Check if markets have gone above alert points if($summary->Last >= $gt_alerts[$summary->MarketName]) { $subject = "market HI!"; $message = $summary->MarketName ." at " . number_format($summary->Last, 8); if($this->log_alert("HI-".$summary->MarketName)) $this->send_alert($subject, $message); } } } $ccex_summaries = json_decode(file_get_contents('https://c-cex.com/t/prices.json')); // C-CEX API call foreach($ccex_summaries as $name => $summary) { if(in_array($name, $markets)) { // Check if markets have droppped below alert points if($summary->lastprice <= $lt_alerts[$name]) { $subject = "market LOW!"; $message = $name ." at " . number_format($summary->lastprice, 8); if($this->log_alert("LOW-".$name)) $this->send_alert($subject, $message); } // Check if markets have gone above alert points if($summary->lastprice >= $gt_alerts[$name]) { $subject = "market HI!"; $message = $name ." at " . number_format($summary->lastprice, 8); if($this->log_alert("HI-".$name)) $this->send_alert($subject, $message); } } } } }

Leave a Reply