Skip to content

Presets

API endpoints for symbol preset management.

Provides CRUD endpoints for managing saved symbol presets in the security master database.

router = APIRouter(prefix='/api/presets', tags=['presets']) module-attribute

PresetRequest

Bases: BaseModel

Request model for creating or updating a preset.

Attributes:

Name Type Description
name str

Name of the preset.

rtype int

Bar period rtype value.

publisher_name str

Name of the publisher.

publisher_id int

ID of the publisher dataset.

symbols list[str]

List of symbol strings in the preset.

Source code in src/onesecondtrader/dashboard/routers/presets.py
class PresetRequest(BaseModel):
    """
    Request model for creating or updating a preset.

    Attributes:
        name:
            Name of the preset.
        rtype:
            Bar period rtype value.
        publisher_name:
            Name of the publisher.
        publisher_id:
            ID of the publisher dataset.
        symbols:
            List of symbol strings in the preset.
    """

    name: str
    rtype: int
    publisher_name: str
    publisher_id: int
    symbols: list[str]
    symbol_type: str = "raw_symbol"

ensure_presets_table()

Create the symbol_presets table if it does not already exist.

Source code in src/onesecondtrader/dashboard/routers/presets.py
def ensure_presets_table() -> None:
    """Create the symbol_presets table if it does not already exist."""
    with connect_presets() as conn:
        conn.execute(
            """
            CREATE TABLE IF NOT EXISTS symbol_presets (
                name TEXT PRIMARY KEY,
                rtype INTEGER NOT NULL,
                publisher_name TEXT NOT NULL,
                publisher_id INTEGER NOT NULL,
                symbols TEXT NOT NULL,
                symbol_type TEXT NOT NULL DEFAULT 'raw_symbol'
            )
            """
        )
        # Migrate existing tables that lack the symbol_type column
        cols = {
            row[1]
            for row in conn.execute("PRAGMA table_info(symbol_presets)").fetchall()
        }
        if "symbol_type" not in cols:
            conn.execute(
                "ALTER TABLE symbol_presets ADD COLUMN symbol_type TEXT NOT NULL DEFAULT 'raw_symbol'"
            )
        conn.commit()

list_presets() async

Return list of all preset objects.

Source code in src/onesecondtrader/dashboard/routers/presets.py
@router.get("")
async def list_presets() -> dict:
    """Return list of all preset objects."""
    with connect_presets() as conn:
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()
        cursor.execute(
            "SELECT name, rtype, publisher_name, publisher_id, symbols, symbol_type "
            "FROM symbol_presets ORDER BY name"
        )
        rows = cursor.fetchall()
    presets = [
        {
            "name": row["name"],
            "rtype": row["rtype"],
            "publisher_name": row["publisher_name"],
            "publisher_id": row["publisher_id"],
            "symbols": json.loads(row["symbols"]),
            "symbol_type": row["symbol_type"],
        }
        for row in rows
    ]
    return {"presets": presets}

get_preset(name) async

Return all fields for a specific preset.

Source code in src/onesecondtrader/dashboard/routers/presets.py
@router.get("/{name}")
async def get_preset(name: str) -> dict:
    """Return all fields for a specific preset."""
    with connect_presets() as conn:
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()
        cursor.execute(
            "SELECT name, rtype, publisher_name, publisher_id, symbols, symbol_type "
            "FROM symbol_presets WHERE name = ?",
            (name,),
        )
        row = cursor.fetchone()
    if row is None:
        return {"error": "Preset not found"}
    return {
        "name": row["name"],
        "rtype": row["rtype"],
        "publisher_name": row["publisher_name"],
        "publisher_id": row["publisher_id"],
        "symbols": json.loads(row["symbols"]),
        "symbol_type": row["symbol_type"],
    }

create_preset(request) async

Create a new symbol preset.

Source code in src/onesecondtrader/dashboard/routers/presets.py
@router.post("")
async def create_preset(request: PresetRequest) -> dict:
    """Create a new symbol preset."""
    with connect_presets() as conn:
        conn.execute(
            "INSERT INTO symbol_presets (name, rtype, publisher_name, publisher_id, symbols, symbol_type) "
            "VALUES (?, ?, ?, ?, ?, ?)",
            (
                request.name,
                request.rtype,
                request.publisher_name,
                request.publisher_id,
                json.dumps(request.symbols),
                request.symbol_type,
            ),
        )
        conn.commit()
    return {"status": "created", "name": request.name}

update_preset(name, request) async

Update an existing symbol preset.

Source code in src/onesecondtrader/dashboard/routers/presets.py
@router.put("/{name}")
async def update_preset(name: str, request: PresetRequest) -> dict:
    """Update an existing symbol preset."""
    with connect_presets() as conn:
        conn.execute(
            "UPDATE symbol_presets SET rtype = ?, publisher_name = ?, publisher_id = ?, "
            "symbols = ?, symbol_type = ? WHERE name = ?",
            (
                request.rtype,
                request.publisher_name,
                request.publisher_id,
                json.dumps(request.symbols),
                request.symbol_type,
                name,
            ),
        )
        conn.commit()
    return {"status": "updated", "name": name}

delete_preset(name) async

Delete a symbol preset.

Source code in src/onesecondtrader/dashboard/routers/presets.py
@router.delete("/{name}")
async def delete_preset(name: str) -> dict:
    """Delete a symbol preset."""
    with connect_presets() as conn:
        conn.execute("DELETE FROM symbol_presets WHERE name = ?", (name,))
        conn.commit()
    return {"status": "deleted", "name": name}