Version: 1.0.1
Advanced Strategy Engine & Live State
The ctx.strategy namespace is fully equipped to handle advanced trailing stops, live position tracking, and global risk management rules.
Live Position State
During a ctx.run() loop, whenever you call ctx.strategy.entry() or close(), the engine synchronously updates internal state trackers. You can read these dynamically on the very next bar to make logic decisions.
ctx.strategy.position_size: Returns the quantity of your open position. Positive numbers mean long, negative numbers mean short, and0means flat.ctx.strategy.position_avg_price: The average entry price of your open trade.ctx.strategy.openprofit(): A function that calculates your floating unrealized profit and loss based on the current bar's close price.
Advanced Exits (Trailing Stops)
ctx.strategy.exit() supports complex trailing stop loss implementations similar to TradingView.
ctx.strategy.exit("ExitID", null, null, null, null, null, trail_points, trail_offset);
Risk Management
You can configure global strategy risk rules that will automatically halt trading if certain drawdown thresholds are breached.
ctx.strategy.risk.max_drawdown(value, type)ctx.strategy.risk.max_intraday_loss(value, type)ctx.strategy.risk.allow_entry_in(direction)