/* 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.addFunction("BuyingPower", DLL.DOUBLE, DLL.STDCALL, "BuyingPower", DLL.STRING);
dll.addFunction("CashValue", DLL.DOUBLE, DLL.STDCALL, "CashValue", 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("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.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.addFunction("RealizedPnL", DLL.DOUBLE, DLL.STDCALL, "RealizedPnL", DLL.STRING);
dll.addFunction("SetUp", DLL.INT, DLL.STDCALL, "SetUp", DLL.STRING, DLL.INT);
dll.addFunction("StopOrders", DLL.STRING, DLL.STDCALL, "StopOrders", DLL.STRING);
dll.addFunction("Strategies", DLL.STRING, DLL.STDCALL, "Strategies", DLL.STRING);
dll.addFunction("StrategyPosition", DLL.INT, DLL.STDCALL, "StrategyPosition", DLL.STRING);
dll.addFunction("TargetOrders", DLL.STRING, DLL.STDCALL, "TargetOrders", 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.
function NTAvgFillPrice(orderId) {
return dll.call("AvgFillPrice", orderId);
}
// 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, 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, "", "", "", "", "");
}
// 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);
}
// Sets the order confirmation mode. 1 = confirmation required, else = no confirmation required.
function NTConfirmOrders(confirm) {
dll.call("ConfirmOrders", confirm);
}
// Indicates if the connection to NinjaTrader is established. 0 = connected, -1 not connected.
function NTConnected(showMessage) {
return (dll.call("Connected", showMessage) == 0)
}
// Get the filled of an order.
function NTFilled(orderId) {
return dll.call("Filled", orderId);
}
// Close all positions and cancels all order at all account.
function NTFlattenEverything() {
return NTCommand("FlattenEverything", "", "", 0, "", 0, 0, "", "", "", "", "");
}
// Get the market position of the current instrument at an 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);
}
// 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, "", "");
}
// Get a string of order IDs of all stop orders of a strategy separated by '|'.
function NTStopOrders(strategyId) {
return dll.call("StopOrders", strategyId);
}
// 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);
}
// Get a string of order IDs of all target orders of a strategy separated by '|'.
function NTTargetOrders(strategyId) {
return dll.call("TargetOrders", strategyId);
}