Skip to content

Algorithm SDK

OPEN_POSITIONS

The positions this algorithm has open, oldest first. It is a list with helpers, not an array: use at(index) rather than [index].

declare const OPEN_POSITIONS: OpenPositions;
interface OpenPositions {
/** The number of open positions. */
readonly length: number;
/** The position at an index, oldest first. */
at(index: number): Position | undefined;
/** The position with a broker identifier. */
get(id: string): Position | undefined;
/** Closes every open position. */
closeAll(): void;
size(): number;
each(iterator: (position: Position, index: number, list: Position[]) => void): void;
forEach(iterator: (position: Position, index: number, list: Position[]) => void): void;
map<T>(iterator: (position: Position, index: number, list: Position[]) => T): T[];
reduce<T>(iterator: (memo: T, position: Position, index: number, list: Position[]) => T, initialValue: T): T;
find(predicate: (position: Position) => boolean): Position | undefined;
filter(predicate: (position: Position) => boolean): Position[];
reject(predicate: (position: Position) => boolean): Position[];
every(predicate: (position: Position) => boolean): boolean;
some(predicate: (position: Position) => boolean): boolean;
where(fields: PositionFields): Position[];
findWhere(fields: PositionFields): Position | undefined;
pluck<K extends keyof Position>(field: K): Array<Position[K]>;
max(iterator: (position: Position) => number): Position;
min(iterator: (position: Position) => number): Position;
}
type PositionFields = Partial<Pick<Position, "ID" | "SESSION_ID" | "INSTRUMENT_ID" | "QUANTITY" | "DIRECTION" | "STATUS" | "STOP_LOSS" | "TAKE_PROFIT">>;
  • length and size(): the number of open positions, the same as OPEN_POSITIONS_COUNT.
  • at(index): the position at an index, oldest first. get(id): the position with a broker identifier.
  • closeAll(): closes them all, like closeAllPositions.
  • each (or forEach), map, reduce, find, filter, reject, every and some work like the array methods of the same names.
  • where(fields) and findWhere(fields): the positions, or the first one, whose fields match, such as { DIRECTION: "LONG" }.
  • pluck(field): one field from each position. max(fn) and min(fn): the position with the largest or smallest value of fn.
function onTick() {
const target = 15 * PIP_SIZE;
OPEN_POSITIONS.each((position) => {
const gained = position.DIRECTION === "LONG" ? BID - position.OPEN_PRICE : position.OPEN_PRICE - ASK;
if (gained > target) {
position.close();
}
});
const longs = OPEN_POSITIONS.where({ DIRECTION: "LONG" });
const profit = OPEN_POSITIONS.reduce((total, position) => {
return total + position.PROFIT;
}, 0);
log(longs.length, "long positions,", profit.toFixed(2), "unrealized");
}