0bps

Strategy vault / Other

Ladder Order Trigger Price Can Be Set

Ladder orders can be bought or sold. The program places a specified number of buy orders or sell orders according to the order interval. If it is a buy order, the first order will be the highest priced order, and the…

JavaScriptOther
Author
Zero
Complexity
Intermediate · 62 lines
Parameters
7

Ladder orders can be bought or sold. The program places a specified number of buy orders or sell orders according to the order interval. If it is a buy order, the first order will be the highest priced order, and the prices of subsequent orders will decrease in sequence. The first sell order will be the cheapest sell order, and the prices of subsequent orders will increase in sequence.

Parameters

NameDefaultWhat it controls
OpType0Place a buy order: Buy | Sell
StartPrice20Initial order price
PriceDiff0.2Order Interval
OrderNum10Number of Orders
Amount0.8Number of coins per order
EnableTriggerfalseUse trigger conditions
TriggerPrice18Trigger price

Source JavaScript

function adjustFloat(v) {
    return Math.floor(parseFloat(v.toFixed(10))*1000)/1000;
}

function GetAccount(e) {
    var account;
    while (!(account = exchange.GetAccount())) {
        Sleep(Interval);
    }
    return account;
}

function GetTicker() {
    var ticker;
    while (!(ticker = exchange.GetTicker())) {
        Sleep(1000);
    }
    return ticker;
}

function main() {
    var ticker = GetTicker();
    var InitPrice = ticker.Last;
    if (EnableTrigger) {
        Log('Current Price: ', InitPrice, InitPrice > TriggerPrice ? 'Price drops below' : 'Price rises above', TriggerPrice, 'triggers ladder order placement');
        while (true) {
            if (InitPrice > TriggerPrice && ticker.Last < TriggerPrice) {
                Log('Current Price', ticker.Last, 'Price drops below ', TriggerPrice, 'element, Start ordering');
                break;
            } else if (InitPrice < TriggerPrice && ticker.Last > TriggerPrice) {
                Log('Current Price', ticker.Last, 'Price rises above ', TriggerPrice, 'element, Start ordering');
                break;
            }
            ticker = GetTicker();
            Sleep(1000);
        }
    }
    var account = GetAccount();
    var needMoney = 0;
    var needStocks = 0;
    for (var i = 0; i < OrderNum; i++) {
        needMoney += (StartPrice - (i * PriceDiff)) * Amount;
        needStocks += Amount;
    }

    if (OpType == 0) {
        if (needMoney > account.Balance) {
            throw "Not enough money to place an order";
        }
        for (var i = 0; i < OrderNum; i++) {
            exchange.Buy(adjustFloat(StartPrice - (i * PriceDiff)), Amount);
        }
    } else {
        if (needStocks > account.Stocks) {
            throw "Not enough coins to place an order";
        }
        for (var i = 0; i < OrderNum; i++) {
            exchange.Sell(adjustFloat(StartPrice + (i * PriceDiff)), Amount);
        }        
    }
    Log("All orders completed");
}

Published by Zero on the FMZ strategy square · last modified 2015-04-22. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Ladder-Order-Trigger-Price-Can-Be-Set.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open