HitBTC Price Monitor

in #crypto4 years ago (edited)

image.png

Javascript & PHP to monitor cryptocurrency prices on HitBTC

If you have an important trade you wish to monitor and be notified by a sound when the last trade matched or exceeded the amount specified the following will achieve this:

First: Install WAMP Server (Free): https://sourceforge.net/projects/wampserver/

This is required to avoid "CORS":

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.

The HTML & JavaScript below call a PHP file which calls: https://api.hitbtc.com/api/2/public/ticker/DGBBTC

Load this to see the data returned. You can adjust the "DGBBTC" to match any coin/currency you wish to monitor for your trade. DGB is the cryptocurrency and BTC is the currency pair in this example.

Create a folder "pricewatch" in the wamp "www" folder. Place the following files [index.htm, getprice.php and any two sound files (adjust the sound files in the html)] in the "pricewatch" folder.

In the html file (file 1 of 2) below you can set your sale price in the variable: fPrice

The sounds can be downloaded here:

https://www.soundsnap.com/tags/whoop_there_it_is
https://www.soundsnap.com/search/audio/error/score

A) Create a HTML file called "index.htm" as per below:

<!DOCTYPE html>
<html lang="en">
<head>
<title>HitBTC Price Monitor</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>

<audio id="pSoldSound" controls="none">
<source src="Crowd Cheering Exterior- Smaller Swell 1- Hollywood Bowl- A.mpeg" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

<audio id="pSoldSoundErr" controls="none">
<source src="Sound design multimedia accent buzz error 02_SFXBible_ss027.mpeg" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

Last Price: <div id="lastprice" class="mylastprice">...</div><br>
Polls: <div class="mypolls">0</div>

<script>

//poll interval in milliseconds
var PollInterval = 10000;

//sound to play when lastprice >= sell price
var pSoldSound = document.getElementById("mySoldSound");
pSoldSound.controls = false;

//sound to be played when sales price not reached
var mySoldSoundErr = document.getElementById("mySoldSoundErr");
mySoldSoundErr.controls = false;

//count of price checks/polls
var iPolls = 0;

function GetPrice()
{
    $.getJSON('getprice.php', function(data)
    {
        iPolls++;
        var fLast = parseFloat(data.last);
        var fPrice = 0.0000025608; //set your sell price amount here
        if (fLast >= fPrice)
        {
            pSoldSound.play();
        }
        else
        {
            pSoldSoundErr.play();
        }
        $(".mylastprice").html(data.last); //update last price
        $(".mypolls").html(iPolls); //update last poll count
    });
}

function refresh()
{
    GetPrice();
    setTimeout(refresh, PollInterval);
}

//initial call, or just call refresh directly
//poll interval set to 10 seconds in this example
setTimeout(refresh, PollInterval);

</script>
</body>
</html>

B) Create "getprice.php" in a text editor and place next to index.html. This is the code that returns the lastprice from hitbtc to the html file.

<?php

function GetJason($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $sz = curl_exec($ch);
    curl_close($ch);
    return $sz;
}

$url = "https://api.hitbtc.com/api/2/public/ticker/DGBBTC";

echo GetJason($url);
?>

C) Launch WAMP and load in your browser: http://localhost/pricewatch/index.htm

*** Click the page when loaded *** to activate it and then sit back and wait for your sale to go through!

Set you computer power options to 'always on' or presentation mode for 24/7 monitoring.