Last active
July 23, 2022 12:37
-
-
Save kloon/2977ba9c72d0fcc7cdddc0e75527b02b to your computer and use it in GitHub Desktop.
Luno.com "Dollar Cost Avg" daily buy script for BTC & ETH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Buy BTC & ETH daily on Luno.com | |
* @author Gerhard Potgieter <[email protected]> | |
* @since 2017-12-07 | |
* https://www.luno.com/invite/JN5Z4 | |
* | |
* This script will do a daily purchase of BTC and ETH on Luno.com based on rand value (ZAR) set. Default to R100 each. Why daily? https://en.wikipedia.org/wiki/Dollar_cost_averaging | |
* | |
* Installation Instructions | |
* Add to Crontab: | |
* @daily php btc-eth-luno-buy.php | |
* | |
* Fill in the $key_id, $key_secret variables as obtained from your Luno account, enter the values you would like to purchase daily in ZAR. | |
*/ | |
// Generate API Keys on Luno @ https://www.luno.com/wallet/settings/api_keys Be sure to select trading permission level. | |
$key_id = 'YOUR_ID'; | |
$key_secret = 'YOUR_SECRET'; | |
// ZAR values to buy. Luno minumum is 10 ZAR. | |
$btc_in_zar = 0; | |
$eth_in_zar = 0; | |
// Auth headers. | |
$auth_headers = stream_context_create( array( | |
'http' => array( | |
'header' => "Authorization: Basic " . base64_encode( $key_id . ':' . $key_secret ), | |
), | |
) ); | |
/** | |
* Account balances. | |
*/ | |
echo "Fetching Luno account balances...\n"; | |
$url = 'https://api.mybitx.com/api/1/balance'; | |
$reply = json_decode( file_get_contents( $url, false, $auth_headers ), true ); | |
$eth_balance = 0; | |
$xbt_balance = 0; | |
$zar_balance = 0; | |
if ( is_array( $reply ) && isset( $reply['balance'] ) ) { | |
foreach ( $reply['balance'] as $balance ) { | |
${strtolower( $balance['asset'] ) . '_balance'} = $balance['balance']; | |
echo $balance['asset'] . ': ' . $balance['balance'] . "\n"; | |
} | |
} else { | |
echo "Failed to fetch account balances.\nQuiting...\n"; | |
exit; | |
} | |
/** | |
* Account balance check, make sure there is enought ZAR to buy. | |
*/ | |
if ( $zar_balance < ( $btc_in_zar + $eth_in_zar ) ) { | |
echo "You do not have enought ZAR in your Luno account to purchase BTC & ETH.\nQuitting..."; | |
exit; | |
} | |
/** | |
* BTC Purchasing. Minimum is 10 ZAR. | |
*/ | |
if ( 10 <= $btc_in_zar ) { | |
// Get BTC Price. | |
echo "Getting BTC price from Luno...\n"; | |
$url = 'https://api.mybitx.com/api/1/ticker?pair=XBTZAR'; | |
$reply = json_decode( file_get_contents( $url ), true ); | |
$btc_price = $reply['ask']; | |
echo "BTC Price: ZAR " . $btc_price . "\n"; | |
// Create a quote on Luno | |
$postdata = http_build_query( | |
array( | |
'type' => 'SELL', | |
'base_amount' => $btc_in_zar, | |
'pair' => 'ZARXBT' | |
) | |
); | |
$options = array( 'http' => | |
array( | |
'method' => 'POST', | |
'header' => "Authorization: Basic " . base64_encode( $key_id . ':' . $key_secret ) . "\nContent-type: application/x-www-form-urlencoded", | |
'content' => $postdata | |
) | |
); | |
$context = stream_context_create( $options ); | |
$url = 'https://api.mybitx.com/api/1/quotes'; | |
$reply = json_decode( file_get_contents( $url, false, $context ), true ); | |
if ( is_array( $reply ) && isset( $reply['id'] ) ) { | |
$quote_id = $reply['id']; | |
echo "Purchasing " . $reply['counter_amount'] . "BTC for " . $btc_in_zar . "\n"; | |
// Exercise quote | |
$options = array( 'http' => | |
array( | |
'method' => 'PUT', | |
'header' => "Authorization: Basic " . base64_encode( $key_id . ':' . $key_secret ), | |
) | |
); | |
$context = stream_context_create( $options ); | |
$url = 'https://api.mybitx.com/api/1/quotes/' . $quote_id; | |
$reply = json_decode( file_get_contents( $url, false, $context ), true ); | |
if ( is_array( $reply ) && isset( $reply['exercised'] ) && 'true' === $reply['exercised'] ) { | |
echo "Purchase complete, you now own " . $reply['base_amount'] . " more BTC.\n"; | |
} elseif ( isset( $reply['error'] ) ) { | |
echo "Error exercising BTC buy quote: " . $reply['error'] . "\nQuitting..."; | |
exit; | |
} else { | |
echo "Something went wrong.\nQuitting..."; | |
exit; | |
} | |
} elseif ( is_array( $reply ) && isset( $reply['error'] ) ) { | |
echo "Error creating BTC buy quote: " . $reply['error'] . "\nQuitting..."; | |
exit; | |
} else { | |
echo "Something went wrong.\nQuitting..."; | |
exit; | |
} | |
} | |
/** | |
* ETH Purchasing. Minimum is 10 ZAR. | |
*/ | |
if ( 10 <= $eth_in_zar ) { | |
// Get ETH Price. | |
echo "Getting ETH price from Luno...\n"; | |
$url = 'https://api.mybitx.com/api/1/ticker?pair=ETHZAR'; | |
$reply = json_decode( file_get_contents( $url ), true ); | |
$eth_price = $reply['ask']; | |
echo "ETH Price: ZAR " . $eth_price . "\n"; | |
// Create a quote on Luno | |
$postdata = http_build_query( | |
array( | |
'type' => 'SELL', | |
'base_amount' => $eth_in_zar, | |
'pair' => 'ZARETH' | |
) | |
); | |
$options = array( 'http' => | |
array( | |
'method' => 'POST', | |
'header' => "Authorization: Basic " . base64_encode( $key_id . ':' . $key_secret ) . "\nContent-type: application/x-www-form-urlencoded", | |
'content' => $postdata | |
) | |
); | |
$context = stream_context_create( $options ); | |
$url = 'https://api.mybitx.com/api/1/quotes'; | |
$reply = json_decode( file_get_contents( $url, false, $context ), true ); | |
if ( is_array( $reply ) && isset( $reply['id'] ) ) { | |
$quote_id = $reply['id']; | |
echo "Purchasing " . $reply['counter_amount'] . "ETH for " . $btc_in_zar . "\n"; | |
// Exercise quote | |
$options = array( 'http' => | |
array( | |
'method' => 'PUT', | |
'header' => "Authorization: Basic " . base64_encode( $key_id . ':' . $key_secret ), | |
) | |
); | |
$context = stream_context_create( $options ); | |
$url = 'https://api.mybitx.com/api/1/quotes/' . $quote_id; | |
$reply = json_decode( file_get_contents( $url, false, $context ), true ); | |
if ( is_array( $reply ) && isset( $reply['exercised'] ) && 'true' === $reply['exercised'] ) { | |
echo "Purchase complete, you now own " . $reply['base_amount'] . " more ETH.\n"; | |
} elseif ( isset( $reply['error'] ) ) { | |
echo "Error exercising ETH buy quote: " . $reply['error'] . "\nQuitting..."; | |
exit; | |
} else { | |
echo "Something went wrong.\nQuitting..."; | |
exit; | |
} | |
} elseif ( is_array( $reply ) && isset( $reply['error'] ) ) { | |
echo "Error creating ETH buy quote: " . $reply['error'] . "\nQuitting..."; | |
exit; | |
} else { | |
echo "Something went wrong.\nQuitting..."; | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i link my luno wallet address with sagwitX2 to generate address