Skip to content

Instantly share code, notes, and snippets.

@FernandoEscher
Forked from chenosaurus/buy.js
Last active November 28, 2017 12:54
Show Gist options
  • Select an option

  • Save FernandoEscher/5103601 to your computer and use it in GitHub Desktop.

Select an option

Save FernandoEscher/5103601 to your computer and use it in GitHub Desktop.

Revisions

  1. FernandoEscher revised this gist Mar 7, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion buy.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,6 @@ var quantity = 10;
    var expectedPrice = 44.00

    var jsonData = { qty: quantity };
    var currentPrice = 0.0

    function onComplete(data, res) {
    console.log(new Date().toString());
  2. FernandoEscher revised this gist Mar 6, 2013. 1 changed file with 17 additions and 3 deletions.
    20 changes: 17 additions & 3 deletions buy.js
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,13 @@
    var sys = require('util'),
    rest = require('restler');

    //set these to your coinbase API key & the amount you want to buy
    //set these to your coinbase API key, the amount you want to buy & the price that you're expecting for
    var apiKey = 'xxx';
    var quantity: 10;
    var quantity = 10;
    var expectedPrice = 44.00

    var jsonData = { qty: quantity };
    var currentPrice = 0.0

    function onComplete(data, res) {
    console.log(new Date().toString());
    @@ -25,7 +27,19 @@ function onComplete(data, res) {
    };

    function buy() {
    rest.postJson('https://coinbase.com/api/v1/buys?api_key=' + apiKey, jsonData).on('complete', onComplete);
    // Will check the price until it reaches the expected value
    rest.get('https://coinbase.com/api/v1/prices/buy').on('complete', function(data){
    console.log("Current price: "+data.amount+". Expected price: "+expectedPrice)

    if(data.amount <= expectedPrice){
    rest.postJson('https://coinbase.com/api/v1/buys?api_key=' + apiKey, jsonData).on('complete', onComplete);
    }else{
    setTimeout(buy, 60000)
    }

    return
    });

    }

    buy();
  3. @chenosaurus chenosaurus revised this gist Mar 6, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions buy.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    //you will need to install node.js and restler first
    //npm install restler

    //run with the following command
    // node buy.js

    var sys = require('util'),
    rest = require('restler');

  4. @chenosaurus chenosaurus renamed this gist Mar 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @chenosaurus chenosaurus created this gist Mar 6, 2013.
    28 changes: 28 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    //you will need to install node.js and restler first
    //npm install restler

    var sys = require('util'),
    rest = require('restler');

    //set these to your coinbase API key & the amount you want to buy
    var apiKey = 'xxx';
    var quantity: 10;

    var jsonData = { qty: quantity };

    function onComplete(data, res) {
    console.log(new Date().toString());
    if (!data.success) {
    console.log(data.errors);
    setTimeout(buy, 5000);
    } else {
    console.log("SUCCESS!");
    }

    };

    function buy() {
    rest.postJson('https://coinbase.com/api/v1/buys?api_key=' + apiKey, jsonData).on('complete', onComplete);
    }

    buy();