Skip to content

Instantly share code, notes, and snippets.

@jseverson
Created December 10, 2025 18:20
Show Gist options
  • Select an option

  • Save jseverson/f96b069529803a07800b28af7c3e1dd4 to your computer and use it in GitHub Desktop.

Select an option

Save jseverson/f96b069529803a07800b28af7c3e1dd4 to your computer and use it in GitHub Desktop.
RunLine Trading Algorithms - Flip Sell, Speed Trade Sell, Spike Guard

RunLine Trading Algorithms

Documentation for the trading algorithms used in the RunLine betting application.


Flips

A flip is a strategy to quickly switch your position from one team to the opposite team by:

  1. Buying shares of the opposing team (fire-and-forget)
  2. Selling all shares of the team you currently own (using flip sell algorithm)

This allows traders to capitalize on market movement or change their position when they realize they picked the wrong team.

How to Trigger a Flip

From the Trading Screen:

  • When you own shares in only one team, a "FLIP!" button appears next to the buy button for the opposite team
  • Tapping FLIP fires the buy order and starts the flip sell algorithm

From the Sell Screen:

  • During an active sell operation, a FLIP button is available
  • Useful if you decide mid-sell that you want to switch sides entirely

Flip Sell Algorithm

The flip sell uses an aggressive pricing strategy to liquidate quickly:

1. Get highest bid price from cached order book (refreshed every 500ms)
2. Set starting sell price = highest bid - 3 cents
3. Place sell order for ALL shares with 3-second expiration
4. If shares remain (partial fill or no fill):
   - Lower price by 3 cents
   - Wait 1 second
   - Repeat from step 3
5. Continue until all shares sold or price hits 1 cent

Example:

Highest bid: 52¢
Try 1: Sell all at 49¢ → 500 shares filled, 500 remain
Try 2: Sell all at 46¢ → 300 shares filled, 200 remain
Try 3: Sell all at 43¢ → 200 shares filled → Done!

Speed Trade Mode

Async execution mode that processes trades in the background without blocking dialogs.

Speed Trade Buy

  • Place order immediately at configured aggression price
  • Return to trading screen instantly
  • Log pending entry (grey) while polling for fill
  • Update log when order completes

Speed Trade Sell Algorithm

Uses cached order book price for immediate execution:

1. Get highest bid price from cached order book (refreshed every 500ms)
2. Set starting sell price = highest bid (no initial offset)
3. Place sell order for ALL shares with 3-second expiration
4. If shares remain (partial fill or no fill):
   - Lower price by 1 cent
   - Wait 1 second
   - Repeat from step 3
5. Continue until all shares sold or price hits 1 cent

Key differences from Flip Sell:

  • Starts at highest bid (not -3¢)
  • Drops by 1¢ per retry (not 3¢)
  • More patient algorithm for better average price

Example:

Highest bid: 52¢
Try 1: Sell all at 52¢ → No fills
Try 2: Sell all at 51¢ → 300 shares filled, 700 remain
Try 3: Sell all at 50¢ → 400 shares filled, 300 remain
Try 4: Sell all at 49¢ → 300 shares filled → Done!

Spike Guard (Price Spike Protection)

Protects against buying during rapid price increases that may indicate manipulation or temporary volatility.

How It Works

The app tracks the last 5 price fetches for each market. A spike is detected when:

  • Price has increased by ≥ 5 cents from the previous fetch

Important: Only price INCREASES trigger spike protection. Price decreases do not block buying (buying during a price drop is favorable).

When Spike is Detected

  • Buy buttons are temporarily disabled
  • User sees a warning indicator
  • Must wait for price to stabilize before buying

Configuration

Setting Value
Price history size 5 prices
Spike threshold 5 cents (increase)

Example:

Price history: [0.45, 0.46, 0.47, 0.48, 0.54]
                                    └── +6¢ spike detected!
Buy buttons disabled until price stabilizes.

Regular Batch Selling (Non-Speed Trade)

For manual selling from the Processing Sell Screen, the app uses a batched approach:

Batch Configuration

Available batch sizes: 1, 50, 100, 500, 1000, 2000, 3000, 5000, 8000, 10000, 20000

Default selection: App picks optimal batch size based on 75% of available liquidity.

Batch Selling Process

WHILE shares remain to sell:

  1. POSITION CHECK
     - Fetch actual shares from Kalshi API
     - Cap remaining shares if position changed externally

  2. CALCULATE BATCH
     - batchSize = min(remainingShares, sharesPerBatch)

  3. EXECUTE ORDER
     - Place limit order for batch at target price
     - Order expires in 5 seconds

  4. CHECK RESULT
     - If filled: update totals, wait 3 seconds, next batch
     - If no fills + auto-decrease ON: drop 3¢, wait 1 second, retry
     - If no fills + auto-decrease OFF: wait 1 second, retry same price

Timing Constants

Event Delay
Between successful batches 3 seconds
Between price decrease retries 1 second
Order expiration (sell) 5 seconds
Order expiration (buy) 15 seconds

Cash Out (Emergency Exit)

An emergency button during sell that forces an aggressive price drop to ensure immediate execution.

How It Works

Formula: cashOutPrice = currentMarketPrice - $0.20 (minimum $0.05)

Example:

  • Current market price: $0.50
  • Cash out triggered → New sell price: $0.30
  • The 20-cent drop ensures immediate matching with buyers

When to Use Cash Out

  • Market is moving against you rapidly
  • Normal sell is taking too long to fill
  • You need to exit the position immediately regardless of price

Quick Reference: Price Formulas

Feature Formula
Flip sell Highest bid - 3¢, then -3¢ per retry
Speed trade sell Highest bid, then -1¢ per retry
Regular sell User-selected (market ± offset)
Auto-decrease on no fill Previous price - 3¢
Cash out Market - 20¢ (min 5¢)
Buy aggression Market + configurable offset
Spike guard threshold 5¢ increase triggers protection

All prices are clamped to the valid range of $0.01 to $0.99.


Algorithm Comparison

Feature Speed Trade Sell Flip Sell
Starting price Highest bid Highest bid - 3¢
Price drop per retry
Order expiration 3 seconds 3 seconds
Retry delay 1 second 1 second
Use case Normal exits Quick position switches
Aggressiveness Moderate High

Last updated: December 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment