Skip to content

Order

An Order object contains order information. Instances of this object are used by Orders interface and also are returned by Sell or Buy commands.

Order are created by issuing Buy or Sell commands. The status field is the first indicator we should check to determine if an order was submitted successfully.

order = Buy(symbol, qty, "day", "limit", price)
if order.status == 'error':
  Alert.info("Order failed")
  Log.error("Order to buy {} of {} failed".format(qty, symbol))
  return
var order = Buy(symbol, qty, "day", "limit", price);
if (order.status === 'error') {
  Alert.info("Order failed");
  Log.error("Order to buy " + qty + " of " + symbol + " failed." );
  return;
}

Status codes

  • accepted: at first an order is in accepted state. This means the broker has successfully received your order - however it has not been processed yet.

  • new: once an order is processed by the broker, it is moved to a new status.

  • duplicate: if your algorithm tries to submit two or more orders with the same parameters: same symbol, same quantity, same limit price, etc. it's status will be set to duplicate and the order WILL NOT be submitted to the broker. This feature can be disabled via configuration, but I'd caution against doing so.

  • rejected or forbidden: in case there is an error, the status may be set to rejected or forbidden with additional information in statusCode field and extendedInfo fields. Such code can result with orders that do not have enough cash or equity to make a transaction. Example: Holding 1000 shares of AMC and issuing two orders to sell 1000 shares of AMC will result in the first order being accepted and the second forbidden.

Order object

Field Field type Description
symbol String the symbol of the underlying
status String order status (‘new’, ‘filled’, ‘partfilled’, ‘canceled’, ‘duplicate’, etc.)
statusCode String status code (as defined by the broker)
side String ‘buy’ or ‘sell’
qty Number the number of shares in the order
type String ‘market’ or ‘limit’
limitPrice Float limit price if type is “limit”
timeInForce String ‘Day’, ‘GTC’ (GoodTillCanceled), etc.
orderId String alphanumeric order id
createdDt Date/time timestamp, i.e.’2020-10-20T17:30:37.769815’
partiallyFilledDt Date/time timestamp of the last partial fill
partialFillQty Number partial fill quantity (applicable to partially filled orders)
extendedInfo String additional information about the order (if any)
# retrieve all open orders
orders = Orders.openOrders()

# iterate the orders array and print to the tasks log
for order in orders:
    Log.info("Open order for {}" . format(order.symbol))
    Log.info("       status: {}" . format(order.status));
    Log.info("         side: {}" . format(order.side));
    Log.info("          qty: {}" . format(order.qty));
    Log.info("         type: {}" . format(order.type));
    Log.info("   limitPrice: {}" . format(order.limitPrice));
    Log.info("  timeInForce: {}" . format(order.timeInForce));
    Log.info("      orderId: {}" . format(order.orderId));
    Log.info("clientOrderId: {}" . format(order.clientOrderId));

# display some info in the UI
Alert.info("Found {} open orders." . format(len(orders)));
//retrieve all open orders
var orders = Orders.openOrders();

//iterate the orders array and print to the tasks log
orders.forEach(function(order) {
        Log.info("Open order for " + order.symbol);
        Log.info("       status: " + order.status);
        Log.info("         side: " + order.side);
        Log.info("          qty: " + order.qty);
        Log.info("         type: " + order.type);
        Log.info("   limitPrice: " + order.limitPrice);
        Log.info("  timeInForce: " + order.timeInForce);
        Log.info("      orderId: " + order.orderId);
        Log.info("clientOrderId: " + order.clientOrderId);
    });

//display some info in the UI
Alert.info("Found " + orders.length + " open orders.");