0bps

Strategy vault / Other

Stop Loss Below Fixed Price

A simple fixed-price stop-loss bot can help you stop losses. It is also a good introductory strategy for cryptocurrency quantitative trading. StopPrice:Fixed price to stop loss Intervel: Intervel(second) of checking…

JavaScriptOther
Author
小草
Complexity
Starter · 49 lines
Parameters
2

A simple fixed-price stop-loss bot can help you stop losses. It is also a good introductory strategy for cryptocurrency quantitative trading.

Parameters:

StopPrice:Fixed price to stop loss

Intervel: Intervel(second) of checking coin price

Parameters

NameDefaultWhat it controls
StopPrice5000Fixed price to stop loss
Intervel10Intervel(second) of checking coin price

Source JavaScript

function CancelPendingOrders() {
    var orders = _C(exchange.GetOrders);
    for (var j = 0; j < orders.length; j++) {
        exchange.CancelOrder(orders[j].Id, orders[j]);
    }
}
function StopLoss(){
    var done = false
    while(!done){
        var ticker = _C(exchange.GetTicker)
        account = _C(exchange.GetAccount)
        var price = ticker.Buy
        var amount = account.Stocks
        try{
            if(amount > 0){
                var id = exchange.Sell(price, amount);
                if(id){
                    exchange.CancelOrder(id)
                }
                else{
                    Log('all coins are sold')
                    Log('total coins: ', account.Stocks)
                    done = true
                }
            }
        }catch(err){
            Log('error, stop')
            done = true
        }
        Sleep(1000);
    }
}
function main() {
    Log('robot starts to run')
    CancelPendingOrders()
    var account = _C(exchange.GetAccount)
    Log('total coins: ', account.Stocks);
    while(true){
        var ticker = _C(exchange.GetTicker)
        if(ticker.Last <= StopPrice){
            Log('Last price is:', ticker.Last, 'Stop price is: ', StopPrice)
            Log('the stop price has reached, start to sell')
            StopLoss();
            break;
        }
        Sleep(Intervel*1000)
    }
    Log('robot has stopped')
}

Published by 小草 on the FMZ strategy square · last modified 2019-07-03. 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/Stop-Loss-Below-Fixed-Price.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