Javascript bot programming using Gemini API, NodeJS and CryptoCompare - DISCUSSION

Unfortunately I was unable to solve the issue.

1 Like

Hi! when I tried yarn add gemini-api, got this error message:

yarn : File C:\Users\ldvcn\AppData\Roaming\npm\yarn.ps1 cannot be loaded because running scripts is disabled on this sy
stem. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1

  • yarn add gemini-api
  •   + CategoryInfo          : SecurityError: (:) [], PSSecurityException
      + FullyQualifiedErrorId : UnauthorizedAccess
    
    

Multiple time…
What can I do? It is really necessary to change ExecutionPolicy on my Windows?
Thanks!

Its referred to a limitation on powershell to run scripts commands, you can try to use windows command prompt instead (CMD), it should not have any limitations.

Carlos Z

Thank you! In CMD is OK.

1 Like

Im having an error on execution of newOrder function from gemini-api library

API key and secret key required to use authenticated methods

What is that ?

You need a api key for the module to work properly.

API documentation:
https://docs.gemini.com/rest-api/#introduction

How do I create an API key?

Remember to use the sandbox api key (check documentation) :nerd_face:

Carlos Z

I resolved it
so if we want to use different variable names for Api Key and Secret and not like in gemini-api library then it needs to be declared this way

const APIkey = ck.APIKEY;
const APIsecret = ck.SECRET;

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

or we can just use the same variable name as GeminiAPI library

const key = ck.APIKEY;
const secret = ck.SECRET;

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

GeminiAPI library

class GeminiAPI {

  constructor({ key, secret, sandbox = false } = { sandbox: false })
1 Like

I’m a bit stuck. My code is as follows:

global.fetch = require("node-fetch");
const GeminiAPI = require("gemini-api").default;
const secret = "2guKgR4uahmHjarfPG8bK6NFEUVN";
const key = "account-BOeBYj3ltMJEOOCNzMgh";
const ccAPIKey = "6e1e4e91e5ade3926c6c3a37ca27bbe3309d56b239b85cc908b95ccc8aabc7f7";

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

const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(ccAPIKey);

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

CryptoCompareAPI.histoHour('BTC', 'USD')
.then(data => {console.log(data)})
.catch(console.error)

When I run this I am getting the following error:

C:\Users\schma\OneDrive\Documents\Trading>node index.js
C:\Users\schma\OneDrive\Documents\Trading\index.js:1
global.fetch = require("node-fetch");
               ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\schma\OneDrive\Documents\Trading\node_modules\node-fetch\src\index.js from C:\Users\schma\OneDrive\Documents\Trading\index.js not supported.
Instead change the require of C:\Users\schma\OneDrive\Documents\Trading\node_modules\node-fetch\src\index.js in C:\Users\schma\OneDrive\Documents\Trading\index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\Users\schma\OneDrive\Documents\Trading\index.js:1:16) {
  code: ←[32m'ERR_REQUIRE_ESM'←[39m
}

C:\Users\schma\OneDrive\Documents\Trading>

I’m not sure why I’m getting the error since I’ve been following along with the videos.

I was able to figure this out. I needed to uninstall node-fetch and install an earlier version.

1 Like

I had a few troubles getting Gemini API but managed to figure it out.

  1. I opened PowerShell as an administrator.
  2. Directed to folder. cd and paste path
  3. Installed yarn, npm install yarn --global
  4. yarn add gemini-api (fails, script disabled)
  5. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7 to bypass policy enter Set-ExecutionPolicy -ExecutionPolicy RemoteSigned and hit yes.
  6. Use the up arrow to run the 4th step (yarn add gemini-api) and enter to install.

    This is what I’ve done just in case anyone has trouble. If there are any mistakes please let me know, thank you!

You might need to reinstall yarn, your version is out of date, and probably one of the dependencies needs the latest version to work properly.

https://classic.yarnpkg.com/lang/en/docs/install/#windows-stable

Carlos Z

1 Like

I’m very new to all this but i found a walk through on setting up and navigating through PowerShell.
https://youtu.be/MY0yIUPDe50

Got It! scrolled through various messages in the forum that were helpful. First made sure the console was up to date and updated dependencies. When making an API Key don’t use the Master key, use primary. Had some missing brackets and added them to the code. I also added

.catch(console.error);

Please need help! I’ve been stuck looking at this code for hours.

index.js

global.fetch = require("node-fetch");
const indicators = require("./indicators.js");
const exchange = require("./exchange.js");


 var hasPosition = false;
  var strategy = function(){
 // If bTC < MA ==> buy (if we have a position)
 // If BTC > MA ==> sell (If we have a postion)

 console.log("     " );
 console.log("=======================");
 console.log("Executing strategy");

  indicators.hourlyMovingAverage("BTC", "USD", 100, function(ma){
   exchange.BitcoinPrice()
   .then(res => {

    var price = res.last;

     console.log("MA: ",ma);
     console.log("Price: ", price);

     if(price < ma && !hasPosition){

       console.log("Buy!");
       exchange.marketBuyBitcoin()
       .then(res=>{
         console.log("Buy successfull");
         hasPosition = true;

         setTimeout(strategy,1000);
       })
       .catch(error => console.error)

     }

    else if(price > ma && hasPosition){

       console.log("Sell!");
       exchange.marketBuyBitcoin()
       .then(res =>{
         console.log("Sell successfull");
         hasPosition = false;

         setTimeout(strategy,1000);
         })
       .catch(error=> console.error)

     }

     else{

       console.log("Hold!");
       setTimeout(strategy,1000);
     }


   })


 });

}

strategy();

exchange.js

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


module.exports = {

  marketBuyBitcoin:function(){
    return restClient.newOrder ({amount:1,
                         price:30000,
                         side:"buy",
                         symbol: "btcusd",
                          options:[ "immediate-or-cancel"]})
  },

  marketSellBitcoin:function(){
    return restClient.newOrder ({amount:1,
                         price:10,
                         side:"sell",
                         symbol: "btcusd",
                          options:[ "immediate-or-cancel"]})
  },

  BitcoinPrice:function(){
    return restClient.getTicker("btcusd");

  }

}

indicators.js

onst CCAPIKey = "************"
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);

module.exports = {

  hourlyMovingAverage:function(cryptoAsset, fiatCurrency, hours, callback){

    if(hours>169){
      console.error("Only up tp 169 hours allowed!")
      return
    }

    CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
    .then(data => {

     data = data.reverse()
      var sum = 0;
      for(var i = 0;i<hours;i++){
        sum+=data[i].close;
      }

    var movingAverage = Math.floor (sum/hours);
    callback(movingAverage);

    })
    .catch(console.error)

  }
}

Does the console shows any error when you run the code?

Carlos Z

Is it that the first order isn’t successful so the console doesn’t print hold every second?

Could you please share your code in the following way?

Carlos Z

Problem solved I think it was the spacing at the very top function.

var strategy = function(){

IT WORKS!

1 Like

global.fetch = require("node-fetch");

ERROR

node-fetch changed to ES modules by default:

SOLUTION:

  1. Uninstall the current version of node-fetch with npm uninstall node-fetch
  2. Install the second version: npm install node-fetch@2
    It has worked for me!
2 Likes