Algomojo API Button Trading Execution Module: A Complete Guide for Amibroker

Introduction

The Algomojo API Button Trading Execution Module enables traders to place orders directly from Amibroker using customizable buttons. This module supports both the Algomojo Bridge and direct API calls for seamless order execution across multiple brokers.

Features

βœ… Supports multiple brokers via Algomojo API.
βœ… Button-based trading for quick execution.
βœ… Can operate with or without Algomojo Bridge.
βœ… Configurable parameters for strategy, order type, and quantity.
βœ… Voice alerts for successful order placement.

Supported Brokers

This module supports the following brokers through Algomojo API:

  • 5PAISA
  • ALICEBLUE
  • AN API INTEGRATION
  • BIGUL
  • COMPOSITEDGE
  • DHANHQ
  • FINVASIA
  • FIRSTOCK
  • FLATTRADE
  • FYERS
  • GOODWILL
  • KOTAK NEO
  • MOTILAL OSWAL
  • PAYTM
  • RUPEEZY
  • SAMCO
  • SHAREKHAN
  • SKY BROKING
  • TRADEJINI
  • TRADESMART
  • UPSTOX
  • ZEBU
  • ZERODHA

How to Set Up in Amibroker

Step 1: Download & Install

  1. Open Amibroker.
  2. Copy the AFL script provided below and save it as Algomojo_Button_Trading.afl.
  3. Place the script inside the AmiBroker\Formulas\Custom directory.

Step 2: Configure API Credentials

  1. Log in to your Algomojo account.
  2. Navigate to My API.
  3. Copy your API Key and API Secret.

Step 3: Add to Amibroker Chart

  1. Open AmiBroker.
  2. Select any chart.
  3. Click on Insert Indicator.
  4. Choose Algomojo_Button_Trading.afl.

Step 4: Configure Order Parameters

  • Broker – Select your broker.
  • API Key – Paste from My API.
  • API Secret – Paste from My API.
  • Exchange – Choose from NSE, NFO, BSE, MCX, etc.
  • Symbol – Enter the trading symbol name(Take the Trading Symbol from Algomojo watchlist).
  • Order Type – MARKET, LIMIT, SL, SL-M.
  • Quantity – Define the order quantity.
  • Price & Trigger Price – Set price levels for LIMIT & SL orders.

Step 5: Enable Button Trading

  1. Set Algo Mode to Enable.
  2. Click BUY, SELL, SHORT, or COVER to place orders.
  3. The order is executed via Algomojo API.

AFL Script (Button Trading Execution)

/*
Algomojo API Button Trading Execution Module
Website: www.algomojo.com
*/

_SECTION_BEGIN("Algomojo - Broker Controls");

// Refresh to process signals in real-time
RequestTimedRefresh(1, False);

// Creating Input Controls for Setting Order Related Information
broker = ParamList("Broker", "5PAISA|ALICEBLUE|AN API INTEGRATION|BIGUL|COMPOSITEDGE|DHANHQ|FINVASIA|FIRSTOCK|FLATTRADE|FYERS|GOODWILL|KOTAK NEO|MOTILAL OSWAL|PAYTM|RUPEEZY|SAMCO|SHAREKHAN|SKY BROKING|TRADEJINI|TRADESMART|UPSTOX|ZEBU|ZERODHA", 0);
ver = ParamStr("API Version", "v1");
apikey = ParamStr("apikey", "xxxxxxxxxxxxxxxxxx"); // Enter your API key here
apisecret = ParamStr("secretkey", "xxxxxxxxxxxxxxxxxx"); // Enter your API secret key here

_SECTION_END();

_SECTION_BEGIN("Algomojo - Order Controls");

strategy = ParamStr("Strategy Name", "Ami Strategy");
exchange = ParamList("Exchange", "NSE|NFO|BSE|BFO|MCX", 0);
symbol = ParamStr("Trading Symbol", "BHEL-EQ");
product = ParamList("Product", "CNC|MIS|NRML", 0);
pricetype = ParamList("Price Type", "MARKET|LIMIT|SL|SL-M", 0);
quantity = Param("Quantity", 1, 1, 100000, 1);
price = Param("Price", 0.00, 0.00, 100000.00, 0.01);
disclosed_quantity = 0;
trigger_price = Param("Trigger Price", 0.00, 0.00, 100000.00, 0.01);
amo = "NO";
splitorder = ParamList("To Split Orders", "NO|YES", 0);
split_quantity = Param("Split Quantity", 500, 1, 100000, 1);
VoiceAlert = ParamList("Voice Alert", "Disable|Enable", 1);
EnableAlgo = ParamList("Algo Mode", "Disable|Enable", 0); // Algo Mode
WithBridge = ParamList("With Algomojo Bridge", "Yes|No", 0); 
resp = "";

// Fetch Broker Code
function GetBrokerCode(broker_name) {
    broker_code = ""; // Default value
    if (broker_name == "5PAISA") broker_code = "fp";
    else if (broker_name == "ALICEBLUE") broker_code = "ab";
    else if (broker_name == "AN API INTEGRATION") broker_code = "an";
    else if (broker_name == "BIGUL") broker_code = "bg";
    else if (broker_name == "COMPOSITEDGE") broker_code = "ce";
    else if (broker_name == "DHANHQ") broker_code = "dh";
    else if (broker_name == "FINVASIA") broker_code = "fv";
    else if (broker_name == "FIRSTOCK") broker_code = "fs";
    else if (broker_name == "FLATTRADE") broker_code = "ft";
    else if (broker_name == "FYERS") broker_code = "fy";
    else if (broker_name == "GOODWILL") broker_code = "gc";
    else if (broker_name == "KOTAK NEO") broker_code = "kt";
    else if (broker_name == "MOTILAL OSWAL") broker_code = "mo";
    else if (broker_name == "PAYTM") broker_code = "pt";
    else if (broker_name == "RUPEEZY") broker_code = "rz";
    else if (broker_name == "SAMCO") broker_code = "sm";
    else if (broker_name == "SHAREKHAN") broker_code = "sk";
    else if (broker_name == "SKY BROKING") broker_code = "sc";
    else if (broker_name == "TRADEJINI") broker_code = "tc";
    else if (broker_name == "TRADESMART") broker_code = "ts";
    else if (broker_name == "UPSTOX") broker_code = "up";
    else if (broker_name == "ZEBU") broker_code = "zb";
    else if (broker_name == "ZERODHA") broker_code = "ze";
    return broker_code;
}

function PlaceOrder_NoBridge(action) {
    resp = "";
    broker_code = GetBrokerCode(broker);
    if (broker_code == "") {
        _TRACE("Error: Invalid Broker Selection.");
        resp = "Error: Invalid Broker Selection.";
    }else{

        // Construct the JSON payload
		api_data = "{ \"api_key\":\"" + apikey + "\", "
                 + " \"api_secret\":\"" + apisecret + "\", "
                 + " \"data\": { "
                 + " \"broker\": \"" + broker_code + "\", "
                 + " \"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 + "\" "
                 + " } }";

        _TRACE("Webhook JSON Data: " + api_data);

        // Define Webhook URL
        webhook_url = "https://amapi.algomojo.com/v1/PlaceOrder";

		// Create XMLHTTP Object for HTTP Request
		http = CreateObject("MSXML2.XMLHTTP");
		if (http == Null) {
			_TRACE("Error: Unable to create HTTP object");
			resp = "Error: Unable to create HTTP object";
		}else{
		
			// Open Connection
			http.Open("POST", webhook_url, False);
			http.SetRequestHeader("Content-Type", "application/json");

			// Send Data
			http.Send(api_data);

			// Get Response
			resp = http.ResponseText;
			
			_TRACE("Webhook Response: " + resp);
			
			if (VoiceAlert == "Enable") Say("Order Placed");
				
		}
    }
    return resp;
}

// Place Order Function (For Button Click)
function PlaceOrder(action) {
    algomojo = CreateObject("AMAMIBRIDGE.Main");
    resp = "";

    if (algomojo == Null) {
        _TRACE("Error: Failed to create algomojo object");
        if (VoiceAlert == "Enable") Say("Failed to create algomojo object");
        resp = "Error: Failed to create algomojo object";
    } else {
        broker_code = GetBrokerCode(broker);
        if (broker_code == "") {
            _TRACE("Error: Invalid Broker Selection.");
            resp = "Error: Invalid Broker Selection.";
        } else {
            // Preparing API data
            api_data = "{ \"broker\": \"" + broker_code + "\","  
                     + " \"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 + "\" }";

            _TRACE("Broker API Request: " + api_data);
            resp = algomojo.AMDispatcher(apikey, apisecret, "PlaceOrder", api_data, "am", ver);
            _TRACE("API Response: " + resp);

            if (VoiceAlert == "Enable") Say("Order Placed");
        }
    }
    return resp;
}

// ?? Button Trading UI
X0 = 20;
Y0 = 40;
X1 = 60;
Y_GAP = 55; // Added gap for new buttons

LBClick = GetCursorMouseButtons() == 9;
MouseX  = Nz(GetCursorXPosition(1));
MouseY  = Nz(GetCursorYPosition(1));

procedure DrawButton (Text, x1, y1, x2, y2, colorFrom, colorTo)
{
    GfxSetOverlayMode(0);
    GfxSelectFont("Verdana", 9, 700);
    GfxSetBkMode(1);
    GfxGradientRect(x1, y1, x2, y2, colorFrom, colorTo);
    GfxDrawText(Text, x1, y1, x2, y2, 32|1|4|16);
}
GfxSetTextColor(colorWhite);

if (EnableAlgo == "Enable") {
    // Buy & Sell Buttons
    DrawButton("BUY", X0, Y0, X0+X1, Y0+50, colorGreen, colorGreen);
    CursorInBUY = MouseX >= X0 AND MouseX <= X0+X1 AND MouseY >= Y0 AND MouseY <= Y0+50;
    BuyClick = CursorInBUY AND LBClick;

    DrawButton("SELL", X0+65, Y0, X0+X1+65, Y0+50, colorRed, colorRed);
    CursorInSELL = MouseX >= X0+65 AND MouseX <= X0+X1+65 AND MouseY >= Y0 AND MouseY <= Y0+50;
    SellClick = CursorInSELL AND LBClick;

    // Short & Cover Buttons (Added Below Buy & Sell)
    DrawButton("SHORT", X0, Y0+Y_GAP, X0+X1, Y0+50+Y_GAP, colorOrange, colorOrange);
    CursorInSHORT = MouseX >= X0 AND MouseX <= X0+X1 AND MouseY >= Y0+Y_GAP AND MouseY <= Y0+50+Y_GAP;
    ShortClick = CursorInSHORT AND LBClick;

    DrawButton("COVER", X0+65, Y0+Y_GAP, X0+X1+65, Y0+50+Y_GAP, colorBlue, colorBlue);
    CursorInCOVER = MouseX >= X0+65 AND MouseX <= X0+X1+65 AND MouseY >= Y0+Y_GAP AND MouseY <= Y0+50+Y_GAP;
    CoverClick = CursorInCOVER AND LBClick;

    if(WithBridge=="Yes"){
        if (BuyClick) PlaceOrder("BUY");
        if (SellClick) PlaceOrder("SELL");
        if (ShortClick) PlaceOrder("SELL");
        if (CoverClick) PlaceOrder("BUY");
    }else{
        if (BuyClick) PlaceOrder_NoBridge("BUY");
        if (SellClick) PlaceOrder_NoBridge("SELL");
        if (ShortClick) PlaceOrder_NoBridge("SELL");
        if (CoverClick) PlaceOrder_NoBridge("BUY");        
    }

}


if (EnableAlgo == "Enable") {

	AlgoStatus = "Algo Enabled";
	GfxSetTextColor( colorGreen ); 
	GfxTextOut( "Algostatus : "+AlgoStatus , 20, 20); 
}
if(EnableAlgo == "Disable")
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor( colorRed ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 20); 
}

_SECTION_END();

FAQs

1. Do I need the Algomojo Bridge to use this module?

  • No, the module supports both bridge-based trading and direct webhook API execution.

2. How can I verify my order execution?

  • You can check the Algomojo Order Book in your broker’s dashboard.

3. What if my broker is not listed?

  • You can contact Algomojo support to request broker integration.

4. Can I use this for automated trading?

  • Yes, this module can be integrated with AFL strategies for automation.

5. What if I get an API error?

  • Ensure API keys are correct and check internet connectivity.

Conclusion

With the Algomojo API Button Trading Execution Module, you can place orders instantly from Amibroker with just a click! Whether you prefer manual trading or automated execution, this module provides speed and efficiency for active traders.

🎯 Get Started Now – Sign up on Algomojo and start trading instantly! πŸš€

Leave a Comment

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