How to Send Orders from Google SpreadSheets using Algomojo API

You might think “Is it really possible to send automated orders from Google Sheets? Are you crazy?” but hey thanks to Google Appscript which is an alternative to VBscript to automate your tasks in your Google Excel Sheets.

What is Google AppScript?

Apps Script lets you do more with Google Spreadsheets and with other Google Products, all on a modern JavaScript platform in the cloud. Build solutions to boost your collaboration and productivity.

You access the Appscript Editor from Extensions->AppScript Editor from your Google Spreadsheet.

Creating Button Controls

The Next Step is to Create the Datasheet that you want to transmit and PlaceOrder button to send and transmit those orders to the Algomojo Platform as shown below.

Button Controls can be created by goto Insert Menu -> Drawings and create a Button Control with “PlaceOrder” as text in the button.

Writing the Google Appscript

Open the Script Editor and Paste the below appscript code and save the code as Algomojo Buttons.

Get the API Key and API Secret Key from Algomojo Platform and replace the apikey and apisecret key in the below code with yours.

function Algomojo() {


var apikey = "af672a6019523acd27bc4d48ca12cb73";
var apisecret = "8b5befaa9ff7e0269326170ac572b67c";
var Version = "v1";
var broker =  SpreadsheetApp.getActiveSheet().getRange('B3').getValue();
var strategy = "Google Sheet";
var exchange = SpreadsheetApp.getActiveSheet().getRange('C3').getValue();
var symbol = SpreadsheetApp.getActiveSheet().getRange('D3').getValue();
var action = SpreadsheetApp.getActiveSheet().getRange('E3').getValue();
var pricetype = SpreadsheetApp.getActiveSheet().getRange('F3').getValue();
var quantity = SpreadsheetApp.getActiveSheet().getRange('G3').getValue();
var price = SpreadsheetApp.getActiveSheet().getRange('H3').getValue();
var trigger_price = SpreadsheetApp.getActiveSheet().getRange('I3').getValue();
var product = SpreadsheetApp.getActiveSheet().getRange('J3').getValue();
var splitorder = SpreadsheetApp.getActiveSheet().getRange('K3').getValue();
var split_quantity = SpreadsheetApp.getActiveSheet().getRange('L3').getValue();


var response = PlaceOrder(apikey, apisecret, Version, broker, strategy, exchange, symbol, action, pricetype, quantity, price, trigger_price, product, splitorder,split_quantity);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange('C9').setValue(response);
}


function PlaceOrder(apikey, apisecret, Version, broker, strategy, exchange, symbol, action, pricetype, quantity, price, trigger_price, product, splitorder,split_quantity){
  
    
    var amo = "NO";
    var disclosed_quantity = "0"

    var InTD = "{\"" 
            + "broker" + "\":\"" + broker + "\",\""
            + "strategy" + "\":\"" + strategy + "\",\"" + "exchange" + "\":\"" + exchange + "\",\"" 
            + "symbol" + "\":\"" + symbol + "\",\"" + "action" + "\":\"" + action + "\",\""  
            + "product" + "\":\"" + product + "\",\"" + "pricetype" + "\":\"" + pricetype + "\",\"" 
            + "quantity" + "\":\"" + quantity + "\",\"" + "price" + "\":\"" + price + "\",\"" 
            + "disclosed_quantity" + "\":\"" + disclosed_quantity + "\",\"" + "trigger_price" + "\":\"" + trigger_price + "\",\"" 
            + "amo" + "\":\"" + amo + "\",\"" + "splitorder" + "\":\"" + splitorder + "\",\"" 
            + "split_quantity" + "\":\"" + split_quantity + "\"}";
   
           
    var result = AMConnect(apikey, apisecret, "am", Version, "PlaceOrder", InTD);
    return result;
}


function AMConnect(api_key, api_secret, Broker, Version, api_name, InTD){
    
    var BaseURL = "https://" + Broker.toLowerCase() + "api.algomojo.com/" + Version + "/";
    var postdata = "{\"" + "api_key" + "\":\"" + api_key + "\",\"" 
                    + "api_secret" + "\":\"" + api_secret + "\",\""
                    + "data" + "\":" + InTD + "}";
  

 
    
    var Url = BaseURL + api_name;
  
     
  
  var raw = postdata;
  Logger.log(raw);
  
    var options = {
      'method' : 'post',
      'contentType': 'application/json',
      // Convert the JavaScript object to a JSON string.
      'payload' : raw
    };
    var result = UrlFetchApp.fetch(Url, options);
    return result.getContentText();
    
}


Run the code.gs with code execution set to Algomojo() function. There should not be any errors and you should be able to see the executions happening in your trading terminal.

Assigning the Button Controls to Google Appscript Function

1)Now right-click over the button and click on the triple dots and select the Assign script option

2)Enter the Function Name as Algomojo in the Assign Script Popup Dialog box

Now come back to Google Spreadsheets and click on the Place Order button and check the orders got executed in the Algomojo Order Log section in realtime.

For sending a different type of orders in your Google Appscript kindly check with the latest algomojo API documentation

The possibilities of creating a Automated Trading task is endless creativity. Playaround with Google Appscript and if in case you are able to build something interesting with google appscript let us know here in the comment section.

Happy Spreadsheet Trading!

Leave a Comment

Your email address will not be published. Required fields are marked *