Step 1: Clone the Template Spreadsheet
Split Order Functionality
function openalgo() {
var apikey = "<your-openalgo-apikey>"; // Replace with your OpenAlgo API key
var hostServer = "https://your-ngrok-domain.ngrok-free.app"; // REPLACE WITH YOUR HOST SERVER
var baseUrl = hostServer + "/api/v1/splitorder"; // Constructed base URL
// Fetching parameters from the active sheet
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().toString();
var price = SpreadsheetApp.getActiveSheet().getRange('H3').getValue().toString();
var trigger_price = SpreadsheetApp.getActiveSheet().getRange('I3').getValue().toString();
var splitsize = SpreadsheetApp.getActiveSheet().getRange('J3').getValue().toString();
var product = SpreadsheetApp.getActiveSheet().getRange('K3').getValue();
// Modify payload based on splitsize
var payload = {
"apikey": apikey,
"strategy": strategy,
"exchange": exchange,
"symbol": symbol,
"action": action,
"quantity": splitsize === "0" ? quantity : quantity,
"splitsize": splitsize === "0" ? quantity : splitsize,
"pricetype": pricetype,
"product": product,
"price": price,
"trigger_price": trigger_price
};
Logger.log("Request Payload: " + JSON.stringify(payload)); // Log request payload
// Sending the split order request
var response = sendSplitOrder(baseUrl, payload);
// Format the response for frontend
var formattedResponse = formatResponse(response);
Logger.log("Formatted Response: " + formattedResponse); // Log formatted response
// Update the response in the sheet with formatted response
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PlaceOrder").getRange('C9').setValue(formattedResponse);
// Log order details
logSplitOrder(response, strategy, exchange, symbol, action, pricetype, quantity, product, price, trigger_price);
return formattedResponse;
}
function formatResponse(response) {
try {
var parsedResponse = JSON.parse(response);
// If response is successful
if (parsedResponse.status === "success") {
var results = parsedResponse.results;
// Format the response for frontend display
var formattedLines = [
"Order Placement Status: " + parsedResponse.status.toUpperCase(),
"Total Orders: " + results.length
];
// Add details for each order
results.forEach(function(order, index) {
formattedLines.push(
"Order " + (index + 1) + ":",
" Order ID: " + order.orderid,
" Quantity: " + order.quantity,
" Status: " + order.status
);
});
return formattedLines.join("\n");
} else {
// Handle error response
return "Order Placement Failed:\n" +
"Error Status: " + parsedResponse.status + "\n" +
"Error Message: " + (parsedResponse.message || "Unknown error");
}
} catch (e) {
// Handle parsing error
return "Error Formatting Response:\n" + e.message;
}
}
function sendSplitOrder(url, payload) {
try {
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
};
var result = UrlFetchApp.fetch(url, options);
return result.getContentText();
} catch (e) {
Logger.log("Error sending split order: " + e.message);
return JSON.stringify({
status: "error",
message: e.message
});
}
}
function logSplitOrder(response, strategy, exchange, symbol, action, pricetype, quantity, product, price, trigger_price) {
try {
var parsedResponse = JSON.parse(response);
if (parsedResponse.status === "success") {
var results = parsedResponse.results;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Logs");
results.forEach(function(order) {
var row = [
strategy,
exchange,
symbol,
action,
pricetype,
quantity,
product,
order.orderid,
order.quantity,
order.status,
price,
trigger_price,
new Date()
];
sheet.appendRow(row);
});
} else {
Logger.log("Error in response: " + response);
}
} catch (e) {
Logger.log("Error logging split order: " + e.message);
}
}
function clearLogs() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Logs");
var lastRow = sheet.getLastRow();
if (lastRow > 1) {
sheet.getRange(2, 1, lastRow - 1, sheet.getLastColumn()).clearContent();
}
}