/* Developed by Brad Matheny, Matheny Enterprises 1.2008 www.ment.com ------------------------------------------------------- Simple MTS Template: */ // Setup MTS try { var mentts = new DLL("mentts.dll"); doMTSInit(); } finally { // debugPrintln("-[ "+getSymbol()+"] **-* Can Not Find MENT_TS DLL *-**"); } // Global Variables var AutoTrade = false var DefaultContracts = 1; // number of shares/contracts to trade. var tsymbol = ""; // extracted base symbol fMake_tsymbol(); // // Setup MTS variables var MTSStatus = 0; var MTSBroker = "IB"; var MTSSymbol = ""; var MTSExchange = ""; var MTSExpiration = ""; var MTSType = ""; var MTSDollarMult = 1; var HiddenOrder = 0; var DiscretionaryAmount = 0.0; var FAAccount = ""; //============================================================== /* These are the only two fields required for FA accounts if you need them. Refer to MTS online help for info on setting up MTS for FA accounts */ var FAGroup = ""; var FAProfile = ""; // var FAMethod = ""; var FAPercentage = ""; var IBCurrency = "USD"; // Control Logic for easier eSignal integration // eSignal uses "Strategy" functions to create backtesting results. I use similar variables to track auto-trading systems. var StrategyisLong = false; var StrategyisShort = false; var StrategyisInTrade = false; var ActivePosition = 0; // In this template, you should create inidividual orderIDs (as below) to check the different orders. var Le_OrderID = null; // Long Entry Order ID var Lx_OrderID = null; // Long Exit Order ID var Se_OrderID = null; // Short Entry Order ID var Sx_OrderID = null; // Short Exit Order ID var StopID = null; // Short Exit Order ID var nEntryPrice = 0; var FilledAmount = 0; var nFontSize = 8; var nLastRawTime = 0; var BarCounter = 0; var aFPArray = new Array(); function preMain() { setShowTitleParameters(false); setShowCursorLabel(true); setPriceStudy(true); setStudyTitle("MTS EFS TEMPLATE"); setCursorLabelName("Holding", 0); /* The declared variables below are simply an example. They do nothing in this code. */ var x = 0; aFPArray[x] = new FunctionParameter( "xVariable1", FunctionParameter.NUMBER); with(aFPArray[x]) { setName("xV1 Length"); setLowerLimit(1); setDefault(10); } x++; aFPArray[x] = new FunctionParameter("xVariable2", FunctionParameter.NUMBER); with(aFPArray[x]) { setName("xV2 Length"); setDefault(10); } x++; aFPArray[x] = new FunctionParameter("vnFontSize", FunctionParameter.NUMBER); with(aFPArray[x]) { setName("FontSize"); setDefault(10); } setupMTSVars(); } // end preMain() function main(xVariable1, xVariable2, vnFontSize) { nFontSize = vnFontSize; if (getValue("rawtime", 0) != nLastRawTime) { nLastRawTime = getValue("rawtime", 0); BarCounter += 1; } MTSStatus = mentts.call("GetBrokerStatus", MTSBroker); ActivePosition = getactiveposition(); DrawButtons(); var tempOrderReturn = ""; if (Le_OrderID != null) { tempOrderReturn = fCheckOrders(Le_OrderID); if (tempOrderReturn == "FILLED") { Le_OrderID = null; } } if (Lx_OrderID != null) { tempOrderReturn = fCheckOrders(Lx_OrderID); if (tempOrderReturn == "FILLED") { Lx_OrderID = null; } } if (Se_OrderID != null) { tempOrderReturn = fCheckOrders(Se_OrderID); if (tempOrderReturn == "FILLED") { Se_OrderID = null; } } if (Sx_OrderID != null) { tempOrderReturn = fCheckOrders(Sx_OrderID); if (tempOrderReturn == "FILLED") { Sx_OrderID = null; } } //StopID = null; // Short Exit Order ID } // End MAIN function DrawButtons() { if (!AutoTrade) { drawTextPixel(4,25, " Auto-Trade OFF "+"@URL=EFS:ATbutton" , Color.white, Color.RGB(0xE0, 0x00, 0x00), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "ATB"); } else { drawTextPixel(4,25, " Auto-Trade ON "+"@URL=EFS:ATbutton" , Color.black, Color.RGB(0x00, 0xE0, 0x00), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "ATB"); } drawTextPixel(4,40, " Sell "+"@URL=EFS:Sellbutton" , Color.black, Color.RGB(0xE0, 0xE0, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "SellBTn"); drawTextPixel(34,40, " Buy "+"@URL=EFS:Buybutton" , Color.black, Color.RGB(0xE0, 0xE0, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "BuyBTn"); if ((Strategy.isLong()) || (StrategyisLong)) { drawTextPixel(63,40, " Close ALL "+"@URL=EFS:CAbutton" , Color.black, Color.RGB(0xE0, 0xFF, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "STBTn"); } else if ((Strategy.isShort()) || (StrategyisShort)) { drawTextPixel(63,40, " Close ALL "+"@URL=EFS:CAbutton" , Color.black, Color.RGB(0xFF, 0xE0, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "STBTn"); } else if ((!Strategy.isInTrade()) || (!StrategyisInTrade)) { drawTextPixel(63,40, " Close ALL "+"@URL=EFS:CAbutton" , Color.black, Color.RGB(0xE0, 0xFF, 0xFF), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "STBTn"); } drawTextPixel(44,10, " Holding : "+Math.floor(ActivePosition)+" ", Color.black, Color.RGB(0xE0, 0xE0, 0xFA), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "Holding"); drawTextPixel(4,10, " Edit "+"@URL=EFS:Editbutton" , Color.black, Color.RGB(0xE0, 0xE0, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, nFontSize, "ARTCB"); } function Editbutton() { Alert.playSound("autoplay.wav"); askForInput("Edit MTS EFS Template Settings"); } function CAbutton() { if ((getCurrentBarIndex() == 0) && (AutoTrade)) { Alert.playSound("autoplay.wav"); TradeMarket(13); // GO FLAT } } function Sellbutton() { Alert.playSound("autoplay.wav"); drawShapeRelative(0, high()+MakeGOffset(), Shape.DOWNARROW, "", Color.red, Shape.TOP | Shape.ONTOP , BarCounter+"SE"); Strategy.doShort("Se", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, close()); //----- RT Autotrade if ((getCurrentBarIndex() == 0) && (AutoTrade)) { TradeMarket(2); // SELL MARKET ORDER } } function Buybutton() { Alert.playSound("autoplay.wav"); drawShapeRelative(0, low()-MakeGOffset(), Shape.UPARROW, "", Color.green, Shape.TOP | Shape.ONTOP , BarCounter+"LE"); Strategy.doLong("Le", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, close()); //----- RT Autotrade if ((getCurrentBarIndex() == 0) && (AutoTrade)) { TradeMarket(1); // BUY MARKET ORDER } } function ATbutton() { Alert.playSound("autoplay.wav"); if (!AutoTrade) { AutoTrade = true; } else { AutoTrade = false; } DrawButtons(); } //--- function to define MTS functions. function doMTSInit() { function InitMTS() { debugPrintln("InitMTS"); if (null != mentts) return; mentts = new DLL("mentts.dll"); mentts.addFunction("SetOption",DLL.INT,DLL.CDECL,"setoption",DLL.STRING,DLL.STRING); mentts.addFunction("LogMessage",DLL.INT,DLL.CDECL,"LogMessage",DLL.STRING); mentts.addFunction("PlaceOrder",DLL.INT,DLL.CDECL,"placeorder", DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING, DLL.INT, DLL.DOUBLE, DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING, DLL.DOUBLE, DLL.STRING); mentts.addFunction("CancelOrder",DLL.INT,DLL.CDECL,"cancelorder",DLL.INT); mentts.addFunction("GetFillAmount",DLL.INT,DLL.CDECL,"getfillamount",DLL.INT); mentts.addFunction("GetFillPrice",DLL.DOUBLE,DLL.CDECL,"getfillprice",DLL.INT); mentts.addFunction("GetOrderStatus",DLL.STRING,DLL.CDECL,"getorderstatus",DLL.INT); mentts.addFunction("GetBrokerStatus",DLL.INT,DLL.CDECL,"getbrokerstatus",DLL.STRING); mentts.addFunction("GetCurrentPosition",DLL.INT,DLL.CDECL,"getcurrentposition",DLL.STRING,DLL.STRING,DLL.STRING,DLL.STRING); mentts.addFunction("FAPlaceOrder",DLL.INT,DLL.CDECL,"FAPlaceOrder",DLL.STRING,DLL.STRING,DLL.STRING,DLL.INT,DLL.DOUBLE,DLL.STRING,DLL.STRING,DLL.STRING,DLL.STRING,DLL.DOUBLE,DLL.STRING,DLL.STRING,DLL.STRING,DLL.STRING,DLL.STRING,DLL.STRING); mentts.addFunction("FAGetNumAccounts",DLL.INT,DLL.CDECL,"FAGetNumAccounts"); mentts.addFunction("FAGetIndexedAccount", DLL.STRING,DLL.CDECL,"FAGetIndexedAccount", DLL.INT); } function setupMTSVars() { // Default all setting to US STOCKS with IB. // debugPrintln("PRE SETUP VARS "+tsymbol); MTSSymbol = tsymbol; MTSExchange = "SMART"; MTSExpiration = ""; MTSType = "stock"; MTSDollarMult = 1; HiddenOrder = 0; FAAccount = ""; FAGroup = ""; FAProfile = ""; FAMethod = ""; FAPercentage = ""; IBCurrency = "USD"; if ((tsymbol == "ES")) { // Emini S&P debugPrintln("Symbol Found "+tsymbol); DefaultContracts = 1; MTSSymbol = "ESU0"; MTSExchange = "GLOBEX"; MTSExpiration = "201009"; MTSType = "future"; MTSDollarMult = 1; HiddenOrder = 0; FAAccount = ""; FAGroup = ""; FAProfile = ""; FAMethod = ""; FAPercentage = ""; IBCurrency = "USD"; } } function UpdatePositions() { if (ActivePosition == 0) { // currently FLAT StrategyisLong = false; StrategyisShort = false; StrategyisInTrade = false; } if (ActivePosition < 0) { // currently SHORT StrategyisLong = false; StrategyisShort = true; StrategyisInTrade = true; } if (ActivePosition > 0) { // currently LONG StrategyisLong = true; StrategyisShort = false; StrategyisInTrade = true; } } function TradeMarket(TradeAction) { /* 1 = Buy Market 2 = Sell Market 13 = GO FLAT */ debugPrintln("Pre Fire Order "+MTSBroker+" "+MTSSymbol+" "+DefaultContracts+" "+MTSExchange+" "+MTSType); if (MTSStatus == 0) { // ========= BUY MARKET ORDERS ========================================= if (TradeAction == 1) { // buy market order // Example of a PlaceOrder function call. Le_OrderID = mentts.call("PlaceOrder", MTSBroker,"BUY","MKT",MTSSymbol,DefaultContracts,0,"GTC",MTSExchange,MTSType,"",0,IBCurrency); // Example of an IBPlaceOrder function call. //Le_OrderID = mentts.call("IBPlaceOrder","BUY","MKT",MTSSymbol,DefaultContracts,0,"GTC",MTSExchange,MTSType,"",0,0,HiddenOrder, FAAccount,FAGroup,FAProfile,FAMethod,FAPercentage,IBCurrency,""); /// OCA Group (at the end of the above line) is empty : "" } // ========= SELL MARKET ORDERS ========================================= if (TradeAction == 2) { // sell market order // Example of a PlaceOrder function call. Se_OrderID = mentts.call("PlaceOrder", MTSBroker,"SELL","MKT",MTSSymbol,DefaultContracts,0,"GTC",MTSExchange,MTSType,"",0,IBCurrency); // Example of an IBPlaceOrder function call. //Se_OrderID = mentts.call("IBPlaceOrder","BUY","MKT",MTSSymbol,DefaultContracts,0,"GTC",MTSExchange,MTSType,"",0,0,HiddenOrder, FAAccount,FAGroup,FAProfile,FAMethod,FAPercentage,IBCurrency,""); /// OCA Group (at the end of the above line) is empty : "" } // ========= GO FLAT ORDERS ========================================= if (TradeAction == 13) { // GO FLAT ORDER if (StrategyisLong) { // Example of a PlaceOrder function call. Lx_OrderID = mentts.call("PlaceOrder", MTSBroker,"SELL","MKT",MTSSymbol,ActivePosition,0,"GTC",MTSExchange,MTSType,"",0,IBCurrency); // Example of an IBPlaceOrder function call. //Lx_OrderID = mentts.call("IBPlaceOrder","SELL","MKT",MTSSymbol,ActivePosition,0,"GTC",MTSExchange,MTSType,"",0,0,HiddenOrder, FAAccount,FAGroup,FAProfile,FAMethod,FAPercentage,IBCurrency,""); /// OCA Group (at the end of the above line) is empty : "" } if (StrategyisShort) { // Example of a PlaceOrder function call. Sx_OrderID = mentts.call("PlaceOrder", MTSBroker,"BUY","MKT",MTSSymbol,Math.abs(ActivePosition),0,"GTC",MTSExchange,MTSType,"",0,IBCurrency); // Example of an IBPlaceOrder function call. //Sx_OrderID = mentts.call("IBPlaceOrder","BUY","MKT",MTSSymbol,Math.abs(ActivePosition),0,"GTC",MTSExchange,MTSType,"",0,0,HiddenOrder, FAAccount,FAGroup,FAProfile,FAMethod,FAPercentage,IBCurrency,""); /// OCA Group (at the end of the above line) is empty : "" } } } else { debugPrintln("MTS Broker Connection Failed - Aborting order process"); } } function fCheckOrders(OrderID) { var MTSOrderStatus = "NOTHING"; if (OrderID != null) { MTSOrderStatus = mentts.call("GetOrderStatus", MTSBroker, OrderID ); } return MTSOrderStatus; } function ReportOrderStatus(OrderID, MTSOrderStatus) { if (MTSOrderStatus == "FILLED") { nEntryPrice = mentts.call("GetFillPrice", MTSBroker, OrderID ); FilledAmount = mentts.call("GetFillAmount", MTSBroker, OrderID ); debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" For :"+FilledAmount+" @ "+nEntryPrice ); } if (MTSOrderStatus == "PARTIAL FILL") { FilledAmount = mentts.call("GetFillAmount", MTSBroker, OrderID ); debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" For :"+FilledAmount+" @ "+nEntryPrice ); } if (MTSOrderStatus == "UNKNOWN") { debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" -- Possible MTS or IB failure" ); } if (MTSOrderStatus == "PENDING") { debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" -- Possible incorrect price or other problem" ); } if (MTSOrderStatus == "PRESUBMITTED") { debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" -- Awaiting SUBMIT status" ); } if (MTSOrderStatus == "SUBMITTED") { debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" -- SUBMIT status" ); } if (MTSOrderStatus == "NOT FOUND") { debugPrintln("-- Order#:"+OrderID+" "+MTSOrderStatus+" -- SUBMIT status achieved" ); } } function getactiveposition() { var tCurrentPosition = 0; tCurrentPosition = mentts.call("GetCurrentPosition",MTSBroker,MTSSymbol,MTSType,MTSExchange,MTSExpiration); // debugPrintln(tCurrentPosition); if (tCurrentPosition < -10000000) { tCurrentPosition = 0; } // this bypasses all dll return errors return tCurrentPosition; } function fMake_tsymbol() { tsymbol = getSymbol(); var PosCount = tsymbol.split(" "); if (PosCount[0]) { tsymbol = PosCount[0]; } } function MakeGOffset() { return (high()-low())/3; }