Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

How to Send Orders from Google SpreadSheets using Algomojo API

2 min read

You might think “Is it really possible to do 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 you access the Appscript Editor from Tools->Script 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 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 = "86cbef19e7e61ccee91e497690d5814e";
var apisecret = "8dbb8a1c91649810c82ccfc0ea9d715a";
var Version = "1.0";
var stgy = "Excel";
var Broker =  SpreadsheetApp.getActiveSheet().getRange('B3').getValue();
var ClientID = SpreadsheetApp.getActiveSheet().getRange('C3').getValue();
var Exchange = SpreadsheetApp.getActiveSheet().getRange('D3').getValue();
var Symbol = SpreadsheetApp.getActiveSheet().getRange('E3').getValue();
var Ttranstype = SpreadsheetApp.getActiveSheet().getRange('F3').getValue();
var prctyp = SpreadsheetApp.getActiveSheet().getRange('G3').getValue();
var qty = SpreadsheetApp.getActiveSheet().getRange('H3').getValue();
var Price = SpreadsheetApp.getActiveSheet().getRange('I3').getValue();
var TrigPrice = SpreadsheetApp.getActiveSheet().getRange('J3').getValue();
var Pcode = SpreadsheetApp.getActiveSheet().getRange('K3').getValue();


var response = PlaceOrder(apikey, apisecret, Broker, Version, stgy, ClientID, Symbol, Exchange, Ttranstype, prctyp, qty, Price, TrigPrice, Pcode);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange('C9').setValue(response);
}


function PlaceOrder(user_apikey, api_secret, Broker, Version, stgy_name, ClntID, Tsym, exch, Ttranstype, prctyp, qty, Price, TrigPrice, Pcode){
  
    var s_prdt_ali = "BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML";
    var Ret = "DAY";
    var discqty = "0";
    var MktPro = "NA";
    if(TrigPrice == ""){
        TrigPrice = "0";
    }
    var AMO = "NO";

    var InTD = "{\"" 
            + "strg_name" + "\":\"" + stgy_name + "\",\""
            + "s_prdt_ali" + "\":\"" + s_prdt_ali + "\",\"" + "Tsym" + "\":\"" + Tsym + "\",\"" 
            + "exch" + "\":\"" + exch + "\",\"" + "Ttranstype" + "\":\"" + Ttranstype + "\",\"" 
            +"Ret" + "\":\"" + Ret + "\",\"" + "prctyp" + "\":\"" + prctyp + "\",\"" 
            +"qty" + "\":\"" + qty + "\",\"" + "discqty" + "\":\"" + discqty + "\",\"" 
            + "MktPro" + "\":\"" + MktPro + "\",\"" + "Price" + "\":\"" + Price + "\",\"" 
            + "TrigPrice" + "\":\"" + TrigPrice + "\",\"" + "Pcode" + "\":\"" + Pcode + "\",\"" 
            + "AMO" + "\":\"" + AMO + "\"}";
   
           
    var result = AMConnect(user_apikey, api_secret, Broker, 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.

Assinging 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!

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Mini Certification Course on Algorithmic Trading Strategies

If you are new to Automated trading? This certification course will help you to kickstart your automated trading with your broker and get to...
Rajandran R
1 min read

How to Build your First Execution Module and Cover…

This tutorial explores how to build your very first execution module in Amibroker and How to transmit orders from Amibroker to Algomojo trading platoform.
Rajandran R
2 min read

How to Fetch Upstox Historical Intraday API Data into…

In the previous session, we learned how Algomojo users can Get Free Upstox Interactive API and Historical Data API and in this tutorial, we...
Rajandran R
1 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More