Order-Flow Simulator
10+ complete strategies using advanced variables and order helpers.
Each block below is runnable as‑is inside the Auto Trading Code box. Every example uses multiple variables and helpers to show real strategy complexity.
Buys in an uptrend when price is above VWAP and RSI is oversold, sizes by risk, scales out on strength, and exits if trend breaks.
// Trend + VWAP + RSI + Risk Sizing
if (oncePerBar('setup')) {
if (trendUp > 0 && closeAboveVwap === 1 && rsi14 < 40 && volRel > 1.1) {
const qty = riskQty({ riskPct: 1, stopPct: 1.2, price });
if (qty > 0 && balance > price * qty) buyMarket(qty);
}
}
if (holdings > 0 && (rsi14 > 70 || macdHist < 0)) {
sellPct(50);
}
if (holdings > 0 && price < ema20) {
sellMarket(holdings);
}
Places stop buys above recent highs, protects with stop sells, and throttles order placement with a cooldown.
// Breakout + Stop Orders + Cooldown
const recentHigh = highest(highs, 20, i);
const recentLow = lowest(lows, 20, i);
if (cooldown('brk', 3000) && recentHigh !== null && trendUp > 0 && bbWidth > 0.2) {
buyStop(recentHigh * 1.002, 3);
}
if (holdings > 0 && recentLow !== null) {
sellStop(recentLow * 0.998, Math.min(holdings, 3));
}
if (holdings > 0 && macdLine < macdSignal && rsiSlope < 0) {
sellPct(25);
}
Buys near the lower band with oversold momentum and sells near the upper band or when momentum fades.
// Mean Reversion + Bollinger Bands + Stoch
if (oncePerBar('mr') && bbLower !== null && bbUpper !== null) {
if (close < bbLower && stochK < 20 && rsi7 < 30 && volRel > 1.0) {
buyValue(300);
}
}
if (holdings > 0 && (close > bbUpper || stochK > 80 || rsi14 > 70)) {
sellValue(300);
}
if (holdings > 0 && closePos < 0.2 && lowerWickPct > 50) {
sellPct(25);
}
Enters when EMA slope, EMA spread, and MACD histogram align with strong volume. Exits if momentum or volume fades.
// Momentum Stack + EMA Slopes + Volume Filter
if (oncePerBar('mom') && ema10Slope > 0 && emaDiff > 0 && macdHist > 0 && volRel > 1.2) {
buy(4);
}
if (holdings > 0 && (ema10Slope < 0 || macdHist < 0 || volRel < 0.8)) {
sellPct(50);
}
if (holdings > 0 && close < sma20 && bodyDir < 0) {
sellMarket(holdings);
}
Arms a breakout when Bollinger width is tight, then uses stop orders to catch expansion and manages risk with stop‑loss.
// Volatility Squeeze + Expansion
if (!mem.squeeze) mem.squeeze = false;
if (bbWidth < 0.15 && atrPct < 1) mem.squeeze = true;
if (mem.squeeze && cooldown('sq', 2000)) {
const hi = highest(highs, 15, i);
const lo = lowest(lows, 15, i);
if (hi !== null && lo !== null) {
buyStop(hi * 1.003, 2);
sellStop(lo * 0.997, 2);
}
}
if (holdings > 0 && (bbWidth > 0.3 || rsi14 > 70)) {
sellPct(50);
}
Trades gaps using gap direction and size; continues gaps with VWAP confirmation or fades extreme gaps when momentum stalls.
// Gap & Go / Gap Fade Hybrid
if (oncePerBar('gap')) {
if (gapDir === 1 && gapPct > 0.2 && closeAboveVwap === 1 && rsi14 > 50) {
buyMarket(3);
}
if (gapDir === -1 && gapPct > 0.2 && closeAboveVwap === 0 && rsi14 < 50) {
sellMarket(3);
}
}
if (holdings > 0 && rsiSlope < 0 && upperWickPct > 60) {
sellPct(50);
}
Buys pullbacks in an uptrend when price holds above EMA20 and shows lower‑wick support, then exits on weakness.
// Trend Pullback with Wicks
if (trendUp > 0 && priceAboveEma20 === 1 && oncePerBar('pb')) {
if (closePos < 0.3 && lowerWickPct > 50 && volRel > 0.9) {
const qty = riskQty({ riskPct: 0.8, stopPct: 1.0 });
if (qty > 0) buyLimit(price * 0.995, qty);
}
}
if (holdings > 0 && (ema10Slope < 0 || close < sma20)) {
sellMarket(holdings);
}
Trades inside ranges using close position and stochastic signals, scaling in/out with percent‑based orders.
// Range Scalper
if (oncePerBar('range') && hlRangePct < 0.8 && atrPct < 1.2) {
if (closePos < 0.2 && stochK < 20) buyPct(10);
if (closePos > 0.8 && stochK > 80) sellPct(25);
}
if (holdings > 0 && rsi14 > 70) sellPct(25);
Detects climax volume and wick rejection to reverse into mean reversion, then takes partial profits on recovery.
// Volume Climax Reversal
if (oncePerBar('climax')) {
if (volChangePct > 80 && upperWickPct > 60 && rsi14 > 70) {
sellMarket(3);
}
if (volChangePct > 80 && lowerWickPct > 60 && rsi14 < 30) {
buyMarket(3);
}
}
if (holdings > 0 && closePos > 0.7) sellPct(30);
Uses mem to switch between trend and risk‑off modes, and avoids repeated entries within the same state.
// Regime Switching with Memory
if (!mem.mode) mem.mode = 'wait';
if (trendUp > 0 && priceAboveSma20 === 1) mem.mode = 'trend';
if (trendUp < 0 && priceAboveSma20 === 0) mem.mode = 'riskoff';
if (mem.mode === 'trend' && oncePerBar('entry') && rsi14 < 45 && closeAboveVwap === 1) {
const qty = riskQty({ riskPct: 1, stopPct: 1.0 });
if (qty > 0) buy(qty);
}
if (mem.mode === 'riskoff' && holdings > 0) {
sellPct(50);
}
Stops trading after a drawdown, reduces exposure when returns fall, and uses PnL to take profit.
// Drawdown Guard + Equity Control
if (returnRate < -5) {
if (holdings > 0) sellMarket(holdings);
hold();
}
if (returnRate < -2 && holdings > 0) {
sellPct(25);
}
if (pnl > 80) {
sellPct(30);
}
Computes a size based on volatility, updates the UI order size, and uses UI‑based buy/sell helpers.
// Adaptive Size + UI Quantity
if (oncePerBar('size')) {
const riskSize = riskQty({ riskPct: 0.8, stopPct: Math.max(0.5, atrPct) });
if (riskSize > 0) setOrderQty(riskSize);
}
if (trendUp > 0 && rsi14 < 40 && closeAboveVwap === 1) {
buyOrderQty();
}
if (holdings > 0 && (rsi14 > 70 || macdHist < 0)) {
sellOrderQty();
}