/********************************* Provided By: Interactive Data Corporation (Copyright © 2012) All rights reserved. This sample eSignal Formula Script (EFS) is for educational purposes only. Interactive Data Corporation reserves the right to modify and overwrite this EFS file with each new release. Description: EFS Automated Trading Sample Script Version: 2.02 09/25/2014 Formula Parameters: Default: Clear Log Before Print true Order Quantity 100 OnOrderUpdate false OnPositionUpdate false OnExecutionUpdate false OnAccountUpdate false OnConnectionUpdate false OnSymbolMap false OnQuoteUpdate false LetUsLearn NOTE: I deleted everything from "EFSAT_EXAMPLE - Automated Trading 2013" above the fuction below so as to make this a library file. **********************************/ function drawButtons() { BUTTONS_FONT = "courier new"; var i = 0; // Trade buttons var offsetX = 300; var offsetY = 25; var distance = 25; // distance between lines var n = 0; drawTextPixel(offsetX, offsetY, " Buy @URL=EFS:buyCallback", Color.teal , Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance, " Sell @URL=EFS:sellCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 65, offsetY, " Buy Stop @URL=EFS:buyStopCallback", Color.teal , Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 65, offsetY + distance, " Sell Stop @URL=EFS:sellStopCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 180, offsetY, " Buy Limit @URL=EFS:buyLimitCallback", Color.teal , Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 180, offsetY + distance, " Sell Limit @URL=EFS:sellLimitCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 305, offsetY, " Buy StopLimit @URL=EFS:buyStopLimitCallback", Color.teal, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 305, offsetY + distance, " Sell StopLimit @URL=EFS:sellStopLimitCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 470, offsetY, " Buy StopLimitTicks @URL=EFS:buyLimitTicksCallback", Color.teal, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 470, offsetY + distance, " Sell StopLimitTicks @URL=EFS:sellLimitTicksCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); // end // Close buttons offsetX = 670; n = 3; drawTextPixel(offsetX, offsetY + distance * (n++), " Close And Cancel All @URL=EFS:closeAndCancelAllCallback", Color.red, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Close And Cancel Shorts @URL=EFS:closeAndCancelShortsCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Close And Cancel Longs @URL=EFS:closeAndCancelLongsCallback", Color.teal, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); offsetX = 500; //offsetY = 10; n = 3; drawTextPixel(offsetX, offsetY + distance * (n++), " Cancel All @URL=EFS:cancelAllCallback", Color.red, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Cancel Shorts @URL=EFS:cancelShortCallback", Color.RGB(155, 0, 255), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Cancel Longs @URL=EFS:cancelLongCallback", Color.teal, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); offsetX = 300; n = 3; drawTextPixel(offsetX, offsetY + distance * (n++), " Reverse Position @URL=EFS:reverseCallback", Color.red, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Close Position @URL=EFS:closeCallback", Color.red, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); // end // Buttons for printing information to FO offsetX = 10; n = 0; drawTextPixel(offsetX, offsetY + distance * (n), " Executions @URL=EFS:executionsStandartCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 135, offsetY + distance * (n++), " Full info @URL=EFS:executionsFullCallback", Color.blue, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n), " Orders @URL=EFS:ordersStandartCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 135, offsetY + distance * (n++), " Full info @URL=EFS:ordersFullCallback", Color.blue, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n), " Positions @URL=EFS:positionsStandartCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 135, offsetY + distance * (n), " Full info @URL=EFS:positionsFullCallback", Color.blue, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 290, offsetY + distance * (n), " Get Current MMS @URL=EFS:PrintCurrentMMSCallback", Color.blue, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 470, offsetY + distance * (n++), " Apply MMS @URL=EFS:ApplyMMSOnPositionCallback", Color.blue, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n), " Accounts @URL=EFS:accountsStandartCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 135, offsetY + distance * (n++), " Full info @URL=EFS:accountsFullCallback", Color.blue, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Connections @URL=EFS:connectionsCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Quote @URL=EFS:quoteCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n++), " Symbol @URL=EFS:symbolCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); n = 8; drawTextPixel(offsetX, offsetY + distance * (n++), " CurrentAccount @URL=EFS:currentAccountCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n), " CurrentConnection @URL=EFS:currentConnectionCallback", Color.green, Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); // end // Connection buttons offsetX = 235; n = 8; drawTextPixel(offsetX, offsetY + distance * (n), " Disconnect @URL=EFS:disconnectCallback", Color.RGB(250, 50, 0), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 150, offsetY + distance * (n++), " UnSubscribeQuote @URL=EFS:unSubscribeCallback", Color.RGB(250, 50, 0), Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX, offsetY + distance * (n), " Connect @URL=EFS:connectCallback", Color.RGB(200, 120, 0) , Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); drawTextPixel(offsetX + 150 , offsetY + distance * (n++), " SubscribeQuote @URL=EFS:subscribeCallback", Color.RGB(200, 120, 0) , Color.white, Text.BUTTON | Text.RELATIVETOLEFT | Text.RELATIVETOTOP, BUTTONS_FONT, 12, i++); // end } // ============================== Trade Buttons Callbacks ============================== function buyCallback() { if (bClearLog) debugClear(); // The OrderID will be returned by the method if not specified as a parameter for tracking purposes. // When the OrderID is specified the execution of the order is an order modification execution. // In such case, if some of optional parameters are not specified, Trade Functions do not change them from the modifying order. // Trade.buyMarket( [Symbol] [, Qty] [, OrderID] [, Route] [, TIF] [, Account] [, Connection] ) var orderid = Trade.buyMarket(sSymbol, nQty); debugPrintln("Trade.buyMarket(" + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function buyStopCallback() { if (bClearLog) debugClear(); var stopPrice = close() + Symbol.minTick(sSymbol) * 10; // Trade.buyStop( Stop [, Symbol] [, Qty] [, OrderID] [, Route] [, TIF] [, Account] [, Connection] ) var orderid = Trade.buyStop(stopPrice, sSymbol, nQty); debugPrintln("Trade.buyStop(" + stopPrice + ", " + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function buyLimitCallback() { if (bClearLog) debugClear(); var limitPrice = close() - Symbol.minTick(sSymbol) * 10; // Trade.buyLimit( Limit [, Symbol] [, Qty] [, OrderID] [, Route] [, TIF] [, Account] [, Connection] ) var orderid = Trade.buyLimit(limitPrice, sSymbol, nQty); debugPrintln("Trade.buyLimit(" + limitPrice + ", " + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function buyStopLimitCallback() { if (bClearLog) debugClear(); var stopPrice = close() + Symbol.minTick(sSymbol) * 10; var limitPrice = close() + Symbol.minTick(sSymbol) * 20; // Trade.buyStopLimit( Stop , Limit [, Symbol] [, Qty] [, OrderID] [, Route] [,TIF] [, Account] [, Connection] ) var orderid = Trade.buyStopLimit(stopPrice, limitPrice, sSymbol, nQty); debugPrintln("Trade.buyStopLimit(" + stopPrice + ", " + limitPrice + ", " + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function buyLimitTicksCallback() { if (bClearLog) debugClear(); var stopPrice = close() + Symbol.minTick(sSymbol) * 10; var limitTicks = 10; // EFS Automated trading has an ability to specify parameters as JSON object using explicit names. // Parameters could be specified in any order within JSON object. // Compulsory parameters should be specified in object while optional could be ommited. var paramsObj = {stop: stopPrice, stopLimitTicks: limitTicks, symbol: sSymbol, qty: nQty}; // Trade.buyStopLimitTick ( Stop , StopLimitTicks [, Symbol] [, Qty] [, OrderID] [, Route] [,TIF] [, Account] [, Connection] ) var orderid = Trade.buyStopLimitTick(paramsObj); debugPrintln("Trade.buyStopLimitTick({stop: " + stopPrice + ", stopLimitTicks: " + limitTicks + ", symbol: " + sSymbol + ", qty: " + nQty + "})"); debugPrintln("OrderID: " + orderid); } function sellCallback() { if (bClearLog) debugClear(); // The OrderID will be returned by the method if not specified as a parameter for tracking purposes. // When the OrderID is specified the execution of the order is an order modification execution. // In such case, if some of optional parameters are not specified, Trade Functions do not change them from the modifying order. // Trade.sellMarket( [Symbol] [, Qty] [, OrderID] [, Route] [, TIF] [, Account] [, Connection] ) var orderid = Trade.sellMarket(sSymbol, nQty); debugPrintln("Trade.sellMarket(" + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function sellStopCallback() { if (bClearLog) debugClear(); var stopPrice = close() - Symbol.minTick(sSymbol) * 10; // Trade.sellStop( Stop [, Symbol] [, Qty] [, OrderID] [, Route] [, TIF] [, Account] [, Connection] ) var orderid = Trade.sellStop(stopPrice, sSymbol, nQty); debugPrintln("Trade.sellStop(" + stopPrice + ", " + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function sellLimitCallback() { if (bClearLog) debugClear(); var limitPrice = close() + Symbol.minTick(sSymbol) * 10; // Trade.sellLimit( Limit [, Symbol] [, Qty] [, OrderID] [, Route] [, TIF] [, Account] [, Connection] ) var orderid = Trade.sellLimit(limitPrice, sSymbol, nQty); debugPrintln("Trade.sellLimit(" + limitPrice + ", " + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function sellStopLimitCallback() { if (bClearLog) debugClear(); var stopPrice = close() - Symbol.minTick(sSymbol) * 10; var limitPrice = close() - Symbol.minTick(sSymbol) * 20; // Trade.sellStopLimit( Stop , Limit [, Symbol] [, Qty] [, OrderID] [, Route] [,TIF] [, Account] [, Connection] ) var orderid = Trade.sellStopLimit(stopPrice, limitPrice, sSymbol, nQty); debugPrintln("Trade.sellStopLimit(" + stopPrice + ", " + limitPrice + ", " + sSymbol + ", " + nQty + ")"); debugPrintln("OrderID: " + orderid); } function sellLimitTicksCallback() { if (bClearLog) debugClear(); var stopPrice = close() - Symbol.minTick(sSymbol) * 10; var limitTicks = 10; // EFS Automated trading has an ability to specify parameters as JSON object using explicit names. // Parameters could be specified in any order within JSON object. // Compulsory parameters should be specified in object while optional could be ommited. var paramsObj = {stop: stopPrice, stopLimitTicks: limitTicks, symbol: sSymbol, qty: nQty}; // Trade.sellStopLimitTick ( Stop , StopLimitTicks [, Symbol] [, Qty] [, OrderID] [, Route] [,TIF] [, Account] [, Connection] ) var orderid = Trade.sellStopLimitTick(paramsObj); debugPrintln("Trade.sellStopLimitTick({stop: " + stopPrice + ", stopLimitTicks: " + limitTicks + ", symbol: " + sSymbol + ", qty: " + nQty + "})"); debugPrintln("OrderID: " + orderid); } // ============================== END of Trade Buttons Callbacks ============================== // ============================== Connection Buttons Callbacks ============================== function subscribeCallback() { if (bClearLog) debugClear(); // Subscribes to quote updates for the specified symbol. Symbol.subscribeQuote(sSymbol); debugPrintln("Subscribed to " + sSymbol + " quote!") } function unSubscribeCallback() { if (bClearLog) debugClear(); // Unsubscribe the quote updates for the specified symbol. Symbol.unsubscribeQuote(sSymbol); debugPrintln("UnSubscribed from " + sSymbol + " quote!") } function connectCallback() { if (bClearLog) debugClear(); var connection = Trade.getCurrentConnectionID(); // Invokes a connection attempt for the default or specified connection ID. Connection.connect(connection); debugPrintln(connection + " isConnected == " + Connection.isConnected(connection)); } function disconnectCallback() { if (bClearLog) debugClear(); var connection = Trade.getCurrentConnectionID(); // Disconnect the default or specified connection ID. Connection.disconnect(connection); debugPrintln(connection + " isConnected == " + Connection.isConnected(connection)); } // ============================== END of Connection Buttons Callbacks ============================== // ============================== Information Buttons Callbacks ============================== function ordersStandartCallback() { ordersCallback(false); } function ordersFullCallback() { ordersCallback(true); } function ordersCallback(bFull) { if (bClearLog) debugClear(); // Returns an array of order IDs for the specified order states, // symbol, account and/or connection. If all parameters are not defined, // EFS uses the chart’s main symbol and currently selected account/connection. var orders = Trade.getOrders(Order.STATE_FILLED | Order.STATE_WORKING | Order.STATE_CANCELED | Order.STATE_CANCELING | Order.STATE_PARTIALFILL | Order.STATE_PLACING | Order.STATE_REJECTED | Order.STATE_UPDATING, null, null, Trade.getCurrentConnectionID()); if (orders == null) return; for (i = 0; i < orders.length; i++) { if (bFull) PrintFullOrder(orders[i]) else PrintOrder(orders[i]); } } function positionsStandartCallback() { positionsCallback(false); } function positionsFullCallback() { positionsCallback(true); } function positionsCallback(bFull) { if (bClearLog) debugClear(); // Returns an array of position IDs for the specified symbol, account and/or connection. var positions = Trade.getPositions(null, null, Trade.getCurrentConnectionID()); if (positions == null) return; for (i = 0; i < positions.length; i++) { if (bFull) PrintFullPosition(positions[i]) else PrintPosition(positions[i]); } } function accountsStandartCallback() { accountsCallback(false); } function accountsFullCallback() { accountsCallback(true); } function accountsCallback(bFull) { if (bClearLog) debugClear(); // Returns an array of accounts for the specified connection. // Returns an array of all accounts for all current connections // if the connection ID is not specified. var accounts = Trade.getAccounts(Trade.getCurrentConnectionID()); if (accounts == null) return; for (i = 0; i < accounts.length; i++) { if (bFull) PrintFullAccount(accounts[i]) else PrintAccount(accounts[i]); } } function currentAccountCallback() { if (bClearLog) debugClear(); // Returns default account. var account = Trade.getCurrentAccount( ) if (account == null) return; PrintAccount(account); } function executionsStandartCallback() { executionsCallback(false); } function executionsFullCallback() { executionsCallback(true); } function executionsCallback(bFull) { if (bClearLog) debugClear(); // Returns an array of execution IDs for the specified symbol, // account and/or connection. If all parameters are not defined, // the function returns all executions. var executions = Trade.getExecutions(null, null, Trade.getCurrentConnectionID()); if (executions == null) return; for (i = 0; i < executions.length; i++) { if (bFull) PrintFullExecution(executions[i]) else PrintExecution(executions[i]); } } function quoteCallback() { if (bClearLog) debugClear(); PrintQuote(); } function connectionsCallback() { if (bClearLog) debugClear(); // Returns an array of connection ID strings for the specified BrokerID. // If the BrokerID is not specified, the returned array will include // all connection ID’s for all the current connections. var connections = Trade.getConnectionIDs(); if (connections == null) return; for (i = 0; i < connections.length; i++) PrintConnection(connections[i]); } function currentConnectionCallback() { if (bClearLog) debugClear(); // Returns default connection ID. var connection = Trade.getCurrentConnectionID ( ) if (connection == null) return; PrintConnection(connection); } function symbolCallback() { if (bClearLog) debugClear(); PrintSymbol(); } // ============================== END of Information Buttons Callbacks ============================== // ============================== Close Buttons Callbacks ============================== function closeCallback() { if (bClearLog) debugClear(); var position = Trade.getPosition(); if (position != null) if (!Position.isFlat(position)) { // Closes an open position for the specified position ID. Trade.closePosition(position); debugPrintln("Trade.closePosition(" + position + ")"); } else { debugPrintln("Position " + position + " is already closed!"); } } function reverseCallback() { if (bClearLog) debugClear(); var position = Trade.getPosition(); if (position != null) { if (!Position.isFlat(position)) { // Closes the existing position and opens reverse position with same quantity. Trade.reversePosition(position); debugPrintln("Trade.reversePosition(" + position + ")"); } else { debugPrintln("Position " + position + " is already closed!"); } } } function closeAndCancelAllCallback() { if (bClearLog) debugClear(); // Cancels all working orders and closes all open positions for the specified symbol, account and/or connection. // Trade.closeAndCancelAll ( [Symbol] [, Account] [, Connection] ) Trade.closeAndCancelAll(); debugPrintln("Trade.closeAndCancelAll()"); } function closeAndCancelLongsCallback() { if (bClearLog) debugClear(); // Closes all open long positions and cancels any related working orders // for long positions for the specified symbol, account and/or connection. // Trade.closeAndCancelLongs ( [Symbol] [, Account] [, Connection] ) Trade.closeAndCancelLongs(); debugPrintln("Trade.closeAndCancelLongs()"); } function closeAndCancelShortsCallback() { if (bClearLog) debugClear(); // Closes all open short positions and cancels any related working orders // for short positions for the specified symbol, account and/or connection // Trade.closeAndCancelShorts ( [Symbol] [, Account] [, Connection] ) Trade.closeAndCancelShorts(); debugPrintln("Trade.closeAndCancelShorts()"); } function cancelAllCallback() { if (bClearLog) debugClear(); // Cancels all working orders for the specified symbol, account and/or connection. // Trade.cancelAllOrders ( [Symbol] [, Account] [, Connection] ) Trade.cancelAllOrders(); debugPrintln("Trade.cancelAllOrders()"); } function cancelLongCallback() { if (bClearLog) debugClear(); // Cancels all long orders for the specified symbol, account and/or connection. // Trade.cancelAllLongs ( [Symbol] [, Account] [, Connection] ) Trade.cancelAllLongs(); debugPrintln("Trade.cancelAllLongs()"); } function cancelShortCallback() { if (bClearLog) debugClear(); // Cancels all short orders for the specified symbol, account and/or connection. // Trade.cancelAllShorts ( [Symbol] [, Account] [, Connection] ) Trade.cancelAllShorts(); debugPrintln("Trade.cancelAllShorts()"); } // ============================== END of Close Buttons Callbacks ============================== // ============================== Print functions ============================== function PrintQuote(symbol, connection) { // Quote object references the quote data for the subscribed symbol(s). // If any of the parameters are not specified, the function will use the current defaults. if (symbol == null) symbol = sSymbol; else if (!Symbol.isMapped(symbol)) { debugPrintln(symbol + " is not mapped to dictionary!"); return; } if (connection == null) connection = Trade.getCurrentConnectionID(); else if (!Trade.getConnectionIDs().contains(connection)) { debugPrintln("Not valid connection: " + connection); return; } // Returns last trade’s time stamp for the specified symbol. debugPrintln("LastTime: " + Quote.lastTime(symbol, connection)); // Returns ask time stamp for the specified symbol. debugPrintln("AskTime: " + Quote.askTime(symbol, connection)); // Returns bid time stamp for the specified symbol. debugPrintln("BidTime: " + Quote.bidTime(symbol, connection)); // Returns the last traded price for the specified symbol. debugPrintln("Last: " + Quote.last(symbol, connection)); // Returns the ask price for the specified symbol. // If quote doesn’t contain an ask price, the function returns null. debugPrintln("Ask: " + Quote.ask(symbol, connection)); // Returns the bid price for the specified symbol. // If quote doesn’t contain a bid price, the function returns null. debugPrintln("Bid: " + Quote.bid(symbol, connection)); debugPrintln("=============== Quote: " + symbol + " | Connection: " + connection + " ============="); } function PrintExecution(executionID) { // Execution object references the specific properties of executions that have been processed by the broker. // Returns true if the specified execution ID action is 'sell' // or false if the execution action is 'buy'. debugPrintln("isSell: " + Execution.isSell(executionID)); // Returns true if the specified execution ID action is 'buy' // or false if the execution action is 'sell'. debugPrintln("isBuy: " + Execution.isBuy(executionID)); // Returns the time stamp of an execution for the specified execution ID. debugPrintln("Time: " + Execution.time(executionID)); // Returns the average price of an execution for the specified execution ID. debugPrintln("Price: " + Execution.price(executionID)); // Returns the number of shares/contracts of an execution for the specified execution ID. debugPrintln("Qty: " + Execution.qty(executionID)); // Returns the connection name for the specified execution ID. debugPrintln("Connection: " + Execution.connection(executionID)); // Returns the account name for the specified execution ID. debugPrintln("Account: " + Execution.account(executionID)); // Returns the symbol for the specified execution ID. debugPrintln("Symbol: " + Execution.symbol(executionID)); debugPrintln("=============== Execution: " + executionID + " ============="); } function PrintFullExecution(executionID) { var executionFields = Execution.getFieldNames(); for (j = executionFields.length - 1; j >= 0; j--) { debugPrintln(executionFields[j] + ": " + Execution.getFieldValue(executionFields[j], executionID)); } debugPrintln("=============== Execution: " + executionID + " ============="); } function PrintSymbol(symbol, connection) { // Symbol object references the symbol properties and methods for quote subscription, // which can be used within the onQuoteChanged() callback and other trading purposes. if (symbol == null) symbol = sSymbol; else if (!Symbol.isMapped(symbol)) { debugPrintln(symbol + " is not mapped to dictionary!"); return; } if (connection == null) connection = Trade.getCurrentConnectionID(); else if (!Trade.getConnectionIDs().contains(connection)) { debugPrintln("Not valid connection: " + connection); return; } // Returns true if the script is subscribed on quote updates for the specified symbol, otherwise returns false. debugPrintln("isQuoteSubscribed:" + Symbol.isQuoteSubscribed (symbol, connection)); // Returns lot size value for the specified symbol. debugPrintln("Lot Size: " + Symbol.lotSize(symbol, connection)); // Returns point value for the specified symbol. debugPrintln("Point Value: " + Symbol.pointValue(symbol, connection)); // Returns minimum price increment (or tick value) for the specified symbol. debugPrintln("MinTick: " + Symbol.minTick(symbol, connection)); // Returns true if the specified symbol is mapped, otherwise returns false. debugPrintln("isMapped: " + Symbol.isMapped(symbol, connection)); debugPrintln("=============== Symbol: " + symbol + " | Connection: " + connection + " ============="); } function PrintAccount(account, connection) { // Account object references broker account information. if (account == null) account = Trade.getCurrentAccount(); else if (!Trade.getAccounts().contains(account)) { debugPrintln("Not valid account: " + account); return; } if (connection == null) connection = Trade.getCurrentConnectionID(); else if (!Trade.getConnectionIDs().contains(connection)) { debugPrintln("Not valid connection: " + connection); return; } // Returns the open PL for the specified account // or default account if parameters are not specified. debugPrintln("OpenPL: " + Account.openPL(account, connection)); // Returns the open PL for all long positions in the specified // account or default account if parameters are not specified. debugPrintln("OpenPLLong: " + Account.openPLLong(account, connection)); // Returns the open PL for all short positions for the specified // account or default account if parameters are not specified. debugPrintln("OpenPLShort: " + Account.openPLShort(account, connection)); // Returns the realized PL for the specified account // or default account if parameters are not specified. debugPrintln("RealizedPL: " + Account.realizedPL(account, connection)); // Returns the total PL for the specified account // or default account if parameters are not specified. debugPrintln("TotalPL: " + Account.totalPL(account, connection)); // Returns the current account balance. debugPrintln("Balance: " + Account.balance(account, connection)); debugPrintln("================= Account: " + account + " | Connection: " + connection + " ============="); } function PrintFullAccount(account) { if (account == null) account = Trade.getCurrentAccount(); else if (!Trade.getAccounts().contains(account)) { debugPrintln("Not valid account: " + account); return; } var accountFields = Account.getFieldNames(); for (j = accountFields.length - 1; j >= 0; j--) { debugPrintln(accountFields[j] + " = " + Account.getFieldValue(accountFields[j], account)); } debugPrintln("=============== Account: " + account + " ============="); } function PrintConnection(connection) { if (connection == null) connection = Trade.getCurrentConnectionID(); else if (!Trade.getConnectionIDs().contains(connection)) { debugPrintln("Not valid connection: " + connection); return; } // Returns the build number of the current broker plug-in. debugPrintln("BuildVersion: " + Connection.buildVersion(connection)); // Returns the broker ID or acronym (i.e. "IB" for Interactive Brokers, // “CQG” for eSignal Futures Trader) so that the EFS environment can be aware of // what broker the formula's trades will be routed to. // This function can be used in conditional statements to set global variables // to various values that may be broker specific, such as routes. debugPrintln("BrokerID: " + Connection.brokerID(connection)); // Returns true/false based on the connection state // of the specified or default connection ID. debugPrintln("isConnected: " + Connection.isConnected(connection)); // Returns false if connected with DEMO account, // true if connected with LIVE account, and null if not connected. debugPrintln("isLiveTrading: " + Connection.isLiveTrading(connection)); debugPrintln("================= Connection: " + connection + " =============="); } function PrintPosition(positionID) { //Position object references the specific properties of positions that have been processed by the broker. // Returns percent of the open P&L for the specified position ID. debugPrintln("openPLPct : " + Position.openPLPct (positionID)); // Returns the open P&L in ticks for the specified position ID. debugPrintln("openPLTicks : " + Position.openPLTicks (positionID)); // Returns the open P&L for the specified position ID. debugPrintln("OpenPL: " + Position.openPL(positionID)); // Returns the realized P&L for the specified position ID. debugPrintln("RealizedPL: " + Position.realizedPL(positionID)); // Returns the total P&L for the specified position ID. debugPrintln("TotalPL: " + Position.totalPL(positionID)); // Returns the pullback for the specified position ID. debugPrintln("Pullback: " + Position.pullback(positionID)); // Returns the pullback in ticks for the specified position ID. debugPrintln("PullbackTicks: " + Position.pullbackTicks(positionID)); // Returns the pullback in percentage for the specified position ID. debugPrintln("PullbackPct : " + Position.pullbackTicks(positionID)); // Returns true/false based on the long position status for the specified position ID. debugPrintln("IsLong: " + Position.isLong(positionID)); // Returns true/false based on the short position status for the specified position ID. debugPrintln("isShort: " + Position.isShort(positionID)); // Returns true/false based on the flat (or neutral) // position status for the specified position ID. debugPrintln("isFlat: " + Position.isFlat(positionID)); // Returns the Average Entry Price for the specified position ID. debugPrintln("Price: " + Position.price(positionID)); // Returns the current number of shares/contracts for the specified position ID. debugPrintln("Qty: " + Position.qty(positionID)); // Returns the connection name for the specified position ID. debugPrintln("Connection: " + Position.connection(positionID)); // Returns the account name for the specified position ID. debugPrintln("Account: " + Position.account(positionID)); // Returns the symbol for the specified position ID. debugPrintln("Symbol: " + Position.symbol(positionID)); debugPrintln("================= Position: " + positionID + " ============="); } function PrintFullPosition(positionID) { var positionFields = Position.getFieldNames(); for (j = positionFields.length - 1 ; j >= 0; j--) { debugPrintln(positionFields[j] + ": " + Position.getFieldValue(positionFields[j], positionID)); } debugPrintln("=============== Position: " + positionID + " ============="); } function PrintOrder(orderID) { // Order object references the specific properties of orders that have been processed by the broker. // Returns the stop price of an order for the specified order ID. debugPrintln("Stop: " + Order.stopPrice(orderID)); // Returns the limit price of an order for the specified order ID. debugPrintln("Limit: " + Order.limitPrice(orderID)); // Returns the specified route of an order for the specified order ID. debugPrintln("Route: " + Order.route(orderID)); // TIF Constants: // Order.TIF_DAY_ORDER // Order.TIF_GOOD_TILL_CANCEL // Order.TIF_IMMEDIATE_OR_CANCEL // Order.TIF_FILL_OR_KILL // Order.TIF_GOOD_TILL_DATE // Returns the time in force of an order for the specified order ID. debugPrintln("Expiry: " + PrintExpiry(Order.tif(orderID))); // Returns true if the order action is ‘buy’ or ‘buy to cover’. // Returns false if the order action is ‘sell’ or ‘sell short’. debugPrintln("isBuy: " + Order.isBuy(orderID)); // Returns true if the order action is ‘sell’ or ‘sell short’. // Returns false if the order action is ‘buy’ or ‘buy to cover’. debugPrintln("isSell: " + Order.isSell(orderID)); // Returns true if the order type is ‘market’. debugPrintln("isMarket: " + Order.isMarket(orderID)); // Returns true if the order type is ‘limit’. debugPrintln("isLimit: " + Order.isLimit(orderID)); // Returns true if the order type is ‘stop’. debugPrintln("isStop: " + Order.isStop(orderID)); // Returns true if the order type is ‘stop limit’. debugPrintln("isStopLimit: " + Order.isStopLimit(orderID)); // Returns the time stamp of an order for the specified order ID. debugPrintln("Time: " + Order.time(orderID)); // Returns the Average Fill Price for the specified order ID. debugPrintln("FillPrice: " + Order.fillPrice(orderID)); // Returns the number of shares/contracts filled for the specified order ID. debugPrintln("FillQty: " + Order.fillQty(orderID)); // Returns the number of shares/contracts placed for an order for the specified order ID. debugPrintln("Qty: " + Order.qty(orderID)); // Order State Constants: // Order.STATE_PLACING // Order.STATE_WORKING // Order.STATE_REJECTED // Order.STATE_CANCELED // Order.STATE_PARTIALFILL // Order.STATE_FILLED // Order.STATE_CANCELING // Order.STATE_UPDATING // Returns a constant for the current state of the specified EFS order ID. debugPrintln("State: " + PrintState(Order.state(orderID))); // Returns the connection ID for the specified order ID. debugPrintln("Connection : " + Order.connection(orderID)); // Returns the account for the specified order ID. debugPrintln("Account: " + Order.account(orderID)); // Returns the symbol for the specified order ID. debugPrintln("Symbol: " + Order.symbol(orderID)); debugPrintln("=============== Order: " + orderID + " ============="); } function PrintFullOrder(orderID) { var orderFields = Order.getFieldNames(); for (j = orderFields.length - 1; j >= 0; j--) { debugPrintln(orderFields[j] + ": " + Order.getFieldValue(orderFields[j], orderID)); } debugPrintln("=============== Order: " + orderID + " ============="); } function PrintState(Orderstate) { switch(Orderstate) { case Order.STATE_PARTIALFILL : return "STATE_PARTIALFILL"; case Order.STATE_PLACING : return "STATE_PLACING"; case Order.STATE_CANCELING : return "STATE_CANCELING"; case Order.STATE_UPDATING : return "STATE_UPDATING"; case Order.STATE_FILLED : return "STATE_FILLED"; case Order.STATE_CANCELED : return "STATE_CANCELED"; case Order.STATE_REJECTED: return "STATE_REJECTED"; case Order.STATE_WORKING : return "STATE_WORKING"; default: return "Unknown ORDERSATE"; } } function PrintExpiry(Expiry) { switch (Expiry) { case Order.TIF_DAY_ORDER : return "TIF_DAY_ORDER "; case Order.TIF_FILL_OR_KILL : return "TIF_FILL_OR_KILL "; case Order.TIF_GOOD_TILL_CANCEL : return "TIF_GOOD_TILL_CANCEL "; case Order.TIF_GOOD_TILL_DATE : return "TIF_GOOD_TILL_DATE "; case Order.TIF_IMMEDIATE_OR_CANCEL : return "TIF_IMMEDIATE_OR_CANCEL "; default : return "Unknown TIF "; } } function PrintCurrentMMSCallback() { debugPrintln("Current MMS: " + Trade.getCurrentMMS()); //Returns default MMS name or null. Default MMS is selected in the trade panel in Chart or in the Order Defaults for Watch and Dashboard. } function ApplyMMSOnPositionCallback() { var appliedMMS = false; let mmsArr = Trade.getMMS(); if(mmsArr != null) { let Positions = Trade.getPositions(); if(Positions != null) for( i = 0; i < Positions.length; i++ ) { if( !Position.isFlat(Positions[i]) ) { Trade.applyMMS(mmsArr[0], Positions[i]); // Applies the passed MMS to the specified position ID. debugPrintln(" " + mmsArr[0] + " on " + Positions[i] + " Position."); debugPrintln("Apply MMS: "); appliedMMS = true; break; } if(i == Positions.length - 1 && !appliedMMS) debugPrintln("ApplyMMS: All Positions is Flatten."); } else debugPrintln("ApplyMMS: There are no Positions"); } else debugPrintln("ApplyMMS: There is no MMS"); }