Cron-driven grid bot for EVM L2 chains via onchainos CLI. Features asymmetric grid steps — different spacing for buy vs sell sides based on trend direction, trend intelligence with multi-timeframe analysis, sell optimization, and HODL Alpha tracking.
Every tick: fetch price → MTF analysis → compute grid level → trend-adaptive decision → execute swap → report to Discord.
Buy and sell sides use different spacing based on trend direction:
TrendBuy SideSell SideEffectBullishTighter (accumulate fast)Wider (hold longer)Buy dense, sell sparse → captures uptrendBearishWider (wait for dip)Tighter (exit fast)Sell dense, buy sparse → reduces downside exposureNeutral/WeakSymmetricSymmetricSymmetric (default)
Key config: ASYM_FACTOR=0.4 (max asymmetry ratio). Asymmetry scales with trend strength and only activates when strength > 0.3.
New grid dict fields: buy_step, sell_step (backward-compatible step = average). Level prices are now non-uniform: below center spaced by buy_step, above center by sell_step.
text
Cron (5min) → Python script → onchainos CLI → OKX Web3 API → Chain
↓ ↓
grid_state.json Wallet (TEE signing)
↓
┌─────────────┐
│ MTF Analysis │ ← price_history (288 bars = 24h)
│ K-line ATR │ ← okx-dex-market kline (1H × 24)
└──────┬──────┘
↓
Trend-Adaptive Grid Decision
↓
Discord embed (notification)
OKX Skill Dependencies (command syntax defined in each skill, do not duplicate here):
okx-dex-swap — quote, approve, swap executionokx-dex-market — K-line / OHLC dataokx-agentic-wallet — wallet switch, balance, contract-call (TEE signing)okx-onchain-gateway — transaction simulationCRITICAL RULE: Steps MUST execute in order. Do NOT skip steps or proceed past a gate that has not been satisfied.
Actions:
okx-dex-swap (swap quote)okx-agentic-wallet (wallet balance)price_history (append, cap at 288 = 24h @ 5min)Gate (ALL must pass):
consecutive_errors < 5)stop_triggered == null)Actions:
Output: mtf dict, kline_vol float
python
def analyze_multi_timeframe(history, price) -> dict:
# Returns {trend, strength, momentum_1h, momentum_4h, structure,
# ema_short, ema_medium, ema_long}
# EMA alignment: short > medium > long → bullish
# Structure: 8h window split into 4 segments
# higher-highs + higher-lows → "uptrend"
# lower-highs + lower-lows → "downtrend"
# else → "ranging"
Gate:
mtf dict has trend and strength fields (graceful fallback to neutral if insufficient history)Actions:
okx-dex-market kline). Falls back to 5min tick history if kline unavailable.VOLATILITY_MULTIPLIER_BASE=2.0VOLATILITY_MULTIPLIER_TREND=3.0_should_delay_sell()Gate:
MIN_TRADE_USD ($5)Actions:
okx-onchain-gateway (diagnostic, non-blocking)okx-dex-swapokx-agentic-wallet contract-call (TEE signing)Gate:
retriable flagActions:
current_portfolio - (initial_eth × current_price)---JSON--- block with enriched fieldsOutput: Discord notification + structured JSON
ParameterDefaultDescriptionGRID_LEVELS``6Number of grid levels. More = finer, more tradesGRID_TYPE``"arithmetic"``"arithmetic" (fixed $ step) or "geometric" (fixed % step)EMA_PERIOD``20EMA lookback for grid center (applied to 1H kline = 20h)VOLATILITY_MULTIPLIER_BASE``2.0Base grid width = multiplier x stddevVOLATILITY_MULTIPLIER_TREND``3.0Wider grid in trending marketsASYM_FACTOR``0.4Max buy/sell asymmetry ratio. 0=symmetric, 1=fully asymmetricGRID_RECALIBRATE_HOURS``12Max hours before forced recalibration
ParameterDefaultDescriptionMTF_SHORT_PERIOD``55-bar EMA (25min @ 5min tick)MTF_MEDIUM_PERIOD``1212-bar EMA (1h @ 5min tick)MTF_LONG_PERIOD``4848-bar EMA (4h @ 5min tick)MTF_STRUCTURE_PERIOD``9696-bar (8h @ 5min tick) for structure detection
ParameterDefaultDescriptionSELL_TRAIL_TICKS``2Wait 2 ticks (10min) of price stability before selling in uptrendSELL_MOMENTUM_THRESHOLD``0.005Skip sell if 1h momentum > 0.5% in strong uptrend
Arithmetic (等差网格): Each level is a fixed USD distance apart. Good for narrow ranges.
text
levels = [center - N*step, ..., center, ..., center + N*step]
Geometric (等比网格): Each level is a fixed percentage apart. Better for wide ranges because step size scales with price. The ratio is derived from the arithmetic step: ratio = 1 + (step / center).
python
# In calc_dynamic_grid(), when GRID_TYPE == "geometric":
ratio = 1 + (step / center)
level_prices = [center * (ratio ** (i - half)) for i in range(GRID_LEVELS + 1)]
Both modes store level_prices in the grid dict for unified level lookup via bisect_right. Asymmetric spacing uses levels below center spaced by buy_step, levels above by sell_step. The _build_level_prices() helper handles both symmetric and asymmetric construction.
Choosing a mode:
MarketRecommendedWhyTight range ($1900-$2100)ArithmeticEven spacing, predictable profit per gridWide range ($1500-$3000)GeometricSteps scale with price, avoids crowding at low endHigh volatilityGeometricNaturally wider steps at higher pricesStablecoin pairsArithmeticFixed small steps (0.1-0.5%)
Step scales with real-time volatility, modulated by trend strength. Splits into directional buy/sell steps:
text
vol_mult = VOLATILITY_MULTIPLIER_BASE (2.0)
if trend_strength > 0.3:
vol_mult = blend(BASE, TREND, strength) (up to 3.0)
# Asymmetric steps based on trend direction
asym = ASYM_FACTOR × strength (if strength > 0.3, else 0)
if bullish:
buy_mult = vol_mult × (1 - asym) # tighter buy
sell_mult = vol_mult × (1 + asym) # wider sell
elif bearish:
buy_mult = vol_mult × (1 + asym) # wider buy
sell_mult = vol_mult × (1 - asym) # tighter sell
buy_step = (buy_mult × ATR) / (GRID_LEVELS / 2)
sell_step = (sell_mult × ATR) / (GRID_LEVELS / 2)
# Both clamped to [price × STEP_MIN_PCT, price × STEP_MAX_PCT], floor $5
step = (buy_step + sell_step) / 2 # backward-compatible average
ParameterDefaultDescriptionSTEP_MIN_PCT``0.010Step floor as fraction of price (1.0%)STEP_MAX_PCT``0.060Step cap as fraction of price (6%)VOL_RECALIBRATE_RATIO``0.3Recalibrate if kline ATR shifts >30% from grid's stored ATR
Recalibration triggers (asymmetric):
buy_step → recalibrate immediately (buying dips is grid's edge)sell_step → require N consecutive ticks confirmation before recalibrating (anti-chase)GRID_RECALIBRATE_HOURSParameterDefaultDescriptionUPSIDE_CONFIRM_TICKS``6Ticks (30min @ 5min interval) price must hold above grid before upside recalibrationMAX_CENTER_SHIFT_PCT``0.03Max 3% grid center shift per recalibration (prevents chasing spikes)
Anti-chase mechanism:
MAX_CENTER_SHIFT_PCT per recalibrationControls how much to trade at each grid level.
StrategyDescription"equal"Every grid level trades the same amount"martingale"BUY more at lower levels, SELL more at higher levels"anti_martingale"Reduces exposure as price moves further from center"pyramid"Largest position at grid center, tapering toward edges"trend_adaptive"(default) In bullish: buy more + sell less. In bearish: sell more + buy lesspython
def _calc_sizing_multiplier(level, grid_levels, direction, mtf=None):
base_mult = 1.0
if SIZING_STRATEGY == "trend_adaptive" and mtf:
trend = mtf.get("trend", "neutral")
strength = mtf.get("strength", 0)
if trend == "bullish":
if direction == "BUY":
base_mult = 1.0 + strength * (MAX - 1.0) # buy aggressively
else:
base_mult = 1.0 - strength * (1.0 - MIN) # sell conservatively
elif trend == "bearish":
# opposite
...
return clamp(base_mult, SIZING_MULTIPLIER_MIN, SIZING_MULTIPLIER_MAX)
ParameterDefaultDescriptionMAX_TRADE_PCT``0.12Max 12% of portfolio per trade (before sizing multiplier)MIN_TRADE_USD``5.0Minimum trade size in USDSLIPPAGE_PCT``1Slippage tolerance for DEX swapGAS_RESERVE``0.003Native token reserved for gas
ParameterDefaultDescriptionMIN_TRADE_INTERVAL``180030min cooldown between same-direction tradesMAX_SAME_DIR_TRADES``3Max consecutive same-direction tradesMAX_CONSECUTIVE_ERRORS``5Circuit breaker thresholdCOOLDOWN_AFTER_ERRORS``3600Cooldown after circuit breaker tripsPOSITION_MAX_PCT_DEFAULT``70Block BUY when ETH > this %POSITION_MIN_PCT_DEFAULT``30Block SELL when ETH < this %POSITION_MAX_PCT_BULLISH``80Allow more ETH in bullish trendPOSITION_MIN_PCT_BEARISH``25Allow less ETH in bearish trend
Additional safety guards:
Three protection mechanisms. When triggered, trading halts and a red Discord alert is sent. Use resume-trading command to clear.
ParameterDefaultDescriptionSTOP_LOSS_PCT``0.15Stop if portfolio drops 15% below cost basisTRAILING_STOP_PCT``0.10Stop if portfolio drops 10% from peakpython
def _check_stop_conditions(state, total_usd, price):
cost_basis = initial + deposits
pnl_pct = (total_usd - cost_basis) / cost_basis
peak = max(stats["portfolio_peak_usd"], total_usd)
# Check: stop_loss, trailing_stop, take_profit
text
1. If stop_triggered is set → log + Discord red alert + refuse trading + return
2. Check _check_stop_conditions → if triggered, set stop_triggered + alert + return
3. Multi-timeframe analysis → get trend context
4. Normal grid logic (cooldown, trend-adaptive position limits, etc.)
5. If SELL in strong uptrend → _should_delay_sell() check
text
1. Fetch token price
2. Read on-chain balances (ETH + USDC)
3. Multi-timeframe analysis → trend/strength/momentum/structure
4. Fetch K-line ATR volatility (hourly cache)
5. Check if grid needs recalibration (breakout / vol shift / age)
→ calc_dynamic_grid() uses trend-adaptive volatility multiplier
→ asymmetric buy_step/sell_step based on trend direction
6. Map price → grid level
7. If level changed:
a. Direction: BUY if level dropped, SELL if rose
b. If SELL in strong uptrend → delay check (trailing + momentum protection)
c. Safety checks (cooldown, trend-adaptive position limits, repeat guard, consecutive limit)
d. Calculate trade size (trend-adaptive sizing)
e. Execute swap via DEX aggregator
f. Record trade, update level ONLY on success
8. Calculate HODL Alpha
9. Report status (JSON + Discord)
python
def calc_dynamic_grid(price, price_history, mtf=None):
center = EMA(1H_kline, EMA_PERIOD) # 20-hour EMA on 1H candles
atr = calc_kline_volatility(candles)
# Trend-adaptive multiplier
vol_mult = VOLATILITY_MULTIPLIER_BASE # 2.0
if mtf and mtf["strength"] > 0.3:
vol_mult = blend(BASE=2.0, TREND=3.0, factor=strength)
# Asymmetric buy/sell multipliers
asym = ASYM_FACTOR * strength if strength > 0.3 else 0
if bullish:
buy_mult = vol_mult * (1 - asym) # tighter buy grid
sell_mult = vol_mult * (1 + asym) # wider sell grid
elif bearish:
buy_mult = vol_mult * (1 + asym) # wider buy grid
sell_mult = vol_mult * (1 - asym) # tighter sell grid
buy_step = clamp((buy_mult * atr) / half, floor, ceil)
sell_step = clamp((sell_mult * atr) / half, floor, ceil)
# Build asymmetric level_prices via _build_level_prices()
# Below center: spaced by buy_step; Above center: spaced by sell_step
level_prices = _build_level_prices(center, buy_step, sell_step, half, grid_type)
return {center, step, buy_step, sell_step, levels, range, vol_pct, type, level_prices}
Examples (at price $2000, ATR=$50):
TrendStrengthbuy_stepsell_stepBuy RangeSell RangeBehaviorNeutral0.1$33$33$1901-$2000$2000-$2099Symmetric, normalBullish0.6$33$54$1901-$2000$2000-$2162Buy dense + sell wideBearish0.6$54$33$1838-$2000$2000-$2099Buy wide + sell denseStrong Bull0.9$24$66$1928-$2000$2000-$2198Max asymmetry
python
def _should_delay_sell(state, current_level, prev_level, mtf, history):
"""Returns skip reason or None."""
# 1. Momentum protection: skip sell if 1h momentum > 0.5% in uptrend
if momentum_1h > SELL_MOMENTUM_THRESHOLD * 100:
if trend == "bullish" and structure == "uptrend":
return "trend_hold (momentum +X.X%)"
# 2. Trailing sell: wait SELL_TRAIL_TICKS (2) before executing
trail = state["sell_trail_counter"]
level_key = f"{prev_level}->{current_level}"
if trail[level_key] < SELL_TRAIL_TICKS:
trail[level_key] += 1
return "sell_trail (N/2)"
# 3. Cleared → proceed with sell
return None
python
def _get_position_limits(mtf):
"""Return (max_pct, min_pct) based on trend."""
if trend == "bullish" and strength > 0.3:
max_pct = 70 + (80 - 70) * strength # allow more ETH
min_pct = 30
elif trend == "bearish" and strength > 0.3:
max_pct = 70
min_pct = 30 - (30 - 25) * strength # allow less ETH
else:
max_pct, min_pct = 70, 30
Returns (amount_in_smallest_unit, failure_info). SELL returns wei (x1e18), BUY returns uUSDC (x1e6).
python
def calc_trade_amount(direction, eth_bal, usdc_bal, price,
current_level=None, grid_levels=None,
mtf=None):
available_eth = eth_bal - GAS_RESERVE_ETH
total_usd = available_eth * price + usdc_bal
max_usd = total_usd * MAX_TRADE_PCT
# Apply trend-adaptive sizing
multiplier = _calc_sizing_multiplier(level, grid_levels, direction, mtf)
max_usd *= multiplier
if direction == "SELL":
return int(min(max_usd / price, available_eth) * 1e18), None # wei
else:
return int(min(max_usd, usdc_bal * 0.95) * 1e6), None # uUSDC
OutcomeUpdate level?RationaleTrade succeededYesGrid crossing consumedTrade failedNoRetry on next tickTrade skipped (cooldown/limit)NoDon't lose the crossingSell delayed (trailing/momentum)NoWill retry next tick
text
# USD-denominated
total_pnl_usd = current_portfolio_usd - cost_basis
hodl_value_usd = initial_eth_amount × current_price
grid_alpha_usd = current_portfolio_usd - hodl_value_usd
# ETH-denominated
current_eth_equivalent = current_portfolio_usd / current_price
initial_eth_equivalent = cost_basis / initial_price
total_pnl_eth = current_eth_equivalent - initial_eth_equivalent
HODL Alpha (key metric): grid_alpha_usd > 0 means grid outperforms pure ETH holding.
json
{
"version": 5,
"grid": {"center": 2000, "step": 43.5, "buy_step": 33.3, "sell_step": 53.7,
"levels": 6, "range": [1900, 2161], "vol_pct": 2.1,
"type": "arithmetic",
"level_prices": [1900, 1933, 1967, 2000, 2054, 2107, 2161]},
"grid_set_at": "ISO timestamp",
"current_level": 3,
"price_history": ["...max 288 (24h at 5min)"],
"trades": [{"time": "...", "direction": "SELL", "price": 2050,
"amount_usd": 25, "est_profit": 1.5, "tx": "0x...",
"grid_from": 2, "grid_to": 3}],
"stats": {
"total_trades": 15,
"realized_pnl": 5.2,
"grid_profit": 3.8,
"initial_portfolio_usd": 1000,
"initial_eth_price": 2000.0,
"portfolio_peak_usd": 1050.0,
"total_deposits_usd": 0.0,
"deposit_history": [],
"trade_attempts": 10, "trade_successes": 10, "trade_failures": 0,
"sell_attempts": 5, "sell_successes": 5,
"buy_attempts": 5, "buy_successes": 5,
"retry_attempts": 0, "retry_successes": 0,
"started_at": "ISO timestamp",
"last_check": "ISO timestamp"
},
"stop_triggered": null,
"last_trade_times": {"BUY": "...", "SELL": "..."},
"last_failed_trade": null,
"last_balances": {"eth": 0.134, "usdc": 257.33, "time": "ISO timestamp"},
"last_quiet_report": "ISO timestamp",
"upside_breakout_ticks": 0,
"approved_routers": ["0x..."],
"errors": {"consecutive": 0, "cooldown_until": null},
"mtf_cache": null,
"kline_cache": null,
"sell_trail_counter": {}
}
Key fields:
grid.type + grid.level_prices: geometric/arithmetic grid support (asymmetric spacing)grid.buy_step / grid.sell_step: directional step sizes; grid.step = average for backward compatstats.initial_eth_price: records ETH price at bot start for HODL Alpha calculationstats.portfolio_peak_usd: highest portfolio value (for trailing stop)stop_triggered: string describing trigger condition, or nulllast_failed_trade: cached for retry command (expires after 10min)upside_breakout_ticks: confirmation counter for upside recalibrationapproved_routers: USDC approval cache to avoid redundant approvalsmtf_cache: cached multi-timeframe analysis resultkline_cache: cached K-line data (1h TTL)sell_trail_counter: tracks sell delay tick counts per level transitionCommandPurposeTriggertickMain loop: price → MTF → grid → trade → reportCron every 5minstatusPrint current grid state, balances, PnL, trendOn demandreportGenerate daily performance report (Chinese)Cron daily 08:00 CSThistoryShow recent trade historyOn demandresetReset grid (recalibrate from scratch), keep trade historyManualretryRetry last failed trade with fresh quote (expires after 10min)AI agent / manualanalyzeOutput detailed market + MTF + round-trip analysis JSONAI agentdepositManually record deposit/withdrawal for PnL trackingManualresume-tradingClear stop_triggered flag and resume tradingManual / AI agentpython
COMMANDS = {
"tick": tick, "status": status, "report": report,
"history": history_cmd, "reset": reset, "retry": retry,
"analyze": analyze, "deposit": deposit,
"resume-trading": resume_trading
}
if __name__ == "__main__":
cmd = sys.argv[1] if len(sys.argv) > 1 else "tick"
COMMANDS.get(cmd, tick)()
The tick command outputs a structured JSON block for AI agent parsing:
text
---JSON---
{
"version": "1.0",
"status": "trade_executed" | "no_trade" | "cooldown" | "trade_failed" | ...,
"market": {
"price": 2090.45, "ema": 2085.3, "volatility_pct": 1.2,
"trend": "bullish", "trend_strength": 0.65,
"momentum_1h": 0.35, "momentum_4h": 1.2,
"structure": "uptrend",
"kline_atr_pct": 1.8
},
"portfolio": {"eth": 0.134, "usdc": 257.33, "total_usd": 538.0, "eth_pct": 52.1},
"grid_level": 3,
"direction": "SELL",
"tx_hash": "0x...",
"failure_reason": "...",
"retriable": true,
"hodl_alpha": 2.15,
"success_rate": {"total_attempts": 182, "successes": 182, "rate_pct": 100.0}
}
The analyze command outputs additional fields:
multi_timeframe: full MTF data (EMA short/medium/long, momentum, structure)round_trips: trade pair analysis (good / micro / loss classification)Two card formats pushed via Discord Bot API:
Trade executed (colored embed):
No trade (grey compact card):
QUIET_INTERVAL (default 1 hour)Automatically detects external balance changes:
text
unexplained_change = (current_balance - last_balance) - sum(recorded_trades_since_last)
if abs(unexplained_change) > $100:
record as deposit or withdrawal → adjust PnL cost basis
grid_bot.log in script directory[YYYY-MM-DD HH:MM:SS] messageConsiderationWhat to adjustToken decimalsUSDC=6, DAI=18, WBTC=8 — affects amount conversionTypical volatilityBTC lower vol → smaller STEP_MIN/MAX_PCT; meme coins → largerLiquidity depthLow liquidity → smaller MAX_TRADE_PCT, add price impact checkGas costsL1 vs L2: adjust GAS_RESERVE and MIN_TRADE_USDStablecoin pairTOKEN/USDC pair: STEP_MIN_PCT can be much tighter (0.2%)Rate limitsAdd 300-500ms delay between consecutive OKX API calls
AI agent should periodically review trading performance and suggest/apply optimizations. Run weekly or when cumulative PnL stalls.
Extract recent trades and pair each BUY with its corresponding SELL to form round trips.
python
# Matching logic: SELL from level A→B matches BUY from level B→A
buy_stack = []
round_trips = []
for trade in trades:
if trade["direction"] == "BUY":
buy_stack.append(trade)
else: # SELL
for j in range(len(buy_stack)-1, -1, -1):
if buy_stack[j]["grid_to"] == trade["grid_from"]:
matched_buy = buy_stack.pop(j)
round_trips.append((matched_buy, trade))
break
Output per round trip:
(sell_price - buy_price) / buy_price x 100%FlagConditionMeaningLOSSspread < 0Bought high, sold lowMICRO0 < spread < 0.3%Profit too small to cover DEX costsGOODspread >= 0.3%Healthy grid profit
Key metrics: win rate, loss impact, micro-trade ratio (if > 30%, step too small).
LOSS trades:
PatternRoot CauseFixBuy @high, sell @low after recalibrationGrid chased a spikeIncrease UPSIDE_CONFIRM_TICKS, reduce MAX_CENTER_SHIFT_PCTBuy @high in trend, sell @low on reversalEMA too reactiveIncrease EMA_PERIOD or GRID_RECALIBRATE_HOURSLoss during flash crashStop-loss too looseTighten STOP_LOSS_PCT
MICRO trades:
PatternRoot CauseFixMany trades with < 0.2% spreadStep too smallIncrease STEP_MIN_PCTRapid back-and-forth at same levelsLow vol, grid too denseIncrease MIN_TRADE_INTERVALTrades cluster in 5-10 min windowsCooldown too shortIncrease MIN_TRADE_INTERVAL
text
STEP_MIN_PCT >= DEX_total_cost x 3
where DEX_total_cost ~ slippage + price_impact ~ 0.1-0.3% on L2
→ STEP_MIN_PCT >= 0.009 to 0.012
UPSIDE_CONFIRM_TICKS = typical_spike_duration / tick_interval
e.g., spikes last ~20min, tick=5min → confirm_ticks = 4-6
MAX_CENTER_SHIFT_PCT = step_pct x 2-3
Simulate new parameters against historical data, then: backup → patch → recalibrate → monitor 24h → re-run analysis.
text
1. Read grid_state.json and grid_bot.log
2. Filter trades to review window (default: last 48h)
3. Pair trades into round trips
4. Compute: win_rate, avg_spread, loss_count, micro_count, total_pnl, hodl_alpha
5. If loss_count > 0: trace each loss to recalibration events
6. If micro_ratio > 30%: recommend STEP_MIN_PCT increase
7. Check MTF data for trend alignment during losses
8. Propose specific parameter changes with backtest evidence
9. On user approval: backup → patch → recalibrate → verify
text
IF Step N fails:
1. Log failure reason to grid_bot.log
2. Increment errors.consecutive
3. If errors.consecutive >= 5: trigger circuit breaker (1h cooldown)
4. Cache failed trade for retry command (10min expiry)
5. DO NOT update grid level
6. Report failure via Discord + JSON output
PatternProblemRecalibrate every tickGrid oscillates, no stable levelsUpdate level on failure/skipSilently loses grid crossingsNo position limitsTrending market → 100% one-sidedFixed step in volatile marketToo small → over-trades; too large → never triggerssell - buy as PnLNet cash flow ≠ profitNo cooldownRapid swings cause burst of trades eating slippageNo stop-lossSingle crash wipes out months of grid profitsMartingale without capExponential position growth → liquidation riskArithmetic grid on wide range$20 step meaningless at $5000 but huge at $500Symmetric recalibrationChasing upside spikes = buying high then selling low on reversalStep floor too lowMicro-profit trades only feed DEX fees, net negative after costsNo center shift capSingle spike can drag grid center 5%+, creating losing positionsFixed sizing in trendsSelling same size in uptrend = giving away alpha to the marketSelling immediately in uptrendSell delay exists for a reason — let trends play outSymmetric grid in strong trendsAsymmetric grids accumulate faster on the favorable sideIgnoring buy_step/ sell_step in profit calcUse actual level_prices differences, not average step