Positions & orders
Beyond placing orders, the runtime exposes built-in functions (handled directly by the engine, not
indicators) to read the account and your existing positions. The key one is OpenOrders() — it
returns your currently open positions, and in paper/live it syncs them from your broker first.
On a restart, reconcile existing positions first. If the strategy re-runs (for example a
daily restart), positions from the previous run may still be open — always check them in
onStart before entering anything new.
Reconciling positions on restart
Walk the open positions with foreach and read each order's properties.
onStart {
// reconcile existing positions before trading anything new
foreach ord in OpenOrders() {
// e.g. close any losing position left from a previous run
if (ord.Profit < 0) {
OrderCloseByTicket(ord.Ticket, INTRADAY, DAY, "cleanup");
}
}
}Order properties
Each order (such as the ord loop variable) exposes these properties via ord.Name:
| Property | Meaning |
|---|---|
ord.Ticket | Unique order id |
ord.Symbol | The order's symbol |
ord.Type | Side — buy or sell |
ord.Volume | Quantity |
ord.Price | Execution price |
ord.SL / ord.TP | Stop loss / take profit |
ord.OpenTime | Entry time |
ord.Magic | Magic number |
ord.Profit | Net profit / loss |
ord.Remark | Remark text |
Account, position & time functions
| Function | Returns |
|---|---|
OpenOrders() | Open orders (syncs from the broker in paper/live) |
OpenPositions() | Open orders held in memory |
PositionsTotal() | Number of open orders |
PositionSize(symbol) | Total quantity held for a symbol |
Balance() / Equity() | Account balance / equity |
IsLocked(symbol) | Whether the symbol is currently locked |
TimeCurrent() / ServerTimeCurrent() | Current time |
DayOfWeek() | Current day of week (number) |
DayAdd(datetime, days) | A datetime shifted by n days |
GetTodayDayName() | Today's name, e.g. "Monday" |
GetWeekDayOpen/High/Low/Close(...) | This week's weekday OHLC |
GetPreviousWeekDayOpen/High/Low/Close(...) | Last week's weekday OHLC |
Ready to run this? Build and backtest strategies in the SutraLipi algo trading platform — no-code builder, tick-level backtesting and one-click live execution. Browse all documentation topics.