Gemini API - Practical Assignment

@ivan I’m getting this error

result: ‘error’,
reason: ‘InvalidNonce’,
message: “Nonce ‘1597150337278’ has not increased since your last call to the Gemini API.”

I thinks it’s because I have 5 orders back to back. Maybe since the order is left in the order book it didn’t increment the nonce?

restClient.newOrder({amount:10, price: 28, side:“sell”, symbol:“ethusd”}).then(response => console.log(response)).catch(error => console.log(error));

restClient.newOrder({amount:100, price: 9, side:“buy”, symbol:“ethusd”}).then(response => console.log(response)).catch(error => console.log(error));

restClient.newOrder({limit:“Fill-or-Kill”,amount:100, price: 28, side:“sell”, symbol:“ethusd”}).then(response => console.log(response)).catch(error => console.log(error));

restClient.newOrder({amount:10, price: 28, side:“buy”, symbol:“ltcusd”}).then(response => console.log(response)).catch(error => console.log(error));

restClient.newOrder({amount:10, price: 28, side:“buy”, symbol:“zecusd”}).then(response => console.log(response)).catch(error => console.log(error));

restClient.newOrder({amount:10, price: 0.00005, side:“buy”, symbol:“ethbtc”}).then(response => console.log(response)).catch(error => console.log(error));;

Hi! I get this error on my Mac in Terminal after running: node index.js.

{ result: 'error',
  reason: 'MissingAccounts',
  message: 'Expected a JSON payload with accounts' }

My code is:

const GeminiAPI = require("gemini-api").default;
const secret = "...";
const key = "...";
const CCAPIKey = "..."
const restClient = new GeminiAPI({key, secret, sandbox:true});

restClient.getMyAvailableBalances()
.then(response => console.log(response))
.catch(console.error);

Previously I was able to successfully implement a buy order:

restClient.newOrder({amount:10, price:10, side:"buy", symbol:"ethusd", account:"primary"})
.then(response => console.log(response))
.catch(console.error);

It seems I have to add account information somehow to the getMyAvailableBalances(), but not sure how to do it. Please help! :slight_smile:

  1. Use getMyAvailableBalances to see your current balances.
  2. Create a few (3-5) different buy limit orders using newOrder function. Make sure that the price is too low for Gemini to fulfil your buy order.
  3. Using getMyActiveOrders get a list of all your active orders. Do you recognise the orders you created in the last step?
  4. Cancel your orders using cancelAllActiveOrders .
  5. Using getMyActiveOrders get a list of all your active orders. Have they been cancelled?

1. Use getMyAvailableBalances to see your current balances.
2. Create a few (3-5) different buy limit orders using newOrder function. Make sure that the price is too low for Gemini to fulfil your buy order.
3. Using getMyActiveOrders get a list of all your active orders. Do you recognise the orders you created in the last step?
4. Cancel your orders using cancelAllActiveOrders .
5. Using getMyActiveOrders get a list of all your active orders. Have they been cancelled?

I had a bit of trouble… When selecting key type I chose Master… I recommend you choose “primary”. The master one only gave me difficulties… when I triple checked to ensure all other aspects were working, I simply made a new API key with the “primary”, not master, and everything worked well. This was my code…

const GeminiAPI = require("gemini-api").default;
const secret = "...***...";
const key = "account-...***...";
const restClient = new GeminiAPI({key, secret, sandbox:true});

setTimeout(() => {
console.log("All available symbols are:")
restClient.getAllSymbols().then(response => console.log(response)).catch(error => console.log(error));
}, 1000);

setTimeout(() => {
console.log("All available balances are:")
restClient.getMyAvailableBalances().then(response => console.log(response)).catch(error => console.log(error));
}, 2000);

setTimeout(() => {
  console.log("\n" + "Let's make an order...")
  restClient.newOrder({amount: 10, price: 110, side: "buy", symbol:"btcusd"}).then(response => console.log(response)).catch(error => console.log(error));
}, 4000);

setTimeout(() => {
  console.log("\n Let's see our orders...")
  restClient.getMyActiveOrders().then(response => console.log(response)).catch(error => console.log(error));
}, 6000);

setTimeout(() => {
  console.log("\n Let's cancel all our orders...")
  restClient.cancelAllActiveOrders().then(response => console.log(response)).catch(error => console.log(error));
}, 8000);

setTimeout(() => {
  console.log("\n Let's check to see the orders were cancelled...")
  restClient.getMyActiveOrders().then(response => console.log(response)).catch(error => console.log(error));
}, 10000);

I had the same issue. I was able to resolve it by giving more permissions to my API key. I believe ‘Requires Heartbeat’ is required.

Note: It may take a while for it to work after giving the key the permissions. I was still having issues the night I was troubleshooting, but it worked the next day. Thanks @ mikekgs for the post.

Hi ! i also got the problem with the “Invalid Nonce” so here is how i solved. Thanx for the course !

1 Like

Hi please find my assignment results below

1 Like
  1. Use getMyAvailableBalances to see your current balances.
  2. Create a few (3-5) different buy limit orders using newOrder function. Make sure that the price is too low for Gemini to fulfil your buy order.
  3. Using getMyActiveOrders get a list of all your active orders. Do you recognise the orders you created in the last step?
  4. Cancel your orders using cancelAllActiveOrders .
  5. Using getMyActiveOrders get a list of all your active orders. Have they been cancelled?
1 Like

Hey I know this is an old post but I have just had the same issue today, so thought I’d let you know how I got around it.

The nonce is a parameter in the request which by default takes a timestamp as its value. If multiple requests are sent in quick succession the timestamp and hence the nonce will be the same. It’s a security feature to stop hackers intercepting your request and re-sending it multiple times.

I got around this issue by generating a custom nonce as shown below:

generateNonce = () => {
    return Math.random() * 1e17
}

restClient.newOrder({ amount: 10, price: 100, side: "buy", symbol: "btcusd", nonce: generateNonce() })
    .then(resp => console.log(resp))
    .catch(error => console.log(error));

estClient.newOrder({ amount: 100, price: 100, side: "buy", symbol: "ethusd", nonce: generateNonce() })
    .then(resp => console.log(resp))
    .catch(error => console.log(error));

etc...

In reality this probably wouldn’t be an issue as you wouldn’t place orders in this way.

Anyway, I hope it provides some clarity for you.

Cheers.

2 Likes
const GeminiAPI = require("gemini-api").default;
const secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const key = "account-xxxxxxxxxxxxxxxxxxxx";
const restClient = new GeminiAPI({key, secret, sandbox:true});


restClient.getMyAvailableBalances()
.then(console.log)
.catch(console.error);

restClient.newOrder({amount:1, price:1000, side:"buy", symbol:"btcusd"})
.then(response => console.log(response))
.catch(error => console.log(error));

restClient.newOrder({amount:2, price:900, side:"buy", symbol:"btcusd"})
.then(response => console.log(response))
.catch(error => console.log(error));

restClient.newOrder({amount:0.5, price:800, side:"buy", symbol:"btcusd"})
.then(response => console.log(response))
.catch(error => console.log(error));

restClient.cancelAllActiveOrders()
.then(console.log)
.catch(console.error);

restClient.getMyActiveOrders()
.then(console.log)
.catch(console.error);

1 Like

1 Like

@NikitaBuzov. I had the same problem and I cleared the browser history and it worked much better.
It is worth trying I also noted that chrome is the best browser to use… I tried Firefox, opera, brave, edge and chrome. I find that chrome works the best and gives you less hassle. Please don’t ask me why.
I hope this might help somewhat.

Happy coding :nerd_face:

Regards,

Nils

1 Like

I did everything that was asked in the homework, but I dont understand how is it that I made 3 orders:

buy 1 BTC for 26500USD
buy 1 ETH for 600USD
buy 2 ETH for 700USD

But only the one for 2 ETH got fulfilled … (at the time the priceof 1 ETH is around 1400USD) … Is that just in that moment one guy was selling ETH at a cheaper than market price???

1 Like

Having the same issue where after placing 3 orders in the same code, the orders will eventually cancel themselves. I’ve tried the generatedNonce from above the orders recommendations but they continue to cancel themselves.

Any help would be great

1 Like

Hi @MosesCruz22, hope you are great.

Would you please share your code so we can help you to figure it out how to solve your issue?

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like
const GeminiAPI = require("gemini-api-node").default;

const secret ="xxxxxxxxxxxxxxxxxxxxxx";
const key = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

const restClient = new GeminiAPI ({key,secret,sandbox :true});



restClient.getMyAvailableBalances()
.then(response => console.log(response))
.catch(error => console.log(error));



restClient.newOrder({ amount: 10, price: 100, side: "buy", symbol: "btcusd", })
    .then(resp => console.log(response.order_id))
    .catch(error => console.log(error));

estClient.newOrder({ amount: 100, price: 100, side: "buy", symbol: "ethusd", n })
    .then(resp => console.log(response.order_id))
    .catch(error => console.log(error));


restClient.newOrder({amount:6,price:100,side:"buy", symbol:"btcusd"})
.then(response => console.log(response.order_id))
.catch(error => console.log(error));


restClient.newOrder({amount:5,price:100,side:"buy", symbol:"btcusd"})
.then(response => console.log(response.order_id))
.catch(error => console.log(error));

restClient.newOrder({amount:1,price:100,side:"buy", symbol:"btcusd"})
.then(response => console.log(response.order_id))
.catch(error => console.log(error));


//Get active Orders
restClient.getMyActiveOrders()
.then(response=> console.log(response))
.catch(error => console.log(error));



console.log("Cancel Open Orders")
restClient.cancelAllActiveOrders()
.then(console.log)
.catch(console.error);
//Get active Orders
restClient.getMyActiveOrders()
.then(response=> console.log(response))
.catch(error => console.log(error));

1 Like

This was fun.

/*jshint esversion: 6 */
console.log("Welcome my API Key");

// Basic SETUP
const GeminiAPI = require("gemini-api").default;
const secret = "mySecret";
const key = "myKey";
const restClient = new GeminiAPI({ key, secret, sandbox: true });


restClient.getMyAvailableBalances()
    .then(response => console.log(response))
    .catch(error => console.log(error));


restClient.newOrder({
        amount: 1,
        price: 11,
        side: "buy",
        symbol: "ethusd"
    })
    .then(response => console.log(response))
    .catch(error => console.log(error));

restClient.newOrder({
        amount: 1,
        price: 12,
        side: "buy",
        symbol: "ethusd"
    })
    .then(response => console.log(response))
    .catch(error => console.log(error));

restClient.newOrder({
        amount: 1,
        price: 13,
        side: "buy",
        symbol: "ethusd"
    })
    .then(response => console.log(response))
    .catch(error => console.log(error));

restClient.getMyActiveOrders()
    .then(response => console.log(response))
    .catch(error => console.log(error));

restClient.cancelAllActiveOrders()
    .then(response => console.log(response))
    .catch(error => console.log(error));

restClient.getMyActiveOrders()
    .then(response => console.log(response))
    .catch(error => console.log(error));

Thanks for the task.
Cheers Ch

1 Like

Im having trouble with making multiple orders. Here is part of my code:

restClient.newOrder({amount:1, price:10, side:"buy", symbol:"ethusd"})
.then(response => console.log(response))
.catch(error => console.log(error));

restClient.newOrder({amount:1, price:10, side:"buy", symbol:"ethusd"})
.then(response => console.log(response))
.catch(error => console.log(error));

Powershell completes just one order (seems like its a random one) and displays the following for the other ones

result: ‘error’,
reason: ‘InvalidNonce’,
message: “Nonce ‘(Number that the system outputs)’ has not increased since your last call to the Gemini API.”

1 Like

hEY @Denzel, hope you are ok.

Please provide the entire code so i can tested it properly, i think you might have a bad configuration in your API key, but i want to validate it.

Here you can check how to post code properly in the forum:
https://forum.ivanontech.com/t/faq-how-to-post-code-in-the-forum/35357/2

Carlos Z