Operations > Automated Trading > Automated Trading Interface (ATI) > DLL Interface >

eSignal Sample Code

Print this Topic Previous pageReturn to chapter overviewNext page

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.

 

Sample Code

/* 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("BuyingPower", DLL.String, DLL.STDCALL, "BuyingPower", DLL.DOUBLE);

   dll.addFunction("CashValue", DLL.String, DLL.STDCALL, "CashValue", DLL.DOUBLE);

   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("ConfirmOrders", DLL.INT, DLL.STDCALL, "ConfirmOrders", DLL.INT);

   dll.addFunction("Connected", DLL.INT, DLL.STDCALL, "Connected", DLL.INT);

   dll.addFunction("Filled", DLL.INT, DLL.STDCALL, "Filled", DLL.STRING, 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("RealizedPnL", DLL.STRING, DLL.STDCALL, "RealizedPnL", DLL.DOUBLE);

   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 (isLastBarOnChart() && NTConnected(1))

   {

       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");

           debugPrint("RealizedPnL: " + NTRealizedPnL("") + "\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.

function NTAvgFillPrice(orderId) {

   return dll.call("AvgFillPrice", orderId, account);

}

 

// Get the buying power of an account.

function NTBuyingPower(account) {

   return dll.call("BuyingPower", 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 "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, "", "", "", "", "");

}

 

// Get the cash value of an account.

function NTCashValue(account) {

   return dll.call("CashValue", account);

}

 

// 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.

function NTFilled(orderId) {

   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");

}

 

// Gets a string of order ids of all orders of an account separated by '|'. If a user defined order id was not originally provided, the internal token id
// value is used since it is guaranteed to be unique.

function NTOrders(account) {

   return dll.call("Orders", account);

}

 

// Get the current status of an order.

function NTOrderStatus(orderId) {

   return dll.call("OrderStatus", orderId, account);

}

 

// Get the realized P&L of an account.

function NTRealizedPnL(account) {

   return dll.call("RealizedPnL", 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 a string of strategy IDs of all strategies of an account separated by '|'. Duplicate ID values can be returned if strategies were initiated outside of the ATI.

function NTStrategies(account) {

   return dll.call("Strategies", account);

}

 

// Get the position of a strategy.

function NTStrategyPosition(strategyId) {

   return dll.call("StrategyPosition", strategyId);

}