Skip to content

One position this algorithm opened, as found in OPEN_POSITIONS. Fields are in capitals.

interface Position {
/** The broker's identifier for the position. */
readonly ID: string;
/** The trading session that opened it. Not set in a backtest. */
readonly SESSION_ID?: string | number;
/** The instrument it is on, such as "EUR_USD:OANDA". */
readonly INSTRUMENT_ID: string;
readonly OPEN_TIME: Date;
/** The ask for a long position, the bid for a short one. */
readonly OPEN_PRICE: number;
/** Size in units of the base currency. */
readonly QUANTITY: number;
readonly DIRECTION: "LONG" | "SHORT";
readonly STATUS: "OPEN" | "CLOSED";
readonly STOP_LOSS?: number;
readonly TAKE_PROFIT?: number;
readonly USED_MARGIN: number;
/** Unrealized profit in your account currency while open; the realized profit once closed. */
readonly PROFIT: number;
readonly CLOSE_TIME?: Date;
readonly CLOSE_PRICE?: number;
/** Closes the position at the market. */
close(): void;
/** The current profit in your account currency, or null if it cannot be worked out yet. */
getProfit(): number | null;
}
  • ID: the broker’s identifier. INSTRUMENT_ID: the instrument, such as "EUR_USD:OANDA". SESSION_ID: the trading session that opened it; not set in a backtest.
  • DIRECTION: "LONG" (opened with a buy) or "SHORT" (opened with a sell). QUANTITY: the size in units of the base currency.
  • OPEN_TIME and OPEN_PRICE: when and at what price it opened; the ask for a long position, the bid for a short one.
  • STOP_LOSS and TAKE_PROFIT: the exit prices, if set. USED_MARGIN: the margin it holds.
  • STATUS: "OPEN" or "CLOSED". PROFIT: the unrealized profit in your account currency while open, and the broker’s final figure once closed.
  • CLOSE_TIME and CLOSE_PRICE: set once it has closed.
  • close(): closes the position at the market.
  • getProfit(): the current profit, or null if it cannot be worked out yet (for example, before a conversion rate has arrived).
function onTick() {
OPEN_POSITIONS.each((position) => {
const ageHours = (TIME.getTime() - position.OPEN_TIME.getTime()) / 3600000;
if (ageHours > 48) {
log("Closing", position.ID, "after", ageHours.toFixed(0), "hours");
position.close();
}
});
}