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 many shares I have, or my entry price)..."
    • Example: POS calculatePosition ...
  • 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 (how much you own, entry price, etc).
  • Example:
    POS calculatePosition size=100, entryPrice=50.25 > 51.00 THEN
    position SET stopLoss=50.75, transmit=true
    • Bot watches: If you have 100 shares from $50.25, and price is above $51.00
    • Bot acts: Sets a stop loss to $50.75 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 calculatePosition size=100, entryPrice=50.25 > 51.00 THEN
position SET stopLoss=50.75, transmit=true
  • Bot checks your position and price, 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 calculatePosition size=100, entryPrice=50.25 > 51.00 THEN
order SET size=50, price=51.25, transmit=true
  • Bot adds to your position if price 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! ๐Ÿš€