SQL-like Trading Rules – Beginner Guide
a beginner guide to the syntax of Stoqey
📘 SQL-like Trading Rules – Beginner Guide
Welcome! 👋 This guide shows you how to tell your trading bot what to watch for and what to do, using a simple rule language that looks a bit like SQL. Imagine you're giving your bot step-by-step instructions, like teaching a friend how to play a game! 🕹️
🧠 Quick-Start: How Rules Work
You write rules to tell your bot:
- What to watch (like prices, your position, or indicators)
- What to do when those things happen (like buy, sell, or set a stop loss)
Think of your bot as a security guard:
- "IF" is what the guard checks first ("If the door is open...")
- "TICK" is the guard watching the outside world (market data)
- "POS" is the guard checking what's inside (your own position)
- "WITH" is extra details ("...with a timer of 3 minutes")
- "THEN" is what the guard does if the condition is true ("...then sound the alarm!")
🏗️ Basic Structure (The Recipe)
Every rule is like a recipe:
- IF 🍳 (optional): Sets up the main ingredient or base rule. Think: "If we start with eggs..."
- TICK ⏰: Watching the market, like "If the clock ticks and the price changes..."
- POS 🧾: Watching your holdings, like "If my position changes or reaches a certain amount..."
- WITH 🧂: Adds extra flavor, like "with a pinch of salt" (extra details about what to check)
- THEN 🚦: What action to take ("then bake for 10 minutes" or "then buy 100 shares")
Example:
[object Object]This means:
👀 The bot sets a stop loss of 30 ticks, watches the volume for the last 3 minutes, and if it's above 50, it places an order for 100 shares at $51.25.
🔑 What Do The Keywords Mean?
- IF:
- "If this is true at the start..."
- Example:
IF stopLoss ticks=30(The bot puts a stop loss of 30 ticks in place, before anything else.) - Note: IF statements are standalone and not followed by THEN.
- TICK:
- "Watch the market, every tick (price/volume change)..."
- Example:
TICK volume ...
- POS:
- "Watch my position (how much P&L I have)..."
- Example:
POS pnlTicks ...
- WITH:
- "With these extra details..."
- Example:
WITH minutes=3(Look at the last 3 minutes)
- THEN:
- "If the above is true, then do this..."
- Example:
THEN order SET ...
Metaphor:
It's like:
IF (put on helmet) THEN TICK (watch the road WITH sunglasses) > 50 THEN (pedal faster!)
🤔 What The Bot Is Watching & Doing (with Examples)
👀 Watching the Market (TICK)
- The bot is keeping an eye on real-time data, like price or volume.
- Example:
[object Object]- Bot watches: 3-minute volume
- Bot acts: If volume > 50, place an order for 100 shares at $51.25
🧾 Watching Your Position (POS)
- The bot checks your current holdings (P&L).
- Example:
[object Object]- Bot watches: If P&L is above 5 ticks
- Bot acts: Sets a stop loss to 30 ticks and sends the change immediately
🧂 Adding Details (WITH)
- "WITH" gives more info about what to check.
E.g., "WITH minutes=5" means look at the last 5 minutes. - Example:
[object Object]- Bot watches: 20-period EMA (average) is above 50.25
- Bot acts: Buy 100 shares at $51.00
🚦 Taking Action (THEN)
- "THEN" tells the bot what to do if the conditions above are met.
- Example:
order SET size=100, price=51.25, transmit=true- The bot prepares and sends an order for 100 shares at $51.25
🧩 Building Blocks (What You Can Use)
🎯 Primitive Actions (for IF)
- These go at the start (with IF), like setting up your safety net.
stopLoss– Put a stop loss on your tradetakeProfit– Set a target for taking profittrailingStopLoss– Make your stop loss follow the price- (and more)
- Example:
[object Object]- Bot puts a stop loss 30 ticks away before doing anything else.
📊 Technical Indicators (for WITH)
- These are tools the bot can use to analyze the market.
volume– How many shares tradedema– Exponential moving averagevolatility– How much price jumps aroundrsi– Relative Strength Index- (and more)
- Example:
[object Object]- Bot checks if 20-period EMA is above 50.25
📚 Common Scenarios (Copy & Tweak!)
🛑 Stop Loss Management
[object Object]- Bot sets a stop loss, watches for a volume surge, then buys if it happens.
🔍 Position Monitoring
[object Object]- Bot checks your position P&L, then moves your stop loss if needed.
📈 Volume-Based Trading
[object Object]- Bot watches for big volume in 5 minutes, then buys 50 shares.
🚀 Volume Breakout (multiple checks)
[object Object]- Bot only buys if both volume and price conditions are met.
🏃 Trailing Stop
[object Object]- Bot sets a trailing stop, then tightens it if price goes up.
➕ Position Scaling
[object Object]- Bot adds to your position if P&L rises.
🔢 Value Types (What Numbers Mean)
- Numbers:
price=50.25(dollars)size=100(shares)ticks=30(distance)
- Time Periods:
minutes=5hours=1
- True/False (yes/no):
transmit=true(do it now)isActive=false
💡 Tips & Tricks (Skim These!)
- 🕒 TICK = market data, POS = your position
- 🛑 IF can only be used for primitive actions (like stopLoss/takeProfit)
- 🧂 WITH is for technical indicators only (like volume, ema, rsi)
- ⚠️ Don't mix up WITH and IF (don't use WITH for stopLoss, etc)
- ✍️ Always put spaces around
>,<,=, andTHEN - 🗒️ Use commas to separate settings:
size=100, price=51.25 - 🚦 Use
transmit=trueif you want the bot to act right away - 🧪 Start simple, test your rule, and build up!
🏁 Final Advice
Start with a tiny rule, see what the bot does, then add more!
Every part of your rule tells the bot what to watch and what to do—just like giving instructions to a smart assistant.
Happy trading! 🚀