Options Button Trading Module
Internet Method (Modern Method)
// Rajandran R - Creator of OpenAlgo
// website - openalgo.in / marketcalls.in
// OpenAlgo - Amibroker Options Button Trading Module (Modern GUI) v2.1
// Date - 21/11/2025
//
// Features:
// - Separate CE and PE button sections with grid layout
// - BUY/SELL Mode: Trade options in BUY mode (long) or SELL mode (short/writing)
// - Smart Exit: Uses placesmartorder with quantity=0 and position_size=0 for clean exits
// - Modern Internet Functions (no VBScript)
// - Static variable tracking for order management
// - Real-time order status display
// - Close All positions with single button
_SECTION_BEGIN("OpenAlgo Options Button Trading Module");
/* Note: This AFL requires AmiBroker 6.35 or higher */
/* --------------------------------------------------------------------------
Version Check Variable
-------------------------------------------------------------------------- */
ReqVer = 6.35;
/* --------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------- */
global IDset;
IDset = 0;
/* --------------------------------------------------------------------------
Helper Functions - Defined at Global Scope
-------------------------------------------------------------------------- */
/* Function to post option order to OpenAlgo API */
function PostOptionOrder(offset_val, opt_type, act, qty, apikey_val, strategy_val, underlying_val, exchange_val, expiry_val, pricetype_val, product_val, price_val, trigger_val, host_val, ver_val)
{
url = host_val + "/api/" + ver_val + "/optionsorder";
/* Construct JSON request body */
body = "{\"apikey\": \"" + apikey_val + "\", " +
"\"strategy\": \"" + strategy_val + "\", " +
"\"underlying\": \"" + underlying_val + "\", " +
"\"exchange\": \"" + exchange_val + "\", " +
"\"expiry_date\": \"" + expiry_val + "\", " +
"\"offset\": \"" + offset_val + "\", " +
"\"option_type\": \"" + opt_type + "\", " +
"\"action\": \"" + act + "\", " +
"\"quantity\": \"" + NumToStr(qty, 1.0) + "\", " +
"\"pricetype\": \"" + pricetype_val + "\", " +
"\"product\": \"" + product_val + "\", " +
"\"price\": \"" + NumToStr(price_val, 1.2) + "\", " +
"\"trigger_price\": \"" + NumToStr(trigger_val, 1.2) + "\", " +
"\"disclosed_quantity\": \"0\"}";
_TRACEF("OpenAlgo OptionsOrder Request: %s", body);
/* Set headers exactly like working solution */
headers = "Content-Type: application/json\r\n" +
"Accept-Encoding: gzip, deflate\r\n";
InternetSetHeaders(headers);
ih = InternetPostRequest(url, body);
response = "";
if (ih)
{
while ((line = InternetReadString(ih)) != "")
{
response = response + line;
}
_TRACEF("OpenAlgo OptionsOrder Response: %s", response);
InternetClose(ih);
}
else
{
_TRACE("OpenAlgo HTTP post failed");
}
return response;
}
/* Function to exit position using placesmartorder with quantity=0 and position_size=0
This API call will close any existing position for the given symbol.
COPIED FROM WORKING SMART CHART MODULE - ExitOrder function */
function ExitPosition(sym, exch, product_type, apikey_val, strategy_val, host_val, ver_val)
{
/* Build postData exactly like working solution */
postData = "{\"apikey\": \"" + apikey_val + "\", " +
"\"strategy\": \"" + strategy_val + "\", " +
"\"symbol\": \"" + sym + "\", " +
"\"action\": \"SELL\", " +
"\"exchange\": \"" + exch + "\", " +
"\"pricetype\": \"MARKET\", " +
"\"product\": \"" + product_type + "\", " +
"\"quantity\": \"0\", " +
"\"position_size\": \"0\"}";
headers = "Content-Type: application/json\r\n" +
"Accept-Encoding: gzip, deflate\r\n";
InternetSetHeaders(headers);
_TRACE("Exit Order Request Sent: " + postData);
/* Build URL exactly like working solution */
bridgeurl = host_val + "/api/" + ver_val;
ih = InternetPostRequest(bridgeurl + "/placesmartorder", postData);
if (ih) {
response = "";
while ((line = InternetReadString(ih)) != "") {
response += line;
}
_TRACEF("Exit Order Response: %s", response);
InternetClose(ih);
} else {
_TRACE("Failed to place exit order.");
}
return response;
}
/* Function to close all positions */
function CloseAllPositions(apikey_val, strategy_val, host_val, ver_val)
{
url = host_val + "/api/" + ver_val + "/closeposition";
body = "{\"apikey\": \"" + apikey_val + "\", " +
"\"strategy\": \"" + strategy_val + "\"}";
_TRACEF("OpenAlgo CloseAllPositions Request: %s", body);
/* Set headers exactly like working solution */
headers = "Content-Type: application/json\r\n" +
"Accept-Encoding: gzip, deflate\r\n";
InternetSetHeaders(headers);
ih = InternetPostRequest(url, body);
response = "";
if (ih)
{
while ((line = InternetReadString(ih)) != "")
{
response = response + line;
}
_TRACEF("OpenAlgo CloseAllPositions Response: %s", response);
InternetClose(ih);
}
else
{
_TRACE("OpenAlgo HTTP post failed");
}
return response;
}
/* Simple JSON value extractor */
function ExtractJsonValue(json_str, key_name)
{
result = "";
/* Find the key in JSON */
search_key = "\"" + key_name + "\":";
pos = StrFind(json_str, search_key);
if (pos >= 0)
{
/* Move past the key */
start = pos + StrLen(search_key);
/* Skip whitespace */
while (start < StrLen(json_str) AND StrMid(json_str, start, 1) == " ")
{
start = start + 1;
}
/* Check if value is quoted string */
if (StrMid(json_str, start, 1) == "\"")
{
start = start + 1; /* Skip opening quote */
end = start;
/* Find closing quote */
while (end < StrLen(json_str) AND StrMid(json_str, end, 1) != "\"")
{
end = end + 1;
}
/* Extract value WITHOUT the closing quote */
result = StrMid(json_str, start, end - start);
}
else
{
/* Numeric or boolean value */
end = start;
while (end < StrLen(json_str))
{
ch = StrMid(json_str, end, 1);
if (ch == "," OR ch == "}" OR ch == " ")
{
break;
}
end = end + 1;
}
result = StrMid(json_str, start, end - start);
}
}
return result;
}
/* --------------------------------------------------------------------------
Version Check and Main Code
-------------------------------------------------------------------------- */
if ( Version() < ReqVer )
{
SetChartOptions(0, chartShowDates);
GfxSetBkMode(1);
GfxSetBkColor(colorBlack);
GfxSetTextColor(colorRed);
GfxSelectFont("Arial", 16, 700);
GfxTextOut("This AFL needs AmiBroker 6.35 or later", 40, 40);
}
else
{
/* --------------------------------------------------------------------------
Global Settings and Initialization
-------------------------------------------------------------------------- */
RequestTimedRefresh(1, False);
EnableTextOutput(False);
SetOption("StaticVarAutoSave", 30);
/* --------------------------------------------------------------------------
Parameters
-------------------------------------------------------------------------- */
apikey = ParamStr("OpenAlgo API Key", "******");
strategy = ParamStr("Strategy Name", "OptionButtons");
host = ParamStr("Host", "http://127.0.0.1:5000");
ver = ParamStr("API Version", "v1");
underlying = ParamStr("Underlying Symbol", "NIFTY");
exchange = ParamList("Exchange", "NSE_INDEX|BSE_INDEX|NFO|BFO", 0);
expiry = ParamStr("Expiry Date (DDMMMYY)", "25NOV25");
/* Offset Parameters */
ATM_offset = ParamStr("ATM Offset", "ATM");
ITM_offset = ParamStr("ITM Offset", "ITM2");
OTM_offset = ParamStr("OTM Offset", "OTM2");
/* Order Parameters */
trade_mode = ParamList("Trade Mode", "BUY|SELL", 0);
LotSize = Param("Lot Size", 75, 1, 10000, 1);
quantity = Param("Quantity (Lots)", 1, 0, 100) * LotSize;
pricetype = ParamList("Order Type", "MARKET|LIMIT|SL|SL-M", 0);
product = ParamList("Product", "NRML|MIS", 0);
price = Param("Limit Price", 0, 0, 100000, 0.05);
trigger_price = Param("Trigger Price", 0, 0, 100000, 0.05);
EnableAlgo = ParamList("Enable Trading", "Disable|Enable", 0);
VoiceAlert = ParamList("Voice Alert", "Disable|Enable", 1);
/* --------------------------------------------------------------------------
Static Variable Names for Tracking Orders
-------------------------------------------------------------------------- */
static_base = Name() + GetChartID() + interval(2) + strategy;
static_algo_status = static_base + "_algostatus";
/* CE Orders */
static_ATM_CE_order = static_base + "_ATM_CE_order";
static_ITM_CE_order = static_base + "_ITM_CE_order";
static_OTM_CE_order = static_base + "_OTM_CE_order";
static_ATM_CE_symbol = static_base + "_ATM_CE_symbol";
static_ITM_CE_symbol = static_base + "_ITM_CE_symbol";
static_OTM_CE_symbol = static_base + "_OTM_CE_symbol";
static_ATM_CE_exchange = static_base + "_ATM_CE_exchange";
static_ITM_CE_exchange = static_base + "_ITM_CE_exchange";
static_OTM_CE_exchange = static_base + "_OTM_CE_exchange";
/* PE Orders */
static_ATM_PE_order = static_base + "_ATM_PE_order";
static_ITM_PE_order = static_base + "_ITM_PE_order";
static_OTM_PE_order = static_base + "_OTM_PE_order";
static_ATM_PE_symbol = static_base + "_ATM_PE_symbol";
static_ITM_PE_symbol = static_base + "_ITM_PE_symbol";
static_OTM_PE_symbol = static_base + "_OTM_PE_symbol";
static_ATM_PE_exchange = static_base + "_ATM_PE_exchange";
static_ITM_PE_exchange = static_base + "_ITM_PE_exchange";
static_OTM_PE_exchange = static_base + "_OTM_PE_exchange";
/* --------------------------------------------------------------------------
GUI Layout and Buttons
-------------------------------------------------------------------------- */
GfxSetTextColor(colorWhite);
GfxSetBkColor(colorBlack);
GfxSelectFont("Arial", 12, 700);
/* Display Algo Status */
x0 = 20;
y0 = 20;
if (EnableAlgo == "Enable")
{
AlgoStatus = "Algo Enabled";
GfxSetTextColor(colorGreen);
GfxTextOut("Trading : " + AlgoStatus, x0, y0);
if (Nz(StaticVarGet(static_algo_status), 0) != 1)
{
_TRACE("Algo Status: Enabled");
StaticVarSet(static_algo_status, 1);
}
}
else
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor(colorRed);
GfxTextOut("Trading : " + AlgoStatus, x0, y0);
if (Nz(StaticVarGet(static_algo_status), 0) != 0)
{
_TRACE("Algo Status: Disabled");
StaticVarSet(static_algo_status, 0);
}
}
/* Display Current Settings */
GfxSetTextColor(colorWhite);
GfxSelectFont("Arial", 10, 400);
GfxTextOut("Underlying: " + underlying + " | Expiry: " + expiry, x0, y0 + 25);
GfxTextOut("Quantity: " + NumToStr(quantity, 1.0) + " | Product: " + product + " | Price Type: " + pricetype, x0, y0 + 42);
/* Trade Mode Display - Large and Prominent */
GfxSelectFont("Arial", 22, 900); /* Extra large and bold */
if (trade_mode == "BUY")
{
GfxSetTextColor(colorBrightGreen);
}
else
{
GfxSetTextColor(colorRed);
}
GfxTextOut("MODE: " + trade_mode, x0, y0 + 60);
/* Button Layout */
btnY = y0 + 95; /* Adjusted to accommodate larger MODE display */
btnW = 110;
btnH = 40;
btnGap = 15;
/* --------------------------------------------------------------------------
Create GUI Buttons - Grid Layout
-------------------------------------------------------------------------- */
if (EnableAlgo == "Enable")
{
/* ========== CALL OPTION (CE) BUTTONS ========== */
/* Row 1: CE Entry Buttons */
GuiButton("ATM CE", ++IDset, x0, btnY, btnW, btnH, notifyClicked);
btnATM_CE = IDset;
GuiSetColors(btnATM_CE, btnATM_CE, 1, colorWhite, colorGreen, colorWhite);
GuiButton("ITM CE", ++IDset, x0 + (btnW + btnGap), btnY, btnW, btnH, notifyClicked);
btnITM_CE = IDset;
GuiSetColors(btnITM_CE, btnITM_CE, 1, colorWhite, colorRed, colorWhite);
GuiButton("OTM CE", ++IDset, x0 + (btnW + btnGap) * 2, btnY, btnW, btnH, notifyClicked);
btnOTM_CE = IDset;
GuiSetColors(btnOTM_CE, btnOTM_CE, 1, colorWhite, colorBlue, colorWhite);
/* Row 2: CE Exit Buttons */
GuiButton("x ATM CE", ++IDset, x0, btnY + btnH + btnGap, btnW, btnH, notifyClicked);
btnExitATM_CE = IDset;
GuiSetColors(btnExitATM_CE, btnExitATM_CE, 1, colorWhite, colorGreen, colorWhite);
GuiButton("x ITM CE", ++IDset, x0 + (btnW + btnGap), btnY + btnH + btnGap, btnW, btnH, notifyClicked);
btnExitITM_CE = IDset;
GuiSetColors(btnExitITM_CE, btnExitITM_CE, 1, colorWhite, colorRed, colorWhite);
GuiButton("x OTM CE", ++IDset, x0 + (btnW + btnGap) * 2, btnY + btnH + btnGap, btnW, btnH, notifyClicked);
btnExitOTM_CE = IDset;
GuiSetColors(btnExitOTM_CE, btnExitOTM_CE, 1, colorWhite, colorBlue, colorWhite);
/* ========== PUT OPTION (PE) BUTTONS ========== */
peY = btnY + (btnH + btnGap) * 2 + 20;
/* Row 3: PE Entry Buttons */
GuiButton("ATM PE", ++IDset, x0, peY, btnW, btnH, notifyClicked);
btnATM_PE = IDset;
GuiSetColors(btnATM_PE, btnATM_PE, 1, colorWhite, colorGreen, colorWhite);
GuiButton("ITM PE", ++IDset, x0 + (btnW + btnGap), peY, btnW, btnH, notifyClicked);
btnITM_PE = IDset;
GuiSetColors(btnITM_PE, btnITM_PE, 1, colorWhite, colorRed, colorWhite);
GuiButton("OTM PE", ++IDset, x0 + (btnW + btnGap) * 2, peY, btnW, btnH, notifyClicked);
btnOTM_PE = IDset;
GuiSetColors(btnOTM_PE, btnOTM_PE, 1, colorWhite, colorBlue, colorWhite);
/* Row 4: PE Exit Buttons */
GuiButton("x ATM PE", ++IDset, x0, peY + btnH + btnGap, btnW, btnH, notifyClicked);
btnExitATM_PE = IDset;
GuiSetColors(btnExitATM_PE, btnExitATM_PE, 1, colorWhite, colorGreen, colorWhite);
GuiButton("x ITM PE", ++IDset, x0 + (btnW + btnGap), peY + btnH + btnGap, btnW, btnH, notifyClicked);
btnExitITM_PE = IDset;
GuiSetColors(btnExitITM_PE, btnExitITM_PE, 1, colorWhite, colorRed, colorWhite);
GuiButton("x OTM PE", ++IDset, x0 + (btnW + btnGap) * 2, peY + btnH + btnGap, btnW, btnH, notifyClicked);
btnExitOTM_PE = IDset;
GuiSetColors(btnExitOTM_PE, btnExitOTM_PE, 1, colorWhite, colorBlue, colorWhite);
/* ========== CLOSE ALL BUTTON ========== */
closeY = peY + (btnH + btnGap) * 2 + 20;
GuiButton("CLOSE ALL", ++IDset, x0, closeY, btnW + 20, btnH, notifyClicked);
btnCloseAll = IDset;
GuiSetColors(btnCloseAll, btnCloseAll, 1, colorWhite, colorDarkRed, colorWhite);
/* Display Order Status */
statusY = closeY + btnH + 30;
GfxSelectFont("Arial", 16, 900);
GfxSetTextColor(colorYellow);
GfxTextOut("Order Status:", x0, statusY);
GfxSetTextColor(colorWhite);
ATM_CE_status = WriteIf(Nz(StaticVarGet(static_ATM_CE_order), 0) == 1, "ACTIVE", "NONE");
ITM_CE_status = WriteIf(Nz(StaticVarGet(static_ITM_CE_order), 0) == 1, "ACTIVE", "NONE");
OTM_CE_status = WriteIf(Nz(StaticVarGet(static_OTM_CE_order), 0) == 1, "ACTIVE", "NONE");
ATM_PE_status = WriteIf(Nz(StaticVarGet(static_ATM_PE_order), 0) == 1, "ACTIVE", "NONE");
ITM_PE_status = WriteIf(Nz(StaticVarGet(static_ITM_PE_order), 0) == 1, "ACTIVE", "NONE");
OTM_PE_status = WriteIf(Nz(StaticVarGet(static_OTM_PE_order), 0) == 1, "ACTIVE", "NONE");
GfxTextOut("CE Orders - ATM: " + ATM_CE_status + " | ITM: " + ITM_CE_status + " | OTM: " + OTM_CE_status, x0, statusY + 20);
GfxTextOut("PE Orders - ATM: " + ATM_PE_status + " | ITM: " + ITM_PE_status + " | OTM: " + OTM_PE_status, x0, statusY + 40);
/* --------------------------------------------------------------------------
Event Loop - Process Button Clicks
-------------------------------------------------------------------------- */
for (i = 0; (cid = GuiGetEvent(i, 0)) > 0; i++)
{
if (GuiGetEvent(i, 1) == notifyClicked)
{
/* ========== CALL OPTION (CE) ENTRIES ========== */
/* ATM CE Entry */
if (cid == btnATM_CE AND Nz(StaticVarGet(static_ATM_CE_order), 0) == 0)
{
_TRACE("Placing ATM CE Entry Order - Mode: " + trade_mode);
response = PostOptionOrder(ATM_offset, "CE", trade_mode, quantity, apikey, strategy, underlying, exchange, expiry, pricetype, product, price, trigger_price, host, ver);
symbol_resolved = ExtractJsonValue(response, "symbol");
exchange_resolved = ExtractJsonValue(response, "exchange");
/* Clean up any trailing quotes that might have been extracted */
symbol_resolved = StrReplace(symbol_resolved, "\"", "");
exchange_resolved = StrReplace(exchange_resolved, "\"", "");
_TRACEF("Cleaned Symbol: [%s]", symbol_resolved);
_TRACEF("Cleaned Exchange: [%s]", exchange_resolved);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success AND symbol_resolved != "")
{
StaticVarSet(static_ATM_CE_order, 1);
StaticVarSetText(static_ATM_CE_symbol, symbol_resolved, True);
StaticVarSetText(static_ATM_CE_exchange, exchange_resolved, True);
if (VoiceAlert == "Enable")
{
Say("ATM CE " + trade_mode + " Order Placed");
}
_TRACEF("ATM CE Order Success: %s on %s", symbol_resolved, exchange_resolved);
}
else
{
_TRACE("ATM CE Order Failed");
if (VoiceAlert == "Enable")
{
Say("ATM CE Order Failed");
}
}
}
/* ITM CE Entry */
if (cid == btnITM_CE AND Nz(StaticVarGet(static_ITM_CE_order), 0) == 0)
{
_TRACE("Placing ITM CE Entry Order - Mode: " + trade_mode);
response = PostOptionOrder(ITM_offset, "CE", trade_mode, quantity, apikey, strategy, underlying, exchange, expiry, pricetype, product, price, trigger_price, host, ver);
symbol_resolved = ExtractJsonValue(response, "symbol");
exchange_resolved = ExtractJsonValue(response, "exchange");
/* Clean up any trailing quotes */
symbol_resolved = StrReplace(symbol_resolved, "\"", "");
exchange_resolved = StrReplace(exchange_resolved, "\"", "");
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success AND symbol_resolved != "")
{
StaticVarSet(static_ITM_CE_order, 1);
StaticVarSetText(static_ITM_CE_symbol, symbol_resolved, True);
StaticVarSetText(static_ITM_CE_exchange, exchange_resolved, True);
if (VoiceAlert == "Enable")
{
Say("ITM CE " + trade_mode + " Order Placed");
}
_TRACEF("ITM CE Order Success: %s on %s", symbol_resolved, exchange_resolved);
}
else
{
_TRACE("ITM CE Order Failed");
if (VoiceAlert == "Enable")
{
Say("ITM CE Order Failed");
}
}
}
/* OTM CE Entry */
if (cid == btnOTM_CE AND Nz(StaticVarGet(static_OTM_CE_order), 0) == 0)
{
_TRACE("Placing OTM CE Entry Order - Mode: " + trade_mode);
response = PostOptionOrder(OTM_offset, "CE", trade_mode, quantity, apikey, strategy, underlying, exchange, expiry, pricetype, product, price, trigger_price, host, ver);
symbol_resolved = ExtractJsonValue(response, "symbol");
exchange_resolved = ExtractJsonValue(response, "exchange");
/* Clean up any trailing quotes */
symbol_resolved = StrReplace(symbol_resolved, "\"", "");
exchange_resolved = StrReplace(exchange_resolved, "\"", "");
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success AND symbol_resolved != "")
{
StaticVarSet(static_OTM_CE_order, 1);
StaticVarSetText(static_OTM_CE_symbol, symbol_resolved, True);
StaticVarSetText(static_OTM_CE_exchange, exchange_resolved, True);
if (VoiceAlert == "Enable")
{
Say("OTM CE " + trade_mode + " Order Placed");
}
_TRACEF("OTM CE Order Success: %s on %s", symbol_resolved, exchange_resolved);
}
else
{
_TRACE("OTM CE Order Failed");
if (VoiceAlert == "Enable")
{
Say("OTM CE Order Failed");
}
}
}
/* ========== CALL OPTION (CE) EXITS ========== */
/* ATM CE Exit */
if (cid == btnExitATM_CE AND Nz(StaticVarGet(static_ATM_CE_order), 0) == 1)
{
exit_symbol = StaticVarGetText(static_ATM_CE_symbol);
exit_exchange = StaticVarGetText(static_ATM_CE_exchange);
if (exit_symbol != "" AND exit_exchange != "")
{
_TRACE("Placing ATM CE Exit Order");
response = ExitPosition(exit_symbol, exit_exchange, product, apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
StaticVarSet(static_ATM_CE_order, 0);
StaticVarSetText(static_ATM_CE_symbol, "");
StaticVarSetText(static_ATM_CE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("ATM CE Exit Placed");
}
_TRACE("ATM CE Exit Success");
}
else
{
_TRACE("ATM CE Exit Failed");
if (VoiceAlert == "Enable")
{
Say("ATM CE Exit Failed");
}
}
}
else
{
_TRACE("ATM CE Exit Failed - No symbol information");
}
}
/* ITM CE Exit */
if (cid == btnExitITM_CE AND Nz(StaticVarGet(static_ITM_CE_order), 0) == 1)
{
_TRACE("Placing ITM CE Exit Order using PlaceSmartOrder");
exit_symbol = StaticVarGetText(static_ITM_CE_symbol);
exit_exchange = StaticVarGetText(static_ITM_CE_exchange);
if (exit_symbol != "" AND exit_exchange != "")
{
response = ExitPosition(exit_symbol, exit_exchange, product, apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
StaticVarSet(static_ITM_CE_order, 0);
StaticVarSetText(static_ITM_CE_symbol, "");
StaticVarSetText(static_ITM_CE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("ITM CE Exit Placed");
}
_TRACE("ITM CE Exit Success");
}
else
{
_TRACE("ITM CE Exit Failed");
if (VoiceAlert == "Enable")
{
Say("ITM CE Exit Failed");
}
}
}
else
{
_TRACE("ITM CE Exit Failed - No symbol information");
if (VoiceAlert == "Enable")
{
Say("No ITM CE Position Found");
}
}
}
/* OTM CE Exit */
if (cid == btnExitOTM_CE AND Nz(StaticVarGet(static_OTM_CE_order), 0) == 1)
{
_TRACE("Placing OTM CE Exit Order using PlaceSmartOrder");
exit_symbol = StaticVarGetText(static_OTM_CE_symbol);
exit_exchange = StaticVarGetText(static_OTM_CE_exchange);
if (exit_symbol != "" AND exit_exchange != "")
{
response = ExitPosition(exit_symbol, exit_exchange, product, apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
StaticVarSet(static_OTM_CE_order, 0);
StaticVarSetText(static_OTM_CE_symbol, "");
StaticVarSetText(static_OTM_CE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("OTM CE Exit Placed");
}
_TRACE("OTM CE Exit Success");
}
else
{
_TRACE("OTM CE Exit Failed");
if (VoiceAlert == "Enable")
{
Say("OTM CE Exit Failed");
}
}
}
else
{
_TRACE("OTM CE Exit Failed - No symbol information");
if (VoiceAlert == "Enable")
{
Say("No OTM CE Position Found");
}
}
}
/* ========== PUT OPTION (PE) ENTRIES ========== */
/* ATM PE Entry */
if (cid == btnATM_PE AND Nz(StaticVarGet(static_ATM_PE_order), 0) == 0)
{
_TRACE("Placing ATM PE Entry Order - Mode: " + trade_mode);
response = PostOptionOrder(ATM_offset, "PE", trade_mode, quantity, apikey, strategy, underlying, exchange, expiry, pricetype, product, price, trigger_price, host, ver);
symbol_resolved = ExtractJsonValue(response, "symbol");
exchange_resolved = ExtractJsonValue(response, "exchange");
/* Clean up any trailing quotes */
symbol_resolved = StrReplace(symbol_resolved, "\"", "");
exchange_resolved = StrReplace(exchange_resolved, "\"", "");
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success AND symbol_resolved != "")
{
StaticVarSet(static_ATM_PE_order, 1);
StaticVarSetText(static_ATM_PE_symbol, symbol_resolved, True);
StaticVarSetText(static_ATM_PE_exchange, exchange_resolved, True);
if (VoiceAlert == "Enable")
{
Say("ATM PE " + trade_mode + " Order Placed");
}
_TRACEF("ATM PE Order Success: %s on %s", symbol_resolved, exchange_resolved);
}
else
{
_TRACE("ATM PE Order Failed");
if (VoiceAlert == "Enable")
{
Say("ATM PE Order Failed");
}
}
}
/* ITM PE Entry */
if (cid == btnITM_PE AND Nz(StaticVarGet(static_ITM_PE_order), 0) == 0)
{
_TRACE("Placing ITM PE Entry Order - Mode: " + trade_mode);
response = PostOptionOrder(ITM_offset, "PE", trade_mode, quantity, apikey, strategy, underlying, exchange, expiry, pricetype, product, price, trigger_price, host, ver);
symbol_resolved = ExtractJsonValue(response, "symbol");
exchange_resolved = ExtractJsonValue(response, "exchange");
/* Clean up any trailing quotes */
symbol_resolved = StrReplace(symbol_resolved, "\"", "");
exchange_resolved = StrReplace(exchange_resolved, "\"", "");
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success AND symbol_resolved != "")
{
StaticVarSet(static_ITM_PE_order, 1);
StaticVarSetText(static_ITM_PE_symbol, symbol_resolved, True);
StaticVarSetText(static_ITM_PE_exchange, exchange_resolved, True);
if (VoiceAlert == "Enable")
{
Say("ITM PE " + trade_mode + " Order Placed");
}
_TRACEF("ITM PE Order Success: %s on %s", symbol_resolved, exchange_resolved);
}
else
{
_TRACE("ITM PE Order Failed");
if (VoiceAlert == "Enable")
{
Say("ITM PE Order Failed");
}
}
}
/* OTM PE Entry */
if (cid == btnOTM_PE AND Nz(StaticVarGet(static_OTM_PE_order), 0) == 0)
{
_TRACE("Placing OTM PE Entry Order - Mode: " + trade_mode);
response = PostOptionOrder(OTM_offset, "PE", trade_mode, quantity, apikey, strategy, underlying, exchange, expiry, pricetype, product, price, trigger_price, host, ver);
symbol_resolved = ExtractJsonValue(response, "symbol");
exchange_resolved = ExtractJsonValue(response, "exchange");
/* Clean up any trailing quotes */
symbol_resolved = StrReplace(symbol_resolved, "\"", "");
exchange_resolved = StrReplace(exchange_resolved, "\"", "");
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success AND symbol_resolved != "")
{
StaticVarSet(static_OTM_PE_order, 1);
StaticVarSetText(static_OTM_PE_symbol, symbol_resolved, True);
StaticVarSetText(static_OTM_PE_exchange, exchange_resolved, True);
if (VoiceAlert == "Enable")
{
Say("OTM PE " + trade_mode + " Order Placed");
}
_TRACEF("OTM PE Order Success: %s on %s", symbol_resolved, exchange_resolved);
}
else
{
_TRACE("OTM PE Order Failed");
if (VoiceAlert == "Enable")
{
Say("OTM PE Order Failed");
}
}
}
/* ========== PUT OPTION (PE) EXITS ========== */
/* ATM PE Exit */
if (cid == btnExitATM_PE AND Nz(StaticVarGet(static_ATM_PE_order), 0) == 1)
{
_TRACE("Placing ATM PE Exit Order using PlaceSmartOrder");
exit_symbol = StaticVarGetText(static_ATM_PE_symbol);
exit_exchange = StaticVarGetText(static_ATM_PE_exchange);
if (exit_symbol != "" AND exit_exchange != "")
{
response = ExitPosition(exit_symbol, exit_exchange, product, apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
StaticVarSet(static_ATM_PE_order, 0);
StaticVarSetText(static_ATM_PE_symbol, "");
StaticVarSetText(static_ATM_PE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("ATM PE Exit Placed");
}
_TRACE("ATM PE Exit Success");
}
else
{
_TRACE("ATM PE Exit Failed");
if (VoiceAlert == "Enable")
{
Say("ATM PE Exit Failed");
}
}
}
else
{
_TRACE("ATM PE Exit Failed - No symbol information");
if (VoiceAlert == "Enable")
{
Say("No ATM PE Position Found");
}
}
}
/* ITM PE Exit */
if (cid == btnExitITM_PE AND Nz(StaticVarGet(static_ITM_PE_order), 0) == 1)
{
_TRACE("Placing ITM PE Exit Order using PlaceSmartOrder");
exit_symbol = StaticVarGetText(static_ITM_PE_symbol);
exit_exchange = StaticVarGetText(static_ITM_PE_exchange);
if (exit_symbol != "" AND exit_exchange != "")
{
response = ExitPosition(exit_symbol, exit_exchange, product, apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
StaticVarSet(static_ITM_PE_order, 0);
StaticVarSetText(static_ITM_PE_symbol, "");
StaticVarSetText(static_ITM_PE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("ITM PE Exit Placed");
}
_TRACE("ITM PE Exit Success");
}
else
{
_TRACE("ITM PE Exit Failed");
if (VoiceAlert == "Enable")
{
Say("ITM PE Exit Failed");
}
}
}
else
{
_TRACE("ITM PE Exit Failed - No symbol information");
if (VoiceAlert == "Enable")
{
Say("No ITM PE Position Found");
}
}
}
/* OTM PE Exit */
if (cid == btnExitOTM_PE AND Nz(StaticVarGet(static_OTM_PE_order), 0) == 1)
{
_TRACE("Placing OTM PE Exit Order using PlaceSmartOrder");
exit_symbol = StaticVarGetText(static_OTM_PE_symbol);
exit_exchange = StaticVarGetText(static_OTM_PE_exchange);
if (exit_symbol != "" AND exit_exchange != "")
{
response = ExitPosition(exit_symbol, exit_exchange, product, apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
StaticVarSet(static_OTM_PE_order, 0);
StaticVarSetText(static_OTM_PE_symbol, "");
StaticVarSetText(static_OTM_PE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("OTM PE Exit Placed");
}
_TRACE("OTM PE Exit Success");
}
else
{
_TRACE("OTM PE Exit Failed");
if (VoiceAlert == "Enable")
{
Say("OTM PE Exit Failed");
}
}
}
else
{
_TRACE("OTM PE Exit Failed - No symbol information");
if (VoiceAlert == "Enable")
{
Say("No OTM PE Position Found");
}
}
}
/* ========== CLOSE ALL POSITIONS ========== */
if (cid == btnCloseAll)
{
_TRACE("Closing All Positions");
response = CloseAllPositions(apikey, strategy, host, ver);
has_success = StrFind(response, "\"status\":\"success\"") >= 0;
if (has_success)
{
/* Reset all CE order tracking */
StaticVarSet(static_ATM_CE_order, 0);
StaticVarSet(static_ITM_CE_order, 0);
StaticVarSet(static_OTM_CE_order, 0);
StaticVarSetText(static_ATM_CE_symbol, "");
StaticVarSetText(static_ITM_CE_symbol, "");
StaticVarSetText(static_OTM_CE_symbol, "");
StaticVarSetText(static_ATM_CE_exchange, "");
StaticVarSetText(static_ITM_CE_exchange, "");
StaticVarSetText(static_OTM_CE_exchange, "");
/* Reset all PE order tracking */
StaticVarSet(static_ATM_PE_order, 0);
StaticVarSet(static_ITM_PE_order, 0);
StaticVarSet(static_OTM_PE_order, 0);
StaticVarSetText(static_ATM_PE_symbol, "");
StaticVarSetText(static_ITM_PE_symbol, "");
StaticVarSetText(static_OTM_PE_symbol, "");
StaticVarSetText(static_ATM_PE_exchange, "");
StaticVarSetText(static_ITM_PE_exchange, "");
StaticVarSetText(static_OTM_PE_exchange, "");
if (VoiceAlert == "Enable")
{
Say("All Positions Closed");
}
_TRACE("Close All Success");
}
else
{
_TRACE("Close All Failed");
if (VoiceAlert == "Enable")
{
Say("Close All Failed");
}
}
}
}
}
}
/* --------------------------------------------------------------------------
Chart Display
-------------------------------------------------------------------------- */
SetChartOptions(0, chartShowArrows | chartShowDates);
Plot(Close, "Close", colorDefault, styleNoTitle | ParamStyle("Style") | GetPriceStyle());
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}",
O, H, L, C, SelectedValue(ROC(C, 1))));
}
_SECTION_END();
VB Script Module (Legacy Method)
Last updated