The eSignal sample code below illustrates the use of the NinjaTrader NtDirect.dll within an eSignal EFS script. The function preMain() loads the various DLL functions. The function main() places an order. There is also additional wrapper samples created as examples. The sample is also saved as an EFS file located at <NinjaTrader Installation Folder>\bin\AutoTrade\NTSample.efs.
|
/* Copyright (c) 2005, NinjaTrader LLC ninjatrader@ninjatrader.com. All rights reserved. */ var dll = new DLL("NtDirect.dll"); var orderPlaced = false; var printDone = false; function preMain() { setPriceStudy(true); setStudyTitle("NT Sample"); dll.addFunction("AvgEntryPrice", DLL.DOUBLE, DLL.STDCALL, "AvgEntryPrice", DLL.STRING, DLL.STRING); dll.addFunction("AvgFillPrice", DLL.DOUBLE, DLL.STDCALL, "AvgFillPrice", DLL.STRING, DLL.STRING); dll.addFunction("Command", DLL.INT, DLL.STDCALL, "Command", DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING, DLL.INT, DLL.STRING, DLL.DOUBLE, DLL.DOUBLE, DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING); dll.addFunction("Connected", DLL.INT, DLL.STDCALL, "Connected"); dll.addFunction("Filled", DLL.INT, DLL.STDCALL, "Filled", DLL.STRING, DLL.STRING); dll.addFunction("GetDouble", DLL.DOUBLE, DLL.STDCALL, "GetDouble", DLL.STRING); dll.addFunction("GetInt", DLL.INT, DLL.STDCALL, "GetInt", DLL.STRING); dll.addFunction("GetString", DLL.STRING, DLL.STDCALL, "GetString", DLL.STRING); dll.addFunction("MarketPosition", DLL.INT, DLL.STDCALL, "MarketPosition", DLL.STRING, DLL.STRING); dll.addFunction("NewOrderId", DLL.STRING, DLL.STDCALL, "NewOrderId"); dll.addFunction("Orders", DLL.STRING, DLL.STDCALL, "Orders", DLL.STRING); dll.addFunction("OrderStatus", DLL.STRING, DLL.STDCALL, "OrderStatus", DLL.STRING, DLL.STRING); dll.addFunction("SetUp", DLL.INT, DLL.STDCALL, "SetUp", DLL.STRING, DLL.INT); dll.addFunction("Strategies", DLL.STRING, DLL.STDCALL, "Strategies", DLL.STRING); dll.addFunction("StrategyPosition", DLL.INT, DLL.STDCALL, "StrategyPosition", DLL.STRING); dll.addFunction("TearDown", DLL.INT, DLL.STDCALL, "TearDown"); } function main() { if (NTConnected()) { if (!orderPlaced) { if (NTBuyMarket("MyOrderId", 1) == 0) // buy 1 unit at market, assign order id (optionally) orderPlaced = true; } else { // print some information on the current position and order debugPrint("Position size: " + NTMarketPosition("") + "\r\n"); debugPrint("AvgEntryPrice: " + NTAvgEntryPrice("") + "\r\n"); debugPrint("OrderStatus: " + NTOrderStatus("MyOrderId", "") + "\r\n"); debugPrint("Filled #: " + NTFilled("MyOrderId", "") + "\r\n"); debugPrint("AvgFillPrice: " + NTAvgFillPrice("MyOrderId", "") + "\r\n"); } } } // Get the average entry price of a position of an account. "account" is optional. function NTAvgEntryPrice(account) { return dll.call("AvgEntryPrice", getSymbol(), account); } // Get the average fill price of an order at an account. "account" is optional (set to "" it not applicable). function NTAvgFillPrice(orderId, account) { return dll.call("AvgFillPrice", orderId, account); } // Place a buy limit order. "orderId" is optional (set to "" it not applicable). function NTBuyLimit(orderId, quantity, limitPrice) { return NTCommand("Place", "", "Buy", quantity, "Limit", limitPrice, 0, "", "", orderId, "", ""); } // Place a buy market order. "orderId" is optional (set to "" it not applicable). function NTBuyMarket(orderId, quantity) { return NTCommand("Place", "", "Buy", quantity, "Market", 0, 0, "", "", orderId, "", ""); } // Place a buy stop order. "orderId" is optional (set to "" it not applicable). function NTBuyStop(orderId, quantity, stopPrice) { return NTCommand("Place", "", "Buy", quantity, "Stop", 0, stopPrice, "", "", orderId, "", ""); } // Place a buy stop limit order. "orderId" is optional (set to "" it not applicable). function NTBuyStopLimit(orderId, quantity, limitPrice, stopPrice) { return NTCommand("Place", "", "Buy", quantity, "StopLimit", limitPrice, stopPrice, "", "", orderId, "", ""); } // Cancel an order by its order id. function NTCancel(orderId) { return NTCommand("Cancel", "", "", 0, "", 0, 0, "", "", orderId, "", ""); } // Cancel all orders at all accounts. function NTCancelAllOrders() { return NTCommand("CancelAllOrders", "", "", 0, "", 0, 0, "", "", "", "", ""); } // Change an order by its order id. function NTChange(orderId, quantity, limitPrice, stopPrice) { return NTCommand("Change", "", "", quantity, "", limitPrice, stopPrice, "", "", orderId, "", ""); } // Close any position of the current instrument at an account. "account" is optional (set to "" it not applicable). function NTClosePosition(account) { return NTCommand("ClosePosition", account, "", 0, "", 0, 0, "", "", "", "", ""); } // Submits a NinjaTrader command. function NTCommand(command, account, action, quantity, orderType, limitPrice, stopPrice, timeInForce, oco, orderId, template, strategy) { return dll.call("Command", command, account, getSymbol(), action, quantity, orderType, limitPrice, stopPrice, timeInForce, oco, orderId, template, strategy); } // Indicates if the connection to NinjaTrader is established. 0 = connected, -1 not connected. function NTConnected() { return (dll.call("Connected") == 0) } // Get the filled of an order at an account. "account" is optional (set to "" it not applicable). function NTFilled(orderId, account) { return dll.call("Filled", orderId, account); } // Close all positions and cancels all order at all account. function NTFlattenEverything() { return NTCommand("FlattenEverything", "", "", 0, "", 0, 0, "", "", "", "", ""); } // Get a double value by its name. function NTGetDouble(name) { return dll.call("GetDouble", name); } // Get an integer value by its name. function NTGetInt(name) { return dll.call("GetInt", name); } // Get a string value by its name. function NTGetString(name) { return dll.call("GetString", name); } // Get the market position of the current instrument at na account. "account" is optional (set to "" it not applicable). function NTMarketPosition(account) { return dll.call("MarketPosition", getSymbol(), account); } // Get a new unqiue order id. function NTNewOrderId() { return dll.call("NewOrderId"); } // Get the current status of an order at an account. "account" is optional (set to "" it not applicable). function NTOrderStatus(orderId, account) { return dll.call("OrderStatus", orderId, account); } // Place a sell limit order. "orderId" is optional (set to "" it not applicable). function NTSellLimit(orderId, quantity, limitPrice) { return NTCommand("Place", "", "Sell", quantity, "Limit", limitPrice, 0, "", "", orderId, "", ""); } // Place a sell market order. "orderId" is optional (set to "" it not applicable). function NTSellMarket(orderId, quantity) { return NTCommand("Place", "", "Sell", quantity, "Market", 0, 0, "", "", orderId, "", ""); } // Place a sell stop order. "orderId" is optional (set to "" it not applicable). function NTSellStop(orderId, quantity, stopPrice) { return NTCommand("Place", "", "Sell", quantity, "Stop", 0, stopPrice, "", "", orderId, "", ""); } // Place a sell stop limit order. "orderId" is optional (set to "" it not applicable). function NTSellStopLimit(orderId, quantity, limitPrice, stopPrice) { return NTCommand("Place", "", "Sell", quantity, "StopLimit", limitPrice, stopPrice, "", "", orderId, "", ""); } // Gets the position of a strategy. function NTStrategyPosition(strategyId) { return dll.call("StrategyPosition", strategyId); } |