Skip to content

Instantly share code, notes, and snippets.

@subratrout
Forked from levelsio/btc-eth-dca-buy.php
Created August 24, 2017 22:41
Show Gist options
  • Save subratrout/602662b3be1b5043be80ad920dc8f6f9 to your computer and use it in GitHub Desktop.
Save subratrout/602662b3be1b5043be80ad920dc8f6f9 to your computer and use it in GitHub Desktop.
This script runs daily and "Dollar Cost Average"-buys $40 BTC and $10 ETH per day
<?
//
// [ BUY BTC & ETH DAILY ON BITSTAMP ]
// by @levelsio
//
// 2017-08-23
//
// 1) buy $40/day BTC
// 2) buy $10/day ETH
//
// add to CRON:
//
// @daily php btc-eth-dca-buy.php
//
// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// find credentials on Bitstamp
$customerID='YOUR_ID';
$bitstampKey='YOUR_KEY';
$bitstampSecret='YOUR_SECRET';
// change values if you like
$btcToBuyInUSD=40;
$ethToBuyInUSD=10;
// <BTC>
// get price
echo "Getting latest BTC price...";
$url='https://www.bitstamp.net/api/ticker/';
$reply=json_decode(file_get_contents($url),true);
$btc_price=$reply['ask'];
echo '$'.$btc_price;
echo "\n";
// if BTC price outlier, exit
if($btc_price<1000 || $btc_price>10000) {
echo "Exiting because BTC price either <$1,000 or >$10,000";
echo "\n";
exit;
}
$btcToBuy=round($btcToBuyInUSD/$btc_price,8);
echo "Buying $".$btcToBuyInUSD." of BTC, which equals ".$btcToBuy." BTC";
echo "\n";
$url='https://www.bitstamp.net/api/v2/buy/market/btcusd/';
$mt = explode(' ', microtime());
$nonce=$mt[1] . substr($mt[0], 2, 6);
$fields = array(
'key' => urlencode($bitstampKey),
'signature' => urlencode(generateBitstampSignature($nonce)),
'nonce' => urlencode($nonce),
'amount' => urlencode($btcToBuy),
);
//url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
if(!$result) {
echo curl_error($ch);
exit;
}
//close connection
curl_close($ch);
echo json_encode($result);
echo "\n\n";
// </BTC>
// <ETH>
// get price
echo "Getting latest ETH price...";
$url='https://www.bitstamp.net/api/v2/ticker/ethusd';
$reply=json_decode(file_get_contents($url),true);
$eth_price=$reply['ask'];
echo '$'.$eth_price;
echo "\n";
// if BTC price outlier, exit
if($eth_price<100 || $eth_price>5000) {
echo "Exiting because ETH price either <$100 or >$5,000";
echo "\n";
exit;
}
$ethToBuy=round($ethToBuyInUSD/$eth_price,8);
echo "Buying $".$ethToBuyInUSD." of ETH, which equals ".$ethToBuy." BTC";
echo "\n";
$url='https://www.bitstamp.net/api/v2/buy/market/ethusd/';
$mt = explode(' ', microtime());
$nonce=$mt[1] . substr($mt[0], 2, 6);
$fields = array(
'key' => urlencode($bitstampKey),
'signature' => urlencode(generateBitstampSignature($nonce)),
'nonce' => urlencode($nonce),
'amount' => urlencode($ethToBuy),
);
//url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
if(!$result) {
echo curl_error($ch);
exit;
}
//close connection
curl_close($ch);
echo json_encode($result);
echo "\n\n";
// </BTC>
function generateBitstampSignature($nonce)
{
global $bitstampKey;
global $bitstampSecret;
global $customerID;
$message = $nonce . $customerID . $bitstampKey;
return strtoupper(hash_hmac('sha256', $message, $bitstampSecret));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment