Skip to main content

AI Makler Deep Dive — Memory, Orders, Workflow Orchestration & Trading Strategies

The AI Makler worker is the most advanced AI node in the ApudFlow worker library. It combines LLM-powered financial analysis with persistent memory, structured order generation, and the ability to trigger other workflows autonomously.

This guide covers everything you need to know to use it effectively in production.

How Memory Works

Storage Architecture

Memory is stored in the MongoDB collection ai_makler_memories. Each workflow gets exactly one document, identified by {workflowId, ownerId}. The document contains a history array — an ordered list of all analysis sessions.

{
"workflowId": "695d8abebad4e733a010d7fd",
"ownerId": "68bd4e274c7e0201b6995a37",
"history": [
{
"timestamp": "2026-06-21T08:00:00",
"mode": "analysis",
"marketState": "BTCUSD: Bullish breakout, high volume",
"dataSummary": "BTCUSD 4h chart...",
"newsSummary": "Fed holds rates steady",
"analysis": "Bitcoin showing strength...",
"orders": [
{
"action": "buy",
"symbol": "BTCUSD",
"entry_price": 86500,
"certainty_pct": 82
}
],
"triggeredWorkflows": [
{
"workflowId": "69a1b0c878882514b120a914",
"workflowName": "Risk Analysis",
"runId": "abc123...",
"status": "triggered"
}
],
"positionsAtTime": []
}
],
"createdAt": "2026-06-21T00:00:00",
"updatedAt": "2026-06-21T08:00:05"
}

What Gets Saved

SavedNOT Saved
Market state summaryRaw price data
Analysis text (truncated to 1000 chars)Full user input
Orders with prices and reasonsThe system prompt
Which workflows were triggeredAPI keys or credentials
Positions at time of analysisInternal execution details

History Trimming

The maxHistoryIterations parameter (1–100) controls how many sessions to keep. When the limit is reached, the oldest entries are dropped. This prevents unbounded memory growth while keeping the most recent and relevant context.

What the Agent Sees From Memory

On each run, the agent receives a compact summary:

PREVIOUS SESSIONS (most recent first):
[1] 2026-06-21T08:00:00 | BTCUSD: Bullish, ETHUSD: Neutral | Orders: 2
[2] 2026-06-20T20:00:00 | BTCUSD: Ranging, high volume | Orders: 1 | Triggered: 1
[3] 2026-06-20T12:00:00 | BTCUSD: Bearish rejection at 88k

This lets the agent connect past observations to current conditions — for example, noticing that a pattern it identified yesterday has now resolved.

Order Structure

When the agent decides to trade, it returns structured orders in the orders array. Each order is a JSON object:

FieldTypeRequiredDescription
actionstringOne of: buy, sell, close, hold
symbolstringTrading instrument symbol
entry_pricenumberSuggested entry price
target_pricenumberProfit target
stop_lossnumberStop-loss level
position_sizenumberPosition size (contracts, lots, units)
order_typestringmarket, limit, stop-loss, take-profit
reasonstringExplanation for the order
certainty_pctnumberConfidence 0–100 (only orders > 60% are generated)

When Orders Are Generated

The agent follows a strict rule: only generate orders when certainty > 60%. This prevents:

  • False signals from ambiguous data
  • Over-trading in unclear market conditions
  • Low-confidence positions that increase risk

In analysis mode, the agent typically returns an empty orders array and focuses on market assessment. In decision mode, it actively looks for trading opportunities.

Order Actions

ActionMeaningExample
buyOpen a long positionBuy BTCUSD at market
sellOpen a short positionSell ETHUSD, expect decline
closeClose an existing positionClose half of BTCUSD position
holdKeep current positions unchangedMarket conditions not clear

Position Management

The currentPositions parameter accepts a JSON array describing open positions:

[
{
"symbol": "BTCUSD",
"side": "buy",
"size": 0.5,
"entry_price": 85000.00,
"current_price": 87200.00,
"pnl_percent": 2.59,
"open_time": "2026-06-20T10:00:00"
},
{
"symbol": "ETHUSD",
"side": "sell",
"size": 2.0,
"entry_price": 2900.00,
"current_price": 2850.00,
"pnl_percent": 1.72,
"open_time": "2026-06-19T14:00:00"
}
]

The agent will review each position and decide:

  • hold — keep the position, conditions still favorable
  • close — exit the position (partial or full)
  • modify — adjust stop-loss or take-profit levels

Position review decisions appear as orders with action: "close" or action: "hold".

Workflow Orchestration

How Triggering Works

When the agent decides to run another workflow, it:

  1. Validates the workflow ID against the allowedWorkflows list
  2. Creates a run document in workflow_runs with trigger: "ai_makler"
  3. Passes data as initialPayload.contextVars — available as {{ variable }} in the target
  4. Pushes to Redis — the executor picks it up asynchronously
  5. Returns the run ID in triggeredWorkflows

The triggered workflow runs fire-and-forget — it executes independently. The agent does not wait for it to complete.

Example: Agent Triggers a Risk Analysis Workflow

In its analysis, the agent decides:

{
"analysis": "High volatility detected on multiple assets...",
"triggerWorkflows": [
{
"workflowId": "69a1b0c878882514b120a914",
"data": {
"symbols": ["BTCUSD", "ETHUSD", "SOLUSD"],
"reason": "volatility_spike",
"severity": "high"
}
}
]
}

The "Risk Analysis" workflow receives the data and processes it immediately.

Configuring Allowed Workflows

In the worker's configuration, list workflow IDs one per line:

69a1b0c878882514b120a914
695d8abebad4e733a010d7fd

Only workflows listed here can be triggered. If the agent tries to trigger a workflow not on the list, it gets an error and the trigger is skipped.

Strategy Patterns

Pattern 1: Multi-Timeframe Analysis With Memory

[Schedule: Every 4h] → [Fetch BTC 1h + 4h + 1d Data] → [AI Makler: analysis] → [Log to User List]

The agent sees all three timeframes and its previous analysis from the last run. Over days, it builds a picture of how the trend evolved.

Pattern 2: News-Aware Signal Generator

[Schedule: Every 1h] → [Fetch News via RSS] → [Fetch Price Data] → [AI Makler: decision]
→ [Condition: ordersCount > 0] → [Run Workflow "Execute Signals"]

The agent reads fresh news, checks current prices, and generates trading signals. If any orders exist, an execution workflow handles them.

Pattern 3: Portfolio Review + Rebalance

[Manual Trigger] → [Fetch All Positions (from exchange)]
→ [Fetch Market Data for All Holdings]
→ [AI Makler: decision, with currentPositions]
→ [Run Workflow "Place Adjustment Orders"]

The agent receives all current positions, reviews each against current market conditions, and decides which to hold, close, or adjust.

Pattern 4: Risk Monitor With Escalation

[Schedule: Every 15min] → [Fetch Volatility Metrics]
→ [AI Makler: analysis, mode=analysis]
→ [Condition: marketState contains "High Risk"]
→ [Run Workflow "Risk Alert"] (fire-and-forget)
→ [AI Makler: decision, mode=decision]
→ [Run Workflow "Execute Hedging"]

The agent monitors risk every 15 minutes. When it detects high risk, it triggers an alert workflow, then switches to decision mode to recommend hedging actions.

Parameter Combinations for Different Investor Types

Conservative (capital preservation)

{
"mode": "analysis",
"investorType": "conservative",
"instrumentTypes": ["stocks", "forex"],
"orderTypes": ["limit", "stop-loss"],
"maxHistoryIterations": 30
}

The agent focuses on low-risk instruments, uses limit orders with tight stop-losses, and keeps a longer history for better pattern recognition.

Moderate (balanced risk/reward)

{
"mode": "decision",
"investorType": "moderate",
"instrumentTypes": ["all"],
"orderTypes": ["market", "limit", "stop-loss"],
"maxHistoryIterations": 20
}

Aggressive (higher risk, shorter timeframes)

{
"mode": "decision",
"investorType": "aggressive",
"instrumentTypes": ["crypto", "stocks"],
"orderTypes": ["market", "limit"],
"maxHistoryIterations": 10
}

Shorter history (faster reactions), all trading instruments, market orders for quick execution.

Performance Tips

  1. Keep data focused — don't dump raw OHLC arrays. Give the agent summaries and key levels. This improves response quality and reduces token usage.

  2. Use analysis mode first — let the agent observe and build memory before switching to decision mode. This gives it context for better decisions.

  3. Limit triggered workflows — the agent can trigger multiple workflows per run. Keep the allowed list focused on what's actually useful.

  4. Monitor memory sizememorySize in the output shows current history depth. If the agent seems stuck on old data, reduce maxHistoryIterations.

  5. Combine with conditions — use a Condition worker after AI Makler to check ordersCount > 0 or latestCertainty > 80 before acting on orders.

  6. Fresh news matters — the agent relies on the news you provide. Connect an RSS or NewsAPI fetcher upstream for best results.

Troubleshooting

SymptomLikely CauseFix
Empty orders in decision modeLow certainty (< 60%)Provide better data or switch to analysis mode
triggeredWorkflows emptyAgent chose not to triggerCheck allowedWorkflows is configured
Memory not growingWrong workflow IDCheck workflowId is consistent across runs
LLM returns parse errorsResponse too long or malformedReduce input data size, check API key

Output Schema Reference

{
"status": "success",
"mode": "decision",
"results": "BTCUSD showing bullish momentum...",
"marketState": "BTCUSD: Bullish, SOLUSD: Bearish",
"orders": [
{
"action": "buy",
"symbol": "BTCUSD",
"entry_price": 86500.00,
"target_price": 92000.00,
"stop_loss": 84000.00,
"position_size": 0.5,
"order_type": "market",
"reason": "Bullish flag breakout on volume",
"certainty_pct": 82
}
],
"ordersCount": 1,
"triggeredWorkflows": [
{
"workflowId": "69a1b0c8...",
"workflowName": "Risk Analysis",
"runId": "abc123...",
"status": "triggered"
}
],
"triggeredCount": 1,
"memorySize": 5,
"memoryMax": 20,
"latestOrder": {"action": "buy", ...},
"latestAction": "buy",
"latestSymbol": "BTCUSD",
"latestCertainty": 82
}