Overview

πŸ“˜ 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:

  1. IF 🍳 (optional): Sets up the main ingredient or base rule. Think: "If we start with eggs..."
  2. TICK ⏰: Watching the market, like "If the clock ticks and the price changes..."
  3. POS 🧾: Watching your holdings, like "If my position changes or reaches a certain amount..."
  4. WITH πŸ§‚: Adds extra flavor, like "with a pinch of salt" (extra details about what to check)
  5. THEN 🚦: What action to take ("then bake for 10 minutes" or "then buy 100 shares")

Example:

IF stopLoss ticks=30
TICK volume WITH minutes=3 > 50 THEN
order SET size=100, price=51.25, transmit=true

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:
    TICK volume WITH minutes=3 > 50 THEN
    order SET size=100, price=51.25, transmit=true
    • 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:
    POS pnlTicks > 5 THEN stopLoss SET ticks=30, transmit=true
    • 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:
    TICK ema WITH period=20 > 50.25 THEN
    order SET size=100, price=51.00, transmit=true
    • 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 trade
    • takeProfit – Set a target for taking profit
    • trailingStopLoss – Make your stop loss follow the price
    • (and more)
  • Example:
    IF stopLoss ticks=30
    • 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 traded
    • ema – Exponential moving average
    • volatility – How much price jumps around
    • rsi – Relative Strength Index
    • (and more)
  • Example:
    TICK ema WITH period=20 > 50.25 THEN ...
    • Bot checks if 20-period EMA is above 50.25

πŸ“š Common Scenarios (Copy & Tweak!)

πŸ›‘ Stop Loss Management

IF stopLoss ticks=30
TICK volume WITH minutes=3 > 50 THEN
order SET size=100, price=51.25, transmit=true
  • Bot sets a stop loss, watches for a volume surge, then buys if it happens.

πŸ” Position Monitoring

POS pnlTicks > 5 THEN stopLoss SET ticks=30, transmit=true
  • Bot checks your position P&L, then moves your stop loss if needed.

πŸ“ˆ Volume-Based Trading

TICK volume WITH minutes=5 > 1000 THEN
order SET size=50, price=51.25, transmit=true
  • Bot watches for big volume in 5 minutes, then buys 50 shares.

πŸš€ Volume Breakout (multiple checks)

TICK volume WITH minutes=5 > 1000 AND
price > 50.25 THEN
order SET size=100, price=50.50, transmit=true
  • Bot only buys if both volume and price conditions are met.

πŸƒ Trailing Stop

IF trailingStopLoss ticks=30
TICK price WITH minutes=5 > 51.00 THEN
position SET trailingStopLoss=25, transmit=true
  • Bot sets a trailing stop, then tightens it if price goes up.

βž• Position Scaling

POS pnlPercent > 2 THEN scaleOut SET steps=0.25, size=25
  • 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=5
    • hours=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 >, <, =, and THEN
  • πŸ—’οΈ Use commas to separate settings: size=100, price=51.25
  • 🚦 Use transmit=true if 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! πŸš€

SQL-like Trading Rules – Beginner Guide